var currentquoteId = "r";
var quoteTimer = 0;

var oldOnLoad;

oldOnLoad = window.onload;


if (typeof window.onload == 'function')
{
	window.onload = function()
	{
		oldOnLoad(); //this calls the previous on load
		loadquote('r');
		changeAdLinks();
	}
}
else
{
	window.onload = function()
	{
		loadquote('r');
		changeAdLinks();
	}
}


function loadquote(direction)
{
	//alert(direction);
	clearInterval(quoteTimer);
	if ( direction != 'p' && direction != 'n' )
	{
		direction = "r";
	}
	
	if (window.XMLHttpRequest)
	{
		// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	}
	else
	{
		// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	id = parseInt(currentquoteId);

	if ( id > 0 )
	{
		if(direction == "p")
		{
			id = id - 1;
		}
		else
		{
			id = id + 1;
		}
	}
	else
	{
		id = "r";
	}
	
	currentquoteId = id;
	quoteURL = "index.php?pid=5679&i=" + currentquoteId + "&t=" + new Date().getTime();
	
	xmlhttp.onreadystatechange=function()
	{
		clearInterval(quoteTimer);
		if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{
			txt=xmlhttp.responseText;

			// extract the id
			start = parseInt(txt.indexOf('<id>'));
			if (start >= 0)
			{
				start = start + 4;
				end = parseInt(txt.indexOf('</id>'));
				if ( end >= 0 )
				{
					strLength = end-start;
					id = parseInt(txt.substr(start, strLength));
				}
				else
				{
					id = 1;
				}
			}
			else
			{
				id = 1;
			}
			currentquoteId = id;

			// extract the quote
			start = parseInt(txt.indexOf('<quote>'));
			if (start >= 0)
			{
				start = start + 7;
				end = parseInt(txt.indexOf('</quote>'));
				if ( end >= 0 )
				{
					strLength = end-start;
					quote = txt.substr(start, strLength);
				}
				else
				{
					quote = "I will not waste chalk";
				}
			}
			else
			{
				quote = "I will not waste chalk";
			}

			// extract the author
			start = parseInt(txt.indexOf('<author>'));
			if (start >= 0)
			{
				start = start + 8;
				end = parseInt(txt.indexOf('</author>'));
				if ( end >= 0 )
				{
					strLength = end-start;
					author = txt.substr(start, strLength);
				}
				else
				{
					author = "Bart Simpson's Chalkboard Openings";
				}
			}
			else
			{
				author = "Bart Simpson's Chalkboard Openings";
			}
			document.getElementById("myDiv").innerHTML='<p style="padding:0; margin:0"><span style=" font-size:16px; font-family:Helvetica;">' + quote +'<br /></span><span style="font-family:Helvetica; font-size:12px; font-style:italic;">' + author + '</span></p>';
		}
		quoteTimer = setInterval("loadquote('n')",5000);
	}
	
	xmlhttp.open("GET", quoteURL ,true);
	xmlhttp.setRequestHeader('Content-Type',  "text/xml");

	xmlhttp.send();
}
var nextAdTimer = 0;
var currentAdId = 1;
var fadeTimer = 0;
var adFaded = false;
var fadeActive = false;

nextAdTimer = setInterval('nextAd()', 5000);

function nextAd()
{
	if( document.getElementById('adHolderFront') )
	{
		loadAd(currentAdId + 1);
	}
}
function loadAd(adId)
{
	if (currentAdId == adId)
	{
		clearInterval ( nextAdTimer );
		nextAdTimer = setInterval('nextAd()', 5000);
	}
	else if ( fadeActive == false )
	{
		fadeActive = true;
		adId = adId > 4 ? 1 : adId;
		fadeAd(adId);
	}
}
function fadeAd(adId)
{
	clearInterval ( nextAdTimer );
	
	if ( document.getElementById('adHolderFront') )
	{
		var adHolderFront = document.getElementById('adHolderFront');
		var adHolderBack = document.getElementById('adHolderBack');
		if (! adFaded)
		{
			if ( adId < 1 || adId > 4 )
			{
				adId = 1;
			}
			adHolderBack.src = "pages/ad/"+adId+".jpg";
			currentAdId = adId;
			fadeTimer = setInterval('fadeInOut()', 50);
		}
		else if ( adFaded )
		{
			adHolderFront.src ="pages/ad/"+currentAdId+".jpg";
			adHolderFront.style.opacity = 1;
			adHolderFront.style.filter = 'alpha(opacity=100)';
			adHolderBack.src = "pages/ad/"+currentAdId+".jpg";
			adHolderBack.style.opacity = 0;
			adHolderBack.style.filter = 'alpha(opacity=0)';

			link = document.getElementById('currentAdLink');
			link.href="?pid=8&ad=" + currentAdId;

			var obj = document.getElementById('adLinks');
			var nodes = obj.childNodes;
			for ( i=0, j=1; i < (nodes.length) ; i++)
			{
				nodeName = nodes[i].nodeName.toLowerCase() ;
				if ( nodeName == 'a' )
				{
					if ( j == currentAdId )
					{	
						nodes[i].className += " selectedAdLink";
					}
					else
					{
						nodes[i].className = "adLink";
					}
					j++;
				}
			}
			
			nextAdTimer = setInterval('nextAd()', 5000);
			adFaded = false;
			fadeActive = false;
		}
	}
}

function fadeInOut()
{
	var adHolderFront = document.getElementById('adHolderFront');
	var adHolderBack = document.getElementById('adHolderBack');
	opacityFront = parseFloat(adHolderFront.style.opacity);

	if ( opacityFront > 1)
	{
		opacityFront = 1.1;
	}

	if ( opacityFront > 0.1 ) 
	{
		opacityFront = opacityFront - 0.1;
		opacityBack = 1.1 - opacityFront;
		adHolderFront.style.opacity = opacityFront;
		adHolderFront.style.filter = 'alpha(opacity=' + opacityFront*100 + ')';

		adHolderBack.style.opacity = opacityBack;
		adHolderBack.style.filter = 'alpha(opacity=' + opacityBack*100 + ')';
	}
	else
	{		
		adFaded = true;
		clearInterval(fadeTimer);
		fadeAd(currentAdId);
	}
}

function changeAdLinks()
{
	if ( document.getElementById('adLinks') )
	{
		
		var obj = document.getElementById('adLinks');
		var nodes = obj.childNodes;
		for ( i=0, j=1; i < (nodes.length) ; i++)
		{
			nodeName = nodes[i].nodeName.toLowerCase() ;
			if ( nodeName == 'a' )
			{
				nodes[i].href = "javascript:loadAd("+ j +");";
				j++;
			}
		}
	}

}


