/*
* Placeholder plugin for jQuery
* ---
* Copyright 2010, Daniel Stocks (http://webcloud.se)
* Released under the MIT, BSD, and GPL Licenses.
*/

(function(b){function d(a){this.input=a;a.attr("type")=="password"&&this.handlePassword();b(a[0].form).submit(function(){if(a.hasClass("placeholder")&&a[0].value==a.attr("placeholder"))a[0].value=""})}d.prototype={show:function(a){if(this.input[0].value===""||a&&this.valueIsPlaceholder()){if(this.isPassword)try{this.input[0].setAttribute("type","text")}catch(b){this.input.before(this.fakePassword.show()).hide()}this.input.addClass("placeholder");this.input[0].value=this.input.attr("placeholder")}},
hide:function(){if(this.valueIsPlaceholder()&&this.input.hasClass("placeholder")&&(this.input.removeClass("placeholder"),this.input[0].value="",this.isPassword)){try{this.input[0].setAttribute("type","password")}catch(a){}this.input.show();this.input[0].focus()}},valueIsPlaceholder:function(){return this.input[0].value==this.input.attr("placeholder")},handlePassword:function(){var a=this.input;a.attr("realType","password");this.isPassword=!0;if(b.browser.msie&&a[0].outerHTML){var c=b(a[0].outerHTML.replace(/type=(['"])?password\1/gi,
"type=$1text$1"));this.fakePassword=c.val(a.attr("placeholder")).addClass("placeholder").focus(function(){a.trigger("focus");b(this).hide()});b(a[0].form).submit(function(){c.remove();a.show()})}}};var e=!!("placeholder"in document.createElement("input"));b.fn.placeholder=function(){return e?this:this.each(function(){var a=b(this),c=new d(a);c.show(!0);a.focus(function(){c.hide()});a.blur(function(){c.show(!1)});b.browser.msie&&(b(window).load(function(){a.val()&&a.removeClass("placeholder");c.show(!0)}),
a.focus(function(){if(this.value==""){var a=this.createTextRange();a.collapse(!0);a.moveStart("character",0);a.select()}}))})}})(jQuery);

/*
 * jQuery Easing v1.3 - http://gsgd.co.uk/sandbox/jquery/easing/
 *
 * Uses the built in easing capabilities added In jQuery 1.1
 * to offer multiple easing options
 *
 * TERMS OF USE - jQuery Easing
 * 
 * Open source under the BSD License. 
 * 
 * Copyright © 2008 George McGinley Smith
 * All rights reserved.
 *
 */
 
$.extend($.easing, {
    easeOutCubic: function (x, t, b, c, d) {
    	return c*((t=t/d-1)*t*t + 1) + b;
    },
    easeInCubic: function (x, t, b, c, d) {
    	return c*(t/=d)*t*t + b;
    },
    easeInOutCubic: function (x, t, b, c, d) {
    	if ((t/=d/2) < 1) return c/2*t*t*t + b;
    	return c/2*((t-=2)*t*t + 2) + b;
    }
});

(function($) {
    function emailAddress(address, makeUrl) {
        this.fakeAddress = address;
        this.makeUrl = makeUrl;
    }
    emailAddress.prototype = {
        properAddress: function() {
            return this.fakeAddress.replace("&nbsp;", "@");
        },
        build: function() {
            if (this.makeUrl) {
                return $("<a/>", {
                    href: "mailto:" + this.properAddress(),
                    text: this.properAddress()
                });
            } else {
                return this.properAddress();
            }
        }
    };
    $.fn.emailify = function() {
        return this.each(function() {
            var address = $(this).html();
            var makeUrl = $(this).hasClass("url");
            var email = new emailAddress(address, makeUrl);
            $(this).html(email.build());
        });
    };
})(jQuery);

$(document).ready(function() {

    /* PLACEHOLDER TEXT IN FORMS */
    $("input[title]").each(function() {
        $(this).attr("placeholder", $(this).attr("title"));
        $(this).removeAttr("title");
    })
    $("input[placeholder], textarea[placeholder]").placeholder();

    // Make clickable email addresses of <span class="email url">email&nbsp;address.com</span>
    $("span.email").emailify();

    /* FORM ACTIVE BEHAVIOUR */
    $("form input").focus(function() {
        $(this).addClass("active");
    });
    $("form input").blur(function() {
        $(this).removeClass("active");
    });
    $("form button").bind("mouseover focus click", function() {
        $(this).addClass("active");
    });
    $("form button").bind("mouseout blur", function() {
        $(this).removeClass("active");
    });

    /* CONTRAST */
    $("a#contrast").click(function(event) {
       event.preventDefault();
       if ($("body").hasClass("contrast")) {
           $("body").removeClass("contrast");
       } else {
           $("body").addClass("contrast");
       }
    });
    
    /* DROP DOWN MENU */
    $("header nav").extend($.gmh.navigation).initialize();
    
});

$.gmh = {};

$.gmh.navigation = {
    options: {
        menuContainerSelector: "ol.grandpa",
        menuBarSelector: "div.menubar",
        menuItemSelector: "ol li",
        menuWithRoundedCornersSelector: "div.menubar > ol",
        roundedClass: "rounded",
        hoverTime: 200,
        foldoutSpeed: 200,
        foldoutEasing: "easeInCubic",
        foldinSpeed: 200,
        foldinEasing: "easeOutCubic"
    },
    initialize: function(options) {
        this.options = $.extend(this.options, options);

        this.menuContainerElement = $(this).find(this.options.menuContainerSelector);
        this.menuItemElement = $(this).find(this.options.menuItemSelector);
        this.menuWithRoundedCornersElement = $(this).find(this.options.menuWithRoundedCornersSelector);
        
        // Remember menu's height and then set it to the menubar height; used for animation purposes
        this.maxHeight = this.menuContainerElement.height();
        this.startHeight = $(this).find(this.options.menuBarSelector).height();
        this.menuContainerElement.height(this.startHeight);
        
        this.attachHandlers();
    },
    /**
     * Attach handlers;
     * - Bit flaky way to make hyperlinks inside the menubar work; they refer to parent() which probably isn't
     *   maintenance safe
     * - Simple hovering over the menubar; this also works when menu is open and user moves mouse over 
     *   different columns. 
     * - Also use a delayed foldout menu for the entire navigation area
     */
    attachHandlers: function() {
        $(this.menuItemElement).hover(function() {
            $(this).addClass("open");                
        }, function() {
            $(this).removeClass("open");
        });
        $(this).hover($.proxy(function() {
            this.openFull();
        }, this), $.proxy(function() {
            this.closeFull();
        }, this));
    },
    /** 
     * Open Full menu with a delay to make sure the user isn't just hovering by 
     * Animate to the calculated maximum height of the full menu
     */ 
    openFull: function() {
        this.allopenTimer = setTimeout($.proxy(function() {
            $(this.menuContainerElement).stop().animate(
                {height: this.maxHeight},
                this.options.foldoutSpeed,
                this.options.foldoutEasing,
                $.proxy(function(){
                    this.menuWithRoundedCornersElement.addClass(this.options.roundedClass);
                }, this)
            );
        }, this), this.options.hoverTime);
        $(this).addClass("open");
    },
    /** 
     * Close the menu; clear the timeout will make sure that if user moved away, it'll never open
     * Animate to the default height of the menu bar
     */
    closeFull: function() {
        clearTimeout(this.allopenTimer);
        $(this.menuContainerElement).stop().animate(
            {height: this.startHeight},
            this.options.foldinSpeed,
            this.options.foldinEasing,
            $.proxy(function() {
                this.removeClass("open");
                this.menuWithRoundedCornersElement.removeClass(this.options.roundedClass);
            }, this)
        );
    }
}
