/*********************
//* 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/
*********************/

var menuOpen = true
var menuGap = 4
var breadPosY = 0
var breadInit = false
var breadcrumbPostions = new Array()
var selectedULs = new Array()
var revertingNav = false
var jqueryslidemenu={

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

buildmenu:function(){
	jQuery(document).ready(function($){
		var $mainmenu=$("#slidemenu>ul")
		var $mainmenuVertOffset=$mainmenu.offset().top
		var $headers=$mainmenu.find("ul").parent()
		var linkToLoad = ''
		
		$mainmenu.find('li:last-child').children('a').addClass('noBtmBorder')
		
		$headers.each(function(i){
			var $curobj=$(this)
			var $subul=$(this).find('ul:eq(0)')
			//set init vars
			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:0})
			//$curobj.children("a:eq(0)").append('<img src="images/navigation/rightArrow.gif" class="rightarrowclass" style="border:0" />')
			$curobj.children("a:eq(0)").addClass('rightArrow')
			$curobj.toggle(
				function(e){
					var $targetul=$(this).children("ul:eq(0)")
					var $targeta=$(this).children("a:eq(0)")
					this._offsets={left:$(this).offset().left, top:$(this).offset().top}
					if ($targetul.queue().length<=1){ //if 1 or less queued animations
						$targetul.css({left:(this._dimensions.w+menuGap)+"px", width:'0px'}).animate({width:this._dimensions.subulw+'px'},jqueryslidemenu.animateduration.over)
						var vertOffset = $mainmenuVertOffset - $targetul.offset().top
						if($targetul.data("topInit")!=true) {
							$targetul.css({top:vertOffset+"px"})
							$targetul.data("topInit", true)
						}
					}
					
					disableSiblings($(this))
					
					$targeta.addClass('select')
				//	$("#readout").append('OPENED '+$targeta.html()+'<br>')
				},
				function(e){
					var $targetul=$(this).children("ul:eq(0)")
					var $targeta=$(this).children("a:eq(0)")
					//disableUL($(this))
					$targetul.animate({width:'0px'},jqueryslidemenu.animateduration.out).hide(0).css({display:'none', visibility:'visible'})
					//deselect all children .select links
					$targetul.find(".select").removeClass('select')
					
				//	$("#readout").append('CLOSED '+$targeta.html()+'<br>')
				}
			) //end toggle
		}) //end $headers.each()
		var $endlinks=$mainmenu.find("li:not(:has(ul))")
		$endlinks.each(function(i){
			var $curobj=$(this)
			//$curobj.children("a:eq(0)").css({"background-color": "#FF0000", "color": "#000000"})
			//we need to use a toggle to override the toggle of the parent ul so it doesn't collapse
			$curobj.toggle( 
				function(e){
					endClick()
				},
				function(e){
					endClick()
				}
			) //end toggle
			function endClick(){
				$curobj.children("a:eq(0)").addClass('select')
				linkToLoad = $curobj.children("a:eq(0)").attr('href')
				// deselect the other open sibling links
				//deselectSiblingLinks($curobj)
				disableSiblings($curobj)
				collapseNav()
			}
		}) //end $endlinks.each()
		
	
		
		$mainmenu.find("ul").css({display:'none', visibility:'visible'})
		
		
		function collapseNav(){
			if(menuOpen){
				var topSelectIndex = 0;
				var $bc = $("#breadcrumb")
				$bc.html('')
				breadcrumbPostions = new Array()
				
				var $tops = $mainmenu.find(".top")
				$tops.each(function(i){
					var $top = $(this)
					var leftPos = $top.offset().left-$mainmenu.offset().left
					var topPos = $top.offset().top-$mainmenu.offset().top
					var wid = this.offsetWidth
					if($(this).hasClass('select')){
						breadPosY = topPos 
						topSelectIndex = i
						//$("#readout").append('topSelectIndex='+i+'<BR>')
					}else {
						$bc.append('<div class="bcTopLink" style="position:absolute;left:'+leftPos+'px;top:'+topPos+'px;width:'+wid+'px;"><a href="#" class="'+$top.attr('class')+'">'+$top.html()+'</a></div>')
					}
				})//end $tops.each()
				
				var $selects = $mainmenu.find(".select")
				selectedULs = $selects
				if(selectedULs.length){
					$selects.each(function(i){
						var $select = $(this)
						var leftPos = $select.offset().left-$mainmenu.offset().left
						var topPos = $select.offset().top-$mainmenu.offset().top
						var wid = this.offsetWidth
						breadcrumbPostions.push(topPos)
						var extraClass = (leftPos==0) ? "" : " noBtmBorder";
						$bc.append('<div class="bcLink" style="position:absolute;left:'+leftPos+'px;top:'+topPos+'px;width:'+wid+'px;"><a href="#" class="'+$select.attr('class')+extraClass+'">'+$select.html()+'</a></div>')
					})//end $selects.each()
				}
				
				var $bcTopLinks = $bc.find(".bcTopLink")
				$bcTopLinks.click(function(event){
					event.preventDefault();
					event.stopPropagation();
					menuOpen = true
					$mainmenu.show()
					$mainmenu.find(".top:eq("+$(this).data("topIndex")+")").parent().click()
					$bc.html('')
				})
				$bcTopLinks.each(function(i){
					$(this).data("topIndex", (i>=topSelectIndex? i+1 : i))
				})//end $bcTopLinks.each()
				
				var maxLinkHeight = 0;
				var $bcLinks = $bc.find(".bcLink")
				$bcLinks.click(function(event){
					event.preventDefault();
					event.stopPropagation();
					expandNav()
				})
				var animationSpeed = 200
				$bcLinks.each(function(i){
					var $link = $(this)
					if($link.outerHeight() > maxLinkHeight) maxLinkHeight = $link.outerHeight()
					if(breadInit) $link.css({top:breadPosY+'px'})
					else $link.delay(animationSpeed).animate({top:breadPosY+'px'},animationSpeed)
				})//end $bcLinks.each()
				if(breadInit){
					$("#mainContent").css({top:(breadPosY+maxLinkHeight+$mainmenu.offset().top)+'px'})
					$mainmenu.hide()
					breadInit = false
				}else{
					$mainmenu.fadeOut(animationSpeed)
					if(revertingNav) {
						revertingNav = false
					}else{
						if(pageType == "casestudy" || pageType == "home" || pageType == "category"){
							if(linkToLoad!='') {
								window.location = linkToLoad
							}
						}else{
							$("#mainContent").delay(animationSpeed).animate({top:(breadPosY+maxLinkHeight+$mainmenu.offset().top)+'px'},animationSpeed, function() {
								if(linkToLoad!='') {
									window.location = linkToLoad
								}
							})
						}
					}
				}
				//$("#readout").append('breadPosY:'+breadPosY+' maxLinkHeight:'+maxLinkHeight+' mainmenu.offset().top:'+$("#slidemenu>ul").offset().top+'<br>')
				menuOpen = false
			}
		}
		
		function revertNav(){
			
			//close current selected nav items
			var $currentSelects = $mainmenu.find(".select")
			$currentSelects.each(function(i){
				disableUL($(this).parent())
				
			})//end $currentSelects.each()
			
			//rebuild current page nav selection
			if(selectedULs.length){
				if(menuOpen) revertingNav = true
				selectedULs.each(function(i){
					var $select = $(this).parent()
					$select.click()
				})//end selectedULs.each()
			}
		}
		
		function expandNav(){
			if(!menuOpen){
				var $bc = $("#breadcrumb")
				var $bcLinks = $bc.find(".bcLink")
				$bcLinks.each(function(i){
					var $link = $(this)
					$link.animate({top:breadcrumbPostions[i]+'px'},200)
				})//end $bcLinks.each()
				$mainmenu.delay(210).fadeIn(100, function() {
					$bc.delay(300).html('')
				})
				menuOpen = true
			}
		}
		
		function disableSiblings($obj){
			// hide the other open sibling lists
			var $otheruls = $obj.siblings()
			
			$otheruls.each(function(i){
				disableUL($(this))
			})//end $otheruls.each()
			//$("#readout").append('<hr width="50" align="left">')
		}
		function disableUL($obj){
			var $ul = $obj.children("ul:eq(0)")
			if($ul.css("display")=="block"){
				var $childuls = $obj.find("ul")
				$childuls.each(function(i){
					//$("#readout").append(i+":"+$(this).parent().children("a:eq(0)").html()+":")
					var $childul = $(this).parent().children("ul:eq(0)")
					if($childul.css("display")=="block"){
						//$("#readout").append('<hr width="50" align="left">(call to close '+$(this).parent().children("a:eq(0)").html()+')<br>')
						$childul.css({display:'none', visibility:'visible', width:'0px'}).click()
					}
				})
				
				//$ul.hide(0).css({display:'none', visibility:'visible', width:'0px'}).click()
				//$(this).children("a:eq(0)").css({"background-color": "", "color": ""})
			}
			$obj.children("a:eq(0)").removeClass('select')
		}
		
		$('body').click(function() {
			revertNav()
			//$("#readout").append('X ')
		 });
		
		//if there is a bread crumb path provided, follow it
		navString = getQueryVar('path');
		if(window.pathOption) if((navString == 'na' || navString=='') && (pathOption!="" && pathOption!="NONE")) navString = pathOption;
		if(navString != 'na' && navString!='') {
			var $targetUL = $mainmenu
			var navStrArray = navString.split("-")
			var fullPath = true;
			var fullPath = false;
			for(var i=0; i<navStrArray.length; i++){
				var newUL = $targetUL.children("#"+navStrArray[i]).children("ul:eq(0)")
				if(newUL.length) {
					$targetUL = newUL
				}else if($targetUL.children("#"+navStrArray[i]).children("a:eq(0)").length){
					fullPath = true;
				}
			}
			if(fullPath){
				$targetUL = $mainmenu
				breadInit = true
				for(var i=0; i<navStrArray.length; i++){
					$targetA = $targetUL.find("#"+navStrArray[i]).children("a:eq(0)")
					$targetUL = $targetUL.find("#"+navStrArray[i]).children("ul:eq(0)")
					if($targetUL.length) {
						$targetUL.click()
					}else{
						$targetA.click()
						break
					}
					//$("#readout").append(navStrArray[i]+" "+$targetUL.length+":"+$targetA.length+"<br>")
				}
			}
		}
		
		//$("#clear").click(function(){$("#readout").html('')})
		
	}) //end document.ready
	
}
}

jqueryslidemenu.buildmenu()
