// copyright K Cortbawi March 2011 
// duplication or unlicensed use prohibited

function fade(theBlock,theText)
{
  var element = document.getElementById(theBlock);
  if(element == null)
    return;
   
  if(element.FadeState == null)
  {
    if(element.style.opacity == null 
        || element.style.opacity == '' 
        || element.style.opacity == '1')
    {
      element.FadeState = 2;
    }
    else
    {
      element.FadeState = -2;
    }
  }
    
  if(element.FadeState == 1 || element.FadeState == -1)
  {
    element.FadeState = element.FadeState == 1 ? -1 : 1;
    element.FadeTimeLeft = element.TimeToFade - element.FadeTimeLeft;
  }
  else
  {
    element.FadeState = element.FadeState == 2 ? -1 : 1;
    element.FadeTimeLeft = element.TimeToFade;
    clearTimeout(element.fadetimer);
    element.fadetimer=setTimeout("animateFade(" + new Date().getTime() + ",'" + theBlock + "','" +theText+ "')", element.FadeSpeed);
  }  
}
function animateFade(lastTick, theBlock,theText)
{  
  var curTick = new Date().getTime();
  var elapsedTicks = curTick - lastTick;
  
  var element = document.getElementById(theBlock);
 
  if(element.FadeTimeLeft <= elapsedTicks)
  {
    element.style.opacity = element.FadeState == 1 ? '1' : '0';
    element.style.filter = 'alpha(opacity = ' 
        + (element.FadeState == 1 ? '100' : '0') + ')';
    element.FadeState = element.FadeState == 1 ? 2 : -2;
	if (element.FadeState == 2) {  // finished fade-in
		element.swapping=false; //alert("fade in complete");
		//checkHeight(theBlock); //alert('done + '+element.FadeState);
		rotateNext(theBlock);
	} else { 
		if (element.swapping) { fadeIn(theBlock,theText); //alert("I want to fade in")
		} // finished fade-out
	}
    return;
  }
 
  element.FadeTimeLeft -= elapsedTicks;
  var newOpVal = element.FadeTimeLeft/element.TimeToFade;
  if(element.FadeState == 1){
    newOpVal = 1 - newOpVal; 
  }
  element.style.opacity = newOpVal;
  element.style.filter = 'alpha(opacity = ' + (newOpVal*100) + ')';
  clearTimeout(element.fadetimer);
  element.fadetimer=setTimeout("animateFade(" + curTick + ",'" + theBlock + "','" +theText+ "')", element.FadeSpeed);
}
function checkHeight(theBlock) {
var x=document.getElementById(theBlock);
var theNewHeight=x.offsetHeight+0; //
if ((theNewHeight>x.theCurrentHeight)&&(x.shrink!=true)) {
	document.getElementById(theBlock+"Container").style.height=theNewHeight+"px"; 
//	document.getElementById(theBlock+"Container").style.backgroundColor="#FF3300"; 
	x.theCurrentHeight=theNewHeight;
//} else {document.getElementById(theBlock+"Container").style.backgroundColor="#000099"; 
}
}
function fadeOut(theBlock,theText) {   // calls fade-out  
	var x=document.getElementById(theBlock); //x.theCurrentHeight=x.offsetHeight;  doesnt work consistently - thank you gecko
	fade(theBlock,theText);
}
function fadeIn(theBlock,theText) {  // called from animateFade when fadeout is complete 
	var x=document.getElementById(theBlock); 	
	if (theText=='undefined') {theText=x.htmlSlides[x.htmlSlideTrak];
	if (theText=='undefined') theText='*'+theText+'*';
	}
	x.innerHTML=theText;
	setTimeout("checkHeight('"+theBlock+"');", 100); //checkHeight(theBlock); needs delay for browser to catch up 
	fade(theBlock);
}
function fadeSwap(theBlock,theText) { 
document.getElementById(theBlock).swapping=true; fadeOut(theBlock,theText); // starts fadeswap 
} 
function rotateFadeSwap(theBlock,theInterval){ 
var x=document.getElementById(theBlock); //x.interval=theInterval;
if (x.htmlSlideTrak>x.htmlSlides.length-1) {
	x.htmlSlideTrak=0.0; if (x.loop!=true) return;
} 
if (x.go){ fadeSwap(theBlock,x.htmlSlides[x.htmlSlideTrak]); x.htmlSlideTrak=x.htmlSlideTrak+1;
}
}
function rotateNext(theBlock){
var x=document.getElementById(theBlock).interval; clearTimeout(x.intervalTimer);
x.intervalTimer=setTimeout("rotateFadeSwap('"+theBlock+"','"+x+"');", x);
}
function stopRotate(theBlock) { var x=document.getElementById(theBlock); if (x.pause) {x.go=false;}
}
function rotate(theBlock) { var x=document.getElementById(theBlock);
x.go=true; if(x.swapping==false) {clearTimeout(x.intervalTimer); x.intervalTimer=rotateFadeSwap(theBlock,x.interval);}
}
