// fade in rates are in %increase/Second and will be effectedat intervals of updateInterval which is in milliseconds
fadeInUpdateInterval = 50;
fadeInRate = .5;
fadeInDuration = 1000;
otherTextFullShade = 0xC0;

// fading from 0 to 100 over 1000
fadeDelta = fadeInRate * 100 * fadeInUpdateInterval / fadeInDuration;
// for to fade in of lie text
fadeTextDelta = fadeInRate * 256 * fadeInUpdateInterval / fadeInDuration;
// for fade out of other text while lie fades in text
fadeOtherThanLieTextDelta = fadeInRate * otherTextFullShade * fadeInUpdateInterval / fadeInDuration;


var lightTimerID = 0;
var thoughtTimerID = 0;
var expressionTimerID = 0;
var understandTimerID = 0;
var treeTimerID = 0;
var lieTimerID = 0;

// get opacity from an element with the given id, opacity is in a range from 0 to 100
function setOpacityForId(id, opacity)
{
	ele = document.getElementById(id);
	ele.style.opacity=opacity/100;
	if (ele.filters != null) ele.filters.alpha.opacity=opacity;
}

function lightEffectFadeStart(fadeDir)
{
	if (lightTimerID != 0) window.clearInterval(lightTimerID);
	lightTimerID = window.setInterval("lightFade(" + fadeDir + ")", fadeInUpdateInterval);
}

function lightFade(direction)
{
	// fade the image
	var newOpacity;
	newOpacity = Number(document.getElementById("light_img").style.opacity) * 100;	
	newOpacity += direction*fadeDelta;
	if ((newOpacity >= 100) && (direction == 1)) {
		window.clearInterval(lightTimerID);
		lightTimerID = 0;
		newOpacity = 100;
	} else if ((newOpacity <= 0) && (direction == -1)) {
		window.clearInterval(lightTimerID);
		lightTimerID = 0;
		newOpacity = 0;
	}	

	setOpacityForId("light_img", newOpacity);
}

function thoughtEffectsFadeStart(fadeDir)
{
	if (thoughtTimerID != 0) window.clearInterval(thoughtTimerID);
	thoughtTimerID = window.setInterval("thoughtFade(" + fadeDir + ")", fadeInUpdateInterval);
}

function thoughtFade(direction)
{
	var newOpacity;
	newOpacity = Number(document.getElementById("thoughts_img").style.opacity) * 100;	
	newOpacity += direction * fadeDelta;
	if ((newOpacity >= 100) && (direction == 1)) {
		window.clearInterval(thoughtTimerID);
		thoughtTimerID = 0;
		newOpacity = 100;
	} else if ((newOpacity <= 0) && (direction == -1)) {
		window.clearInterval(thoughtTimerID);
		thoughtTimerID = 0;
		newOpacity = 0;
	}	
	
	setOpacityForId("thoughts_img", newOpacity);
}

function understandEffectsFadeStart(fadeDir)
{
	if (understandTimerID != 0) window.clearInterval(understandTimerID);
	understandTimerID = window.setInterval("understandFade(" + fadeDir + ")", fadeInUpdateInterval);
}

function understandFade(direction)
{
	var newOpacity;
	newOpacity = Number(document.getElementById("understand_img").style.opacity) * 100;	
	newOpacity += direction * fadeDelta;
	if ((newOpacity >= 100) && (direction == 1)) {
		window.clearInterval(understandTimerID);
		understandTimerID = 0;
		newOpacity = 100;
	} else if ((newOpacity <= 0) && (direction == -1)) {
		window.clearInterval(understandTimerID);
		understandTimerID = 0;
		newOpacity = 0;
	}	
	
	setOpacityForId("understand_img", newOpacity);
}

function treeTalkEffectsFadeStart(fadeDir)
{
	if (treeTimerID != 0) window.clearInterval(treeTimerID);
	treeTimerID = window.setInterval("treeTalkFade(" + fadeDir + ")", fadeInUpdateInterval);
}

function treeTalkFade(direction)
{
	var newOpacity;
	newOpacity = Number(document.getElementById("treeTalk_img").style.opacity) * 100;	
	newOpacity += direction * fadeDelta;
	if ((newOpacity >= 100) && (direction == 1)) {
		window.clearInterval(treeTimerID);
		treeTimerID = 0;
		newOpacity = 100;
	} else if ((newOpacity <= 0) && (direction == -1)) {
		window.clearInterval(treeTimerID);
		treeTimerID = 0;
		newOpacity = 0;
	}	
	
	setOpacityForId("treeTalk_img", newOpacity);
}

function expressionEffectFadeStart(fadeDir)
{
	if (expressionTimerID != 0) window.clearInterval(expressionTimerID);
	expressionTimerID = window.setInterval("expressionFade(" + fadeDir + ")", fadeInUpdateInterval);
}

function expressionFade(direction)
{
	// fade the image
	var newOpacity;
	newOpacity = Number(document.getElementById("expression_img").style.opacity) * 100;	
	newOpacity += direction * fadeDelta;
	if ((newOpacity >= 100) && (direction == 1)) {
		window.clearInterval(expressionTimerID);
		expressionTimerID = 0;
		newOpacity = 100;
	} else if ((newOpacity <= 0) && (direction == -1)) {
		window.clearInterval(expressionTimerID);
		expressionTimerID = 0;
		newOpacity = 0;
	}	
	
	setOpacityForId("expression_img", newOpacity);
}


function lieEffectsFadeStart(fadeDir)
{
	if (lieTimerID != 0) window.clearInterval(lieTimerID);
	if (fadeDir == 1) // fade in the background white
		lieTimerID = window.setInterval("lieFadeInBackground()", fadeInUpdateInterval);
	else
		lieTimerID = window.setInterval("lieFadeOut()", fadeInUpdateInterval);
}

function lieFadeInBackground()
{
	currentLieCellShade = getGrayValueFromColor(document.getElementById("lie_cell").bgColor);
	currentLieCellShade += fadeTextDelta;
			
	if (currentLieCellShade >= 255) {
		window.clearInterval(lieTimerID);
		lieTimerID = window.setInterval("lieFadeInText()", fadeInUpdateInterval);;
		currentLieCellShade = 255;
	}	
	
	document.getElementById("lie_text").style.color = createFontColorForGrayValue(currentLieCellShade); 
	document.getElementById("lie_cell").bgColor = createBackgroundColorForGrayValue(currentLieCellShade); 
	
} // lieFadeInBackground

// fade in the lie text and fade out all other text
function lieFadeInText() 
{
	currentLieCellShade = getGrayValueFromColor(document.getElementById("lie_cell").bgColor);
	currentLieCellShade += -1 * fadeTextDelta;
	currentLieTextShade = getGrayValueFromColor(document.getElementById("lie_text").style.color);
	currentLieTextShade += fadeTextDelta;
	// use thoughts for all shades
	currentOtherTextShade = getGrayValueFromColor(document.getElementById("thoughts_text").style.color);
	currentOtherTextShade += -1 * fadeOtherThanLieTextDelta;


	if (currentLieTextShade >= 255) { // fade the text all the way in
		currentLieTextShade = 255;		
	}	
			
	if (currentLieCellShade <= 0) { // fade out the background
		currentLieCellShade = 0;
	}	
	
	if (currentOtherTextShade <= 0) { // fade out the background
		currentOtherTextShade = 0;
	}	

	if ((currentLieCellShade == 0) && 
	    (currentLieTextShade == 255) && 
	    (currentOtherTextShade == 0)) {
		window.clearInterval(lieTimerID);	
		lieTimerID = 0;
	}

	document.getElementById("expression_text").style.color = 
		document.getElementById("light_text").style.color = 
		document.getElementById("treeTalk_text").style.color = 
		document.getElementById("understand_text").style.color = 
		document.getElementById("thoughts_text").style.color = 
		createFontColorForGrayValue(currentOtherTextShade);
	
	document.getElementById("lie_text").style.color = createFontColorForGrayValue(currentLieTextShade);
	document.getElementById("lie_cell").bgColor = createBackgroundColorForGrayValue(currentLieCellShade);
} // lieFadeInText

// fade the background and the text to black and fade all other text back to original
function lieFadeOut()
{
	currentLieCellShade = getGrayValueFromColor(document.getElementById("lie_cell").bgColor);
	currentLieCellShade += -1 * fadeTextDelta;
	currentLieTextShade = getGrayValueFromColor(document.getElementById("lie_text").style.color);
	currentLieTextShade += -1 * fadeTextDelta;
	// use thoughts for all other colors
	currentOtherTextShade = getGrayValueFromColor(document.getElementById("thoughts_text").style.color);
	currentOtherTextShade += fadeOtherThanLieTextDelta;
		
	if (currentLieCellShade <= 0) {
		currentLieCellShade = 0;
	} 
	
	if (currentLieTextShade <= 0) {
		currentLieTextShade = 0;
	}	
			
	if (currentOtherTextShade >= otherTextFullShade) { // fade out the background
		currentOtherTextShade = otherTextFullShade;
	}				
			
	if ((currentLieCellShade == 0) && 
	    (currentLieTextShade == 0) &&
	    (currentOtherTextShade == otherTextFullShade)) {
		window.clearInterval(lieTimerID);	
		lieTimerID = 0;
	}

	document.getElementById("expression_text").style.color = 
		document.getElementById("light_text").style.color = 
		document.getElementById("treeTalk_text").style.color = 
		document.getElementById("understand_text").style.color = 
		document.getElementById("thoughts_text").style.color =
		createFontColorForGrayValue(currentOtherTextShade);

	document.getElementById("lie_text").style.color = createFontColorForGrayValue(currentLieTextShade);
	document.getElementById("lie_cell").bgColor = createBackgroundColorForGrayValue(currentLieCellShade); 
} // lieFadeOut

// if this is set to false then hex format for will be used,... otherwise rgb will be used
REGEXP_RGB_GRAYVALUE = /rgb\((\d+),/ ;

function getGrayValueFromColor(color)
{
	if (color.length == 0) return 0;
	
	USE_RGB_COLORS = (color.indexOf("rgb") > -1);
	
	if (USE_RGB_COLORS) {
		var arr;
 		arr = REGEXP_RGB_GRAYVALUE.exec(color);
 		return Number(arr[1]);
	} else {
		return Hex2Decimal(color.substring(1, 3));
	}
}

// takes a gray value from the range 0 - 255 and returns a color equivalent either in 
// hex or in rgb depending on the value of USE_RGB_COLORS which is set automagically 
// by getGrayValueFromColor
function _createColorForGrayValue(gray, hex)
{
	if (hex) {
		var h = Decimal2Hex(gray,2);
		return "#" + h + h + h;
	} else {
		gray = gray.toFixed(0);
		return "rgb(" + gray + ", " + gray + ", " + gray + ")";
	}
}

function createBackgroundColorForGrayValue(gray) {
	return _createColorForGrayValue(gray, true);
}


function createFontColorForGrayValue(gray) {
	return _createColorForGrayValue(gray, false);
}

///////////////////Hex Conversions/////////////////////

	

// convert a single digit (0 - 16) into hex

function enHex(aDigit)

{

    return("0123456789ABCDEF".substring(aDigit, aDigit+1))

}

// convert a hex digit into decimal

function deHex(aDigit)
{
    return("0123456789ABCDEF".indexOf(aDigit))
}

// Convert a 24bit number to hex, the second value is the number of 4 bit blocks (or hex digits) to convert, max is 6

function Decimal2Hex(n, hexDigits)
{
    if ((hexDigits < 1) || (hexDigits > 6))
    	return "";
    
    ret = "";
    switch (hexDigits) {
    	case 6:
    	   ret += enHex((0xf00000 & n) >> 20);
    	case 5:
    	   ret += enHex((0x0f0000 & n) >> 16);
    	case 4:
    	   ret += enHex((0x00f000 & n) >> 12);
    	case 3:
    	   ret += enHex((0x000f00 & n) >> 8);
    	case 2:
    	   ret += enHex((0x0000f0 & n) >> 4);
    	case 1:
    	   ret += enHex((0x00000f & n) >> 0);
    }

    return ret;
}

// Convert a six character hex to decimal
function Hex2Decimal(hexNum)

{
    var tmp = ""+hexNum.toUpperCase()

    while (tmp.length < 6) tmp = "0"+tmp
   	return ((deHex(tmp.substring(0,1)) << 20) +
       	    (deHex(tmp.substring(1,2)) << 16) + 
            (deHex(tmp.substring(2,3)) << 12) +
            (deHex(tmp.substring(3,4)) << 8) +
            (deHex(tmp.substring(4,5)) << 4) +
   	        (deHex(tmp.substring(5,6))))
}

