// TRIGGER EVENTS ON-LOAD
// addEvent function from http://www.quirksmode.org/blog/archives/2005/10/_and_the_winner_1.html
function addEvent( obj, type, fn ) {
	if (obj.addEventListener) {
		obj.addEventListener( type, fn, false );
	} else if (obj.attachEvent) {
		obj["e"+type+fn] = fn;
		obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
		obj.attachEvent( "on"+type, obj[type+fn] );
	}
}

// removeEvent function from http://www.quirksmode.org/blog/archives/2005/10/_and_the_winner_1.html
function removeEvent( obj, type, fn ) {
	if (obj.removeEventListener) {
		obj.removeEventListener( type, fn, false );
	} else if (obj.detachEvent) {
		obj.detachEvent( "on"+type, obj[type+fn] );
		obj[type+fn] = null;
		obj["e"+type+fn] = null;
	}
}

// OPEN A PAGE 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'), '', 'width=780, height=500, scrollbars=yes, resizable=yes, toolbar=yes, location=yes, directories=no, menubar=yes, copyhistory=no');
		if (newWindow) {
			if (newWindow.focus) {
			newWindow.focus();
			}
		return false;
		}
	return true;
	}
}

// CALL THIS FUNCTION TO INITIATE FUNCTION THAT OPENS CERTAIN LINKS IN NEW WINDOWS
function getNewWindowLinks() {
	// Check that the browser is DOM compliant
	if (document.getElementById && document.createElement && document.appendChild) {		
		// var strNewWindowAlert = " (opens in a 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" - Use for PDF documents and the like
			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;
			}
			// Find all links with a class name of "off-site" (Added by GML)
			else if (/\boff\-site\b/.test(link.className)) {				
				link.onclick = openInNewWindow;
			}			
		}
	objWarningText = null;
	}
}

// LOOK FOR TABLES AND CREATE ALTERNATE ROWS
// This auto adjusts alternate rows as new rows are added
function createAlternateRows() {
	// Check that the browser is DOM compliant
	if (document.getElementById) {		
		// Find all table rows		
		var row;
		var tablerows = document.getElementsByTagName('tr');
		var counter = 0;
		for (var i = 0; i < tablerows.length; i++) {
			row = tablerows[i];
			if (counter == 1) {
				row.className = 'alt';
				counter = 0;
			}
			else {
				row.className = '';
				counter ++;
			}							
		}	
	}
}

// LOOK FOR UNORDERED LISTS TO CREATED COLUMNAR LISTS
function createColumnarLists() {
	// Check that the browser is DOM compliant
	if (document.getElementById) {		
		// Find all unordered lists	
		var ul;
		var ullists = document.getElementsByTagName('ul');		
		for (var i = 0; i < ullists.length; i++) {
			ul = ullists[i];
			// Find all lists with a class name of "columnar"
			if (/\bcolumnar\b/.test(ul.className)) {
				var counter = 0;
				// Get all children
				for (var j = 0; j < ul.childNodes.length; j++) {								
				 	var li = ul.childNodes[j];					
					// Check to make sure this is an element node rather than a white space (text) node
					if (li.nodeType == '1') {
						// Add classes to list items
						if (counter == 1) {
							li.className = 'column2';							
							counter = 0;						
						}
						else {
							li.className = 'column1';							
							counter ++;						
						}
						// Append additional class if no content exists
						if (li.innerHTML == '') {
							li.className = li.className + ' none';
						}
					}								
				}				
			}					
		}
	}
}

// PSEUDO-SELECT MENU version 1.1 GML
// EMULATE DROP-DOWN SELECT BOX SO THAT SEARCH ENGINES CAN FOLLOW LINKS
function emulateSelectElement() {
	var theElement = document.getElementById('pseudoselect');	
	if (theElement != null) {
		theElement.style.position = 'absolute';		
		var menuOpen = 'block';
		var menuClose = 'none';
		var theList = document.getElementById('pseudoselect_options');
		switch (theList.style.display) {
			case menuOpen:			
				theList.style.display = menuClose;
				break;
			case menuClose:			
				theList.style.display = menuOpen;
				break;
			default:			
				theList.style.display = menuClose;
				break;
		}
	}
}

// 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);			
			}
		}	
	}
}

// SET FOCUS ON PAGES WITH USER FORMS
// Look for inputs tags with the class name of "first"
function goToFirstInput() {		
	var objInput;		
	var aryInput = document.getElementsByTagName('input');
	for (var i = 0; i < aryInput.length; i++) {
		objInput = aryInput[i];
		// Find all inputs with a class name of "first"
		if (/\bfirst\b/.test(objInput.className)) {				
			objInput.focus();							
		}				
	}	
}

// IMAGE REPLACEMENT FOR HEADERS
function replaceHeaders() {
	// Check that the browser is DOM compliant
	if (document.getElementById) {
		// Find all image headers
		var span;
		var spans = document.getElementsByTagName('span');
		for (var i = 0; i < spans.length; i++) {
			span = spans[i];
			// Find all spans with a class name of "image-header"
			if (/\bimage\-header\b/.test(span.className)) {
				var theImage = span.id;	
				// Check to make sure image is specified	
				//alert(theImage);		
				if (theImage != '') {			
					// Hide text		
					span.style.display = 'block';
					// Show image
					span.style.backgroundImage = 'url(/html/images/headers/' + theImage + '.gif)';
				}						
			}
		}
	}
}

// OPEN TIRE PRODUCT POPUP
function openTireImage(theURL) {
	window.open(theURL,'thirdparty','width=500,height=620,top=10,left=10,toolbar=no,resizable=yes,scrollbars=yes');
}

// PAGE TABS
function initTabs(tabNumber) {
	if (document.getElementById) {
		// Get selected tab
		var selectedTab;					
		if (tabNumber != undefined) {
			selectedTab = tabNumber;
		}
		else {						
			selectedTab = 1; // Default
		}
		// Hide all tabs
		if (document.getElementById) {			
			var tab;
			var tabs = document.getElementsByTagName('div');
			for (var i = 0; i < tabs.length; i++) {
				tab = tabs[i];
				// Find all div elements with a class name of "tabcontent"
				if (/\btabcontent\b/.test(tab.className)) {				
					tab.style.display = 'none'; // Hide
				}
			}	
		}
		// Show selected tab
		var tabElement = 'text' + selectedTab;
		document.getElementById(tabElement).style.display = 'block';
	}
}			

// ON-LOAD EVENTS
addEvent(window, 'load', getNewWindowLinks);
addEvent(window, 'load', replaceHeaders);
addEvent(window, 'load', goToFirstInput);
addEvent(window, 'load', createMailtoLinks);
addEvent(window, 'load', createAlternateRows);
addEvent(window, 'load', createColumnarLists);
addEvent(window, 'load', emulateSelectElement);