﻿jQuery.noConflict();
//adjust dropdown
var leftAdjust = 20;

var rightmegamenu = {

    effectduration: 500, //duration of animation, in milliseconds
    delaytimer: 500, //delay after mouseout before menu should be hidden, in milliseconds    
    megamenulabels: [],
    megamenus: [], //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:" ? 'blank.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, megamenu_pos) {
        var megamenu = this.megamenus[megamenu_pos]
        var $anchor = megamenu.$anchorobj
        var $menu = megamenu.$menuobj
        //var menuleft = ($(window).width() - (megamenu.offsetx - $(document).scrollLeft()) > megamenu.actualwidth) ? megamenu.offsetx : megamenu.offsetx - megamenu.actualwidth + megamenu.anchorwidth//get x coord of menu
        var menuright = megamenu.offsetx - megamenu.actualwidth + megamenu.anchorwidth
        var menutop = megamenu.offsety + megamenu.anchorheight   //get y coord of menu        
        $menu.css({ left: menuright + "px", top: menutop + "px" })
        this.$shimobj.css({ width: megamenu.actualwidth + "px", height: megamenu.actualheight + "px", left: menuright + "px", top: menutop + "px", display: "block" })
    },

    showmenu: function(e, megamenu_pos) {
        var megamenu = this.megamenus[megamenu_pos]
        var $menu = megamenu.$menuobj
        var $menuinner = megamenu.$menuinner
        if ($menu.css("display") == "none") {
            this.alignmenu(jQuery, e, megamenu_pos)
            $menu.css("z-index", ++this.zIndexVal)
            $menu.stop(true, true).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, megamenu_pos)
        }
        return false
    },

    hidemenu: function(e, megamenu_pos) {
        var megamenu = this.megamenus[megamenu_pos]
        var $menu = megamenu.$menuobj
        var $menuinner = megamenu.$menuinner
        $menuinner.css('visibility', 'hidden')
        this.$shimobj.css({ display: "none", left: 0, top: 0 });
        $menu.stop(true, true).hide()
    },

    definemenu: function(anchorid, menuid, revealtype, liId) {
        this.megamenulabels.push([anchorid, menuid, revealtype, liId])
    },

    findPosX: function(obj) {
        var curleft = 0;
        if (obj.offsetParent)
            while (1) {
            curleft += obj.offsetLeft;
            if (!obj.offsetParent)
                break;
            obj = obj.offsetParent;
        }
        else if (obj.x)
            curleft += obj.x;
        return curleft;
    },

    findPosY: function(obj) {
        var curtop = 0;
        if (obj.offsetParent)
            while (1) {
            curtop += obj.offsetTop;
            if (!obj.offsetParent)
                break;
            obj = obj.offsetParent;
        }
        else if (obj.y)
            curtop += obj.y;
        return curtop;
    },


    render: function($) {
        for (var i = 0, labels = this.megamenulabels[i]; i < this.megamenulabels.length; i++, labels = this.megamenulabels[i]) {
            if ($('#' + labels[0]).length != 1 || $('#' + labels[1]).length != 1) //if one of the two elements are NOT defined, exist
                return
            this.megamenus.push({ $anchorobj: $("#" + labels[0]), $menuobj: $("#" + labels[1]), $menuinner: $("#" + labels[1]).children('ul:first-child'), revealtype: labels[2], hidetimer: null, $liObject: $("#" + labels[3]) })
            var megamenu = this.megamenus[i]
            megamenu.$liObject.add(megamenu.$menuobj).attr("_megamenupos", i + "pos")
            megamenu.$anchorobj.add(megamenu.$menuobj).attr("_megamenupos", i + "pos") //remember index of this drop down menu
            megamenu.actualwidth = 325 
            megamenu.actualheight = megamenu.$menuobj.outerHeight()           
            megamenu.offsetx = findPosX(document.getElementById(labels[3]))
            megamenu.offsety = findPosY(document.getElementById(labels[3]))
            megamenu.anchorwidth = megamenu.$liObject.outerWidth()
            megamenu.anchorheight = megamenu.$liObject.outerHeight()            
            megamenu.liwidth = megamenu.$liObject.outerWidth()
            $(document.body).append(megamenu.$menuobj) //move drop down menu to end of document
            megamenu.$menuobj.css("z-index", ++this.zIndexVal).hide()
            megamenu.$menuinner.css("visibility", "hidden")
            var isMouseOver = false;
            megamenu.$menuobj.mouseenter(function() {
                isMouseOver = true;
            });
            megamenu.$menuobj.mouseleave(function(e) {
                isMouseOver = false;
                var menuinfo = rightmegamenu.megamenus[parseInt(this.getAttribute("_megamenupos"))];
                menuinfo.hidetimer = setTimeout(function() {
                    rightmegamenu.hidemenu(e, parseInt(menuinfo.$menuobj.get(0).getAttribute("_megamenupos")))
                }, rightmegamenu.delaytimer);

            });
            config = {
                sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)
                interval: 250, // number = milliseconds for onMouseOver polling interval
                over: function(e) {
                    var menuinfo = rightmegamenu.megamenus[parseInt(this.getAttribute("_megamenupos"))];
                    rightmegamenu.showmenu(e, parseInt(menuinfo.$menuobj.get(0).getAttribute("_megamenupos")));
                }, // function = onMouseOver callback (REQUIRED)
                timeout: 250, // number = milliseconds delay before onMouseOut
                out:
                    function(e) {
                        if (!isMouseOver) {
                            var menuinfo = rightmegamenu.megamenus[parseInt(this.getAttribute("_megamenupos"))];
                            menuinfo.hidetimer = setTimeout(function() {
                                rightmegamenu.hidemenu(e, parseInt(menuinfo.$menuobj.get(0).getAttribute("_megamenupos")))
                            }, rightmegamenu.delaytimer);
                        } else {
                            return;
                        }
                    } // function = onMouseOut callback (REQUIRED)
            };
            //$("ul#global-nav-menu li").hoverIntent(config);

            megamenu.$liObject.hoverIntent(config);

        } //end for loop
        if (/Safari/i.test(navigator.userAgent)) { //if Safari
            $(window).bind("resize load", function() {
                for (var i = 0; i < rightmegamenu.megamenus.length; i++) {
                    var megamenu = rightmegamenu.megamenus[i]
                    var $anchorisimg = (megamenu.$anchorobj.children().length == 1 && megamenu.$anchorobj.children().eq(0).is('img')) ? megamenu.$anchorobj.children().eq(0) : null
                    if ($anchorisimg) { //if anchor is an image link, get offsets and dimensions of image itself, instead of parent A
                        megamenu.offsetx = $anchorisimg.offset().left
                        megamenu.offsety = $anchorisimg.offset().top
                        megamenu.anchorwidth = $anchorisimg.width()
                        megamenu.anchorheight = $anchorisimg.height()
                    }
                }
            })
        }
        else {
            $(window).bind("resize", function() {
                for (var i = 0; i < rightmegamenu.megamenus.length; i++) {
                    var megamenu = rightmegamenu.megamenus[i]
                    megamenu.offsetx = megamenu.$anchorobj.offset().left
                    megamenu.offsety = megamenu.$anchorobj.offset().top
                }
            })
        }
        rightmegamenu.addshim($)
    }

}

jQuery(document).ready(function($) {

    rightmegamenu.render($)  

})