
var ddsmoothmenu={
  transition: {overtime:100, outtime:100}, //duration of slide in/ out animation, in milliseconds
  detectwebkit: navigator.userAgent.toLowerCase().indexOf("applewebkit")!=-1, //detect WebKit browsers (Safari, Chrome etc)

getajaxmenu:function($, setting){ 
  var $menucontainer=$('#'+setting.contentsource[0]) 
  $menucontainer.html("Loading Menu...")
  $.ajax({
    url: setting.contentsource[1], //path to external menu file
    async: true,
    error:function(ajaxrequest){
    $menucontainer.html('Error fetching content. Server Response: '+ajaxrequest.responseText)
  },
  success:function(content){
    $menucontainer.html(content)
    ddsmoothmenu.buildmenu($, setting)
  }
})},

buildmenu:function($, setting){
  var smoothmenu=ddsmoothmenu
  var $mainmenu=$("#"+setting.mainmenuid+">ul") //reference main menu UL
  var $headers=$mainmenu.find("ul").parent()
  $headers.hover(
    function(e){
      $(this).children('a:eq(0)').addClass('selected')
    },
    function(e){
      $(this).children('a:eq(0)').removeClass('selected')
    }
  )
  $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.children("a:eq(0)").css(this.istopheader? {} : {}).append()

  $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)
    }
  ) 
}) 

$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)
    }
  })
}

} 

ddsmoothmenu.init({
	mainmenuid: "smoothmenu1", 
	contentsource: "markup" 
})