var ns4 = (document.layers);
var ie4 = (document.all && !document.getElementById);
var ie5 = (document.all && document.getElementById);
var ns6 = (!document.all && document.getElementById);
var maxRight = 10;
var browser = "";
var currentPos = 10;
var currItem=0;
var prodCategory = "";
var scrollRunning = false;
var countdown;
var loopID = null;
var loopRunning = false;
var loopDelay = 10;
var rotateRunning = true;
var rotateDelay = 10;
var direction = "";
var scrollDelay = 100;

// ROTATES THROUGH THE IMAGES BEFORE A THUMBNAIL IS CLICKED
function rotateProduct(product)
{
	// GET OUR BROWSER VERSION
	getBrowser();
	
	// GET OUR CATEGORY
	prodCategory = product;
	
	// USE ACTIVE X TO CREATE A NEW MSXML OBJECT
	loadXML("/xml/" + product + ".xml");
	
	// START THE TIMER
	rotateDelay = 5;
	//StopTheRotate();
	StartTheRotate(prodCategory, currItem + 1);
}

// STARTS OUR TIMER WHEN CALLED
function StartTheRotate(productType, nextProduct)
{
	// IF CLOCK IS 0 FLIP, OR ELSE SUBTRACT A SECOND AND LOOP
	if (rotateRunning == false)
	{
		
	}
	else if (rotateDelay==0 && rotateRunning == true)    
    {
		currItem = currItem + 1;
		rotateProduct(category, currItem);
    }
    else
    {
        rotateDelay = rotateDelay - 1;
        rotateRunning = true;
        timerID = self.setTimeout("StartTheRotate()", delay);         
    }
}

// MOVES THE DIV CONTAINING THUMBNAILS TO THE LEFT
function moveLeft()
{	
	// STOP THE AUTO ROTATE FROM TAKING PLACE
	//StopTheClock();

	direction = "left";
	
	var maxLeft = ((pieces * -82) + 246);

	getBrowser();
	
	// NETSCAPE 4
	if(browser == "ns4")
	{	
		var picture = document.images;
	}
	// IE 4
	else if(browser == "ie4")
	{
		var picture = document.all.images.style;
	}
	// W3C - IE 5+ AND NETSCAPE/FIREFOX/MOZILLA 6+ AND OPERA
	else if(browser == "w3c")
	{		
		var picture = document.getElementById('images').style;
	}
	
	if (currentPos > maxLeft)
	{
		currentPos = currentPos - 10;
		picture.left = currentPos;
	
		// Set the length of the timer, in seconds
		countdown = 1;
		StopLoop();
		StartLoop();
	}
	else
	{
		StopLoop();
	}
}

// MOVES THE DIV CONTAINING THUMBNAILS TO THE RIGHT
function moveRight()
{
	// STOP THE AUTO ROTATE FROM TAKING PLACE
	//StopTheClock();
		
	direction = "right";
	
	getBrowser();
	
	var maxLeft = (pieces * -82);

	// NETSCAPE 4
	if(browser == "ns4")
	{
		var picture = document.images;
	}
	// IE 4
	else if(browser == "ie4")
	{
		var picture = document.all.images.style;
	}	
	// W3C - IE 5+ AND NETSCAPE/FIREFOX/MOZILLA 6+ AND OPERA
	else if(browser == "w3c")
	{
		var picture = document.getElementById('images').style;
	}

	if (currentPos >= maxLeft && currentPos != 10)
	{
		currentPos = currentPos + 10;
		picture.left = currentPos;
	
		// Set the length of the timer, in seconds
		countdown = 1;
		StopLoop();
		StartLoop();
	}
	else
	{
		StopLoop();
	}
}

//
function StopLoop()
{
    if(loopRunning)
        clearTimeout(loopID)
    loopRunning = false
}

//
function StartLoop()
{
    if (countdown==0)
    {
        StopLoop();

		if(direction == "left")
		{
			moveLeft();
		}
		else if(direction == "right")
		{
			moveRight();
		}
		else
		{
			StopLoop();
		}
    }
    else
    {
        self.status = countdown;
        countdown = countdown - 1;
        loopRunning = true;
        loopID = self.setTimeout("StartLoop()", scrollDelay);
    }
}

// CHANGES THE PREVIEW BACKGROUND BASED ON THE IMAGE CLICKED
function updatePreview(background)
{
	// GET OUR BROSWER VERSION
	getBrowser();
	
	// NETSCAPE 4
	if(browser == "ns4")
	{
		var picture = document.previewer;
	}
	// IE 4
	else if(browser == "ie4")
	{
		var picture = document.all.previewer.style;
	}	
	// W3C - IE 5+ AND NETSCAPE/FIREFOX/MOZILLA 6+ AND OPERA
	else if(browser == "w3c")
	{
		var picture = document.getElementById('previewer').style;
	}
	
	// SET THE NEW BACKGROUND
	picture.background = "url(/images/" + background + ")";
	
	// STOP THE ROTATION!
	rotateRunning = false;
}

// CALLED FOR IE TO START THE ROTATIONS
function rotateIE()
{
	// GET THE MAX AMOUNT OF RECORDS
	upperBounds = xmlObj.childNodes.length;
	
	// CREATE OUR VARIABLE TO STORE OUR DATE
	productFull = "";
	productText = "";

	// IF WE HAVE LOOPED THROUGH OUR RESULTS, RESET NEXT TO 0
	if (currItem == upperBounds)
	{
		currItem = 0;
	}
	
	// GET THE FULL IMAGE AND TEXT FROM THE XML FILE
	productFull = xmlObj.childNodes(currItem).getAttribute("full");
	productText = xmlObj.childNodes(currItem).getAttribute("text");
	
	// IE 4
	if(browser == "ie4")
	{
		var previewer = document.all.previewer.style;
	}
	// W3C - IE 5+ AND NETSCAPE/FIREFOX/MOZILLA 6+ AND OPERA
	else if(browser == "w3c")
	{		
		var previewer = document.getElementById('previewer').style;
	}
	
	// SET THE NEW BACKGROUND
	previewer.background = "url(/images/" + productFull +")";
}

// CALLED FOR MOZILLA TO START THE ROTATIONS
function rotateMoz()
{	
	// CREATE AN OBJECT TO HOLD OUR XML DATA
	xmlObj = xmlDoc.documentElement.childNodes;
	
	// GET THE MAX AMOUNT OF RECORDS
	var upperBounds = xmlObj.length;
	
	// CREATE OUR VARIABLE TO STORE OUR DATE
	productFull = "";
	productText = "";

	// IF WE HAVE LOOPED THROUGH OUR RESULTS, RESET NEXT TO 0
	if (currItem == upperBounds)
	{
		currItem = 0;
	}
	
	// GET THE FULL IMAGE AND TEXT FROM XML FILE
	if (xmlObj[currItem].nodeType == 1)
	{ 
		// element node
		productFull = xmlObj[currItem].getAttribute("full");
		productText = xmlObj[currItem].getAttribute("text");
		var previewer = document.getElementById('previewer').style;
	
		// SET THE NEW BACKGROUND
		previewer.background = "url(/images/" + productFull +")";	
    }
    else
    {

    }
}

// STOP OUR TIMER 
function StopTheRotate()
{
    if(timerRunning)
    {
        clearTimeout(timerID);
    }
    rotateRunning = false;
}

//LOAD OUR XML
function loadXML(xmlFile)
{
	// MAKES CALLS TO OUR XML BASED ON BROWSER TYPE
	if (document.implementation && document.implementation.createDocument)
	{
		// CREATE OUR MOZILLA COMPLIANT XML OBJECT
		xmlDoc = document.implementation.createDocument("", "", null)
		xmlDoc.onload = rotateMoz; 
		xmlDoc.load(xmlFile);
	}
	else if (window.ActiveXObject)
	{
		// CREATE OR IE COMPLIANT XML OBJECT
		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.async = false; 
		xmlDoc.onreadystatechange=verify;
		xmlDoc.load(xmlFile);
		xmlObj=xmlDoc.documentElement;
			
		// CALL CODE FOR IE
		rotateIE();
	}
}

// CALLED TO VERIFY THAT OUR XML FILE HAS LOADED
function verify()
{
  // 0 Object is not initialized
  // 1 Loading object is loading data
  // 2 Loaded object has loaded data
  // 3 Data from object can be worked with
  // 4 Object completely initialized
  if (xmlDoc.readyState != 4)
  {
      return false;
  }
}

// DETERMINES WHAT BROWSER VERSION WE HAVE/STANDARDS COMPLIANCE
function getBrowser()
{
	if(ns4)
	{
		browser = "ns4";
	}
	// Explorer 4
	else if(ie4)
	{
		browser = "ie4";
	}
	// W3C - Explorer 5+ and Netscape 6+
	else if(ie5 || ns6)
	{
		browser = "w3c";
	}
}
