$(document).ready(function() {
		
		 
		 /*1.0 Pnav Container Edge Collision Detection (Author: Braeden Black | Avalon Media
			   Modified by Ryan Martinez
		 -------------------------------------------------------------------------*/
	
		$("#pnav ul li").hover(function(){
		
			var WindowSize		= $(window).width(); // Check size of browser window in order to accuratly caculate menu position
			var ContainerWidth 	= $("#container").width(); // get width of Container Div
			var L2MenuWidth		= $(this).children("ul").width(); // Set width of drop menu
			var L3MenuWidth		= 240; // Set width of drop menu sub menu minus the offset of its position relative to parent menu
			
			// Get position of pnav item reletive to window size
			var L1navPos		= $(this).offset(); 
			// Find the position of the left edge of the Container div
			var ContainerEdge 	= Math.round((WindowSize - ContainerWidth) / 2);
			// Find the position of the pnav link reletive to the container
			L1navPos 			= Math.round(L1navPos.left - ContainerEdge);
			// Find out how close the right edge of the dropmenu is to the right edge of the container div
			var L2navPos		= ContainerWidth - L1navPos - L2MenuWidth;
			// Find out how close the right edge of the menus submenu is to the right edge of the container div
			var L3navPos		= L2navPos - L3MenuWidth;
			// Find out if drop menu has class or not
			var HasClass		= $(this).children("ul").hasClass("alt-side");
			
			// if dropmenu is a negitive number and doesn't have class altside
			if (L2navPos < 0 && HasClass == false) {
				// Add Class
				$("ul",this).addClass("alt-side");
			}
			// if menu submenu is a negitive number and doesn't have class altside
			if (L3navPos < 0 && HasClass == false) {
				// Add Class
				$("ul li ul",this).addClass("alt-side");
			}
		});
		
		/*2.0 Pnav slide down / slide over effects
		 -------------------------------------------------------------------------*/
		/*hide the subnavigation on initial load*/
		$('#pnav ul li ul').hide();
		
		/* initiate two variable, menuTime: for holding the amount of time, and menuReference (a pointer);*/
		var menuTime;
		var menuReference;
		
		//---------------------------------------------
		//	HOVER TOP LEVEL NAV
		//---------------------------------------------
	
		$('#pnav > ul > li').hover( 
		function(){ 
			clearTimeout(menuTime);
			menuReference = $(this);
			menuTime =  setTimeout(function(){showSubnav(menuReference);},300); 
		},
		function(){ 
			clearTimeout(menuTime);
			menuReference = $(this);
			hideSubnav(menuReference)
		}
		);
		
		// function showSubnav will receive a reference to a particular li and show its child ul.subnav (Subnav SHOW ANIMATION)
		function showSubnav(menuReference){
			$(menuReference).find('ul').first().slideDown('fast');
		}
		// function hideSubnav will receive a reference to a particular li and hide its child ul.subnav (Subnav HIDE ANIMATION)
		function hideSubnav(menuReference){
			$(menuReference).find('ul').first().slideUp('fast');
		}
		
		//---------------------------------------------
		//	HOVER SECOND LEVEL NAV
		//---------------------------------------------
		 var menuTime2;
		 var menuReference2;
		
		$('#pnav ul li ul li').hover( 
		function(){ 
			clearTimeout(menuTime2);
			menuReference2 = $(this);
			menuTime2 =  setTimeout(function(){slideOpenSubnav(menuReference2);},200); 
		},
		function(){ 
			clearTimeout(menuTime2);
			menuReference2 = $(this);
			slideHideSubnav(menuReference2)
		}
		);
		
		// function showSubnav will receive a reference to a particular li and show its child ul.subnav (Subnav SHOW ANIMATION)
		function slideOpenSubnav(menuReference2){
			if ( $.browser.msie ) {
				 $(menuReference2).find('ul').slideDown('fast');
			}
			else if( $(menuReference2).children('ul').hasClass("alt-side") ){	
				$(menuReference2).find('ul').css({ "opacity": 0, "right": 150, "display":'block'}).animate({ opacity:'1' , right: '210', height: 'auto' }, 250, function() { // Animation complete.
				});
			}
			else{	
				$(menuReference2).find('ul').css({ "opacity": 0, "left": 150, "display":'block'}).animate({ opacity:'1' , left: '200', height: 'auto' }, 250, function() { // Animation complete.
				});
			}
		
		}
		// function hideSubnav will receive a reference to a particular li and hide its child ul.subnav (Subnav HIDE ANIMATION)
		function slideHideSubnav(menuReference2){
			if ( $.browser.msie ) {
				 $(menuReference2).find('ul').fadeOut('fast');
			}
			else if( $(menuReference2).children('ul').hasClass("alt-side") ){
				$(menuReference2).find('ul').animate({ opacity:'hide' , right: '150'}, 250, function(){});
			}
			else{
				$(menuReference2).find('ul').animate({ opacity:'hide' , left: '150'}, 250, function(){});
			}
		}	 
});

























