if (window != top) top.location.href=location.href

if (document.getElementById && document.createElement) {

	// Alternate stylesheet switcher based on Paul Sowden's code from http://www.alistapart.com/articles/alternate/

	function setActiveStyleSheet(title) {
		var i, a, main;
		for(i=0; (a = document.getElementsByTagName('link')[i]); i++) {
			if(a.getAttribute('rel').indexOf('style') != -1 && a.getAttribute('title')) {
				a.disabled = true;
				if(a.getAttribute('title') == title) a.disabled = false;
			}
		}
		if (title != 'Default') createCookie('style', title, 0);
		if (title == 'Default') createCookie('style', '', -1);
	}
	
	function selectStyleSheet(title) {
		var i, a, main;
		for(i=0; (a = document.getElementsByTagName('link')[i]); i++) {
			if(a.getAttribute('rel').indexOf('style') != -1 && a.getAttribute('title')) {
				a.disabled = true;
				if(a.getAttribute('title') == title) a.disabled = false;
			}
		}
		var title = getActiveStyleSheet();
		createCookie('style', title, 0);
	}
	
	function getActiveStyleSheet() {
		var i, a;
		for(i=0; (a = document.getElementsByTagName('link')[i]); i++) {
			if(a.getAttribute('rel').indexOf('style') != -1 
			&& a.getAttribute('title') 
			&& !a.disabled) return a.getAttribute('title');
		}
		return null;
	}
	
	function getPreferredStyleSheet() {
		var i, a;
		for(i=0; (a = document.getElementsByTagName('link')[i]); i++) {
			if(a.getAttribute('rel').indexOf('style') != -1
				&& a.getAttribute('rel').indexOf('alt') == -1
				&& a.getAttribute('title')
				) return a.getAttribute('title');
		}
		return null;
	}
	
	 // Cookie functions by http://www.scottandrew.com/
	
	function createCookie(name,value,days) {
		if (days) {
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			var expires = '; expires='+date.toGMTString();
		}
		else expires = '';
		document.cookie = name+'='+value+expires+'; path=/';
	}
	
	function readCookie(name) {
		var nameEQ = name + '=';
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++) {
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
		}
		return null;
	}

	var cookie = readCookie('style'); // Get stored selected alternate style
	var title = cookie ? cookie : getPreferredStyleSheet();
	setActiveStyleSheet(title); // Re-enable previously selected alternate style
	
	window.onload = function() {

		oSearchPhrase = document.getElementById('sp-q'); // Declare global variable for the search field prompt text
		oSearchPhrase.onfocus = function () {
			if (oSearchPhrase.value == 'Enter search phrase here') oSearchPhrase.value = '';
		}
		oSearchPhrase.onblur = function () {
			if (oSearchPhrase.value == '') oSearchPhrase.value = 'Enter search phrase here';
		}

		if (oSearchPhrase != null) oSearchPhrase.value = 'Enter search phrase here'; // If the search field is present, place the prompt text in it
		if ((typeof d) != 'undefined') { // Check if the variable "d" exists, if it does then the page has the parts tree menu on it, initialize it
			initializeMenu('brakes', 'hbrakes', '0');
			initializeMenu('engine', 'hengine', '1');
			initializeMenu('cockpit', 'hcockpit', '2');
			initializeMenu('inframe', 'hinframe', '3');
			initializeMenu('body', 'hbody', '4');
			initializeMenu('cooling', 'hcooling', '5');
			initializeMenu('wheels', 'hwheels', '6');
			initializeMenu('other', 'hother', '7');
		}
	}
	
	window.onunload = function(e) { // Save in a cookie the chosen alternate style if the user has selected it via the browser supplied menu rather than via the controls inside the viewport (doesn't work in Opera)
		var title = getActiveStyleSheet();
		if (title != 'Default') createCookie('style', title, 0); // Store the style preference in a cookie if other than "Default"
		if (title == 'Default') createCookie('style', '', -1); // Delete the style cookie if the "Default" style is chosen
	}

	function panelToggle() { // Check the currently active stylesheet and change it conditionally
		var title = getActiveStyleSheet();
		if (title == 'Default') setActiveStyleSheet('No panel');
		if (title == 'No panel') setActiveStyleSheet('Default');
		if (title == 'No navbar') setActiveStyleSheet('No panel or navbar');
		if (title == 'No panel or navbar') setActiveStyleSheet('No navbar');
	}

	function navToggle() { // Check the currently active stylesheet and change it conditionally
		var title = getActiveStyleSheet();
		if (title == 'Default') setActiveStyleSheet('No navbar');
		if (title == 'No panel') setActiveStyleSheet('No panel or navbar');
		if (title == 'No navbar') setActiveStyleSheet('Default');
		if (title == 'No panel or navbar') setActiveStyleSheet('No panel');
	}

	function verify() { // Check to see if the user entered a search phrase, event: onsubmit
		if ((oSearchPhrase.value == '') || (oSearchPhrase.value == 'Enter search phrase here')){
			alert('Enter a search phrase in the field first');
			return false;
		}
	}

	var pMenuState = readCookie('partMenuState'); // Check if the parts tree menu has open node(s) stored in a cookie
	
	if (pMenuState != null) { // Recreate the array from the state stored in the cookie
		var menuState = pMenuState.split(',');
	}
	else { // Create the array if not stored in the cookie
		var menuState = new Array('0','0','0','0','0','0','0','0');
	}
	
	function initializeMenu(menuId, actuatorId, node) {// Tree menu functions based on code by Dave Lindquist
		var menu = document.getElementById(menuId);
		var actuator = document.getElementById(actuatorId);
		var opened = menuState[node]; // Extract this tree node's stored open state from the array
		if (opened == '1') { // Re-open the tree node if it was previously opened
				menu.style.display = 'block'; // Enable the <ul>
				actuator.style.background = 'url(../img/minus.png) no-repeat 0 .3em'; // Change the "+" actuator image to "-"
		}
		if (menu == null || actuator == null) return;
		actuator.onclick = function() {
			var display = menu.style.display;
			if (menu.style.display == 'block') {
				menu.style.display = 'none'; // Disable the <ul>
				actuator.style.background = 'url(../img/plus.png) no-repeat 0 .3em'; // Change the "-" actuator image to "+"
				menuState[node] = '0'; // Set this tree node's open state to "0"
				createCookie ('partMenuState',menuState,0); // Store state of tree nodes in a cookie
			}
			else {
				menu.style.display = 'block'; // Enable the <ul>
				actuator.style.background = 'url(../img/minus.png) no-repeat 0 .3em'; // Change the "+" actuator image to "-"
				menuState[node] = '1'; // Set this tree node's open state to "1"
				createCookie ('partMenuState',menuState,0); // Store state of tree nodes in a cookie
			}
			return false;
			}
	}
}
