// JavaScript Document

var c=0;
var t;
var timer_is_on=0;
var curImg = 0;
var k=0;

function rotateImgClick(){
	if (!document.getElementsByTagName) return false;
	if (!document.getElementById) return false;
	if (!document.getElementById("mainimg")) return false;	
	if (!document.getElementById("thumb")) return false;	

	document.getElementById("thumb").style.display="block";
	
	var thumbnails = document.getElementById("thumb").getElementsByTagName("a");
	var furniture = document.getElementById("mainimg"); 

	for (var i=0; i< thumbnails.length; i++){
		thumbnails[i].onclick = function () {
			var newImgSrc = this.getAttribute("href");
			var newImgAlt = this.getAttribute("title");
			updateAllImg();
			this.firstChild.setAttribute("src",updateImgName(this.firstChild.getAttribute("src")));
			setMainImage(newImgSrc,newImgAlt);
			
			return false;
		}
		thumbnails[i].onmouseover =function() {
			//stopCount();
		}
	
	}
	furniture.onmouseover =function() {
		//stopCount();
	}
	furniture.onmouseout =function() {
		//u=setTimeout("doTimer()",4000);
	}

	
	return false;
}
function setMainImage(src,alt){
	var furniture = document.getElementById("mainimg"); 
	furniture.setAttribute("src", src);
	furniture.setAttribute("alt", alt);
}
function updateAllImg(){
	var imgThumb = document.getElementById("thumb").getElementsByTagName("img");
	for (var i=0; i< imgThumb.length; i++){
		if(imgThumb[i].getAttribute("src").search("cur-") > 0){
			imgThumb[i].setAttribute("src", imgThumb[i].getAttribute("src").replace("cur-","sm-"));
		}
	}
	return false;
}

function updateImgName(img){
	var newimg;
	if(img.search("sm-") > 0){
		newimg = img.replace("sm-","cur-");	
		return newimg;
	}
	else {
		return false;
	}
	
}



function timedCount()
{
	nextImg();
	t=setTimeout("timedCount()",4000);
}

function doTimer()
{
if (!timer_is_on)
  {
  	timer_is_on=1;
  	timedCount();
  }
}

function stopCount()
{
	clearTimeout(t);
	timer_is_on=0;
}

function nextImg(){
	var allthumbs = document.getElementById("thumb").getElementsByTagName("a");
	var newImgSrc = allthumbs[curImg].getAttribute("href");
	var newImgAlt = allthumbs[curImg].getAttribute("title");
	
	updateAllImg();
	allthumbs[curImg].firstChild.setAttribute("src",updateImgName(allthumbs[curImg].firstChild.getAttribute("src")));
	setMainImage(newImgSrc,newImgAlt);
	
	if(curImg < allthumbs.length-1) {
		curImg++;	
	}
	else {curImg=0;}

	
}

window.onload=function(){
	rotateImgClick();
	//doTimer();
}
