
/*
Navigation expanding/highlighting
*********************************/

(function()
{
    /**
    Navigation expanding/highlighting.

    Expects HTML in the following format:

    <div id="navigation">
    <ul>
        <li>
            <a href="/">Home</a>
        </li>
        <li>
            <a href="/about/">About Us</a>
            <ul>
                <li>
                    <a href="/about/beliefs/">Our Beliefs</a>
                </li>
                <li>
                    <a href="/about/faq/">Frequently Asked Questions</a>
                </li>
            </ul>
        </li>
        <li>
            <a href="/contact/">Contact us</a>
        </li>
    </ul>
    </div>
    */

    function init()
    {
        // Add class 'selected' to links to current page
        mylinks = jQuery('#navigation a');
        mylinks.each(selectedIfCurrent);

        // Hide all 'inner' navigation lists
        innerLists = jQuery('#navigation > ul > li > ul');
        innerLists.each(hide);

        // Show 'inner' lists if  link for current page
        innerLists.each(showIfCurrent);
    }

    function selectedIfCurrent()
    {
        pathname = this.pathname
        if( ! RegExp('^/{1}').test(pathname) )
            pathname = '/'+pathname;

        // Homepage
        if(window.location.pathname == '/' && pathname == '/')
        {
            jQuery(this).addClass('selected');
            return false;
        }

        pattern = RegExp('^'+pathname);
        if(pattern.test(window.location.pathname) && pathname != '/')
        {
            jQuery(this).addClass('selected');
        }
    }

    function hide()
    {
        jQuery(this).css('display', 'none');
    }

    function showIfCurrent()
    {
        // Skip homepage
        if(window.location.pathname == '/')
            return;

        // Check links inside our list
        mylist = jQuery(this);
        anchors = mylist.find('a');
        link_found = false;
        pattern = RegExp('^'+window.location.pathname);
        for(i=0; i<anchors.length; i++)
        {
            a = anchors[i];
            pathname = a.pathname;
            if( ! RegExp('^/{1}').test(pathname) )
                pathname = '/'+pathname;

            if(pattern.test(pathname))
                mylist.css('display', 'inline');

            //alert(a.pathname + ' -> ' + window.location.pathname);
        }
    }

    // Initialisation -- have init() run once page has finished loading
    jQuery(document).ready( init );

})();

/*
HTML5 Placeholder text.

A manual implementation of the HTML5 placeholder attribute.
*/
(function()
{
    /**
    Provides 'auto-hiding' default values in text and textarea fields.
    The default values are obtained from the HTML using the alt attribute.  When
    this script is run, default values are added to every form text input and
    text area element that has an alt attribute.

    Adding default values is as simple as including this file and adding non-empty
    alt tags to your text input and textarea elements.

    @author    Leon Matthews <leon@lost.co.nz>
    @copyright Copyright 2008-2010 Leon Matthews. All rights reserved.
    @link      http://www.lost.co.nz/
    */

    /**
    Remove default text from form element on focus
    */
    function onFocusHandler()
    {
        var alt = this.getAttribute('placeholder');
        if( this.value == alt)
        {
            this.value = '';
        }
    }

    /**
    Restore default text on form element on blur, if empty
    */
    function onBlurHandler()
    {
        var alt = this.getAttribute('placeholder');
        if( ! this.value )
        {
            this.value = alt;
        }
    }

    /**
    Add event handlers to all form elements in document that have alt attributes
    */
    function init()
    {
        var i, needsValidation, form, j, elem, alt;

        // Loop through all forms in document
        for( i=0; i<document.forms.length; i++)
        {
            needsValidation = false;
            form = document.forms[i];

            // Loop through elements in current form
            for( j=0; j < form.elements.length; j++ )
            {
                elem = form.elements[j];

                // We care about text, and Textarea elements
                if( elem.type == 'text' || elem.type == 'textarea')
                {
                    alt = elem.getAttribute('placeholder');
                    if( alt )
                    {
                        // Add default text (if empty)
                        if( ! elem.value )
                        {
                            elem.value = alt;
                        }

                        // Install event handlers
                        elem.onfocus = onFocusHandler;
                        elem.onblur = onBlurHandler;
                    }
                }
            }
        }
    }

    // Initialisation -- have init() run once page has finished loading
    var $;
    if( $ && $(document).ready )        // jQuery
    {
        $(document).ready( init );
    }
    else if( window.addEventListener)   // DOM Level-2
    {
        window.addEventListener('load', init, false);
    }
    else if( window.attachEvent)        // Sigh... Internet Explorer
    {
        window.attachEvent('onload', init);
    }

})();

