/*********************
//* jQuery Multi Level CSS Menu #2- By Dynamic Drive: http://www.dynamicdrive.com/
//* Last update: Nov 7th, 08': Limit # of queued animations to minmize animation stuttering
//* Menu avaiable at DD CSS Library: http://www.dynamicdrive.com/style/
*********************/

//Update: April 12th, 10: Fixed compat issue with jquery 1.4x

//Specify full URL to down and right arrow images (23 is padding-right to add to top level LIs with drop downs):
var arrowimages={down:['downarrowclass', '', 23], right:['rightarrowclass', '']}

var jqueryslidemenu={

animateduration: {over: 200, out: 200}, //duration of slide in/ out animation, in milliseconds

buildmenu:function(menuid, arrowsvar){
	jQuery(document).ready(function($){
		//console.log(navigator.userAgent);
		var $mainmenu=$("#"+menuid+">ul")
		var $headers=$mainmenu.find("ul").parent()
		$headers.each(function(i){
			var $curobj=$(this)
			var $subul=$(this).find('ul:eq(0)')
			this._dimensions={w:this.offsetWidth, h:this.offsetHeight, subulw:$subul.outerWidth(), subulh:$subul.outerHeight()}
			this.istopheader=$curobj.parents("ul").length==1? true : false
			$subul.css({top:this.istopheader? this._dimensions.h + 5 +"px" : -5})
			/*
$curobj.children("a:eq(0)").css(this.istopheader? {paddingRight: arrowsvar.down[2]} : {}).append(
				'<img src="'+ (this.istopheader? arrowsvar.down[1] : arrowsvar.right[1])
				+'" class="' + (this.istopheader? arrowsvar.down[0] : arrowsvar.right[0])
				+ '" style="border:0;" />'
			)
*/
			if( ( navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i)) || (navigator.userAgent.match(/ndroid/i)) ){
			    $('html').click(function(e){
				if( !$(e.target).parents().andSelf().is('#'+menuid) )
				    $('#'+menuid+' ul ul').slideUp(jqueryslidemenu.animateduration.out)
			    });
			    $curobj.click(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

				//console.log( $(this).find('ul:eq(0)') );
				$(this).parent('ul').children('li').children('ul').hide()

				menuleft=(this._offsets.left+menuleft+this._dimensions.subulw>$(window).width())? (this.istopheader? -this._dimensions.subulw+this._dimensions.w : -this._dimensions.w) : menuleft
				if ($targetul.queue().length<=1) //if 1 or less queued animations
				    $targetul.css({left:menuleft+1+'px', width:this._dimensions.subulw+'px'}).slideDown(jqueryslidemenu.animateduration.over)

				if( !this.istopheader )
				    return false;
			    });
			}else{
			    $curobj.hoverIntent({
			    	over: function(){
			    	$(this).parent("li").children('a').stop();
			    		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
												
						if ($targetul.queue().length<=1) //if 1 or less queued animations
					    	$targetul.css({left:menuleft+1+"px", width:this._dimensions.subulw+'px'}).slideDown(jqueryslidemenu.animateduration.over)
					    
					    //$(this).children("ul:eq(0)").parent('li').children('a').css('color', '#3C97AB !important');
					    if ($targetul.children('li:eq(0)').attr('class') === "first"){
			    			//$targetul.parent().children('a').css('color', '#FFF');
			    		} else {
			    		
			    			
			    		
			    			//alert($targetul.parent('li').children('a').html());
							$targetul.parent('li').css('color','#3C97AB !important');
							$targetul.parent('li').children('a').css('color','#3C97AB !important');
			    		}
			    		
			    		if ($targetul.children('li:eq(0)').hasClass('first_sub')){
				    		$targetul.hover(function(){	
			    				$(this).parent("li").children('a').css('color','#3C97AB !important');
			    			}, function(){
			    				$(this).parent("li").children('a').css('color','#3C97AB !important');
			    			});
			    		}
											    
			    	},
			    	timeout: 100,
			    	out: function(){
			    		var $targetul=$(this).children("ul:eq(0)");
			    		if ($targetul.children('li:eq(0)').attr('class') === "first"){
			    			$targetul.parent().children('a').css('color', '#FFF');
			    		} else {
			    			$targetul.parent().children('a').css('color', '#919B9D');
							$targetul.parent().hover(function(){
								$(this).children('a').css('color', '#3C97AB !important');	
							}, function(){
								$(this).children('a').css('color', '#919B9D !important');	
							})
			    		}
			    		$targetul.slideUp(jqueryslidemenu.animateduration.out)
			    	}
			    });
			    $curobj.click(function(){
					$(this).children("ul:eq(0)").hide()
			    });
			}
		}) //end $headers.each()
		$mainmenu.find("ul").css({display:'none', visibility:'visible'})
		
		if (window.BrowserDetect.browser === "Firefox" || window.BrowserDetect.browser === "Explorer"){
			$('.jqueryslidemenu ul li ul li a').css('color','#919B9D');
		} else {
			$('.jqueryslidemenu ul li ul li a').css('color','#919B9D !important');
			$('.jqueryslidemenu ul li ul li a').hover(function(){
				$(this).css('color','#3C97AB !important');
			}, function(){
				$(this).css('color','#919B9D !important');
			});
		}
		
		$('.jqueryslidemenu ul li').find('ul').each(function(){
			
				$(this).children('li').last().children('a').each(function(){
					$(this).css({'border-bottom': 'none', 'margin-bottom': '-5px'});
				});

		});
	}) //end document.ready
}
}

//build menu with ID="myslidemenu" on page:
jqueryslidemenu.buildmenu("menu-div", arrowimages);


