//****************************************************************************************
// JS Lib Functions
//****************************************************************************************
function $$(element) {
  return document.getElementById(element);
}


function opacity(id, opacStart, opacEnd, millisec) {
	//speed for each frame
	var speed = Math.round(millisec / 100);
	var timer = 0;

	//determine the direction for the blending, if start and end are the same nothing happens
	if(opacStart > opacEnd) {
		for(i = opacStart; i >= opacEnd; i--) {
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
			timer++;
		}
	} else if(opacStart < opacEnd) {
		for(i = opacStart; i <= opacEnd; i++)
			{
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
			timer++;
		}
	}
}

//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 + ")";
	if (opacity == 0) $$(id).style.display = "none";
	else $$(id).style.display = "block";
}

function shiftOpacity(id, millisec) {
	//if an element is invisible, make it visible, else make it ivisible
	if(document.getElementById(id).style.opacity == 0) {
		opacity(id, 0, 100, millisec);
	} else {
		opacity(id, 100, 0, millisec);
	}
}

function shiftDisplay(id){
	if ($$(id).style.display == "none") $$(id).style.display = "block";	
	else $$(id).style.display = "none";	
}

function blendimage(divid, imageid, imagefile, millisec) {
    var speed = Math.round(millisec / 100);
    var timer = 0;
    
    //set the current image as background
    document.getElementById(divid).style.backgroundImage = "url(" + document.getElementById(imageid).src + ")";
    
    //make image transparent
    changeOpac(0, imageid);
    
    //make new image
    document.getElementById(imageid).src = imagefile;

    //fade in image
    for(i = 0; i <= 100; i++) {
        setTimeout("changeOpac(" + i + ",'" + imageid + "')",(timer * speed));
        timer++;
    }
}

function cycleOpacity(div, imgDiv, imageArray, paramsObj, loopCount){
	if (paramsObj.curLoop == paramsObj.loop){
		// Done loop
		document.getElementById("debug").innerHTML += "DONE!<br/>";
		return;
	}
	
	if (!paramsObj || loopCount == paramsObj.loop) return;
	if (!paramsObj.delay) paramsObj.delay = 2000;
	if (!paramsObj.curIndex) paramsObj.curIndex = 0;
	if (!paramsObj.curLoop) paramsObj.curLoop = 0;
	var aryString = "[";
	for (var i=0; i<imageArray.length; i++){
		aryString += "'" + imageArray[i] + "'";
		if (i+1 != imageArray.length) aryString += ","
	}
	aryString += "]";
	try{
		//document.getElementById("debug").innerHTML += document.getElementById(div).style.backgroundImage + "<br/>";
		//document.getElementById("debug").innerHTML += imageArray[paramsObj.curIndex];
		//document.getElementById("debug").innerHTML += "<br/><br/>";
	}catch(e){}
	
	blendimage(div, imgDiv, imageArray[paramsObj.curIndex], paramsObj.duration, paramsObj.delay);
	
	paramsObj.curIndex = paramsObj.curIndex + 1;
	if (paramsObj.curIndex == imageArray.length) {
		paramsObj.curIndex = 0;
		paramsObj.curLoop = paramsObj.curLoop + 1;
	}
	var pObjString = "{";
	if (paramsObj.curIndex) pObjString += "'curIndex':" + paramsObj.curIndex + ", ";
	pObjString += "'duration':" + paramsObj.duration + ",";
	pObjString += "'loop':" + paramsObj.loop + ",";
	pObjString += "'curLoop':" + paramsObj.curLoop + ",";
	if (paramsObj.delay) pObjString += "'delay':" + paramsObj.delay;
	pObjString += "}";
	setTimeout("cycleOpacity('" + div + "','" + imgDiv + "'," + aryString + "," + pObjString + ")", paramsObj.delay + paramsObj.duration);
}

/* Custom */

function showProdDesc(index){
	$$("prodDetail").style.backgroundImage = "url(" + prodData[index].img + ")";
	var html = "";
	//if (prodData[index].weight) html = "<p><br/>(" + prodData[index].weight + ") ";
	html += "<strong>" + prodData[index].title+ "</strong></p><p>" + prodData[index].desc + "</p>";
	$$("prodDesc").innerHTML = html;
}

