﻿var ddsmoothmenu = {
    transition: { overtime: 300, outtime: 300 },
    ///////Stop configuring beyond here///////////////////////////
    detectwebkit: navigator.userAgent.toLowerCase().indexOf("applewebkit") != -1, //detect WebKit browsers (Safari, Chrome etc)   

    buildmenu: function($, setting) {
        var smoothmenu = ddsmoothmenu
        var $mainmenu = $("#" + setting.mainmenuid + ">ul") //reference main menu UL
        var $headers = $mainmenu.find("ul").parent()
        $headers.each(function(i) {
            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 ? this._dimensions.h + "px" : 0 })

            $curobj.hover(
			function(e) {
			    var $targetul = $(this).children("ul:eq(0)")
			    this._offsets = { left: $(this).offset().left, top: $(this).offset().top }
			    var menuleft = this.istopheader ? 0 : this._dimensions.w
			    menuleft = (this._offsets.left + menuleft + this._dimensions.subulw > $(window).width()) ? (this.istopheader ? -this._dimensions.subulw + this._dimensions.w : -this._dimensions.w) : menuleft //calculate this sub menu's offsets from its parent
			    if ($targetul.queue().length <= 1) { //if 1 or less queued animations
			        $targetul.css({ left: menuleft + "px", width: this._dimensions.subulw + 'px' }).animate({ height: 'show', opacity: 'show' }, ddsmoothmenu.transition.overtime)

			    }
			},
			function(e) {
			    var $targetul = $(this).children("ul:eq(0)")
			    $targetul.animate({ height: 'hide', opacity: 'hide' }, ddsmoothmenu.transition.outtime)

			}
		) //end hover
        }) //end $headers.each()
        $mainmenu.find("ul").css({ display: 'none', visibility: 'visible' })
    },

    init: function(setting) {
        if (typeof setting.customtheme == "object" && setting.customtheme.length == 2) {
            var mainmenuid = '#' + setting.mainmenuid
            document.write('<style type="text/css">\n'
			+ mainmenuid + ', ' + mainmenuid + ' ul li a {background:' + setting.customtheme[0] + ';}\n'
			+ mainmenuid + ' ul li a:hover {background:' + setting.customtheme[1] + ';}\n'
		+ '</style>')
        }
        jQuery(document).ready(function($) { //override default menu colors (default/hover) with custom set?
            if (typeof setting.contentsource == "object") { //if external ajax menu
                ddsmoothmenu.getajaxmenu($, setting)
            }
            else { //else if markup menu
                ddsmoothmenu.buildmenu($, setting)
            }
        })
    }

} //end ddsmoothmenu variable

    //Initialize Menu instance(s):
    ddsmoothmenu.init({ mainmenuid: "divSmoothMenu", contentsource: "markup" })