﻿var header = {
    curMenu: {},
    defSearchText: {},
    defEmail: {},
    defMobil: {},

    init: function() {
        this.defEmail = "Enter email address";
        this.defMobil = "Enter mobile number";
        var emailBox = $(".signupemail");
        var mobileBox = $(".signupmobile");

        emailBox.attr("value", header.defEmail).focus(function() {
            if ($(this).attr("value") == header.defEmail) $(this).attr("value", "")
        }).blur(function() {
            if ($(this).attr("value") == "") {
                $(this).attr("value", header.defEmail);
            }
        });

        mobileBox.attr("value", header.defMobil).focus(function() {
            if ($(this).attr("value") == header.defMobil) $(this).attr("value", "")
        }).blur(function() {
            if ($(this).attr("value") == "") {
                $(this).attr("value", header.defMobil);
            }
        });

        $(".searchTerm").keypress(function(event) {
            if ((event.which && event.which == 13) || (event.keyCode && event.keyCode == 13)) {
                if ($(this).attr("value") == "") {
                    event.preventDefault();
                } else {
                    document.getElementById('ctl00_search_button').focus();
                }
            } else return true
        })

        var siteLinks = $("#siteNav a");
        var navMenuShadow = $("#navMenuShadow")

        siteLinks.mouseenter(function() {
            //$(this).prev().css("visibility", "visible");
        if (($("#siteNav li").index($(this).parent()) < 14) && ($("#siteNav li").index($(this).parent()) != 4) && ($("#siteNav li").index($(this).parent()) != 10)) {
                header.curMenu = $(".dropDownMenu").eq($("#siteNav li").index($(this).parent()) / 2 + 0.5);
                var locleft = $(this).position().left;
                if (header.curMenu.width() <= $(this).parent().width()) {
                    header.curMenu.width($(this).parent().width() + 5);
                    header.curMenu.css("background-color", "#C4C4C4");
                }
                if (header.curMenu.width() > (965 - locleft)) locleft = (locleft + 7) - (header.curMenu.width() - $(this).width()); //locleft = locleft - (locleft + menuToShow.width() - 965)
                header.curMenu.css("left", locleft).fadeIn(200);
                //$("#navMenuJoint").show().css("left", locleft).height(5).width($(this).parent().width());
                navMenuShadow.show().css("left", locleft + 5).height(header.curMenu.height() - 5).width(header.curMenu.width());
                $(".navMenuJoint").eq($("#siteNav li").index($(this).parent())).show().height(3).width($(this).parent().width());
            }
        })
        siteLinks.mouseleave(function(event) {
            $(this).prev().css("visibility", "hidden");
            if (!header.determineHide(header.curMenu.get(0), event)) {
                //$("#navMenuJoint").hide(0);
                navMenuShadow.hide(0);
                header.curMenu.hide(0);
            }
        })
        navMenuShadow.fadeTo(0, 0.6)
        $(".dropDownMenu").mouseleave(function(event) {
            //if (event.toElement.id != 'navMenuJoint') {
            $(this).hide(0);
            //$("#navMenuJoint").hide(0);
            navMenuShadow.hide(0);
            //}
        })
    },

    determineHide: function(menu, e) {
        var e = window.event || e
        var c = e.relatedTarget || e.toElement
        while (c && c != menu) try { c = c.parentNode } catch (e) { c = menu }
        if (c == null) {
            return false;
        }
        if (c == menu) {
            return true;
        }
        if (e.toElement.id == 'navMenuJoint') {
            return true;
        }
        else {
            return false;
        }
    }
}

$(document).ready(function() {
    header.init();
});