/*var TimeToFade = 4*1000.0;*/

function animateFade(lastTick, eid)
{ 
  var curTick = new Date().getTime();
  var elapsedTicks = curTick - lastTick;
 
  var element = document.getElementById(eid);
 
  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;
    return;
  }
 
  element.FadeTimeLeft -= elapsedTicks;
  var newOpVal = element.FadeTimeLeft/TimeToFade;
  if(element.FadeState == 1)
    newOpVal = 1 - newOpVal;

  element.style.opacity = newOpVal;
  element.style.filter = 'alpha(opacity = ' + (newOpVal*100) + ')';
  setTimeout("animateFade(" + curTick + ",'" + eid + "')", 33);
}

function fade(eid)
{
//http://blog.paranoidferret.com/index.php/2007/12/20/javascript-tutorial-simple-fade-animation/ 
  var element = document.getElementById(eid);

  if(element == null)
    return;

  if(element.FadeState == null)
  {
    if(element.style.opacity == null
        || element.style.opacity == ''
        || element.style.opacity == '0')
    {
      element.FadeState = -2;
    }
    else
    {
      element.FadeState = 2;
    }
  }
   
  if(element.FadeState == 1 || element.FadeState == -1)
  {
    element.FadeState = element.FadeState == 1 ? -1 : 1;
    element.FadeTimeLeft = TimeToFade - element.FadeTimeLeft;
  }
  else
  {
    element.FadeState = element.FadeState == 2 ? -1 : 1;
    element.FadeTimeLeft = TimeToFade;
    setTimeout("animateFade(" + new Date().getTime() + ",'" + eid + "')", 33);
  } 
}

function makeNews(c){
	this.copy = c;
	this.write = writeNews;
}
function writeNews(){
	var str = "";

	//str += this.copy + "<br>";
	str += this.copy;
	return str;
}

function createCookie(name,value) {

	var date = new Date();
	date.setTime(date.getTime()+(10*60*1000));
	var expires = "; expires="+date.toGMTString();
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) 
			return parseInt(c.substring(nameEQ.length,c.length));
	}
	return 0;
}

var nIndex = parseInt(readCookie("newsrt"));
var modulusidx=0;
var timerID = null;

function rotateNews(){
   var len = newsArray.length;
	if (len==0)
		return "";
	if(nIndex >= len)
		nIndex = 0;
	if (modulusidx%2==0) {
  	document.getElementById('stories').innerHTML = newsArray[nIndex];
   	createCookie("newsrt",++nIndex);
  }
  if (len>1) {
   	fade('stories');
		modulusidx++;
 		timerID = setTimeout('rotateNews()',((modulusidx%2==0)?TimeToFade:TimeToStay));
 	}
 	else {
		document.getElementById('stories').style.opacity=1;
  	document.getElementById('stories').style.filter = 'alpha(opacity=100)';
	}
}

function pauseNews() {
  //document.body.style.cursor='wait';
	if (timerID != null) {
		clearTimeout(timerID);
		timerID = null;
	}
}

function playNews() {
	if (timerID == null) {
		timerID = setTimeout('rotateNews()', 1000);
		document.body.style.cursor = 'default';
	}
}
//window.onload=rotateNews;

function addLoadEvent_rn(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}
addLoadEvent_rn(rotateNews);




