// WRITE E-MAIL ADDRESS TO HIDE IT FROM SPAM SPIDERS
function writeAddress(name,url) {
	document.write('<a href="mailto:' + name + '@' + url + '">' + name + '@' + url + '</a>');
}

// FOR ADMINISTRATION TOOL
function submitForm() {
	document.forms[0].submit();
} 

// CLOSE LAYER - Used to close content "windows" 
function closeLayer(idName) {	
	document.getElementById(idName).style.display='none';	
} 

// MACROMEDIA CODE USED WITH VARIOUS FUNCTIONS
function MM_findObj(n, d) { //v4.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}

// MACROMEDIA CODE FOR SWAPPING IMAGES
// Used to swap images on the Motorcycle page
// This code is used rather than simplier background switching so that images can be printed
function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

// OPEN OFF-SITE LINK (Links to related sites)
function leaveSite(theURL) {	
	window.open('http://' + theURL,'offsite','width=780,height=450,toolbar=yes,resizable=yes,scrollbars=yes,menubar=yes,location=yes,status=yes');
	// NOTE: Changed to be consistent with URLs entered via the administration tool 4/6/2006, GML
	// var answer = confirm('You are about to leave the CooperTires.com Web site.\n\nTo continue to ' + theURL + ', click "OK."\nTo remain on the CooperTires.com Web site, click "Cancel."')
	// if (answer) {
	// 	location.href='http://' + theURL;
	// }
}

// OPEN THIRD-PARTY CONTENT LINK (Links to off-site content with a Cooperworld design)
function openThirdParty(theURL) {
	window.open(theURL,'thirdparty','width=665,height=485,toolbar=yes,resizable=yes,scrollbars=yes');
}

// OPEN TIRE PRODUCT POPUP
function openTireImage(theURL) {
	window.open(theURL,'thirdparty','width=340,height=308,toolbar=no,resizable=yes,scrollbars=yes');
}
// OPEN TIRE PRODUCT POPUP
function popUpCS4(theURL) {
	window.open(theURL,'thirdparty','width=800,height=600,toolbar=no,resizable=no,scrollbars=no');
}
// OPEN TIRE PRODUCT POPUP
function popUpGFE(theURL) {
	window.open(theURL,'thirdparty','width=1050,height=630,toolbar=no,resizable=no,scrollbars=no');
}

// OPEN NEW WINDOW FOR COOPER-HOSTED CONTENT
// Initially used only on Best Drives pages - Now also for Toolbar items
function popUp(theURL,scrollbars) {
	var detailWin
	detailWin=window.open(theURL,'winDetails','width=817,height=565,resizable=yes,scrollbars=yes,toolbar=yes,location=yes,directories=no,status=no,menubar=no,top=50,left=50');
	// NOTE: Changed per client request 4/6/2006, GML	
	// if (scrollbars=='scroll') {
	// 	detailWin=window.open(theURL,'winDetails','width=660,height=442,resizable=yes,scrollbars=yes,toolbar=no,location=no,directories=no,status=no,menubar=no,top=50,left=50');	
	// }
	// else {
	// 	detailWin=window.open(theURL,'winDetails','width=660,height=442,resizable=yes,scrollbars=no,toolbar=no,location=no,directories=no,status=no,menubar=no,top=50,left=50');
	// }
	// self.name = 'mainWin';
	detailWin.focus();
}

function pictureUPopUp() {
	window.open('/pictureu/index.aspx?batch=COOP&noshow=true','picWin','width=820,height=570,scrollbars=yes,toolbar=yes,menubar=no');
}

function clipSyndicatePopUp(clip) {
	if (clip == 'johnny-unser') {		
		window.open('/html/video/johnny-unser.aspx','clip','width=345,height=373,scrollbars=no,toolbar=no,menubar=no,resiable=yes');
	}
	else {
		alert('The video which you selected is not available at this time. We apologize for the inconvenience.')
	}	
}

// RESIZE WINDOW FOR USE ON TIRE SPECS PAGES (Windows called via popUp() function above)
// Allow large tire specificiations tables to fit in framework - required larger background frame
// Currently only used for Tire Specs
function resizeWindow(page) {
	// REM per client 4/6/2006, GML window.resizeTo(807,590);
}

// JUMP DOWN ON PAGE
// Used on Dealer Locator page when different countries are selected
function jumpDown(combo,locale) {
	if (locale != "") {
		combo.value = "default";
		document.location.hash=locale;
	}
}

// OPEN NON-HTML PAGES IN A NEW WINDOW
// Create the new window
function openInNewWindow(e) {
	var event;
	if (!e) event = window.event;
	else event = e;
	// Abort if a modifier key is pressed
	if (event.shiftKey || event.altKey || event.ctrlKey || event.metaKey) {
		return true;
	}
	else {		
		var newWindow = window.open(this.getAttribute('href'), 'popup');
		if (newWindow) {
			if (newWindow.focus) {
			newWindow.focus();
			}
		return false;
		}
	return true;
	}
}
// Add the openInNewWindow function to the onclick event of links with a class name of "non-html"
function getNewWindowLinks() {
	// Check that the browser is DOM compliant
	if (document.getElementById && document.createElement && document.appendChild) {		
		var strNewWindowAlert = " (opens in new window)";
		// Find all links
		//var objWarningText;
		var link;
		var links = document.getElementsByTagName('a');
		for (var i = 0; i < links.length; i++) {
			link = links[i];
			// Find all links with a class name of "non-html"
			if (/\bnon\-html\b/.test(link.className)) {
				// Create an em element containing the new window warning text and insert it after the link text
				//objWarningText = document.createElement("em");
				//objWarningText.appendChild(document.createTextNode(strNewWindowAlert));
				//link.appendChild(objWarningText);
				link.onclick = openInNewWindow;
			}
			// Added for off-site links (GML)
			else if (/\boff\-site\b/.test(link.className)) {				
				link.onclick = openInNewWindow;
			}
			// Added for in-site Cooper links (GML)
			else if (/\bmain\-site\b/.test(link.className)) {					
				link.onclick = function () {
					window.opener.location.href = this.getAttribute('href');	
					window.close();				
					}				
			}
		}
	//objWarningText = null;
	}
}

// ==============================================================
// PSEUDO-SELECT MENU version 3 - Greg Laycock, Fahlgren
// Emulate SELECT element so that search engines can follow links
// ==============================================================
// Initialize PSEUDO-SELECT elements (use on-load)
function initPseudoSelects() {
    // Check that the browser is DOM compliant
	if (document.getElementsByTagName) {
	    var theClass;
		var objDiv;
	    var aryDivs = document.getElementsByTagName('div');
	    for (var i = 0; i < aryDivs.length; i++) {
		    objDiv = aryDivs[i];
		    // Find all divs with a class name of "pseudo-select"
		    if (/\bpseudo\-select\b/.test(objDiv.className)) {
			   	theClass = objDiv.className;
				objDiv.className = theClass + '-active';
				objDiv.onclick = emulateSelectElement2;
			}
	    }
    }
}
// Open and close PSEUDO-SELECT elements
function emulateSelectElement2() {
	var on = 'pseudo-select-active on'
	var off = 'pseudo-select-active off'
	switch (this.className) {
		case on:
			this.className = off;
			break;
		case off:
		default:			
			this.className = on;
			break;
	}
}

// EMULATE DROP-DOWN SELECT BOX SO THAT SEARCH ENGINES CAN FOLLOW LINKS
// LEGACY SUPPORT
function emulateSelectElement() {
	var menuOpen = 'block';
	var menuClose = 'none';
	var theElement = document.getElementById('pseudoselect_options');
	switch (theElement.style.display) {
		case menuOpen:			
			theElement.style.display = menuClose;
			break;
		case menuClose:			
			theElement.style.display = menuOpen;
			break;
		default:			
			theElement.style.display = menuClose;
			break;
	}
}
// ==============================================================
// ==============================================================

// ADJUSTMENT MADE WHEN PSEUDO-SELECT ELEMENT UPDATED
// This allows header to scroll if JavaScript is not available - not the best, but functional
function adjustHeader() {
	var theHeader = document.getElementById('header');
	theHeader.className = "has-js";
}

// LOOK FOR CLOAKED LINKS AND MAKE THEM CLICKABLE
// Hopefully this will help hide e-mail addresses from spam spiders
function createMailtoLinks() {
	// Check that the browser is DOM compliant
	if (document.getElementById && document.createElement && document.appendChild) {			
		var email; // E-mail address
		var mailto; // The mailto hyperlink
		var cloak; // The mailto span element
		var spans = document.getElementsByTagName('span'); // Array of span elements
		for (var i = 0; i < spans.length; i++) {
			cloak = spans[i];
			// Find all span elements with a class name of "cloak"
			if (/\bcloak\b/.test(cloak.className)) {				
				email = cloak.innerHTML;
				cloak.innerHTML = "";									
				mailto = document.createElement('a');															
				mailto.href = 'mailto:' + email;
				mailto.innerHTML = email;
				cloak.appendChild(mailto);			
			}
		}	
	}
}

// ANIMATE COOPERWORLD ROLLOVERS
// Requires jQuery
function animateRollovers() {
	// Cooperworld
	var hotspotWidth;
	var hotspotHeight;	
	$.each($("div.hotspot"), function() {	
		hotspotWidth = $(this).children("a").css("width");
		hotspotHeight = $(this).children("a").css("height");
		$(this).css("width",hotspotWidth);
		$(this).css("height",hotspotHeight);		
	});	
	$("div.hotspot a").css("display","none");
	$("div.hotspot a").css("background-position","0px 0px");
	$("div.hotspot").hover(function() {		
		$(this).children("a").fadeIn();				
	}, function() { 
		$(this).children("a").fadeOut("fast"); 
	});	
	// Main navigation
	var hotspotSelector;
	$("div#standard li").hover(function() {
		hotspotSelector = "div#hotspot_" + $(this).attr("id");
		if (hotspotSelector) {
			$(hotspotSelector).children("a").fadeIn();	
		}
	}, function() {
		$("div.hotspot a").fadeOut("fast");
	});
}

// ON-LOAD EVENTS 
$(document).ready(function() {				
	getNewWindowLinks();	
	createMailtoLinks();
	initPseudoSelects();
	animateRollovers();
});