/******************************************
Name:			product.js
Date:			29/05/2008
Author:			Andrei Vais
Description:	Javascript file to functionality to the product page
******************************************/

/****
Used to swap the sample preview image with a bigger version of the clicked thumbnail.
****/

//functions calls
addLoadEvent(prepareProductGallery);

function prepareProductGallery()
{
	if (!document.getElementsByTagName) return false;
	if (!document.getElementById) return false;
	if (!document.getElementById("ProductAvailableSamples")) return false;

	var gallery = document.getElementById("ProductAvailableSamples");
	var galleryElements = gallery.getElementsByTagName("img");
	
	document.getElementById("ProductSample_FullCarpet").onclick = function() 
	{
		return swapImage("ProductSample", document.getElementById("ProductSample_FullCarpet").getAttribute("href"));
	}
	document.getElementById("ProductSample_CloseUp").onclick = function() 
	{
		return swapImage("ProductSample", document.getElementById("ProductSample_CloseUp").getAttribute("href"));
	}

	//alert('gallery: ' + galleryElements.length);
	
	for (var i=0; i < galleryElements.length; i++)
	{
		galleryElements[i].onclick = function()
		{
		  return showPic(this, "ProductSample");
		}
	}
}

function showPic(whichpic, placeholder)
{
	if (!document.getElementById(placeholder)) return true;
	
	var FullCarpetPath = document.getElementById("ProductSample_FullCarpet").getAttribute("href");	
	var CloseUpPath = document.getElementById("ProductSample_CloseUp").getAttribute("href");
	var OldSamplePath = document.getElementById("ProductSample").firstChild.getAttribute("src");
	var NewSample = whichpic.getAttribute("src");

	FullCarpetPath= FullCarpetPath.substring(0, FullCarpetPath.lastIndexOf("/"));
	CloseUpPath= CloseUpPath.substring(0, CloseUpPath.lastIndexOf("/"));
	OldSamplePath = OldSamplePath.substring(0,OldSamplePath.lastIndexOf("/"));
	NewSample = NewSample.substring(NewSample.lastIndexOf("/"), NewSample.length);
	
	document.getElementById("ProductSample_ColourName").firstChild.nodeValue = whichpic.getAttribute("alt");
	if (OldSamplePath.indexOf("/samples_closeup_images") != -1)
	{
		document.getElementById("ProductSample").firstChild.setAttribute("src", OldSamplePath + NewSample.replace('T_', 'C_'));
	}
	else if (OldSamplePath.indexOf("/samples_images") != -1)
	{
		document.getElementById("ProductSample").firstChild.setAttribute("src", OldSamplePath + NewSample.replace('T_', 'I_'));
	}
	else
	{
		alert('An error occurred while trying to display the selected image.');
	}
	
	document.getElementById("ProductSample").firstChild.setAttribute("alt", whichpic.getAttribute("alt"));
	
	document.getElementById("ProductSample_FullCarpet").setAttribute("href", FullCarpetPath + NewSample.replace('T_', 'I_'));	
	document.getElementById("ProductSample_FullCarpet").onclick = function() 
	{
		return swapImage("ProductSample", FullCarpetPath + NewSample.replace('T_', 'I_'));
	}

	document.getElementById("ProductSample_CloseUp").setAttribute("href", CloseUpPath + NewSample.replace('T_', 'C_'));	
	document.getElementById("ProductSample_CloseUp").onclick = function() 
	{
		return swapImage("ProductSample", CloseUpPath + NewSample.replace('T_', 'C_'));
	}

	return false;
}

function swapImage(whichpic, newimagepath)
{
	document.getElementById("ProductSample").firstChild.setAttribute("src", newimagepath);
	
	for (var i=0; i < document.getElementById("ProductSample").getElementsByTagName("a").length; i++)
	{
		if (document.getElementById("ProductSample").getElementsByTagName("a")[i].className == "Selected")
		{
			document.getElementById("ProductSample").getElementsByTagName("a")[i].className = "Unselected";
		}
		else
		{
			document.getElementById("ProductSample").getElementsByTagName("a")[i].className =  "Selected";
		}
	}
	
	return false;
}