jQuery(document).ready(function(){

    var $ = jQuery,
        getById = function(n){ return document.getElementById(n); };
        
        /* Nav turns on/off functions */
                

    function Over() {
    	$(".thirdcats").hide();
    	url=this.toString();
    	urlarray=url.split('=');
   		cgid=urlarray[1];
   		if (!cgid) {
   			var tempurl=url.split(',');
   			urlarray=tempurl[0].split('/');
   			cgid=urlarray[urlarray.length-1];
   		}
  		if (cgid=='Our_Athletes')
   			cgid="athletes";
  		if (cgid=='community_landing')
   			cgid="community";
  		if (cgid=='Events_Landing')
   			cgid="events";
  		if (cgid=='Technologies')
   			cgid="innovation";

   		sel="subcats-"+cgid;
    	submenu=$("#"+sel);
    	subheight=parseInt(submenu.height());
    	if (subheight>400) {
    		subheight=(subheight/2)+9;
    	}
    
    	men="menu-"+cgid;        
    	menu=$("#"+men);
    	boxselector="blackbox-"+cgid;
    	box=$("#"+boxselector);

    	var position=menu.offset();
    	itemtop=position.top-14;
    
    	originalTop=itemtop;
    	subpageadjust=0;
		var bottomborder=$("#sliding-navigation");
    	logoPos=bottomborder.position();
    	baseline=parseInt(logoPos.top)+bottomborder.height()+17;
		topadjust=0;
		
		var subsectionheight=0;
		var subsection=$("#subcatsection");
		if (subsection.length>0) {
			subsectionheight=subsection.height()-1;
			if($.browser.msie)
				subsectionheight=subsectionheight+2;			
		}
		
	   	if (parseInt(position.top+subheight)>baseline) {
    		itemtop=baseline-subheight;
    		topadjust=-33;
    		subpageadjust=6-100;
    		bb="blackbox-"+cgid;
    		mm="#menu-"+cgid;
    		mpos=parseInt($(mm).offset().top,10);
    		var sh=parseInt(submenu.height())-(baseline-mpos+menu.height()+10);
        	
    		$("#"+bb).css('top',sh);
         }
 
      	$("#"+sel).css('left',position.left+120);
      	$("#"+sel).css('left',position.left+120);
      	$("#"+sel).css('top',itemtop-topadjust);
      	
      	boxpos=box.offset();
      	if (boxpos && topadjust==0)
      		box.css('top',itemtop-originalTop+13+subpageadjust);
      	
      	if (boxpos && topadjust>0)
      		box.css('top',itemtop-7+subpageadjust-subsectionheight);

      
    	$(".subcats").hide();

      	bb="blackbox-"+cgid;
      	$("#"+bb).hide();
      	$("#"+sel).animate({width:'toggle'},90);
      	$("#"+bb).show();
        
    }; 
    
    var hoverElement;
    
    function Out() {
    	url=this.toString();
    	urlarray=url.split('=');
    	cgid=urlarray[1];
       	sel="subcats-"+cgid;
 	
    	if (sel==hoverElement) {
    	}
    	else 
    	{ 
    		$("#"+sel).animate({width:'toggle'},90);
   	}
    	
    }; 

    function SubOver() {  

    	elem=this.id.toString();
    	$("#"+elem).show();
    	hoverElement=elem;
      	
    }; 
    
    function SubOut() {  
    	$(".subcats").hide();
    	hoverElement="";
    	
    }; 
    function ThirdOver() {  
    	elem=this.id.toString();
    	elem=elem.replace('&','_');
       	var submenu=$("#thirdcats-"+elem);
       	var submenu=$("#thirdcats-"+elem);
        $(".thirdcats").hide();
  //  	$(".subcats").hide();
    	var thisitem=$("#"+elem);
    	
    	thispos=thisitem.position();
	if(thispos) {
		       	tp=parseInt(thispos.top,10)-12;
		      	lt=parseInt(thispos.left,10)+120;
		
		    	submenu.css('top',tp+"px");
		    	submenu.css('left',lt+"px");
		    	submenu.animate({width:'toggle'},90);
		
		    	if (submenu.height()>2) {
		    		tb="thirdbox-"+elem;
			    //  	$("#"+tb).hide();
			      	$("#"+tb).animate({width:'toggle'},90);
			    //  	$("#"+tb).show();
		    	}
		    	else {
		    		submenu.css('opacity',0);
		    	}
	}
    	hoverElement="thirdcats-"+elem; 		   
    }; 
    
    function ThirdOut() {  
       	elem=this.id.toString();
    	var submenu=$("#thirdcats-"+elem);
    	
    	sel="thirdcats-"+elem;

     	if (sel==hoverElement) {
    	}
    	else 
    	{ 
    		submenu.animate({width:'toggle'},90);
    	}
    	hoverElement="";
    }; 

    function FourthOver() {  
    	var submenu=$("#thirdcats-"+elem);
    	submenu.show();
    }; 

    function FourthOut() {  
    	$(".thirdcats").hide();
    }; 

    
    
    $('.subcats').mouseover(function() {
    	elem=this.id.toString();
    	$("#"+elem).show();
    }); 
    
    $('.thirdcats').mouseover(function() {
    	var submenu=$("#thirdcats-"+elem);
   	submenu.show();
    }); 


    $('#main_content').mouseout(function() {
    	$(".thirdcats").hide();
       	$(".subcats").hide();
    	
    }); 

///

    $(".menu").hoverIntent({
		sensitivity: 1, 
		interval: 250, 
		over: Over, 
		timeout: 400, 
		out: Out
	});

    $(".subcats").hoverIntent({
		sensitivity: 1, 
		interval: 2, 
		over: SubOver, 
		timeout: 400, 
		out: SubOut
	});

    $(".subcat").hoverIntent({
		sensitivity: 1, 
		interval: 250, 
		over: ThirdOver, 
		timeout: 400, 
		out: ThirdOut
	});

    $(".thirdcats").hoverIntent({
		sensitivity: 1, 
		interval: 2, 
		over: FourthOver, 
		timeout: 300, 
		out: FourthOut
	});

 
});


