/** Triumph Javascript - JDG **/
/* Javascript stylesheet */
document.write("<link rel=\"StyleSheet\" href=\"/includes/triumphjs.css\" type=\"text/css\" media=\"screen\" />");

/* General Functions */
function fnPrint (n) {
 window.print()
}
function fnPrintIFrame(n) {
	frames[n].focus();
	frames[n].print();	
}	
function fnSubmit(formName) { 
 eval("document.forms['" + formName + "'].submit();");
}  
function fnPopUp(filePath,height,width,attributes){
 var sScreenX = screen.width
 var sScreenY = screen.height
 var x = sScreenX - width
 var y = sScreenY - height
 var winLeft = x/2
 var winTop =  y/2 
 window.open(filePath,"","width=" + width + ",height=" + height + "," + attributes + ",left=" + winLeft + ",top=" + winTop)
}

/********************************************************************************

Name:     PopupThis
Description: Standard popup, but takes target from href on link.  Scrollbars are 
hidden unless screen resolution is less than 1024 x 768, when scrollbars are present.
 
*********************************************************************************/

function fnPopupThis(targ,width, height) {
	var obj = targ.href;
	var sScreenX = screen.width;
	var sScreenY = screen.height;
	var x = sScreenX - width;
	var y = sScreenY - height;
	var winLeft = x/2;
	var winTop =  y/2;	
	var scrollStatus = "no";
	
	if ((screen.width>=1024) && (screen.height>=768)) {
 		var scrollStatus = "no";
		}
	else {
  		var scrollStatus = "yes";
		}

	window.open(obj,"","width=" + width + ",height="+ height + ",scrollbars=" + scrollStatus + ",left=" + winLeft + ",top=" + winTop + ",resizable=yes, status=no");	
	}
	
	

/*--// Chromeless popups */
function fnChromeless(showTarget, classToHide, iFrameToHide) {
 //if macie or Opera
 if ((navigator.appVersion.indexOf("Mac") != -1) && (navigator.userAgent && navigator.userAgent.indexOf("MSIE") >= 0) || navigator.userAgent.indexOf("Opera") != -1)  {    
   var ofrs = document.getElementsByTagName("IFRAME");
   for (i=0; i< ofrs.length; i++) { 
   ofrs[i].style.visibility = "hidden";
   }
   // Hide all
   var oDivs = document.getElementsByTagName("DIV");
   for (i=0; i< oDivs.length; i++) {  
    if (oDivs[i].className == classToHide) {
     oDivs[i].style.display = "none";
    }   
   }
   //If declared, show a layer
   if (showTarget) {  
    	var oShow = document.getElementById(showTarget);
    	oShow.style.display = "block";    
		oShow.style.top = "383px";  
		
		var ofrs = oShow.getElementsByTagName("IFRAME");
	   	for (i=0; i< ofrs.length; i++) { 
	  		ofrs[i].style.visibility = "visible";   
	   	}
			 	
   } else {
   var ofrs = document.getElementsByTagName("IFRAME");
   for (i=0; i< ofrs.length; i++) { 
  		ofrs[i].style.visibility = "visible";   
   }
   
     } 
 } else {
 
  // Hide all chromeless, show all iframes
  var oDivs = document.getElementsByTagName("DIV");
  var oIFrames = document.getElementsByTagName("IFRAME");
  
  for (i=0; i< oDivs.length; i++) {  
   if (oDivs[i].className == classToHide) {
   oDivs[i].style.top = "-300px";
   }  
  } 
  for (i=0; i< oIFrames.length; i++) {  
    oIFrames[i].style.visibility = "visible";    
  }
   
  //If declared, show a layer
  if (showTarget) {
 
   if (iFrameToHide) { //hide iframe containing form
    document.getElementById(iFrameToHide).style.visibility = "hidden";   
   }  
    var oShow = document.getElementById(showTarget); 	
   oShow.style.top = "383px";
  }  
 }
}  

  function getObj(name)
{
  if (document.getElementById)
  {
   this.obj = document.getElementById(name);
 this.style = document.getElementById(name).style;
  }
  else if (document.all)
  {
 this.obj = document.all[name];
 this.style = document.all[name].style;
  }
  else if (document.layers)
  {
 this.obj = getObjNN4(document,name);
 this.style = this.obj;
  }
}
function getObjNN4(obj,name)
{
 var x = obj.layers;
 var foundLayer;
 for (var i=0;i<x.length;i++)
 {
  if (x[i].id == name)
    foundLayer = x[i];
  else if (x[i].layers.length)
   var tmp = getObjNN4(x[i],name);
  if (tmp) foundLayer = tmp;
 }
 return foundLayer;
}

function fnSetScrollHeight(sTargetFrame,sType,iImageHeight){
 var sPopUpHeight;
 
 if(sType == "large"){
  sPopUpHeight = 410
 }else{
  sPopUpHeight = 300
 }
 
 var iHeight = sPopUpHeight - (iImageHeight)
 
 if (iHeight > 0){
  var oScrollingLayer = new getObj(sTargetFrame);
  oScrollingLayer.style.height = iHeight;
 }
}
/*----------------------------------------------------------*/
/* add arrows to links inside 'content' div */
function fnLinks() {
 
 var oContent = document.getElementById("content");
 if(oContent != null) {
  var oATags = oContent.getElementsByTagName("A"); 
 
  for (i=0; i< oATags.length; i++) {
  
   //**Don't Display arrow if a tag contains the following class**//
   if(oATags[i].className.indexOf("noArrowLink") == -1 && oATags[i].className != "pdf"){
    oATags[i].innerHTML += "&nbsp;<img src=\"/stylesheet_images/arrow.gif\" align=\"bottom\" />";
   }     
  } 
 }
}
/*----------------------------------------------------------*/
/**Highlight a Search Result**/
function fnHighLightResult(iResult){
 var sElement;
 var oResult;
 
 for(i=1;i<11;i++){
  sElement = "result" + i
  oResult = document.getElementById(sElement)
  
  if(i == iResult){
   oResult.className = "selectedResult" 
  }else{
   oResult.className = "result"    
  } 
 }
}
/*----------------------------------------------------------*/
/**Link to a page(where the link is retrieved from a form element)**/
function fnLinkFromForm(oFormElement){
 document.location.href = oFormElement.value
}
function fnDisplayResultWithPopUp(sInfoFrame, sInfoHref, sPopupFrame, sPopupHref) { 
 parent.frames[sInfoFrame].document.location.href=sInfoHref;
 parent.frames[sPopupFrame].document.location.href=sPopupHref;

}
 
function fnUpdateIFrameContents(sDetails){
 var aFrames = sDetails.split(",")
 var sDetails;
 
 for (i=0;i<aFrames.length;i++){
  if (aFrames[i] != ""){
   sDetails = aFrames[i].split("|");
   if ((sDetails[0] != "") && (sDetails[1] != "")){
    parent.frames[sDetails[0]].document.location.href=sDetails[1];
   }
  }  
 }
}

function fnChangeDropdown(){
	document.forms[0].action="?";
	document.forms[0].target = "";
	document.forms[0].submit();

}

function fnGetResults(sType, sSection) {
 
	var sList;
 	var sTarget;
 	
 	switch (sType) {
  		case "ratpacks": 
  			sList = "ratPacksList.aspx";
  			sHome  = "ratpacks.aspx";
  			break;
  		case "dealers":
  			sList = "dealersList.aspx";
  			sHome = "dealers.aspx";
  			break;
  		case "clothing":
  			sList = "clothingList.aspx";
  			sHome = "clothing.aspx";
     		break;
		case "accessories":
			sList = "accessoryList.aspx";
			sHome = "accessories.aspx";
			break;
  	}  
  	
   if (sSection == "list") {
   	document.forms[0].action = sList
   	document.forms[0].target = "eventsFrame";
   } else {
   	document.forms[0].action = sHome;
   }
	
	document.forms[0].submit();
}

function showClothingPopup(iID, sType, showTarget, classToHide, iFrameToHide){
	
	parent.frames["popUpFrame"].document.location.href="clothingPopup.aspx?id=" + iID + "&amp;type=" + sType;
	fnChromeless(showTarget, classToHide, iFrameToHide	);	
	
}

/********************************************************************************

Name: 				Flash embed
Description:		Writeout flash code. Fixes Eolas update for IE as well.
@param	swf		String path to swf file
@param	width		Number value of movie width
@param	height	Number value of movie height

*********************************************************************************/
function fnEolasFix() {
	if(navigator.userAgent.toLowerCase().indexOf("msie 6")){	
		obj = document.getElementsByTagName("OBJECT"); // Find <object> tags and...
		numberOfMovies = obj.length-1; // ...count them
		
		for(i=numberOfMovies; i>=0; i--) {														
					
			// For each object, make an <embed> with same dimensions			
			f 					= document.createElement("embed");									
			f.width			= obj[i].width;
			f.height			= obj[i].height;	
			f.type			= "application/x-shockwave-flash";
			
				
			// Copy parameters of <object> to <embed>	
			params 			= obj[i].getElementsByTagName("PARAM");					
			for(j in params) {
				if(params[j].name == "movie" ) {				
					aPath 	= params[j].value.split("?")
					file 		= aPath[0];							
					if(aPath.length>1) {		
						query = aPath[1].replace(new RegExp(/\./g), "%2E");
					} else {
						query 	= "";
					}							
					f.setAttribute("src",file+"?"+query);	
				} else {
					f.setAttribute(params[j].name, params[j].value);
				}				
			}																						
			
			// Replace object with new flash <embed>
			obj[i].parentNode.replaceChild(f,obj[i]);	
		
		}
	}
}

//--Links to Accessory Popup to a the accessory list for a specific bike--//
function fnLinkToCAPopup(type, sBike){

	var sPopUpUrl;
	var sUrl;
	var scrollStatus = "no";
	
	if ((screen.width>=1024) && (screen.height>=768)) {
 		var scrollStatus = "no";
		}
	else {
  		var scrollStatus = "yes";
		}
	/*  */
	switch(type){
		case "clothing" :
			sPopUpUrl = "clothingAccessoriesPopup.aspx?GID=1&BIKE=" + sBike;
			sUrl = "clothing.aspx"
			break;

		case "accessories" : 	
			sPopUpUrl = "clothingAccessoriesPopup.aspx?GID=2&BIKE=" + sBike;
			sUrl = "accessories.aspx"
			break;
	}
	
	window.open(sPopUpUrl,'CAPopUp','width=1010,height=695,scrollbars=' + scrollStatus + ',resizable=yes, status=no'); 
	document.location.href=sUrl;
}




/********************************************************************************
 
Name:     Flash plugin detect
 
*********************************************************************************/

/* Create new object called flash */
var flash = new Object();	

/* set flash installed to false by default */
flash.installed = false;

if (navigator.plugins && navigator.plugins.length) {
	for (x=0; x < navigator.plugins.length; x++) {
		
		if (navigator.plugins[x].name.indexOf('Shockwave Flash') != -1) {		
			flash.installed = true;					
			flash.version = eval(navigator.plugins[x].description.split('Shockwave Flash ')[1].split('.')[0]);					
			break;
		}
	}
}
else if (window.ActiveXObject) {
	for (x = 2; x <= 20; x++) {
		try {
			oFlash = eval("new ActiveXObject('ShockwaveFlash.ShockwaveFlash." + x + "');");
			if(oFlash) {		
				flash.installed = true;	
				flash.version = x;
			}
		}
		catch(e) {}
	}
}

/********************************************************************************
 
Name:     		Flash embed
Description:  	Writeout flash code. Fixes Eolas update for IE as well.  
				No-flash content should be placed in a div with ID of "noFlashContent" 
				before flash embed JS.  This div should be hidden by the javascript stylesheet.
@param swf  	String path to swf file
@param width    Number value of movie width
@param height   Number value of movie height

Notes:			This can be combined with the print stylesheet, to hide flash 
				from printing, and to print non-flash content
				
*********************************************************************************/

// Run flash embed code if flash installed, else show non-flash content 

flash.insert = function(swf,width,height,version) {	
	swf = swf.replace(/\./g,"%2E") 		// encode periods as %2e	
	if(!version){version = 6;}			// set default flash version if not passed as @param
	if(flash.installed && flash.version>=version) { 	
		flashObject = '<embed src="'+swf+'" menu="false" quality="high" width="'+width+'px" height="'+height+'px" type="application/x-shockwave-flash" salign="T" pluginspage="http://www.macromedia.com/go/getflashplayer" />'
		document.write(flashObject);
		
	} else {
		fnShowNoFlash();	
	} 
}



/********************************************************************************
 
Name:     		Show No Flash Content
Description:  	Find all divs with "noFlashContent" in their id, put them back on
				screen.

@param:			N/A

Notes:			Use an ID attribute of noFlashContent+number sequentially for 
				any non-flash code sections. 
				
*********************************************************************************/

function fnShowNoFlash() {	
	/* set the function up */
	var divs = document.getElementsByTagName("DIV"); 	// get array of html DIV's
	var id = "noFlashContent";							// set the ID to find
	
	for(i=0;i<=divs.length-1;i++) {						// loop through divs
		if (divs[i].id.match(id)) {						// match object ID to var id
			x = $(divs[i].id);							// use $() to reference ID
			fnShow(x);									// run fnShow() to show div
		}
	}
}


/********************************************************************************
 
Name:     		Show Content
Description:  	Amend the styles of the ID to put them on screen

@param x:		ID of object to show. 

Notes:			This has been made separate from the main show-hide stuff so we 
				can amend this without having to change other functions 
				
*********************************************************************************/

function fnShow(x) {	
	x.style.position = "relative";				// put the div back on screen
	x.style.top = 0;
	x.style.left = 0; 		
}



/***********************************************************************************
fnPopupAudio : Pops up a audio player in new window, using the href of the given 
					<a> object as the audio file. 
					
SAME AS THE FUNCTION IN NEW triumphAssets JS file except this requires HREF STRING as its used in c&a popup					
					
Usage in admin tool: <a href="javascript:fnPopupAudio('/media/arrow.mp3')">AUDIO LINK</a>
***********************************************************************************/
function fnPopupAudio(targ) {

	targ = "/sharedContent/sound/popupSound.asp?file=" + targ;
	
	// Show sound popup using existing fnPopUp
	fnPopUp(targ,150,371,"resizable=yes");
}

/***********************************************************************************
fnPopupVideo : Pops up a video player in new window, using the href of the given 
					<a> object as the video file. Width and height are the size of the video file.
					
SAME AS THE FUNCTION IN NEW triumphAssets JS file except this requires HREF STRING as its used in c&a popup					

Usage in admin tool: <a href="javascript:fnPopupVideo('/media/video.mov',320,240)">MOVIE LINK</a>
***********************************************************************************/
function fnPopupVideo(targ, width, height) {
	targ = "/sharedContent/movie/popupMovie.aspx?width=" + width + "&height=" + height + "&video=" + targ;
	
	// Show sound popup using existing fnPopup
	fnPopUp(targ, height + 105, width + 40,"resizable=yes");
}


/**
 * Gets fired when the user changes country on the new Dealer pages.
 * It will show or hide the state drop down for USA or Canada.
 */
function fnDealerCountryChanged(oDropdown, sIdPrefix) {
	var oUS = document.getElementById(sIdPrefix + "_lstStatesUS");
	var oCA = document.getElementById(sIdPrefix + "_lstStatesCA");
	
	if (oUS && oCA) {		
		// show one if you selected a country that has a state drop.
		if (oDropdown.options[oDropdown.selectedIndex].value == "US") {
			oUS.style.display = "";
			oCA.style.display = "none";
		}
		else if (oDropdown.options[oDropdown.selectedIndex].value == "CA") {
			oCA.style.display = "";
			oUS.style.display = "none";
		}
		else {
			// hide both if any other country chosen.
			oCA.style.display = "none";
			oUS.style.display = "none";
		}
	}
}

