/* Global vars */
var image_state; // At present the main image can either be 'normal' or 'zoomed'
var current_image;
var current_image_zoomed;

/*************************************
Image state
**************************************/
function changeImageState(state, calling_link)
{
	// Show the loading message
	itemColourImageLoading();	
	
	// Set the image state
	image_state=state;

	// Modify the appearance of the calling link
	calling_link.className="active";

	// Modify the appearance of the other link
	var other_link;
	if (calling_link.id=="zoomed")
	{
		other_link = document.getElementById("full");
	}
	else
	{
		other_link = document.getElementById("zoomed");
	}

	//alert(other_link.id);
	other_link.className="inactive";

	// Update the image
	imageUpdate();
}

/*************************************
Updates the large image according to
the current state
**************************************/
function imageUpdate()
{
	// Change the image according to current state
	var img;
	img = document.getElementById("carpetimg");

	if (image_state=="zoomed")
	{		
		//img.src = "http://burmatexpreview.themajestic.co.uk/images/" + current_image_zoomed;    
		img.src = current_image_zoomed;
	}
	else  
	{		
		//img.src = "http://burmatexpreview.themajestic.co.uk/images/" + current_image;
		img.src = current_image;
	}
}

/*************************************
Sets the current state
**************************************/
function setItemColour( imgfile, imgname, zoomedimage )
{
	// Show the loading message
	itemColourImageLoading();

	// Set the images
	current_image = imgfile;
	current_image_zoomed = zoomedimage;
	// Update the image
	imageUpdate();

	// Change the colour text
	var dv;
	dv = document.getElementById("product_colour_description");
	dv.innerHTML = imgname;

	// Scroll the page
	scrollpage(210,50,50);
}

/*************************************
Fading functions
**************************************/
//change the opacity for different browsers
function changeOpac(opacity, id) 
{
    var object = document.getElementById(id).style;
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";

	// After we've faded the message out, we need to hide it and then return its opacity to 100% for next time
	if (opacity==0)
	{
		document.getElementById(id).style.display = "none";
		changeOpac(100,id);
	}
}

// Called when the image has been loaded. Fades out the loading message
function itemColourImageLoaded()
{    
    var speed = 5; //speed for each frame
    var timer = 0;
	var opacStart = 100; // starting opacity
	var opacEnd = 0; // ending opacity
	var id= "product_loading"; // id of the element we're fading out
	var obj = document.getElementById(id);

	// Only begin the process once we've displayed the message
	if (obj.style.display == "block")
	{
		// Loop through the opacities
		for(i = opacStart; i >= opacEnd; i--) 
		{
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
			timer++;
		}
	}
}

function itemColourImageLoading()
{
	document.getElementById('product_loading').style.display = 'block';
}

/*******************************************************
Function scrolls the page upward to a specified position.
Parameters: ultimate position, decrement, time interval (ms)
********************************************************/
function scrollpage(y,decr,interval)
{
	var newpos;
	var x=0;
	
	/* Calculate the position to move to - the current position minus the decrement */
	newpos = document.documentElement.scrollTop-decr;	

	/* If movement is needed to get to our new position */
	if (newpos!=y)
	{
		/* Get the exact position on the last run */
		if ((newpos-y)<decr)
		{
			window.scroll(x,y);
		}
		else
		{
			window.scroll(x,newpos);
			scrolldelay = setTimeout("scrollpage("+y+","+decr+")",interval); // scrolls every 100 milliseconds
		}
	}
	
}
