/**
* hoverIntent r5 // 2007.03.27 // jQuery 1.1.2+
* <http://cherne.net/brian/resources/jquery.hoverIntent.html>
* 
* @param  f  onMouseOver function || An object with configuration options
* @param  g  onMouseOut function  || Nothing (use configuration options object)
* @author    Brian Cherne <brian@cherne.net>
*/
(function($){$.fn.hoverIntent=function(f,g){var cfg={sensitivity:7,interval:100,timeout:0};cfg=$.extend(cfg,g?{over:f,out:g}:f);var cX,cY,pX,pY;var track=function(ev){cX=ev.pageX;cY=ev.pageY;};var compare=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);if((Math.abs(pX-cX)+Math.abs(pY-cY))<cfg.sensitivity){$(ob).unbind("mousemove",track);ob.hoverIntent_s=1;return cfg.over.apply(ob,[ev]);}else{pX=cX;pY=cY;ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}};var delay=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);ob.hoverIntent_s=0;return cfg.out.apply(ob,[ev]);};var handleHover=function(e){var p=(e.type=="mouseover"?e.fromElement:e.toElement)||e.relatedTarget;while(p&&p!=this){try{p=p.parentNode;}catch(e){p=this;}}if(p==this){return false;}var ev=jQuery.extend({},e);var ob=this;if(ob.hoverIntent_t){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);}if(e.type=="mouseover"){pX=ev.pageX;pY=ev.pageY;$(ob).bind("mousemove",track);if(ob.hoverIntent_s!=1){ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}}else{$(ob).unbind("mousemove",track);if(ob.hoverIntent_s==1){ob.hoverIntent_t=setTimeout(function(){delay(ev,ob);},cfg.timeout);}}};return this.mouseover(handleHover).mouseout(handleHover);};})(jQuery);;var docHt;
var winHt;
var nav_timerRunning = false;
var nav_timer;
var len;
var activeChild = false;
var defaultSubNav = false;
var defaultNavResult;
var openedDefaultNav;
var defaultNavPadding;
var navResultsIsHover = false;
var activeSubNav;
var bobbling = false;
var fading = false;

(function($) {
	
	$.fn.setupToyoNav = function () {	
				
		$.preloadImages("http://toyotires.com/sites/all/modules/toyo_nav/active.png", "http://toyotires.com/sites/all/modules/toyo_nav/dormant.png", "http://toyotires.com/sites/all/modules/toyo_nav/personalHover.png", "http://toyotires.com/sites/all/modules/toyo_nav/parentHover.png");
				
		$("<div id='toyo-nav-results'></div>").prependTo("body");
		$("<div id='toyo-nav-spacer'></div>").appendTo("#toyo-nav-results");
		
		<!-- height/width necessary for proper IE 6 PNG --> 

		$("<img id='tnav_hov' height='18' width='12' src='http://toyotires.com/sites/all/modules/toyo_nav/arrowAlt.gif' />").appendTo("body");
		$("<img id='tshadow-1' height='721' width='3' src='http://toyotires.com/sites/all/modules/toyo_nav/shadow_sliver.png' />").appendTo("body");
		
		$("<div id='close-drawer'></div>").appendTo("#toyo-nav-results");
		
	//	$("<img id='tshadow-2' height='721' width='4' src='http://toyotires.com/sites/all/modules/toyo_nav/shadow_sliver.png' />").appendTo("#toyo-nav-results");
			
		$("#close-drawer").click(function() { closeNav(); });	
			
		$("#toyo-nav").animate({
		  opacity: 0.95
		}, { duration: 1});	
		

		// Close toyo-nav-results when clicking outside	

		$("body").click(function(e) {
			if (e.pageX > 480 && activeSubNav != 'null' && activeSubNav) {
				closeNav();
			}
		});
		
		// check for target-blank
		
		$("#toyo-nav .target-blank").each(function(i){	
			$("a", this).attr("target", "_blank");
		});		
		
		$("#toyo-nav li ul").each(function(i){
		 	$(this).children().filter(':last').addClass('last');
			
			// ORDERING IS IMPORTANT WHEN SOME HAVE ACCORDIANS, SOME NOT
			$.data($(this).get(0),"order", i);
		});
		
		
		$('#toyo-nav').find('.active-trail').each(function(i){
		 	activationRecursion($(this));
			return false; // equivalent of breaking loop.  SHOULD only be one ACTIVE element in nav.
		});			

	
		$("#toyo-nav ul li ul:has(ul)").each(function(i){

			// This is the accordian treatment
			$(this).addClass('accordian');			
			$(this).addClass('submenu_b' + i);
			
			// THIS ADDRESSES THE POSSIBILITY OF LOSS OF ORDER IN #toyo-nav-results
			if ($(this).prev()) {
				$.data($(this).prev().get(0),"pre", $(this));	
			}

			
			$(this).parent().addClass('submenu_b' + i);
			
			// overwrites sibling so all will show on parent rollover
			$(this).siblings().addClass('submenu_b' + i);

			if (!$.data($(this).parent().get(0),"top")) {
				
				// We want the padding-top declaration on the first child.
				// just selection for :first-child would fail, b/c we keep appending element elsewhere, thus it's ALWAYS the first-child
				position = $(this).parent().parent().position();
	
				$.data($(this).parent().get(0),"top", position.top);
					
				if ($(this).parent().hasClass('defaultSubNav')) {
					defaultSubNav = $(this).parent();
					defaultNavResult = '#toyo-nav-results .submenu_b' + i;
					defaultNavPadding = position.top;
				}

/*
				$(this).parent().hover(
				  function () {
					nav_stopTimer();
				  }, 
				  function () {
					nav_startTimer();
				  }
				);
*/
				$(this).parent().hoverIntent(
				  function () {
					activateArrow($(this));
					activateNavResult('#toyo-nav-results .submenu_b' + i, $(this));
				  }, 
				  function () {}
				);
			}
			
			$(this).appendTo("#toyo-nav-results");
		});
		

		// Now the others
		
		$("#toyo-nav ul li ul:not(:has(ul))").each(function(i){

			$(this).addClass('submenu_a' + i);
			// overwrites sibling so all will show on parent rollover
			$(this).siblings().addClass('submenu_a' + i);	
		
			if (!$.data($(this).parent().get(0),"top")) {
		
				position = $(this).parent().parent().position();
	
				$.data($(this).parent().get(0),"top", position.top);
	
				if ($(this).parent().hasClass('defaultSubNav')) {
					defaultSubNav = $(this).parent();
					defaultNavResult = '#toyo-nav-results .submenu_a' + i;
					defaultNavPadding = position.top;
				}
					
	/*
				$(this).parent().hover(
				  function () {
					nav_stopTimer();
				  }, 
				  function () {
					nav_startTimer();
				 }
				);
		*/		
				
				$(this).parent().hoverIntent(
				  function () {
					activateArrow($(this));
					activateNavResult('#toyo-nav-results .submenu_a' + i, $(this));
				  }, 
				  function () { }
				);
			
			}
			
			if ($.data($(this).get(0),"pre"))
				$(this).insertBefore(($.data($(this).get(0),"pre")));
			else
				$(this).appendTo("#toyo-nav-results");	

				
		});


		$("#toyo-nav-results ul.accordian li:has(ul)").each(function(i){
			
			$("<div class='results-toggle'><a href='javascript:void(0);'></a></div>").insertAfter($(this).children('a'));

			 $(this).children('a').bind("click", function(event){
			//	event.preventDefault();
			//	event.stopPropagation();
			 });
	
	
	
			$(this).hoverIntent(
			  function () {
	
					if (!$(this).children('.results-toggle').children('a').hasClass('toggleOff')) {
					$(this).children('.results-toggle').children('a').addClass('parentHover');
				}
			  }, 
			  function () {
					$(this).children('.results-toggle').children('a').removeClass('parentHover');
			  }
			);
		
			
			 $(this).children('.results-toggle').bind("click", function(event){
				
				$(this).siblings('ul').slideToggle(); 
				
				if ($(this).children('a').hasClass("toggleOff")) {
					$(this).children('a').removeClass('toggleOff');
				} else {
					$(this).children('a').removeClass('parentHover');
					$(this).children('a').removeClass('personalHover');
					$(this).children('a').addClass('toggleOff');
				}
					
				$(this).parent().siblings().children('ul:visible').slideToggle();
				$(this).parent().siblings().children('.results-toggle').children('a').removeClass('toggleOff');
			
			});

			$(this).children('.results-toggle').children('a').hoverIntent(
			  function () {
	
				if (!$(this).hasClass('toggleOff')) {
					$(this).addClass('personalHover');
				}
	
			  }, 
			  function () {
				$(this).removeClass('personalHover');
	
			  }
			);
			
			
			if ($(this).is(':has(".active-trail")')){
				$(this).children('.results-toggle').click();
			};
		
		});
	

	

		$('#toyo-nav-results').hover(
		  function (e) {
			
			nav_stopTimer();
				
			navResultsIsHover = true;
				
			if ($('#toyo-nav-results').width() == 26 && e.pageY < 731) {
	
				
				openedDefaultNav = false;
				
				$("#toyo-nav-results").animate({				   
				  opacity: 0.8
				}, 400, openDefaultSubNav);
				
			}
			
		  }, 
		  function () {
		  
		  	navResultsIsHover = false;
		  
			if ($('#toyo-nav-results').width() == 26) {
				
				if (!openedDefaultNav)
				$("#toyo-nav-results").stop();
				
			}
			  
			// nav_startTimer();
		  }
		);


		docHt = $(document).height();
		winHt = $(window).height();

		$(window).resize(function(){
		 setNavHeight();
		});

		setNavHeight();


	$("#tnav_hov").hoverIntent(
		function() { 
			nav_stopTimer();
			if ($('#toyo-nav-results').width() == 26) {
				openDefaultSubNav();
			}
		},
		function() { 
		 
		}
	 );


		if (defaultSubNav) {
				
			$("<img id='tshadow-3' height='721' width='3' src='http://toyotires.com/sites/all/modules/toyo_nav/shadow_sliver.png' />").appendTo("body");
		
			closeNav();
		}
	
	
	// $('#tshadow-1').pngFix();
	
		$('.special-dealer').empty();
		$('#special-dealer-contents').appendTo('.special-dealer');
		
		$('.special-tire-finder').empty();
		$('#tire-finder-contents').appendTo('.special-tire-finder');	
		
		

	}

	// END OF SETUP




	function activationRecursion(activeObj) {
	
		/*
		IF activeObj is an anchor, tag the parent LI as active and switch to dealing with li heirarchy.
		Recursion pattern is based on UL/LI
		
		BEGIN RECURSIVE LOOP
		 - IF parent().parent(UL) id != #toyo-nav,  parent (LI) addClass active.  continue loop.
		 - ELSE IF parent().parent(UL) id == #toyo-nav,  parent (LI) addClass active. defaultSubNavObj = $(this). 
		// break loop. Go home and eat bon bons.  Reflect on your awesomeness.
		*/	
	
		if ($(activeObj).is('a')) {
//			alert("anchor");
			$(this).parent().addClass('active-trail') // LI
			activeChild = $(activeObj).parent();			
		}
		else {
//			alert("li");
			activeChild = activeObj;
		}

		// nav is not that deep, but loop is throttled as a failsafe

		var currentChild = activeChild;
		var findTop;
		
		for (h=0;h<=8;h++)
		{
			findTop = $(currentChild).parent().parent().parent().attr('id');
			
			if (findTop == 'toyo-nav') {
				currentChild.addClass('defaultSubNav');
			//	alert($(defaultSubNav).html());
				break;				
			}
			
			currentChild = currentChild.parent().parent();
			currentChild.addClass('active-trail');

		}

	}



	function setNavHeight() {
	
		// "unsticks" window when sizing it down and kills some scroller flakiness.
		// winHt -= 25;
		
		 docHt = $(document).height();
		 winHt = $(window).height();	
		 
		 	
	//	$("#toyo-nav-results").height(docHt);
	//	$("#toyo-nav").height(docHt);		
		 	 
		 
		 if (docHt > winHt) {
		 	$("#toyo-nav-results").height(docHt);
			$("#toyo-nav").height(docHt);
		 }	else {
		 	$("#toyo-nav-results").height(winHt);
			$("#toyo-nav").height(winHt);		 
		 }
		 
	}

	function nav_startTimer() {
		nav_timer = setTimeout(function(){closeNav();},800); // nav_timer holds the id of the timer
	 	nav_timerRunning = true; // whenever you start a timer set the nav_timerRunning flag to true 
	}
	
	function nav_stopTimer() {
		if (nav_timerRunning) {
			clearTimeout(nav_timer); 
		}
	}

	

	
	function bobbleComplete() {
		bobbling = false;
	}
	
	function bobbleIn() {
	
		bobbling = true;
		
		// The performance of IE is just bad news on this front.  Disabling bobble.
		if (!$.browser.msie) {
		
			$("#toyo-nav-results").animate({ 
				width: "160px", paddingLeft: "20px", paddingRight: "40px"
			}, 100, "linear", bobbleOut );
		
		}
			  
	}
		
	function bobbleOut() {
	
		// The performance of IE is just bad news on this front.  Disabling bobble.
		if (!$.browser.msie) {	
		
			 $("#toyo-nav-results").animate({
				opacity: 0.9,
				width: "170px", paddingLeft: "30px", paddingRight: "30px"
			 }, 250, "swing", bobbleComplete);
			 
		} 
	}



	function doFade() {
		if (!fading) {
			doFadeOut();
		}
	}
		
	function doFadeOut() {
		fading = true;
		$('#close-nav').fadeOut(200);	
		$(activeSubNav).fadeOut(200, doFadeIn );
	}

	function doFadeIn() {
		// This causes some gumminess. Use only if you're getting into trouble.
		// $('#toyo-nav-results').children('ul').hide(); // CATCH-ALL
		fading = false;
		$(activeSubNav).fadeIn(200);
		$('#close-nav').fadeIn(200);	
	}
	
	
	
	function activateNavResult(e, paddingObj) {
		
			
		setNavResultPadding(paddingObj);	
		
		// if(defaultSubNav) {
			$("#tshadow-3").hide();
			
			
		// }
		
		if (activeSubNav != e && activeSubNav) {
		
			
			doFade();
			
			$('#toyo-nav-results').children('ul').hide(); // CATCH-ALL. CRAZY LITTLE FIX FOR FLAKINESS.
		
			activeSubNav = e;
			
			// BOBBLE THE NAV A BIT
			if (!bobbling)
				bobbleIn();
			

			return;
		}
	
		
		activeSubNav = e;
		
		
			
		if ($.browser.msie) {
			$(activeSubNav).show();
			$('#close-drawer').show();
		}
		else {
			$(activeSubNav).fadeIn(200);
			$('#close-drawer').fadeIn(200);
		}
		
		
		$("#toyo-nav-results").height($("#toyo-nav").height());

		  $("#toyo-nav-results").animate({
			  width: '170px',
			  paddingLeft: '30px',
			  paddingRight: '30px',
			  opacity: 0.9
			}, { duration: 200, queue: true});
		  
		  

	}
	
	var previousAdjust;
	
	function activateArrow(obj, secondary) {
		
		var positionObj = obj.position();	
		var arrowHt = $('#tnav_hov').height();
		var parentHt;
	
		parentHt = obj.height();
		
	
		var adjusted = positionObj.top + ((parentHt - arrowHt) / 2);


		if (previousAdjust != adjusted) { // || obj.not(':has(ul)')
			deactivateArrow();	
		}
		
		previousAdjust = adjusted;
	

		
		$('#tnav_hov').css({ 'top' : adjusted });
		
		if (secondary) {
			$('#tnav_hov').css({ 'left' : '239px' });
		}
		else {
			$('#tnav_hov').css({ 'left' : '196px' });
		}

// FADE ON PNGs w/o bkg color is bad in IE 7
		
		if ($.browser.msie) {
			$('#tnav_hov').show();
		}
		else {
			$('#tnav_hov').fadeIn(400);
		}
	}
	
	function deactivateArrow() {
		$('#tnav_hov').hide();							
	}
	

	function closeNav() { 
	
		// whenever the timer 'awakes' set the flag to false: 
		nav_timerRunning = false; 
		
		
		deactivateArrow();
		$('#close-drawer').hide();
		$('#toyo-nav-results').children('ul:visible').hide();
		
		activeSubNav = null;
		
		// left: "0px", 

		if (!defaultSubNav) {
		
			$("#toyo-nav-results").animate({
			  width: '1px',
			  paddingLeft: '0px',
			  paddingRight: '60px',					   
			  opacity: 0
			}, { duration: 200, queue: true });	
		
			// This is so that display:none.  Otherwise acts as invisible blocker of far left links
			$("#toyo-nav-results").fadeOut(200);
			
		} else {
		
		
		// IE 7 does not display opacity transitions correctly with alpha channel PNG 8
		// Otherwise a fadeIn in tandem  with $("#toyo-nav-results").animate() would be a tad nicer.
		// This softens things a little for the better browsers
		//	if(!$.browser.msie){
		//	$("#tshadow-3").fadeIn(200);
		// }
			
		//	$("#toyo-nav-results").bind("hover", activateNavResult(defaultNavResult));				
			$("#toyo-nav-results").animate({
			  width: '26px',
			  paddingLeft: '0px',
			  paddingRight: '0px',					   
			  opacity: 0.3
			}, 200, showExtraShadow);
			
		activateArrow(defaultSubNav, true);
		}

	} 

	function showExtraShadow() {
		// IE 7 does not display opacity transitions correctly with alpha channel PNG 8
		// This is a fall back.  Not sure about it yet.
		$("#tshadow-3").show();
	}

	function openDefaultSubNav() {
		openedDefaultNav = true;
		activateNavResult(defaultNavResult);
		activateArrow(defaultSubNav, false);	
	}
	
	function setNavResultPadding(e) {
	
		if (e)	
			var top = $.data(e.get(0),"top");
		else
			var top = defaultNavPadding;
			
		$("#toyo-nav-spacer").height(top);
		var calc = top - 30;
		$('#close-drawer').css({'top': calc});
	}


	$.preloadImages = function()
	{
	  for(var i = 0; i<arguments.length; i++)
	  {
		$("<img>").attr("src", arguments[i]);
	  }
	}


})(jQuery);

;


jQuery(document).ready(function(){
	jQuery().setupToyoNav();
});

