// main navigation and language selector behavior
// extends jquery 1.4
// @author: modlojik.com

	$(document).ready(function() {
	
		// global variable declarations
		var fadeOutSpeed=200;
			// var for subnav timeout
			var fadeOut,fadeOut0,fadeOut1, fadeOut2,fadeOut3,fadeOut4;
			// holding vars for visible subnav UL and its trigger IMG
			var currentSub, currentTrigger;
		
		
		// language selector icon behaviors---------------------------
		
			//set language background image to language IMG src and set its y position appropriately
		//	$('div .language').css("background","url("+$('div .language').children().attr("src")+") no-repeat 0 -"+0+"px");
			//on mouse over fade IMG to 0
			$('div .language').mouseover(function(){
	//		$(this).find("input").css("visibility","hidden");
	//		fade($(this).children(),0,false);
			});
			//on mouse out fade IMG to 1
			$('div .language').mouseout(function(){
	//		$(this).find("input").css("visibility","visible");
	//		fade($(this).children(),1,false);
			});

			// $('div .language').click() lives in ajax.js
			
		//----------------------------------------------------------------------
		
		// main navigation behavior-------------------------------------
		
			// initially fade all subnav to 0
			$("ul[id='nav']").find('ul').each(function(){
				$(this).fadeTo(0,0);hide($(this));	
			});
	
				var _captureClass,_captureFade=-1;
		$("ul[id='nav']").find('a').each(function(){			
	
	
	
			$(this).mouseover(function(){	//	trigger IMG mouseover
				// set css width and height of $(this) A so it doesn't collapse when IMG fades out
	 			$(this).css("width",$(this).children().width()+"px");
				$(this).css("height",$(this).children().height()/2+"px");
	 			
				// set  background image of $(this) A to the src of its child IMG and set its y position to half its height
				yOffset=$(this).children().innerHeight()/2;	
				$(this).css("background","url("+$(this).children().attr("src")+") no-repeat 0 -"+yOffset+"px");
				
				// fade IMG to 0
			fade($(this).children(),0,false);
				
		//		$('.lev_1_0').mouseover(function(){$("#slider").slideDown(500);});
//--------------------------------------------------	
				show($(this).next());
				fade($(this).next(),1,false);
				clearTimeout(fadeOut);
						
				_this=$(this).parent().parent();
				_top=_this.parent().parent();
				_next=$(this).next();
		
				isNav=_this.attr("id")=='nav';
				isSub=_this.hasClass("sub");
				hasSub=_next.hasClass("sub");
				isSubSub=_top.hasClass("sub");	
				
 
				
				if(hasSub){// like services, training, language instruction
						clearTimeout(fadeOut0);
						clearTimeout(fadeOut2);
						clearTimeout(fadeOut1);
					if(_captureFade==fadeOut4 && _next.attr("class").substr(4,8)==_captureClass)
						clearTimeout(fadeOut4);
				}
				else if(isSubSub){
						clearTimeout(fadeOut4);
						clearTimeout(fadeOut1);
						clearTimeout(fadeOut3);
						clearTimeout(fadeOut0);
				}
				else if(isSub){// like interpretion, translation
						clearTimeout(fadeOut1);
						clearTimeout(fadeOut2);
				}
				 
			}); // end mouseover
			

			$(this).mouseout(function(){	//	trigger IMG mouseout
			
				// fade IMG
			fade($(this).children(),1,false);

				_subClass=_next.attr("class");
				_selfClass=_this.attr("class");
				_parentClass=_top.attr("class");
 				
				if(_subClass)
				_targSub=_subClass.substr(4,8);
				if(_selfClass)
				_targSelf=_selfClass.substr(4,8);
				if(_parentClass)
				_targParent=_parentClass.substr(4,8);
						



				if(isNav && hasSub){// like services
						fadeOut2=setTimeout(function(){fade($("ul[class$='"+_targSub+"']"),0,true);},fadeOutSpeed);	
				}
				else if(isSub && hasSub){// like training, other
						fadeOut1=setTimeout(function(){fade($("ul[class$='"+_targSelf+"']"),0,true);},fadeOutSpeed);	
						fadeOut3=setTimeout(function(){fade($("ul[class$='"+_targSub+"']"),0,true);},fadeOutSpeed);
			}
				else if(isSubSub){// like language instruction
						fadeOut0=setTimeout(function(){fade($("ul[class$='"+_targParent+"']"),0,true);},fadeOutSpeed);	
						fadeOut4=setTimeout(function(){fade($("ul[class$='"+_targSelf+"']"),0,true);},fadeOutSpeed);
						_captureFade=fadeOut4;
						_captureClass=_targSelf;
				}
				else if(isSub){// like interpreting, translation
						fadeOut1=setTimeout(function(){fade($("ul[class$='"+_targSelf+"']"),0,true);},fadeOutSpeed);	
				}
						 
			}); // end mouseout
			
		});

 });
	
	// helper functions
	function fade(targ,dir,call){
		speed=200;
		if(call)
			targ.fadeTo(speed,dir,function(){hide(targ);});
		else
			targ.fadeTo(speed,dir);
			
		slideContent();
	}
	function hide(targ){
	//	targ.css("visibility","hidden");
		targ.css("display","none");
					slideContent();

	}
	function show(targ){
	//	targ.css("visibility","visible");
		targ.css("display","block");
	}
	function slideContent(){

  if($('.lev_1_0').is(':visible') || $('.lev_1_1').is(':visible'))
			$("#slider").slideDown(160);	
		else
			$("#slider").slideUp(160);	
 }
