var nthoughts = 6209;
var thoughtDelay=5000;
var thoughtPrefix="thoughts/oc";
var Playing = true;
function bn(type,active,colour){
  return "Buttons/s_"+type+"_"+active+"_"+colour+".png";
}
var colour="p"; /* Purple, b is blue */
var pauseNeutral=bn("p","n",colour);
var pauseActive=bn("p","a",colour);
var playNeutral=bn("f","n",colour);
var playActive=bn("f","a",colour);
var rewindNeutral=bn("rw","n",colour);
var rewindActive=bn("rw","a",colour);
var fastforwardNeutral=bn("ff","n",colour);
var fastforwardActive=bn("ff","a",colour);

/*
var pauseNeutral="Buttons/PauseNeutral.png";
var pauseActive="Buttons/PauseActive.png";
var playNeutral="Buttons/ForwardNeutral.png";
var playActive="Buttons/ForwardActive.png";
var rewindNeutral="Buttons/RewindNeutral.png";
var rewindActive="Buttons/RewindActive.png";
*/

var pausePlay="<a href=\"#\" class=button onMouseDown=\"return activatePausePlay()\" onMouseUp=\"return armPausePlay()\" onMouseOver=\"return armPausePlay()\" onMouseOut=\"return disarmPausePlay()\"><img id='pausePlayButton' class='b' src=\""+pauseNeutral+"\"></a>";

var rewind="<a href=\"#\" class=button onMouseDown=\"return activateRewind()\" onMouseUp=\"return armRewind()\" onMouseOver=\"return armRewind()\" onMouseOut=\"return disarmRewind()\"><img id='rewindButton' class='b' src=\""+rewindNeutral+"\"></a>";

var fastforward="<a href=\"#\" class=button onMouseDown=\"return activateFastforward()\" onMouseUp=\"return armFastforward()\" onMouseOver=\"return armFastforward()\" onMouseOut=\"return disarmFastforward()\"><img id='fastforwardButton' class='b' src=\""+fastforwardNeutral+"\"></a>";

var buttonsHTML="<td class=buttons align=right><table>"+
   "<tr><td>"+rewind+"</td></tr>"+
   "<tr><td>"+pausePlay+"</td></tr>"+
   "<tr><td>"+fastforward+"</td></tr>"+
  "</table></td>";
var thoughtHTMLPrefix="<table class='thoughtTable'><tr><td id=innerThought class=thought>";
var thoughtHTMLSuffix="</td>"+buttonsHTML+"</tr></table>";  
var t;
var bt;
var offTime=100;
var onTime=200;
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	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 c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}
function checkCookie(){
    if (readCookie("rt")==null){
       createCookie("rt",6200,30);
    } 
}
function thoughtFile(n){
  return thoughtPrefix+n+".xml";
}

function armPausePlay(){
  document.getElementById('pausePlayButton').src=Playing?pauseActive:playActive;
}
function disarmPausePlay(){
  document.getElementById('pausePlayButton').src=Playing?pauseNeutral:playNeutral;
}
function activatePausePlay(){
  if (Playing){
    stopThoughts();
    Playing=false;
  }else{
    continueThoughts();
    Playing=true;
  }
}
function armRewind(){
  document.getElementById('rewindButton').src=rewindActive;
}
function disarmRewind(){
  document.getElementById('rewindButton').src=rewindNeutral;
}
function activateRewind(){
  if (Playing){
    stopThoughts();
    Playing=false;
  }
  disarmPausePlay();
  decrementThought();
}
function armFastforward(){
  document.getElementById('fastforwardButton').src=fastforwardActive;
}
function disarmFastforward(){
  document.getElementById('fastforwardButton').src=fastforwardNeutral;
}
function activateFastforward(){
  if (Playing){
    stopThoughts();
    Playing=false;
  }
  disarmPausePlay();

 incrementThought();
}
function prepBlink(){
  unFadeThought();
	if (Playing){
          bt=setTimeout('unFadeThought();',offTime);
          t=setTimeout('blink()', thoughtDelay);
	}
}
function updateThoughtDisplay(i){
    var tf=thoughtFile(i);
    eraseCookie("rt");
        createCookie("rt",i,30);
    var callback = {
      success: function(o){
	var inner=document.getElementById('innerThought');
	if (inner==null){
         document.getElementById('thought').innerHTML=thoughtHTMLPrefix+(o.responseText)+thoughtHTMLSuffix;
	}else{
	  inner.innerHTML=o.responseText;
	}
	prepBlink();
      },
      failure: 	function (o){
        document.getElementById('thought').innerHTML=thoughtHTMLPrefix+"<span style='color:red'>Just a moment... Just a moment...</span>"+thoughtHTMLSuffix;
	//	Playing=false;
	prepBlink();
      }
    }
    var connectionObject= YAHOO.util.Connect.asyncRequest('GET',tf,callback);
}
function decrementThought(){
  checkCookie();   
    var i=parseInt(readCookie("rt"))-1;
    if (i<1) {  i= nthoughts; }
    updateThoughtDisplay(i);
}

function incrementThought(){
  checkCookie();
    var i=parseInt(readCookie("rt"))+1;
    if (i> nthoughts) {  i=1; }
    updateThoughtDisplay(i);
}
function startThoughts(){
//  eraseCookie('rt');
    t=setTimeout('blink()', thoughtDelay);
}
function continueThoughts(){
 // Starts with thought one already there
  incrementThought();
}
 function stopThoughts(){
  clearTimeout(t);
}

function fadeThought(){
      document.getElementById('thought').style.opacity=0.8;
      //  if (navigator.appName=="Microsoft Internet Explorer"){
      //	document.getElementById('thought').filters.alpha.opacity = 80;
      //}
}
function unFadeThought(){
      document.getElementById('thought').style.opacity=1.0;
      //if (navigator.appName=="Microsoft Internet Explorer"){
      //document.getElementById('thought').filters.alpha.opacity = 100;
      //}
}

function hideThought(cycle){
    fadeThought();
    bt=setTimeout('showThought('+(cycle+1)+')',offTime);
}
function showThought(cycle){
    if (cycle < 5) {
      unFadeThought();
      bt=setTimeout('hideThought('+cycle+')',onTime);
    } else {
      clearTimeout(bt);
      incrementThought();
    }
}
function blink(){
   clearTimeout(t);
   hideThought(1);
}

function startThoughts(){
 // Starts with thought one already there
//  eraseCookie('rt');
    t=setTimeout('blink()', thoughtDelay);
}
function continueThoughts(){
//  eraseCookie('rt');
//    t=setTimeout('blink()', thoughtDelay);
  incrementThought();
}
 function stopThoughts(){
  clearTimeout(t);
}

continueThoughts();
