/* jQuery Mega Menu v1.02
* Last updated: June 29th, 2009. This notice must stay intact for usage 
* Author: JavaScript Kit at http://www.javascriptkit.com/
* Visit http://www.javascriptkit.com/script/script2/jScale/ for full source code
*/

//jQuery.noConflict();

var jPrimaryNavigation = {

    effectduration: 200, //duration of animation, in milliseconds
    delaytimer: 200, //delay after mouseout before menu should be hidden, in milliseconds

    //No need to edit beyond here
    PrimaryNavigationlabels: [],
    PrimaryNavigations: [], //array to contain each block menu instances
    zIndexVal: 1000, //starting z-index value for drop down menu
    $shimobj: null,

    addshim: function ($) {
        $(document.body).append('<IFRAME id="outlineiframeshim" src="' + (location.protocol == "https:" ? '/empty.htm' : 'about:blank') + '" style="display:none; left:0; top:0; z-index:999; position:absolute; filter:progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)" frameBorder="0" scrolling="no"></IFRAME>')
        this.$shimobj = $("#outlineiframeshim")
    },

    alignmenu: function ($, e, PrimaryNavigation_pos) {
        var PrimaryNavigation = this.PrimaryNavigations[PrimaryNavigation_pos]
        var $anchor = PrimaryNavigation.$anchorobj
        var $menu = PrimaryNavigation.$menuobj
        var menuleft = ($(window).width() - (PrimaryNavigation.offsetx - $(document).scrollLeft()) > PrimaryNavigation.actualwidth) ? PrimaryNavigation.offsetx : PrimaryNavigation.offsetx - PrimaryNavigation.actualwidth + PrimaryNavigation.anchorwidth //get x coord of menu
        //var menutop=($(window).height()-(PrimaryNavigation.offsety-$(document).scrollTop()+PrimaryNavigation.anchorheight)>PrimaryNavigation.actualheight)? PrimaryNavigation.offsety+PrimaryNavigation.anchorheight : PrimaryNavigation.offsety-PrimaryNavigation.actualheight
        var menutop = PrimaryNavigation.offsety + PrimaryNavigation.anchorheight  //get y coord of menu
        $menu.css({ left: menuleft - 2 + "px", top: menutop + "px" })
        this.$shimobj.css({ height: PrimaryNavigation.actualheight + "px", left: menuleft - 2 + "px", top: menutop + "px", display: "block" })





    },

    showmenu: function (e, PrimaryNavigation_pos) {
        var PrimaryNavigation = this.PrimaryNavigations[PrimaryNavigation_pos]
        var $menu = PrimaryNavigation.$menuobj
        var $menuinner = PrimaryNavigation.$menuinner
        if ($menu.css("display") == "none") {
            this.alignmenu(jQuery, e, PrimaryNavigation_pos)
            $menu.css("z-index", ++this.zIndexVal)
            $menu.slideDown(this.effectduration, function () {
                $menuinner.css('visibility', 'visible')
            })
        }
        else if ($menu.css("display") == "block" && e.type == "click") { //if menu is hidden and this is a "click" event (versus "mouseout")
            this.hidemenu(e, PrimaryNavigation_pos)
        }
        return false
    },

    hidemenu: function (e, PrimaryNavigation_pos) {

        var PrimaryNavigation = this.PrimaryNavigations[PrimaryNavigation_pos]
        var $menu = PrimaryNavigation.$menuobj
        var $menuinner = PrimaryNavigation.$menuinner
        $menuinner.css('visibility', 'hidden')
        this.$shimobj.css({ display: "none", left: 0, top: 0 })
        $menu.slideUp(this.effectduration)
    },

    definemenu: function (anchorid, menuid, revealtype) {
        this.PrimaryNavigationlabels.push([anchorid, menuid, revealtype])
    },

    render: function ($) {
        for (var i = 0, labels = this.PrimaryNavigationlabels[i]; i < this.PrimaryNavigationlabels.length; i++, labels = this.PrimaryNavigationlabels[i]) {
            if ($('#' + labels[0]).length != 1 || $('#' + labels[1]).length != 1) //if one of the two elements are NOT defined, exist
                return
            this.PrimaryNavigations.push({ $anchorobj: $("#" + labels[0]), $menuobj: $("#" + labels[1]), $menuinner: $("#" + labels[1]).children('ul:first-child'), revealtype: labels[2], hidetimer: null })
            var PrimaryNavigation = this.PrimaryNavigations[i]
            PrimaryNavigation.$anchorobj.add(PrimaryNavigation.$menuobj).attr("_PrimaryNavigationpos", i + "pos") //remember index of this drop down menu
            PrimaryNavigation.actualwidth = PrimaryNavigation.$menuobj.outerWidth()
            PrimaryNavigation.actualheight = PrimaryNavigation.$menuobj.outerHeight()
            PrimaryNavigation.offsetx = PrimaryNavigation.$anchorobj.offset().left
            PrimaryNavigation.offsety = PrimaryNavigation.$anchorobj.offset().top
            PrimaryNavigation.anchorwidth = PrimaryNavigation.$anchorobj.outerWidth()
            PrimaryNavigation.anchorheight = PrimaryNavigation.$anchorobj.outerHeight()
            $(document.body).append(PrimaryNavigation.$menuobj) //move drop down menu to end of document
            PrimaryNavigation.$menuobj.css("z-index", ++this.zIndexVal).hide()
            PrimaryNavigation.$menuinner.css("visibility", "hidden")
            PrimaryNavigation.$anchorobj.bind(PrimaryNavigation.revealtype == "click" ? "click" : "mouseenter", function (e) {
                var menuinfo = jPrimaryNavigation.PrimaryNavigations[parseInt(this.getAttribute("_PrimaryNavigationpos"))]
                clearTimeout(menuinfo.hidetimer) //cancel hide menu timer
                return jPrimaryNavigation.showmenu(e, parseInt(this.getAttribute("_PrimaryNavigationpos")))
            })
            PrimaryNavigation.$anchorobj.bind("mouseleave", function (e) {
                var menuinfo = jPrimaryNavigation.PrimaryNavigations[parseInt(this.getAttribute("_PrimaryNavigationpos"))]
                if (e.relatedTarget != menuinfo.$menuobj.get(0) && $(e.relatedTarget).parents("#" + menuinfo.$menuobj.get(0).id).length == 0) { //check that mouse hasn't moved into menu object
                    menuinfo.hidetimer = setTimeout(function () { //add delay before hiding menu
                        jPrimaryNavigation.hidemenu(e, parseInt(menuinfo.$menuobj.get(0).getAttribute("_PrimaryNavigationpos")))
                    }, jPrimaryNavigation.delaytimer)
                }
            })
            PrimaryNavigation.$menuobj.bind("mouseenter", function (e) {
                var menuinfo = jPrimaryNavigation.PrimaryNavigations[parseInt(this.getAttribute("_PrimaryNavigationpos"))]
                clearTimeout(menuinfo.hidetimer) //cancel hide menu timer
            })
            PrimaryNavigation.$menuobj.bind("mouseleave", function (e) {
                var menuinfo = jPrimaryNavigation.PrimaryNavigations[parseInt(this.getAttribute("_PrimaryNavigationpos"))]
                menuinfo.hidetimer = setTimeout(function () { //add delay before hiding menu
                    jPrimaryNavigation.hidemenu(e, parseInt(menuinfo.$menuobj.get(0).getAttribute("_PrimaryNavigationpos")))
                }, jPrimaryNavigation.delaytimer)
            })
        } //end for loop
        if (/Safari/i.test(navigator.userAgent)) { //if Safari
            $(window).bind("resize load", function () {
                for (var i = 0; i < jPrimaryNavigation.PrimaryNavigations.length; i++) {
                    var PrimaryNavigation = jPrimaryNavigation.PrimaryNavigations[i]
                    var $anchorisimg = (PrimaryNavigation.$anchorobj.children().length == 1 && PrimaryNavigation.$anchorobj.children().eq(0).is('img')) ? PrimaryNavigation.$anchorobj.children().eq(0) : null
                    if ($anchorisimg) { //if anchor is an image link, get offsets and dimensions of image itself, instead of parent A
                        PrimaryNavigation.offsetx = $anchorisimg.offset().left
                        PrimaryNavigation.offsety = $anchorisimg.offset().top
                        PrimaryNavigation.anchorwidth = $anchorisimg.width()
                        PrimaryNavigation.anchorheight = $anchorisimg.height()
                    }
                }
            })
        }
        else {
            $(window).bind("resize", function () {
                for (var i = 0; i < jPrimaryNavigation.PrimaryNavigations.length; i++) {
                    var PrimaryNavigation = jPrimaryNavigation.PrimaryNavigations[i]
                    PrimaryNavigation.offsetx = PrimaryNavigation.$anchorobj.offset().left
                    PrimaryNavigation.offsety = PrimaryNavigation.$anchorobj.offset().top
                }
            })
        }
        jPrimaryNavigation.addshim($)
    }

}

jQuery(document).ready(function ($) {
    jPrimaryNavigation.render($)
})


