// select first tab
//document.getElementById("box" + activeTab).style.display = "inline";

// a
links = document.getElementsByTagName('a');
for (i=0; i<links.length; i++)
{
	
//---------------- color button ----------------

	if (links[i].className.match("colorButton"))
	{
		links[i].onclick = function()
		{
			// if not selected already
			if (model != modelContainer.innerHTML)
			{
				// if "Accessories" or "Sample" selected, switch bottom tab to "Specifications"
				//if (activeTab == "Accessories" || activeTab == "Sample") tabButton(tabNames[0]);
				
				// reload current tab
				tabButton(activeTab);
				
				// set cookie for ajaxColor;
				document.cookie = 'ajaxColor = ' + this.rel + '; expires = 0; path = /';
				
				// change detail image
				if (document.getElementById("ajaxDetailImage")) colChange("ajaxDetailImage", model, "src")
				
				// change zoom link 1
				//if (document.getElementById("ajaxZoom1")) colChange("ajaxZoom1", model, "href");
				
				// change zoom link 2
				noZoomImg[0] = 0;
				for (iz = 1; iz < noZoomImg.length; iz++) if (model == noZoomImg[iz]) noZoomImg[0] = iz;
				if (noZoomImg[0])
				{
					//alert("No-zoom found\nIndex: " + noZoomImg[0] + "\nId: " + model);
					document.getElementById("ajaxZoom2").style.display = "none";
				}
				else
				{
					//alert("Seems it exists. Showing Zoom In link.");
					colChange("ajaxZoom2", model, "href");
					document.getElementById("ajaxZoom2").style.display = "";
				}
				
				// change dimensions link
				noDimeImg[0] = 0;
				for (iz = 1; iz < noDimeImg.length; iz++) if (model == noDimeImg[iz]) noDimeImg[0] = iz;
				if (noDimeImg[0])
				{
					//alert("No-dim found\nIndex: " + noDimeImg[0] + "\nId: " + model);
					document.getElementById("ajaxDimensions").style.display = "none";
				}
				else
				{
					//alert("Seems it exists. Showing Dimensions link.");
					colChange("ajaxDimensions", model, "href");
					document.getElementById("ajaxDimensions").style.display = "";
				}
				
				// change aux images
				for (aux = 1; aux < 3; aux++)
				{
					// fix model for the second auxiliary image
					var auxModel = model;
					if (aux > 1) auxModel += aux;

					// see if it's not in non-existent list
					noAuxImg[0] = 0;
					for (iz = 1; iz < noAuxImg.length; iz++) if (auxModel == noAuxImg[iz]) noAuxImg[0] = iz;
					if (noAuxImg[0])
					{
						// hide container
						if (document.getElementById("ajaxAuxContainer" + aux)) document.getElementById("ajaxAuxContainer" + aux).style.display = "none";
					}
					else
					{
						// change image
						//if (document.getElementById("ajaxAuxImage" + aux)) colChange("ajaxAuxImage" + aux, auxModel, "src");
						if (document.getElementById("ajaxAuxImage" + aux)) document.getElementById("ajaxAuxImage" + aux).src = document.getElementById("imgAuxUrlField" + aux).value.replace(/(.*\/)+.*\.jpg/i, "$1" + auxModel + ".jpg");
						
						// change link 1
						if (document.getElementById("ajaxAuxLink" + aux)) colChange("ajaxAuxLink" + aux, auxModel, "href");
						
						// show container
						if (document.getElementById("ajaxAuxContainer" + aux)) document.getElementById("ajaxAuxContainer" + aux).style.display = "";
					}
				}
				
				// change model in small specs
				modelContainer.innerHTML = model;
				
				// change color in small specs
				document.getElementById("smallSpecsContainerColor").innerHTML = this.title;

				// change price
				document.getElementById("ajaxPriceContainer").innerHTML = changePrice(ajaxItems[interKey][colorKey]["price"], ajaxItems[interKey][colorKey]["price_backup"], ajaxItems[interKey][colorKey]["note_sale"]);
	
				// uncheck firerated
				if (document.getElementById("ajaxFirerating")) document.getElementById("ajaxFirerating").checked = false;
				
				// backordered?
				if (document.getElementById("ajaxBackOrdered")) ajaxItems[interKey][colorKey]["backordered"]*1 ? document.getElementById("ajaxBackOrdered").innerHTML = "<div class=\"ajaxBackOrdered\">This product is backordered</div>" : document.getElementById("ajaxBackOrdered").innerHTML = "&nbsp;";
				
				// deselect all colors
				for (j=0; j<links.length; j++) if (links[j].className.match("colorButton")) if (document.getElementById("colorButtonImage" + links[j].rel)) document.getElementById("colorButtonImage" + links[j].rel).style.borderColor = "#f1f4ea";
				
				// select selected color
				if (document.getElementById("colorButtonImage" + this.rel)) document.getElementById("colorButtonImage" + this.rel).style.borderColor = "#ffae00";
			}
		}
		links[i].onmousedown = function()
		{
			mouseDown = 1;

			// find model
			for (i in ajaxItems[interKey])
			{
				if (ajaxItems[interKey][i]["product_color_id"] == this.rel)
				{
					colorKey = i;
					break;
				}
			}
			model = ajaxItems[interKey][colorKey]["model"];
			modelContainer = document.getElementById("smallSpecsContainerModel");

			// if not selected already
			if (model != modelContainer.innerHTML)
			{
				this.style.top = "1px";
				this.style.left = "1px";
			}
		}
		links[i].onmouseup = function()
		{
			// if not selected already
			if (model != modelContainer.innerHTML)
			{
				this.style.top = "0px";
				this.style.left = "0px";
			}
		}
		links[i].onmouseout = function()
		{
			// if mouse was down
			if (mouseDown)
			{
				// if not selected already
				if (model != modelContainer.innerHTML)
				{
					this.style.top = "0px";
					this.style.left = "0px";
				}
				mouseDown = 0;
			}
		}
	}
	
// ---------------- Zoom In ----------------

	if (links[i].id.match("ajaxZoom"))
	{
		links[i].onclick = function()
		{
			zoomer(this.target, 900, 650);
		}
	}
	
// ---------------- Dimensions ----------------

	if (links[i].id.match("ajaxDimensions"))
	{
		links[i].onclick = function()
		{
			zoomer(this.target, 900, 650);
		}
	}
	
// ---------------- Auxiliary ----------------

	if (links[i].id.match("ajaxAuxLink"))
	{
		links[i].onclick = function()
		{
			zoomer(this.target, 900, 650)
		}
	}
	
// ---------------- Add to cart ----------------

	if (links[i].id.match("addToCart"))
	{
		links[i].onclick = function()
		{
			// quantity
			quantity = document.getElementById("inputQty");
			var boxQty = document.getElementById("boxQty") ? document.getElementById("boxQty").value : 0;

			// 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 = itemsQty * boxQty;
				if (!msgOnAir)
				{
					document.getElementById("ajaxMsg").className = "ajaxMsgNone";
					ajaxAjax('model=' + model + '&quantity=' + itemsQty, 'atc-processor.php', 'ajaxMsg');
				}
			}
			else msgShow("ajaxMsg", "ajaxMsgBad", "Enter<br>quantity<br>number");
			return false;
		}
	}
	
// ---------------- View cart ----------------

	if (links[i].id.match("viewCartno"))
	{
		links[i].onclick = function()
		{
			msgShow("ajaxMsg", "ajaxMsgBad", "Cart<br>is<br>empty");
			return false;
		}
	}
	if (links[i].id.match("viewCartyes"))
	{
		links[i].onclick = function()
		{
			window.location = "../cart5.php";
			return false;
		}
	}
}

// select
selects = document.getElementsByTagName('select');
for (i=0; i<selects.length; i++)
{
	
// ---------------- Select Edge Type ----------------

	if (selects[i].id.match("interlockSelector"))
	{
		selects[i].onchange = function()
		{				
			// find model
			for (i=0; i<2; i++) if (ajaxItems[i][colorKey]['interlocking'] == this.value) interKey = i;
			model = ajaxItems[interKey][colorKey]['model'];
			modelContainer = document.getElementById("smallSpecsContainerModel");
			
			// set cookie for ajaxInterlocking
			document.cookie = 'ajaxInterlocking = ' + this.value + '; expires = 0; path = /';
			
			// change detail image
			if (document.getElementById("ajaxDetailImage"))
			{
				var image = document.getElementById("ajaxDetailImage");
				image.src = image.src.replace(/(.*\/)+.*\.jpg/i, "$1" + model + ".jpg");
			}
			
			// change zoom link 1
			/*if (document.getElementById("ajaxZoom1"))
			{
				var zoom = document.getElementById("ajaxZoom1");
				zoom.href = zoom.href.replace(/(.*\/)+(closeups\.php\?model=).+/i, "$1$2" + model);
			}*/
			
			// change zoom link 2
			if (document.getElementById("ajaxZoom2"))
			{
				var zoom = document.getElementById("ajaxZoom2");
				zoom.href = zoom.href.replace(/(.*\/)+(closeups\.php\?model=).+/i, "$1$2" + model);
			}
			
			// change dimensions link
			if (document.getElementById("ajaxDimensions"))
			{
				var dims = document.getElementById("ajaxDimensions");
				dims.href = dims.href.replace(/(.*\/)+(dimensions\.php\?model=).+/i, "$1$2" + model);
			}
			
			// change model in small specs
			modelContainer.innerHTML = model;
			
			// change price
			document.getElementById("ajaxPriceContainer").innerHTML = changePrice(ajaxItems[interKey][colorKey]["price"], ajaxItems[interKey][colorKey]["price_backup"], ajaxItems[interKey][colorKey]["note_sale"]);
			
			// reload specs tab
			ajaxAjax("model=" + model + "&interKey=" + interKey, "specs-processor.php?debug=" + ajaxDebug, "boxSpecificationsContainer");
			
			// uncheck firerated
			if (document.getElementById("ajaxFirerating")) document.getElementById("ajaxFirerating").checked = false;
			
			return false;
		}
	}
}

// inputs
inputs = document.getElementsByTagName('input');
for (i=0; i<inputs.length; i++)
{
		
// ---------------- Add firerating ----------------
		
	if (inputs[i].id.match("ajaxFirerating"))
	{
		inputs[i].onclick = function()
		{
			var newItem = this.checked ? ajaxItems[3][interKey][colorKey] : ajaxItems[interKey][colorKey];
			document.getElementById("smallSpecsContainerModel").innerHTML = newItem['model']; // change model
			document.getElementById("ajaxPriceContainer").innerHTML = changePrice(newItem['price'], newItem['price_backup'], newItem['note_sale']); // change price
		}
	}
}

// reviews button
if (document.getElementById("reviewsButton"))
{
	document.getElementById("reviewsButton").onclick = function()
	{
		tabButton("Reviews");
	}
}

// all functions declared in ajax.js