// vars
msgs = [];
msgOn = [];
msgOff = [];
tabs = [];
boxes = [];
activeTab = tabNames[0];
if (productModel) model = productModel;
else model = ajaxItems[interKey][colorKey]["model"];
specs_colorKey = -1;
acc_colorKey = -1;
sample_colorKey = -1;
mouseDown = 0;
keepOn = [];
errorCount = 0;
noZoomImg = [];
noZoomImg[0] = 0;
noDimeImg = [];
noDimeImg[0] = 0;
noAuxImg = [];
noAuxImg[0] = 0;
ok = '<br class="okSeparator"><div class="okRollover"><a href="javascript: void(0);" onclick="msgOk(\'ajaxMsg\')" title="Ok"><img src="../images/buttons/fb-ok.gif" alt="Ok" /></a></div>';
msgOnAir = 0;
delay = 2000; // msg delay in ms

// ajax
function ajaxAjax(post, processor, container)
{
	// temporarily replace data with loader
	target = document.getElementById(container);
	target.innerHTML = "<div align=\"center\"><img src=\"../images/ajax/ajax-loader.gif\"></div>";

	// create XMLHttpRequest
	var XMLHttpRequestObject = false;
	if (window.XMLHttpRequest) XMLHttpRequestObject = new XMLHttpRequest();
	else if (window.ActiveXObject) XMLHttpRequestObject = new ActiveXObject ("Microsoft.XMLHTTP");
	
	// send XMLHttpRequest
	if (XMLHttpRequestObject)
	{
		// find processor
		XMLHttpRequestObject.open("POST", "../includes/product/" + processor, true);
		XMLHttpRequestObject.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		
		// receive XMLHttpRequest
		XMLHttpRequestObject.onreadystatechange = function()
		{
			if (XMLHttpRequestObject.readyState == 4 || XMLHttpRequestObject.readyState == "complete")
			{
				var response = XMLHttpRequestObject.responseText;
				
				// parse javascript
				var b = response.split(/\<\/?script[^\<]*\>/i)[1];
				eval(b);
				
				// output XMLHttpRequest response
				target.innerHTML = response;
			}
		}
		// POST data
		XMLHttpRequestObject.send(post + "&container=" + container);
	}
}

// change tab
function tabButton(that)
{
	activeTab = that;
	
	// accessories (mark this line in headers.php)
	if (specs_colorKey != colorKey && that == "Specifications")
	{
		specs_colorKey = colorKey;
		ajaxAjax("model=" + ajaxItems[interKey][colorKey]["model"] + "&interKey=" + interKey, "specs-processor.php?debug=" + ajaxDebug, "box" + that + "Container");
	}

	// accessories (mark this line in headers.php)
	if (acc_colorKey != colorKey && that == "Accessories")
	{
		acc_colorKey = colorKey;
		ajaxAjax("line=" + ajaxItems[interKey][colorKey]["product_line"] + "&style=" + ajaxItems[interKey][colorKey]["product_style_id"] + "&color=" + ajaxItems[interKey][colorKey]["product_color_id"] + "&model=" + ajaxItems[interKey][colorKey]["model"] + "&type=" + ajaxItems[interKey][colorKey]["product_type_id"], "acc-processor.php?debug=" + ajaxDebug, "box" + that + "Container");
	}
	
	// sample (mark this line in headers.php)
	if (sample_colorKey != colorKey && that == "Sample")
	{
		sample_colorKey = colorKey;
		ajaxAjax("style=" + ajaxItems[interKey][colorKey]["product_style_id"] + "&color=" + ajaxItems[interKey][colorKey]["product_color_id"] + "&type=Sample", "acc-processor.php?debug=" + ajaxDebug, "box" + that + "Container");
	}
	
	// turn off tabs
	for (i=0; i<tabNames.length; i++) document.getElementById("tabContainer"+tabNames[i]).innerHTML = "<a href=\"javascript: void(0);\" id=\"tab" + tabNames[i] + "\" onclick=\"tabButton('" + tabNames[i] + "')\"><img src=\"../images/ajax/tab.jpg\"><br /><span>" + tabNames[i] + "</span></a>";
	
	// highlight one
	document.getElementById("tabContainer"+that).innerHTML = "<img src=\"../images/ajax/tab-on.jpg\"><br /><span>" + that + "</span>";
	
	// turn off boxes
	for (i=0; i<tabNames.length; i++) document.getElementById("box" + tabNames[i]).style.display = "none";
	
	// open one
	document.getElementById("box" + that).style.display = "inline";
	
	//return false;
}

// fade
function setOpacity(container, value)
{
	msgs[container] = document.getElementById(container);
	
	// firefox
	msgs[container].style.opacity = value/10;
	
	// internet explorer
	msgs[container].style.filter = 'alpha(opacity=' + value*10 + ')';
}

// accessories: add to cart
function accAtc(that)
{
	var model = document.getElementById("ajaxAccSSModel" + that);
	
	// quantity
	var quantity = document.getElementById("inputQty" + that);
	var boxQty = 0;
	if (document.getElementById("boxQty" + that)) boxQty = document.getElementById("boxQty" + that).value;
	
	// clean quantity from letters and symbols
	quantity.value = quantity.value.replace(/[^0-9]*/g, "");
	var itemsQty = quantity.value;

	// if quantity valid, send ajax request
	if (itemsQty * 1)
	{
		if (boxQty) itemsQty *= boxQty;
		if (!msgOnAir)
		{
			document.getElementById('ajaxMsg' + that).className = "ajaxMsgNone";
			ajaxAjax('model=' + model.innerHTML + '&quantity=' + itemsQty, 'atc-processor.php', 'ajaxMsg' + that);
		}
	}
	else msgShow("ajaxMsg" + that, "ajaxMsgBad", "Enter<br>quantity<br>number");
	return false;
}

// accessories: add firerating
function accFirerating(that)
{
	var model = document.getElementById("ajaxAccSSModel" + that);
	model.innerHTML.substr(model.innerHTML.length - 3) == "-FR" ? fr = 1 : fr = 0;
	
	// change model
	if (document.getElementById("ajaxFirerating" + that).checked && !fr) model.innerHTML += "-FR";
	if (!document.getElementById("ajaxFirerating" + that).checked && fr) model.innerHTML = model.innerHTML.substr(0, model.innerHTML.length - 3);
	
	// change price
	model = model.innerHTML.replace(/-/g, "");
	if (document.getElementById("accAjaxPriceValue1" + that))
	{
		// no sale
		document.getElementById("accAjaxPriceValue1" + that).innerHTML = "$" + jsItems[model + "price"];
	}
	else
	{
		// sale
		document.getElementById("accAjaxPriceValue2" + that).innerHTML = "$" + jsItems[model + "price_backup"];
		document.getElementById("accAjaxPriceValue3" + that).innerHTML = "$" + jsItems[model + "price"];
	}
}

function colChange(elem, modl, type)
{
	if (document.getElementById(elem))
	{
		elem = document.getElementById(elem);
		switch (type)
		{
			case "href":
				// change link
				elem.href = elem.href.replace(/(.*\/)+(.*\.php\?model=).+/i, "$1$2" + modl);
				break;
				
			case "src":
				// change src
				elem.src = elem.src.replace(/(.*\/)+.*\.jpg/i, "$1" + modl + ".jpg");
				break;
				
			default:
				// do nothing
				break;
		}
	}
	return false;
}

function msgOk(container)
{
	if (document.getElementById(container)) document.getElementById(container).className = "preload";
}

function msgShow(container, clas, text)
{
	if (document.getElementById(container))
	{
		if (!msgOnAir) // lock button until message disappears
		{
			msgOnAir = 1;
			setOpacity(container, 10);
			var target = document.getElementById(container);
			target.className = clas;
			for (var i = 0; i <= 10; i++)
			{
				setTimeout("setOpacity('" + container + "', " + i + ")", 500 - 50 * i + delay);
				setTimeout("msgOnAir = 0", 500 + delay);
			}
			if (text) target.innerHTML = text;
		}
	}
	return false;
}

function number_format(that)
{
	var n = 2; // number of decimals to show
	var d = '.'; // decimals separator (ignored if n == 0)
	var t = ','; // thousands separator
	//that = 12345.67; // debug
	that = (Math.round(that * Math.pow(10, n))).toString();
	var l = that.length - n;
	var out = n ? d + that.substr(l, n) : '';
	if (l)
	{
		for (i = 3; i < l; i += 3) out = t + that.substr(l - i, 3) + out;
		out = that.substr(0, l - i + 3) + out;
	}
	else out = '0' + out;
	return out;
}

function changePrice(price, price_backup, note_sale)
{
	price = price * 1;
	price_backup = price_backup * 1;
	var out = "<div class=\"ajaxPrice\">Price: ";
	if (price_backup > price) // sale
	{
		out += "<span class=\"ajaxPriceValue2\" id=\"accAjaxPriceValue2\">\$" + number_format(price_backup) + "</span> <span class=\"ajaxPriceValue3\" id=\"accAjaxPriceValue3\">\$" + number_format(price) + "</span>";
		if (note_sale)
		{
			out += "<span class=\"small warn\"><sup>&dagger;</sup></span>";
			if (document.getElementById("ajaxNoteSale")) document.getElementById("ajaxNoteSale").innerHTML = "<span class=\"warn\">&dagger;</span>&nbsp;&mdash;&nbsp;" + note_sale;
		}
	}
	else out += "<span class=\"ajaxPriceValue1\" id=\"accAjaxPriceValue1\">\$" + number_format(price) + "</span>";
	out += " each</div>";
	return out;
}