// JavaScript Document
/************************************************************************/
/* Tire tools module currently resides in this js file for              */
/* ease of troubleshooting the rightModules.js file                     */
/************************************************************************/
var colorArray = new Array();
colorArray['tire1'] = new Array("213","73","0");
colorArray['tire2'] = new Array("254","217","0");
colorArray['tire3'] = new Array("140","190","79");


/************************************************************************/
/* old tires removed from the product line								*/
/************************************************************************/
//colorArray['tire4'] = new Array("220","41","30"); //should be red
//colorArray['tire5'] = new Array("0","160","218");
//colorArray['tire6'] = new Array("0","160","218");

var currentTire = null;
var DOCWIDTH = 984;
var DOCHEIGHT = 620;

var displayDivID = "div#landingPage_splash"; // set currently displayed div
var featuresDivID = "div#featuresTab1_content"; // set displayed features tab

// returns the value associated with a particular strParamName from the URL
//
function getURLParam(strParamName){
  var strReturn = "";
  var strHref = window.location.href;
  if ( strHref.indexOf("?") > -1 ){
    var strQueryString = strHref.substr(strHref.indexOf("?")).toLowerCase();
    var aQueryString = strQueryString.split("&");
    for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
      if (
aQueryString[iParam].indexOf(strParamName.toLowerCase() + "=") > -1 ){
        var aParam = aQueryString[iParam].split("=");
        strReturn = aParam[1];
        break;
      }
    }
  }
  return unescape(strReturn);
}

/////////////////////////////////////////////////////////////////////
// used to notify analytics of a change in view within the tires pages
// that might normally constitute a change to the pageview.
//
function notifyAnalytics(divId) {
	var thisPath = window.location.pathname;
	if(divId) {
		pageTracker._trackPageview(thisPath+"?dispSection="+divId);
	} else {
		pageTracker._trackPageview(thisPath);
	}
}

$(document).ready(function () {
	// sets the mouseover functions for the tireNav
    $('div.tireNavLink').mouseover(function () {
			var tireID=$(this).attr('id');
			// show the overlayed images for the current tire and
			// change the tire label to the appropriate color
			$(this).children("div.tireNavOverlay").show();
			$(this).children("a.tireNavTxtLink").children("span.tire_label").css('color','rgb('+colorArray[tireID][0]+','+colorArray[tireID][1]+','+colorArray[tireID][2]+')');
			// hide the tireNavOverlay for all other tires and
			// change the tire label of other tires to white
			$(this).siblings("div.tireNavLink").children("div.tireNavOverlay").hide();
			$(this).siblings("div.tireNavLink").children("a.tireNavTxtLink").children("span.tire_label").css('color','#ffffff');
	});
	// add a mouseout function for the sub-pages
	$('div.tireNavLink').mouseout(function () {
			$(this).children("div.tireNavOverlay").hide();										 
			$(this).children("a.tireNavTxtLink").children("span.tire_label").css('color','#ffffff');
	});

/******************************************************************************/
/* scrolling content window control											  */
/* uses the jquery.js file and the jquery-ui.js file which is a build of the  */
/* jquery ui.core.js file and ui.draggable.js file as built and minified at	  */
/* http://jqueryui.com/ using ver 1.7.2 for jQuery 1.3+ with no theme selected*/
/******************************************************************************/
/* NOTE: jquery.js and jquery-ui.js have been consolidated into one file to   */
/* reduce the number of http calls.											  */
/******************************************************************************/
// determine if the scroll function is needed
	if($('div.scrollingContainerInset')) {
		// sets the dragging function of the Tire Tools div scrollbar
		// first determines if the list is taller than the container
		// if not, it will hide the scroll bar container
		if($('div.scrollingContainerInset').height() < $('div.scrollingContent').height()) {
			$('div.scrollbar').draggable({axis:'y', containment:'parent', drag: function(event, ui) {
				// determines the percentage by which to scroll the content
				// in the links container by getting the position of the
				// draggable object (scrollbar) and dividing it by
				// it's draggable area (height of it's container minus it's
				// own height). the 2 is subtracted to compensate for the borders
				// of this draggable object.
				var dragPercent = ui.position.top/($('div.scrollbarContainer').height()-$('div.scrollbar').height() - 2);
				$('div.scrollingContent').css('top', Math.floor(dragPercent*($('div.scrollingContainerInset').height()/2-$('div.scrollingContent').height())));
				}  // end of function(event, ul)
			}); // end of draggable
		} else {
			
			$('div.scrollbarContainer').hide();
			$('div.scrollingContainerInset').width(734);
		}
	} // end if div.scrollingContainer

	///////////////////////////////////////////
	// set specsDiv positioning	for screen	 //
	// only. should not affect print styles  //
	///////////////////////////////////////////
	
	// get the screenStyle css and reference it with mycss
	//
	var mycss;
	for (i=0;i<document.styleSheets.length; i++) {
		if(document.styleSheets[i].title=="screenStyle") {
			mycss=document.styleSheets[i];
			break;
		}
	}
	
	// determine which method to use for referencing rules
	// cssRules=Moz rules=IE
	//
	var myrules=mycss.cssRules? mycss.cssRules: mycss.rules;
	
	// get the particular target rule and assign a reference
	//
	for(i=1; i<myrules.length;i++) {
		if(myrules[i].selectorText.toLowerCase()=="div#specsdiv") {
			var targetRule=myrules[i];
			break;
		}
	}
	// get width of browser window
	//
	var windowWidth = $(window).width();
	
	// change left position of the targetRule
	//
	targetRule.style.left = Math.floor((windowWidth - $("div#specsDiv").width())/2) + "px";


	//////////////////////////////////////////////
	// set the visible div based on URL parameters
	//
	var tirePage_displayDiv = getURLParam("dispSection");
	//alert(tirePage_displayDiv);
	switch(tirePage_displayDiv)
	{
		case "sizes":
		case "sizes#":
			//alert("sizes");
			$("div#sizesDiv").show();
			break;
		case "features":
		case "features#":
		//alert("features");
			$("div#featuresDiv").show();
			break;
		case "specs":
		case "specs#":
		//alert("specs");
			$("div#specsDiv").show();
			break;
		case "":
			//alert('empty');
			$("div#landingPage_splash").show();
			break;
		default:
			$("div#landingPage_splash").show();
	 }
	 
	//////////////////////////////////////////////
	// set tire label link functions for switching
	 // visible divs and notifying analytics
	$("div.rd_labelDiv p a").click(function () {
		var newDisplayDivID = "div#"+$(this).attr("id")+"Div";
		if(newDisplayDivID!=displayDivID) {
			// set new displayDiv
			displayDivID=newDisplayDivID;
			// move all displayDivs off stage
			$("div.displayDiv").hide();
			// move current displayDiv on stage
			$(displayDivID).show();
			notifyAnalytics($(this).attr("id"));
		}
	});
	
	// features display tab controls
	//
	$("div.featuresTab").click(function () {
		var newFeaturesDivID = "div#" + $(this).attr("id") + "_content";
		if(newFeaturesDivID!=featuresDivID) {
			// set new features div
			featuresDivID=newFeaturesDivID;
			// hide all siblings except for the featuresTabs container
			$(featuresDivID).siblings().not("div#featuresTabs").hide();
			// show appropriate features div
			$(featuresDivID).show();
		}
	});
	
	// features callout and description controls
	//
	$("div.features_callout").mouseover(function () {
		var clickedCalloutID = $(this).attr("id");
		var pattern = /_/;
		result = clickedCalloutID.split(pattern);
		result = "div#features_description_" + result[2];
		//alert(result);
		$("div.features_description").hide();
		$(result).show();
	
	});
	
	// specs page close button controls
	//
	$("div#closeButton").click(function () {
		$("div#specsDiv").hide();
		$("div#landingPage_splash").show();
		displayDivID = "div#landingPage_splash";
		notifyAnalytics();
	});

	// specs page print button controls
	//
	$("div#printButton").click(function () {
		window.print();
		notifyAnalytics("printedSpecsSheet");
	});

});