

var ddsmoothmenu={
    detectwebkit: navigator.userAgent.toLowerCase().indexOf("applewebkit")!=-1, //detect WebKit browsers (Safari, Chrome etc)
    detectie6: document.all && !window.XMLHttpRequest,
    buildmenu:function(jQuery, setting){
        var smoothmenu=ddsmoothmenu
        var jQuerymainmenu=jQuery("#"+setting.mainmenuid+">ul") //reference main menu UL
        jQuerymainmenu.parent().get(0).className=setting.classname || "ddsmoothmenu"
        var jQueryheaders=jQuerymainmenu.find("ul.nlvl0,ul.nlvl1,ul.nlvl2").parent()
        jQueryheaders.hover(
            function(e){
                jQuery(this).addClass('selected')
                jQuery(this).children('a:eq(0)').addClass('selected')
            },
            function(e){
                jQuery(this).removeClass('selected')
                jQuery(this).children('a:eq(0)').removeClass('selected')
            }
        )
        
        jQueryheaders=jQuerymainmenu.find("ul.nlvl1").parent() /*ul.nlvl0,*/
        jQueryheaders.each(function(i){ //loop through each LI header
            var jQuerycurobj=jQuery(this).css({zIndex: 100-i}) //reference current LI header
            var jQuerysubul=jQuery(this).find('ul:eq(0)').css({display:'block'})
            this._dimensions={w:this.offsetWidth, h:this.offsetHeight, subulw:jQuerysubul.outerWidth(), subulh:jQuerysubul.outerHeight()}
            this.istopheader=jQuerycurobj.parents("ul").length==1? true : false //is top level header?
            //jQuerysubul.css({top:this.istopheader && setting.orientation!='v'? this._dimensions.h+"px" : 0})
            jQuerysubul.css({top:'36px'})
        
            var maxColHeight = (jQuerysubul.outerHeight() / 2)
            maxColHeight<130 ? maxColHeight=jQuerysubul.outerHeight() : false;
            var spacingTopBottom = 15
            var colHeight = 0
            var colHeightLag = 0
            var bOneCol = true
            var bTwoCol = false
            
            jQuerysubul.css({height:(maxColHeight+spacingTopBottom+spacingTopBottom+spacingTopBottom)+'px'})
            
            jQuery.each(jQuerysubul.children('li'),function(){
                if (bOneCol) {
                    jQuery(this).css({position:'absolute', top:colHeight+spacingTopBottom+'px'})
                    colHeight=colHeight+jQuery(this).outerHeight()
                    colHeight >= maxColHeight ? bOneCol=false : false;
                }
                else {
                    bTwoCol=true
                    jQuery(this).css({position:'absolute', top:colHeightLag+spacingTopBottom+'px', left:'230px'})
                    colHeightLag=colHeightLag+jQuery(this).outerHeight()
                }
            });
            
            if (bTwoCol) {
                jQuerysubul.css({width:'460px',height:colHeight+spacingTopBottom+spacingTopBottom+'px'})
            }
            else {
                jQuerysubul.css({width:'230px',height:colHeight+spacingTopBottom+spacingTopBottom+'px'})
            }
            this._dimensions={w:this.offsetWidth, h:this.offsetHeight, subulw:jQuerysubul.outerWidth(), subulh:jQuerysubul.outerHeight()}
            
            jQuerycurobj.hover(
                function(e){
                    var header=jQuerycurobj.get(0) //reference header LI as DOM object
                    header._offsets={left:jQuerycurobj.offset().left, top:jQuerycurobj.offset().top}
                    var menuleft=header.istopheader && setting.orientation!='v'? 0 : header._dimensions.w
                    menuleft=(header._offsets.left+menuleft+header._dimensions.subulw>jQuery(window).width())? (header.istopheader && setting.orientation!='v'? -header._dimensions.subulw+header._dimensions.w : -header._dimensions.w) : menuleft //calculate this sub menu's offsets from its parent
                    jQuerysubul.css({top:"36px", display:'block', visibility:'visible', left:menuleft+"px", width:header._dimensions.subulw+'px'})
                },
                function(e){
                    jQuerysubul.css({display:'none'})
                }
            ) //end hover
        }) //end jQueryheaders.each()
    },
    init:function(setting){
        ddsmoothmenu.buildmenu(jQuery, setting)
        jQuery('.ddsmoothmenu ul.nlvl1').css({display:'none'})
    }
} //end ddsmoothmenu variable

function ieFixSubnavHeader()
{
	jQuery('li.nlvl1').each(
		function(index, element)
		{
			var el = jQuery(element);
			var blocker = jQuery('<div></div>');
			blocker.css({'width': el.width() - 6, 'height': '3px', 'background-color': 'white', 'position': 'absolute', 'z-index': 500, 'left': '3px', 'top': el.height()});
			blocker.addClass('subnavBlocker');
			el.append(blocker);
		}
	);
}
