$(function() {

	// add close button popup videos
	$(".video").append('<a class="video-button" href="#" onclick="tb_remove(); return false;"></a>');


});

function openLogin() {
	$("#quick-login").animate({ height: "toggle" }, "slow");
	$("#quick-login input[@name=login_user_name]").focus();
	return false;
}

function closeLogin() {
	$("#quick-login").animate({ height: "hide" }, "slow");
	return false;
}

function showSubmenu(layer) {
	$("#"+layer).animate({ height: "toggle" }, "slow");
}

function showHide(layer) {
	$("#"+layer).slideToggle("normal");
}

var shipFirstName="";
var shipLastName="";
var shipAddress="";
var shipAddress2="";
var shipCity="";
var shipState="";
var shipZipCode="";

function saveOldValues(form) {
	shipFirstName = form.shipping_first_name.value;
	shipLastName = form.shipping_last_name.value;
	shipAddress = form.shipping_address.value;
	shipAddress2 = form.shipping_address2.value;
	shipCity = form.shipping_city.value;
	shipState = form.shipping_state.selectedIndex;
	shipZipCode = form.shipping_zip_code.value;
}

function sameAsBilling(form) {
	if(form.same_as_billing.checked) {
		saveOldValues(form);
		form.shipping_first_name.value = form.billing_first_name.value;
		form.shipping_last_name.value = form.billing_last_name.value;
		form.shipping_address.value = form.billing_address.value;
		form.shipping_address2.value = form.billing_address2.value;
		form.shipping_city.value = form.billing_city.value;
		form.shipping_state.selectedIndex = form.billing_state.selectedIndex;
		form.shipping_zip_code.value = form.billing_zip_code.value;
	} else {
		form.shipping_first_name.value = shipFirstName;
		form.shipping_last_name.value = shipLastName;
		form.shipping_address.value = shipAddress;
		form.shipping_address2.value = shipAddress2;
		form.shipping_city.value = shipCity;
		form.shipping_state.selectedIndex = shipState;
		form.shipping_zip_code.value = shipZipCode;
	}
}

function emailSwitch() {
	if(!document.getElementsByTagName && !document.createElement && !document.createTextNode) return;
	var nodes = document.getElementsByTagName("span");
	for(var i=nodes.length-1;i>=0;i--) {
		if(nodes[i].className=="mail") {
			var at = / at /;
			var dot = / dot /g;
			var node = document.createElement("a");
			var address = nodes[i].firstChild.nodeValue;

			address = address.replace(at, "@");
			address = address.replace(dot, ".");

			node.setAttribute("href", "mailto:"+address);
			node.appendChild(document.createTextNode(address));
			
			var prnt = nodes[i].parentNode;
			for(var j=0;j<prnt.childNodes.length;j++) {
				if(prnt.childNodes[j] == nodes[i]) {
					if(!prnt.replaceChild) return;
					prnt.replaceChild(node, prnt.childNodes[j]);
					break;
				}
			}
		}
	}
}

function playMp3(id, el) {

	/*player +='            <script type="text/javascript">\n';
	player +='              // <![CDATA[\n';
	player +='                var so = new SWFObject("http://www.safetyfirstracing.com/musicplayer.swf", "music_preview", "0", "0", "7", "#FFFFFF");\n';
	player +='                so.addVariable("autoload", "true");\n';
	player +='                so.addVariable("song_url", "'+el+'");\n';
	player +='                so.addVariable("autoplay", "true");\n';
	player +='                so.write("play-podcast");\n';
	player +='              // ]]>\n';
	player +='            </script>\n';*/

	var player = '<embed type="application/x-shockwave-flash" src="http://www.safetyfirstracing.com/musicplayer.swf" id="music_preview" name="music_preview" bgcolor="#FFFFFF" quality="high" flashvars="autoload=true&amp;song_url='+el+'&amp;autoplay=true" height="0" width="0">';

	$("#play-podcast").html(player);
	$(".play a").removeClass("stop").addClass("start");
	$("#"+id+" .play").html("<a class=\"stop\" href=\"#\" onclick=\"stopMp3('"+id+"', '"+el+"'); return false;\"></a>");
}

function stopMp3(id, el) {
	$("#play-podcast").html("");
	$("#"+id+" .play").html("<a class=\"start\" href='"+el+"' onclick=\"playMp3('"+id+"', this); return false;\"></a>");
}

function swapLayers(layer1, layer2) {
	$("#"+layer1).hide();
	$("#"+layer2).show();
}

function showDetails(layer) {
	$("#"+layer).slideToggle("normal");
}

function getAvailableModels(man) {
	$.getJSON("/ajax/update_models.php",{ bikeman: man }, function(j) {
		var options = "";
		for(var i=0; i < j.length; i++) {
			options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
		}
		$("#mod").html(options);
		$("#mod option:first").attr("selected", "selected");
	});
}

function setDetails(id, options) {
	opts = options.split("|");
	var sku="";
	var regp="";
	var salep="";

	sku = opts[0];
	regp = "$"+number_format(opts[1], 2, '.', ',');
	salep = opts[2];

	if(options) {
		if(salep > 0) {
			$("#item_"+id+" h4.rp").addClass("strike").html("Price "+regp);
			$("#item_"+id+" h4.sp").html("Sale Price "+"$"+number_format(salep, 2, '.', ','));
		} else {
			$("#item_"+id+" h4.rp").removeClass("strike").html("Price "+regp);
			$("#item_"+id+" h4.sp").html("");
		}
		$("#item_"+id+" h4.sku").html("Product Number "+sku);
	} else {
		$("#item_"+id+" h4.rp").removeClass("strike").html("");
		$("#item_"+id+" h4.sp").html("");
		$("#item_"+id+" h4.sku").html("");
	}
}

// removes yellow background in form fields set by google's auto-fill toolbar 
if (window.attachEvent) window.attachEvent("onload",setListeners);

function setListeners() {
	inputList = document.getElementsByTagName("INPUT");
	for(i=0;i<inputList.length;i++){
		inputList[i].attachEvent("onpropertychange",restoreStyles);
		inputList[i].style.backgroundColor = "";
	}
	selectList = document.getElementsByTagName("SELECT");
	for(i=0;i<selectList.length;i++){
		selectList[i].attachEvent("onpropertychange",restoreStyles);
		selectList[i].style.backgroundColor = "";
	}
}

function restoreStyles() {
	if(event.srcElement.style.backgroundColor != "")
		event.srcElement.style.backgroundColor = "";
}

// number rounding by Mathias Bynens <http://mathiasbynens.be/>
function number_format(a, b, c, d) {
	a = Math.round(a * Math.pow(10, b)) / Math.pow(10, b);
	e = a + '';
	f = e.split('.');
	if(!f[0]) f[0] = '0';
	if(!f[1]) f[1] = '';
	if(f[1].length < b) {
		g = f[1];
		for(i=f[1].length + 1; i <= b; i++) {
			g += '0';
		}
		f[1] = g;
	}
	if(d != '' && f[0].length > 3) {
		h = f[0];
		f[0] = '';
		for(j = 3; j < h.length; j+=3) {
			i = h.slice(h.length - j, h.length - j + 3);
			f[0] = d + i +  f[0] + '';
		}
		j = h.substr(0, (h.length % 3 == 0) ? 3 : (h.length % 3));
		f[0] = j + f[0];
	}
	c = (b <= 0) ? '' : c;
	return f[0] + c + f[1];
}



/**
 * SWFObject v1.4: Flash Player detection and embed - http://blog.deconcept.com/swfobject/
 *
 * SWFObject is (c) 2006 Geoff Stearns and is released under the MIT License:
 * http://www.opensource.org/licenses/mit-license.php
 *
 * **SWFObject is the SWF embed script formarly known as FlashObject. The name was changed for legal reasons.
 */
if(typeof deconcept == "undefined") var deconcept = new Object();
if(typeof deconcept.util == "undefined") deconcept.util = new Object();
if(typeof deconcept.SWFObjectUtil == "undefined") deconcept.SWFObjectUtil = new Object();
deconcept.SWFObject = function(swf, id, w, h, ver, c, useExpressInstall, quality, xiRedirectUrl, redirectUrl, detectKey){
	if (!document.createElement || !document.getElementById) { return; }
	this.DETECT_KEY = detectKey ? detectKey : 'detectflash';
	this.skipDetect = deconcept.util.getRequestParameter(this.DETECT_KEY);
	this.params = new Object();
	this.variables = new Object();
	this.attributes = new Array();
	if(swf) { this.setAttribute('swf', swf); }
	if(id) { this.setAttribute('id', id); }
	if(w) { this.setAttribute('width', w); }
	if(h) { this.setAttribute('height', h); }
	if(ver) { this.setAttribute('version', new deconcept.PlayerVersion(ver.toString().split("."))); }
	this.installedVer = deconcept.SWFObjectUtil.getPlayerVersion(this.getAttribute('version'), useExpressInstall);
	if(c) { this.addParam('bgcolor', c); }
	var q = quality ? quality : 'high';
	this.addParam('quality', q);
	this.setAttribute('useExpressInstall', useExpressInstall);
	this.setAttribute('doExpressInstall', false);
	var xir = (xiRedirectUrl) ? xiRedirectUrl : window.location;
	this.setAttribute('xiRedirectUrl', xir);
	this.setAttribute('redirectUrl', '');
	if(redirectUrl) { this.setAttribute('redirectUrl', redirectUrl); }
}
deconcept.SWFObject.prototype = {
	setAttribute: function(name, value){
		this.attributes[name] = value;
	},
	getAttribute: function(name){
		return this.attributes[name];
	},
	addParam: function(name, value){
		this.params[name] = value;
	},
	getParams: function(){
		return this.params;
	},
	addVariable: function(name, value){
		this.variables[name] = value;
	},
	getVariable: function(name){
		return this.variables[name];
	},
	getVariables: function(){
		return this.variables;
	},
	getVariablePairs: function(){
		var variablePairs = new Array();
		var key;
		var variables = this.getVariables();
		for(key in variables){
			variablePairs.push(key +"="+ variables[key]);
		}
		return variablePairs;
	},
	getSWFHTML: function() {
		var swfNode = "";
		if (navigator.plugins && navigator.mimeTypes && navigator.mimeTypes.length) { // netscape plugin architecture
			if (this.getAttribute("doExpressInstall")) this.addVariable("MMplayerType", "PlugIn");
			swfNode = '<embed type="application/x-shockwave-flash" src="'+ this.getAttribute('swf') +'" width="'+ this.getAttribute('width') +'" height="'+ this.getAttribute('height') +'"';
			swfNode += ' id="'+ this.getAttribute('id') +'" name="'+ this.getAttribute('id') +'" ';
			var params = this.getParams();
			 for(var key in params){ swfNode += [key] +'="'+ params[key] +'" '; }
			var pairs = this.getVariablePairs().join("&");
			 if (pairs.length > 0){ swfNode += 'flashvars="'+ pairs +'"'; }
			swfNode += '/>';
		} else { // PC IE
			if (this.getAttribute("doExpressInstall")) this.addVariable("MMplayerType", "ActiveX");
			swfNode = '<object id="'+ this.getAttribute('id') +'" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="'+ this.getAttribute('width') +'" height="'+ this.getAttribute('height') +'">';
			swfNode += '<param name="movie" value="'+ this.getAttribute('swf') +'" />';
			var params = this.getParams();
			for(var key in params) {
			 swfNode += '<param name="'+ key +'" value="'+ params[key] +'" />';
			}
			var pairs = this.getVariablePairs().join("&");
			if(pairs.length > 0) {swfNode += '<param name="flashvars" value="'+ pairs +'" />';}
			swfNode += "</object>";
		}
		return swfNode;
	},
	write: function(elementId){
		if(this.getAttribute('useExpressInstall')) {
			// check to see if we need to do an express install
			var expressInstallReqVer = new deconcept.PlayerVersion([6,0,65]);
			if (this.installedVer.versionIsValid(expressInstallReqVer) && !this.installedVer.versionIsValid(this.getAttribute('version'))) {
				this.setAttribute('doExpressInstall', true);
				this.addVariable("MMredirectURL", escape(this.getAttribute('xiRedirectUrl')));
				document.title = document.title.slice(0, 47) + " - Flash Player Installation";
				this.addVariable("MMdoctitle", document.title);
			}
		}
		if(this.skipDetect || this.getAttribute('doExpressInstall') || this.installedVer.versionIsValid(this.getAttribute('version'))){
			var n = (typeof elementId == 'string') ? document.getElementById(elementId) : elementId;
			n.innerHTML = this.getSWFHTML();
			return true;
		}else{
			if(this.getAttribute('redirectUrl') != "") {
				document.location.replace(this.getAttribute('redirectUrl'));
			}
		}
		return false;
	}
}

/* ---- detection functions ---- */
deconcept.SWFObjectUtil.getPlayerVersion = function(reqVer, xiInstall){
	var PlayerVersion = new deconcept.PlayerVersion([0,0,0]);
	if(navigator.plugins && navigator.mimeTypes.length){
		var x = navigator.plugins["Shockwave Flash"];
		if(x && x.description) {
			PlayerVersion = new deconcept.PlayerVersion(x.description.replace(/([a-z]|[A-Z]|\s)+/, "").replace(/(\s+r|\s+b[0-9]+)/, ".").split("."));
		}
	}else{
		try{
			var axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash");
			for (var i=3; axo!=null; i++) {
				axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash."+i);
				PlayerVersion = new deconcept.PlayerVersion([i,0,0]);
			}
		}catch(e){}
		if (reqVer && PlayerVersion.major > reqVer.major) return PlayerVersion; // version is ok, skip minor detection
		// this only does the minor rev lookup if the user's major version 
		// is not 6 or we are checking for a specific minor or revision number
		// see http://blog.deconcept.com/2006/01/11/getvariable-setvariable-crash-internet-explorer-flash-6/
		if (!reqVer || ((reqVer.minor != 0 || reqVer.rev != 0) && PlayerVersion.major == reqVer.major) || PlayerVersion.major != 6 || xiInstall) {
			try{
				PlayerVersion = new deconcept.PlayerVersion(axo.GetVariable("$version").split(" ")[1].split(","));
			}catch(e){}
		}
	}
	return PlayerVersion;
}
deconcept.PlayerVersion = function(arrVersion){
	this.major = parseInt(arrVersion[0]) != null ? parseInt(arrVersion[0]) : 0;
	this.minor = parseInt(arrVersion[1]) || 0;
	this.rev = parseInt(arrVersion[2]) || 0;
}
deconcept.PlayerVersion.prototype.versionIsValid = function(fv){
	if(this.major < fv.major) return false;
	if(this.major > fv.major) return true;
	if(this.minor < fv.minor) return false;
	if(this.minor > fv.minor) return true;
	if(this.rev < fv.rev) return false;
	return true;
}
/* ---- get value of query string param ---- */
deconcept.util = {
	getRequestParameter: function(param){
		var q = document.location.search || document.location.hash;
		if(q){
			var startIndex = q.indexOf(param +"=");
			var endIndex = (q.indexOf("&", startIndex) > -1) ? q.indexOf("&", startIndex) : q.length;
			if (q.length > 1 && startIndex > -1) {
				return q.substring(q.indexOf("=", startIndex)+1, endIndex);
			}
		}
		return "";
	}
}

/* add Array.push if needed (ie5) */
//if (Array.prototype.push == null) { Array.prototype.push = function(item) { this[this.length] = item; return this.length; }}

/* add some aliases for ease of use/backwards compatibility */
var getQueryParamValue = deconcept.util.getRequestParameter;
var FlashObject = deconcept.SWFObject; // for legacy support
var SWFObject = deconcept.SWFObject;yParamValue = deconcept.util.getRequestParameter;
var FlashObject = deconcept.SWFObject; // for legacy support
var SWFObject = deconcept.SWFObject;