var quoteSpan;
var sourceTd;

var quoteArray = new Array();
var sourceArray = new Array();
var dimState=1;
var i=0;

var xmlHttp = createXmlHttpRequestObject(); 

function createXmlHttpRequestObject() 
{	
  var xmlHttp;
  if(window.ActiveXObject)
  {
    try
    {
      xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    catch (e) 
    {
      xmlHttp = false;
    }
  }
  else
  {
    try 
    {
      xmlHttp = new XMLHttpRequest();
    }
    catch (e) 
    {
      xmlHttp = false;
    }
  }
  if (!xmlHttp) 
    alert("Error creating the XMLHttpRequest object.");
  else 
    return xmlHttp;
}



function process()
{
  if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
  { 
	send="http://www.sfcityguides.org/give_quotes.php";
    xmlHttp.open("GET",send, true);  
    xmlHttp.onreadystatechange = handleServerResponse;
    xmlHttp.send(null);
  }
  else
    setTimeout('process()', 1000);
}

// executed automatically when a message is received from the server
function handleServerResponse() 
{	
   if (xmlHttp.readyState == 4) 
  {
    if (xmlHttp.status == 200) 
    {  
      xmlResponse = xmlHttp.responseXML;
      xmlDocumentElement = xmlResponse.documentElement;
      quoteArray=xmlDocumentElement.getElementsByTagName("quote");
      sourceArray=xmlDocumentElement.getElementsByTagName("source");
	  start();
    } 
    else 
    {
      alert(xmlHttp.status);
    }
  }
}

function start() 
{	

	var quoteLength=quoteArray.length;
	
	quoteSpan=document.getElementById("quote");
	sourceTd=document.getElementById("source");
	
	if(dimState==1){
		if(i<quoteLength-1){
			i++;
		}
		else{
			i=0;
		}
		dimState=0;
		quoteSpan.firstChild.nodeValue='"'+quoteArray.item(i).firstChild.data+'"';
		sourceTd.firstChild.nodeValue="-"+sourceArray.item(i).firstChild.data;;
		bright();
	}
	
	setTimeout("start()",1000);
}

function dim()
{
	var opQuote=quoteSpan.style.opacity;
	opQuote=parseFloat(opQuote)-0.1;
	
	quoteSpan.style.opacity=opQuote;
	sourceTd.style.opacity=opQuote;
	
	if(opQuote>0){
		setTimeout("dim()",150);
	}
	else{
		dimState=1;
	}
}


function bright()
{
	var opQuote=quoteSpan.style.opacity;
	opQuote=parseFloat(opQuote)+0.1;	
	
	quoteSpan.style.opacity=opQuote;
	sourceTd.style.opacity=opQuote;
	
	if(opQuote<1){
		setTimeout("bright()",150);
	}
	else{		
		setTimeout("dim()",6000);
	}
}
