var autoScrollEvent;
var itemBackground = "itemTable_2";
var itemForeground = "itemTable_1";
var noOfItems = 0;

function countItems(){
	itemList = document.getElementsByTagName(document.getElementById("itemHeader_1").tagName);
	for(i=0;i<itemList.length;i++){
		if(itemList[i].id.indexOf("itemHeader") > -1){
			noOfItems = noOfItems+1;
		}
	}	
}

function bringToFront(topLayerID,drop){
	
	
	var prefix = topLayerID.split("_")[0];	
	topLayer = document.getElementById(topLayerID)
	var layerList=document.getElementsByTagName(topLayer.tagName);
	for(i=0;i<layerList.length;i++){
		if(layerList[i].id.indexOf(prefix) > -1){
			layerList[i].style.zIndex = layerList.length-i;
			if(drop){
				layerList[i].style.display="none";
			}
		}
	}
	
	
	topLayer.style.opacity = 0.0;
	topLayer.style.filter = "alpha(opacity=0)";
	topLayer.style.display="block";
	if(prefix.indexOf("button") > -1){
		topLayer.style.zIndex = 1002;		
	} else {
		if(prefix.indexOf("header") > -1){
			topLayer.style.zIndex = 1001;		
		} else {
			topLayer.style.zIndex = 1000;
		}
	}
	
}


function fade(direction,elementID,opacity,target){
	
	fadeElement = document.getElementById(elementID);
	if (direction == "in") {
		opacity = opacity +10;
		fadeElement.style.display = "block";								
		fadeElement.style.opacity = opacity / 100;
		fadeElement.style.filter = "alpha(opacity=" + (opacity) + ")";
		if(opacity < target){
				t = setTimeout('fade(\"'+direction+'\",\"'+elementID+'\",'+opacity+','+target+')', 60);
		}
	}
	if (direction == "out") {
		opacity = opacity -10;
		fadeElement.style.opacity = opacity / 100;
		fadeElement.style.filter = "alpha(opacity=" + (opacity) + ")";
		if(opacity > target){
				t = setTimeout('fade(\"'+direction+'\",\"'+elementID+'\",'+opacity+','+target+')', 60);
		} else {
			fadeElement.style.display = "none";
		}
	}	
}

function enable(itemIndex,autoScroll){
	
	if(!autoScroll){clearTimeout(autoScrollEvent);}
	layerIndex = itemBackground.split("_")[1];
	document.getElementById("itemImage_"+layerIndex).src = document.getElementById("thumbImage_"+itemIndex).src;
	document.getElementById("itemLink_"+layerIndex).href = document.getElementById("hiddenLink_"+itemIndex).href;
	bringToFront(itemBackground,0);
	bringToFront("itemHeader_"+itemIndex,1);
	fade("in",itemBackground,0,100);
	header = document.getElementById("itemHeader_"+itemIndex)
	header.style.opacity = 1.0;
	header.style.filter = "alpha(opacity=100";
	if(itemBackground == "itemTable_1"){
		itemBackground = "itemTable_2";
	}else{
		itemBackground = "itemTable_1";
	}
	
	if(parseInt(itemIndex)== parseInt(noOfItems)){
		autoScrollEvent = setTimeout('enable(1,1)',3000);
	} else {
		autoScrollEvent = setTimeout('enable('+(itemIndex+1)+',1)',3000);
	}
}

