/**
 * @author ponnamykiep
 */


function absoluteXfade(aNodeStrings){
	dojox.fx.crossFade({
		nodes: aNodeStrings,
		duration:1000
	}).play();
};

function doCrossFade(aNodeStrings){
	dojox.fx.crossFade({
		nodes: aNodeStrings,
		duration:1000
	}).play();
}

function crossFade(aNodeStrings, duration, loop, firstIndex, secondIndex){
	var strArray = "";
	if (duration == undefined) duration = 7;
	if (loop == undefined) loop = true;
	for (var i in aNodeStrings){
		if (i == aNodeStrings.length-1) strArray += "'" + aNodeStrings[i] + "'";
		else strArray += "'" + aNodeStrings[i] + "',";
		if (firstIndex != undefined) dojo.byId(aNodeStrings[i]).style.visibility = "visible";
		/*
		if (i != firstIndex && i != secondIndex && firstIndex != undefined && secondIndex != undefined){
			dojo.byId(aNodeStrings[i]).style.visibility = "hidden";
		}else{
			dojo.byId(aNodeStrings[i]).style.visibility = "visible";
		dojo.byId("debug").innerHTML = firstIndex + ":" + i;
		}
		*/
	}
	if (!firstIndex && !secondIndex) {
		firstIndex = 0;
		secondIndex = 1;
		var s = "crossFade([" + strArray + "]," + duration + "," + loop + "," + firstIndex + "," + secondIndex + ")";
		setTimeout("crossFade([" + strArray + "]," + duration + "," + loop + "," + firstIndex + "," + secondIndex + ")", duration);
		//eval(s);
		return;
	}
	if (secondIndex > aNodeStrings.length-1) {
		secondIndex = 0;
		if (!loop) return;
	}
	doCrossFade([aNodeStrings[firstIndex], aNodeStrings[secondIndex]]);
	firstIndex = secondIndex;
	secondIndex++;
	
	setTimeout("crossFade([" + strArray + "]," + duration + "," + loop + "," + firstIndex + "," + secondIndex + ")", duration);	
};

function stopLoop(){ _anim.stop(); }

function getHTML(url, targetDiv) { // 
	dojo.xhrGet( { // 
	// The following URL must match that used to test the server.
	url: url, 
	handleAs: "text",
	
	timeout: 5000, // Time in milliseconds
	
	// The LOAD function will be called on a successful response.
	load: function(response, ioArgs) { // 
	  targetDiv.innerHTML = response; // 
	  return response; // 
	},
	
	// The ERROR function will be called in an error case.
	error: function(response, ioArgs) { // 
	  console.error("HTTP status code: ", ioArgs.xhr.status); // 
	  return response; // 
	  }
	});
}

function getQueryStringVal(arg, url) {
    var URL = (url) ? url : document.location.href;
    var qs = URL.substring( URL.indexOf("?"), URL.length );
    var theArg = new String ("?" + arg + "=" );
    var retVal = "";

    if (qs.indexOf(theArg) >= 0 ) {
        var equalIndex = qs.indexOf("=");
        if (qs.indexOf("&") >= 0 )
          retVal = qs.substring( equalIndex+1 , qs.indexOf("&"));
        else
          retVal = qs.substring( equalIndex+1, qs.length );
    } else {
        theArg = new String("&" + arg + "=" );
        var argIndex = qs.indexOf(theArg);
        if ( argIndex  >= 0 ) {
            retVal = qs.substring( argIndex + theArg.length, qs.length);
            var ampIndex = retVal.indexOf("&");
            if ( ampIndex >= 0 ) retVal = retVal.substring( 0, ampIndex );
        }
    }
    return retVal;
}

function hideLargePic(id){
	$(id).style.display = "none";
}

function viewLargePic(id, img, top, left){
	$(id).style.left = left + "px";
	$(id).style.top = top + "px";
	$(id).firstChild.src = img;
	$(id).style.display = "block";
}





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);
}

