// Carousel 2 (v2.2)

// v2.2 - added homepage tagging

// global variables for use everywhere on this carousel

var leftpos=0;
var timeVar2;
var countme=0;

var autoTimeVar;	// initialise variable used for autoScroll timeout
var paused=true;	// variable to detect if pause scroll must be used
var rewinding=false;	// variable to use to check if carousel is in the process of rewinding


// functions for carousel

function getCarouselData(pathToXML) {	// function to retrieve XML file and set all values for carousel to be printed to page

// Open XML file and get contents
if (window.XMLHttpRequest)	{
	getData=new XMLHttpRequest();
	}
else {
	getData=new ActiveXObject("Microsoft.XMLHTTP");
	}
getData.open("GET",pathToXML,false);
getData.send(null);
crslData=getData.responseXML;

// get values into variables
cImgPath=crslData.getElementsByTagName("carousel")[0].getAttribute("imagepath"); // relative to the SupplyImages folder
cAutoScrollFlag=crslData.getElementsByTagName("carousel")[0].getAttribute("auto"); // flag for switching on or off autoscroll
cElements=crslData.getElementsByTagName("element");	// get all elements of carousel
cImages=crslData.getElementsByTagName("imagename");	// get all images of all elements of carousel
cItemName=crslData.getElementsByTagName("name");	// get all item names of all elements of carousel
cPrice=crslData.getElementsByTagName("price");		// get all prices of all elements of the carousel

// use contents to set size of ribbon
if ((crslData.getElementsByTagName("carousel")[0].getAttribute("type"))=="home") {
	cElmWidth=218;	// sets up carousel for homepage (i.e. no text, 4 elements)
	}
else if ((crslData.getElementsByTagName("carousel")[0].getAttribute("type"))=="department") {
	cElmWidth=172;	// sets up carousel for department page (i.e. with text, 5 elements)
	}	
ribbonLength=(cElements.length*cElmWidth)+(cElements.length*12);
windowLength=ribbonLength-920;		// used in moveright extreme calculations
}



function printCarousel() {	// function to write the carousel to the page for use
	
// modify markup for carousel
document.getElementById("ribbon").style.width=ribbonLength+"px";	// adjust size of ribbon to accomodate all elements

	
// write carousel to page
for (i=0;i<cElements.length;i++) {
	if ((crslData.getElementsByTagName('carousel')[0].getAttribute('type'))=="home") {
		var formatTags=cItemName[i].childNodes[0].nodeValue.replace(/\s/gi, "-");
		document.write("<a href=\""+cElements[i].getAttribute('link')+"\" manual_cm_re=\"1-_-SecondCarousel-_-Position"+(i+1)+"-"+formatTags+"\">");
		document.write("<img class=\"carouselElement\" src=\"SupplyImages/"+cImgPath+"/"+cImages[i].childNodes[0].nodeValue+".jpg\" alt=\""+cImages[i].getAttribute('altname')+"\" width=\""+cElmWidth+"px\" border=\"0\" />");
		document.write("</a>");
	}
	else if ((crslData.getElementsByTagName('carousel')[0].getAttribute('type'))=="department") {
		document.write("<div class=\"carouselElement\">");
		document.write("<a href=\""+cElements[i].getAttribute('link')+"\">");
		document.write("<img class=\"deptCarouselElement\" src=\"SupplyImages/"+cImgPath+"/"+cImages[i].childNodes[0].nodeValue+".jpg\" alt=\""+cImages[i].getAttribute('altname')+"\" width=\""+cElmWidth+"px\" border=\"0\" />");
		document.write("</a><br />");
		document.write("<span class=\"crsl_prod_name\"><a href=\""+cElements[i].getAttribute('link')+"\">"+cItemName[i].childNodes[0].nodeValue+"</a></span><br />");
		if ((cPrice[i].childNodes[0].nodeValue != "0") && (cPrice[i].childNodes[0].nodeValue != "0.00") && (cPrice[i].childNodes[0].nodeValue != "n/a")) {
			if (cPrice[i].getAttribute('range') == "yes") {
				document.write("<span class=\"from_txt\">from</span> ");
			}
			
			document.write("&pound;"+cPrice[i].childNodes[0].nodeValue+"</div>");
		}
		else {
			document.write("</div>");
		}
	}
}
document.getElementById("left_button").src="SupplyImages/redesign/homepage/leftArrowEnd.gif";
correctors();	// run correctors function to fix cross browser display issues
}




// Carousel Control (interaction) functions

var theRightBttn=document.getElementById("right_button");
var theLeftBttn=document.getElementById("left_button");

var theRibbon=document.getElementById("ribbon");

// move left UI function

function moveleft()	{
	
var theRightBttn=document.getElementById("right_button");
var theLeftBttn=document.getElementById("left_button");

var theRibbon=document.getElementById("ribbon");

if (leftpos==0)	{
	clearTimeout(timeVar2);
	theLeftBttn.src="SupplyImages/redesign/homepage/leftArrowEnd.gif";
	countme=0;
	}
else {
	if	(leftpos<windowLength)	{
		theRightBttn.src="SupplyImages/redesign/homepage/rightArrow.gif";
		}
	leftpos=leftpos-40;
	if (leftpos<0) {
		leftpos=0;	
	}
	theRibbon.style.left=-(leftpos)+"px";
	countme=countme+40;
	if (countme>=920)
	{
		stopmove();
		countme=0;
	}
	else
	{
	timeVar2=setTimeout("moveleft()",10);
	}
	}
}


// move right UI function

function moveright()	{

var theRightBttn=document.getElementById("right_button");
var theLeftBttn=document.getElementById("left_button");

var theRibbon=document.getElementById("ribbon");

if (leftpos>=windowLength)	{
	clearTimeout(timeVar2);
	theRightBttn.src="SupplyImages/redesign/homepage/rightArrowEnd.gif";
	countme=0;
	}
else {
	if	(leftpos>1)	{
		theLeftBttn.src="SupplyImages/redesign/homepage/leftArrow.gif";
		}
	leftpos=leftpos+40;
	if (leftpos>windowLength) {
		leftpos=windowLength;
	}
	theRibbon.style.left=-(leftpos)+"px";
	countme=countme+40;
	if (countme>=920)
	{
		stopmove();
		countme=0;
	}
	else
	{
	timeVar2=setTimeout("moveright()",10);
	}
	
	}
}


// function to stop moving ribbon

function stopmove()	{

var theRightBttn=document.getElementById("right_button");
var theLeftBttn=document.getElementById("left_button");

clearTimeout(timeVar2);
if (leftpos>=windowLength) {
	theRightBttn.src="SupplyImages/redesign/homepage/rightArrowEnd.gif";
}
if (leftpos<=1) {
	theLeftBttn.src="SupplyImages/redesign/homepage/leftArrowEnd.gif";
}
}

// Auto scroll functions


function stopAutoScroll() {
	if (cAutoScrollFlag == "on")
	{
		clearTimeout(autoTimeVar);
		clearTimeout(timeVar2);
		paused=false;
	}
	else
	{
		return null;
	}
}

function pauseAutoScroll() {
	if (cAutoScrollFlag == "on")
	{
		if (paused==false) {
			return null;
		}
		else {
		clearTimeout(autoTimeVar);
		clearTimeout(timeVar2);
		}
	}
	else
	{
		return null;
	}
}

function resumeAutoScroll() {
if (cAutoScrollFlag == "on")
{
	if (paused==true) {
		if (rewinding==true) {
			timeVar2=setTimeout("rewind()",2500);
		}
		else {
			autoTimeVar=setTimeout("autoScroll()",2500);
		}
	}
	else {
		return null;
	}
}
else
{
	return null;
}
}

function rewind() {

var theRightBttn=document.getElementById("right_button");
var theLeftBttn=document.getElementById("left_button");
var theRibbon=document.getElementById("ribbon");
	
if (leftpos==0)	{
	clearTimeout(timeVar2);
	theLeftBttn.src="SupplyImages/redesign/homepage/leftArrowEnd.gif";
	theRightBttn.src="SupplyImages/redesign/homepage/rightArrow.gif";
	countme=0;
	autoTimeVar=setTimeout("autoScroll()",10);
	rewinding=false;
	}
else {
	leftpos=leftpos-5;
	if (leftpos<0) {
		leftpos=0;	
		}
	theRibbon.style.left=-(leftpos)+"px";
	countme=countme+5;
	if (countme>=windowLength) {
		stopmove();
		countme=0;
		}
	rewinding=true;
	timeVar2=setTimeout("rewind()",10);	
	}
	
}


function autoScroll() {
if (cAutoScrollFlag == "on")
{
	if (leftpos==0) {
		leftpos=leftpos+1;
		autoTimeVar=setTimeout("autoScroll()",5000);	
	}
	else if (leftpos<windowLength){
	if (leftpos==1) {
		leftpos=0;
	}
	moveright();
	autoTimeVar=setTimeout("autoScroll()",5000);
	}
	else {
		clearTimeout(autoTimeVar);
		clearTimeout(timeVar2);
		rewind();
	}
}
else
{
	return null;
}
}


// detectors for IE6 and IE7

function correctors()	{
var brwsN=navigator.appName;
var brwsV=navigator.appVersion;
var brwsCheck=brwsV.indexOf("MSIE 6.0");
var brwsCheck2=brwsV.indexOf("MSIE 7.0");


if (brwsCheck>(-1) || brwsCheck2>(-1))	{
	document.getElementById('ribbonContainer').style.position="absolute";
	}

}

