

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