function CUSlideHttpClient()
{
	var itsHttpRequest;
	var itsCallBack;
	var self = this;

	if (window.XMLHttpRequest)
	{
		itsHttpRequest = new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{
		try
		{
			itsHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			try
			{
				itsHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e)
			{}
		}
	}

	if (!itsHttpRequest)
	{
		alert( "noHttpRequest");
		return false;
	}

	this.handleChangedState = function()
	{
		if (itsHttpRequest.readyState == 4)
		{
			if (itsCallBack)
			{
				itsCallBack(itsHttpRequest);
			}
		}
	};

	this.get = function(theUrl,theCallBack)
	{
		itsCallBack = theCallBack;
		itsHttpRequest.open("GET",theUrl,true);
		itsHttpRequest.onreadystatechange = this.handleChangedState;
		itsHttpRequest.send(null);
	};

	this.put = function(theUrl,theData,theCallBack)
	{
		itsCallBack = theCallBack;
		itsHttpRequest.open("PUT",theUrl,true);
		itsHttpRequest.onreadystatechange = this.handleChangedState;
		itsHttpRequest.send(theData);
	};

	this.del = function(theUrl,theCallBack)
	{
		itsCallBack = theCallBack;
		itsHttpRequest.open("DELETE",theUrl,true);
		itsHttpRequest.onreadystatechange = this.handleChangedState;
		itsHttpRequest.send(null);
	};

	this.post = function(theUrl,theData,theCallBack)
	{
		itsCallBack = theCallBack;
		itsHttpRequest.open("POST",theUrl,true);
		itsHttpRequest.onreadystatechange = this.handleChangedState;
		itsHttpRequest.send(theData);
	};
};


function CUSlideAjaxXML()
{
	var itsUrl;
	var itsXmlProcessor;
	var itsHttpClient = new CUSlideHttpClient();


	this.setUrl = function(inUrl)
	{
		itsUrl = inUrl;
	};


	this.setXmlProcessor = function(inCallback)
	{
		itsXmlProcessor = inCallback;
	};


	function callback(inHttpResponse)
	{
		var theXMLDoc;
		var theData = inHttpResponse.responseText;
		if (window.DOMParser)
		{
			var theParser = new DOMParser();
			theXMLDoc = theParser.parseFromString(theData,"text/xml");
		}
		else
		{
			theXMLDoc = new ActiveXObject("Microsoft.XMLDOM");
			theXMLDoc.async = "false";
			theXMLDoc.loadXML(theData);
		}
		itsXmlProcessor(theXMLDoc);
	};


	this.eval = function()
	{
		itsHttpClient.get(itsUrl, callback)
	};

}

if (CUSlide == null) var CUSlide = {};

CUSlide.itsSlides = [];
CUSlide.itsCurrentSlide = -1;
CUSlide.itsPaused = false;
CUSlide.itsLink = null;
CUSlide.itsImage = null;
CUSlide.itsFadeValue = 0;
CUSlide.itsFadeVelocity = 0;
CUSlide.itsBackwards = false;
CUSlide.itsChoice = -1;
CUSlide.itsTimeouts = [];

CUSlide.onLoad = function()
{
	CUSlide.itsLink = document.getElementById("slide-link");
	CUSlide.itsImage = document.getElementById("slide-image");
	var theBack = document.getElementById("slide-back");
	var theForward = document.getElementById("slide-forward");
	var thePlayPause = document.getElementById("slide-play-pause");
	if (theBack)
	{
		CUSlide.addEvent(theBack,"click",CUSlide.manualBack);
	}
	if (theForward)
	{
		CUSlide.addEvent(theForward,"click",CUSlide.manualForward);
	}
	if (thePlayPause)
	{
		CUSlide.addEvent(thePlayPause,"click",CUSlide.playOrPause);
	}
	for (var i = 1; (i <= 5); i++)
	{
		var theButton = document.getElementById("slide-" + i);
		if (theButton)
		{
			CUSlide.addEvent(theButton,"click",(function(inSlideNumber) {return function() {CUSlide.manualChoose(inSlideNumber - 1)};})(i));
		}
	}
	var theSlideDiv = document.getElementById("slide-show");
	if (theSlideDiv)
	{
		CUSlide.getSlideData(theSlideDiv.getAttribute("xmlsrc"));
	}
}

CUSlide.getSlideData = function(inUrl)
{
	if (inUrl)
	{
		var theSlideLoader = new CUSlideAjaxXML();
		theSlideLoader.setXmlProcessor(CUSlide.processSlideData);
		theSlideLoader.setUrl(inUrl);
		theSlideLoader.eval();
	}
}

CUSlide.processSlideData = function(inXMLDoc)
{
	var i;
	var theSlide;
	var thePhoto;
	var theLink;
	var theDuration;
	var theSlideObject;
	if (inXMLDoc)
	{
		var theSlides = inXMLDoc.getElementsByTagName("SLIDE");
		if (theSlides)
		{
			CUSlide.itsCurrentSlide = 0;
			for (i = 0; (i < theSlides.length); i++)
			{
				theSlide = theSlides[i];
				thePhoto = theSlide.getElementsByTagName("PHOTO")[0].childNodes[0].nodeValue;
				theLink = theSlide.getElementsByTagName("link")[0].childNodes[0].nodeValue;
				theDuration = theSlide.getElementsByTagName("DURATION")[0].childNodes[0].nodeValue;
				theSlideObject = {};
				theSlideObject.photo = thePhoto;
				theSlideObject.link = theLink;
				theSlideObject.duration = theDuration;
				CUSlide.itsSlides.push(theSlideObject);
			}
		}
		CUSlide.changeSlide();
	}
}

CUSlide.changeSlide = function()
{
	if (CUSlide.itsLink && CUSlide.itsImage)
	{
		CUSlide.itsLink.setAttribute("href", CUSlide.itsSlides[CUSlide.itsCurrentSlide].link);
		CUSlide.itsImage.setAttribute("src", CUSlide.itsSlides[CUSlide.itsCurrentSlide].photo);
		CUSlide.cancelTimeouts();
		CUSlide.itsTimeouts.push(setTimeout("CUSlide.fadeIn()", 300));
	}
}

CUSlide.fadeOut = function()
{
	var theButton = document.getElementById("slide-" + (CUSlide.itsCurrentSlide + 1));
	if (theButton)
	{
		theButton.className = "slide-off";
	}
	CUSlide.itsFadeValue = 1;
	CUSlide.itsFadeVelocity = -0.05;
	CUSlide.cancelTimeouts();
	CUSlide.itsTimeouts.push(setTimeout("CUSlide.loop()", 33));
}

CUSlide.fadeIn = function()
{
	CUSlide.itsFadeValue = 0;
	CUSlide.itsFadeVelocity = 0.05;
	var theButton = document.getElementById("slide-" + (CUSlide.itsCurrentSlide + 1));
	if (theButton)
	{
		theButton.className = "slide-on";
	}
	CUSlide.cancelTimeouts();
	CUSlide.itsTimeouts.push(setTimeout("CUSlide.loop()", 33));
}

CUSlide.fadedIn = function()
{
	if (!(CUSlide.itsPaused))
	{
		CUSlide.cancelTimeouts();
		CUSlide.itsTimeouts.push(setTimeout("CUSlide.fadeOut()", CUSlide.itsSlides[CUSlide.itsCurrentSlide].duration));
	}
}

CUSlide.fadedOut = function()
{
	if (CUSlide.itsChoice != -1)
	{
		CUSlide.itsCurrentSlide = CUSlide.itsChoice;
		CUSlide.itsChoice = -1;
	}
	else
	{
		if (CUSlide.itsBackwards)
		{
			CUSlide.itsBackwards = false;
			CUSlide.itsCurrentSlide--;
			if (CUSlide.itsCurrentSlide < 0)
			{
				CUSlide.itsCurrentSlide = CUSlide.itsSlides.length - 1;
			}
		}
		else
		{
			CUSlide.itsCurrentSlide++;
			if (CUSlide.itsCurrentSlide >= CUSlide.itsSlides.length)
			{
				CUSlide.itsCurrentSlide = 0;
			}
		}
	}
	CUSlide.changeSlide();
}

CUSlide.cancelTimeouts = function()
{
	for (var i = 0; (i < CUSlide.itsTimeouts.length); i++)
	{
		clearTimeout(CUSlide.itsTimeouts[i]);
	}
	CUSlide.itsTimeouts = [];
}

CUSlide.manualBack = function()
{
	CUSlide.cancelTimeouts();
	CUSlide.itsBackwards = true;
	CUSlide.fadeOut();
}

CUSlide.manualForward = function()
{
	CUSlide.cancelTimeouts();
	CUSlide.fadeOut();
}

CUSlide.pause = function()
{
	CUSlide.cancelTimeouts();
	CUSlide.itsPaused = true;
}

CUSlide.unpause = function()
{
	CUSlide.cancelTimeouts();
	CUSlide.itsPaused = false;
	CUSlide.fadeOut();
}

CUSlide.playOrPause = function()
{
	var thePlayPause = document.getElementById("slide-play-pause");
	if (thePlayPause.className == 'slide-play')
	{
		thePlayPause.className = "slide-pause";
		CUSlide.unpause();
	}
	else
	{
		thePlayPause.className = "slide-play";
		CUSlide.pause();
	}
}

CUSlide.manualChoose = function(inSlideNumber)
{
	CUSlide.cancelTimeouts();
	CUSlide.itsChoice = inSlideNumber;
	CUSlide.fadeOut();
}

CUSlide.loop = function()
{
	var i;
	var theOpacity;
	var theSlide = CUSlide.itsImage;
	if (CUSlide.itsFadeVelocity != 0)
	{
		theOpacity = CUSlide.itsFadeValue;
		theOpacity += CUSlide.itsFadeVelocity;
		if (theOpacity <= 0)
		{
			theOpacity = 0;
			CUSlide.itsFadeVelocity = 0;
			CUSlide.itsFadeValue = theOpacity;
			theSlide.style.opacity = theOpacity;
			theSlide.style.filter = 'alpha(opacity = ' + (theOpacity * 100) + ')';
			CUSlide.fadedOut();
		}
		else if (theOpacity >= 1)
		{
			theOpacity = 1;
			CUSlide.itsFadeVelocity = 0;
			CUSlide.itsFadeValue = theOpacity;
			theSlide.style.opacity = theOpacity;
			theSlide.style.filter = 'alpha(opacity = ' + (theOpacity * 100) + ')';
			CUSlide.fadedIn();
		}
		else
		{
			CUSlide.itsFadeValue = theOpacity;
			theSlide.style.opacity = theOpacity;
			theSlide.style.filter = 'alpha(opacity = ' + (theOpacity * 100) + ')';
			CUSlide.cancelTimeouts();
			CUSlide.itsTimeouts.push(setTimeout("CUSlide.loop()", 33));
		}
	}
}

CUSlide.addEvent = function(inObject, inType, inFunction)
{
	if (inObject.addEventListener)
	{
		inObject.addEventListener( inType, inFunction, false );
		EventCache.add(inObject, inType, inFunction);
	}
	else if (inObject.attachEvent)
	{
		inObject["e" + inType + inFunction] = inFunction;
		inObject[inType + inFunction] = function() { inObject["e" + inType + inFunction]( window.event ); }
		inObject.attachEvent("on" + inType, inObject[inType + inFunction]);
		EventCache.add(inObject, inType, inFunction);
	}
	else
	{
		inObject["on" + inType] = inObject["e" + inType + inFunction];
	}
}

var EventCache = function()
{
	var theListEvents = [];
	return {
		theListEvents : theListEvents,
		add : function(node, sEventName, fHandler){
			theListEvents.push(arguments);
		},
		flush : function()
		{
			var i, theItem;
			for(i = theListEvents.length - 1; i >= 0; i = i - 1)
			{
				theItem = theListEvents[i];
				if(theItem[0].removeEventListener){
					theItem[0].removeEventListener(theItem[1], theItem[2], theItem[3]);
				};
				if(theItem[1].substring(0, 2) != "on"){
					theItem[1] = "on" + theItem[1];
				};
				if(theItem[0].detachEvent){
					theItem[0].detachEvent(theItem[1], theItem[2]);
				};
				theItem[0][theItem[1]] = null;
			};
		}
	};
}();

CUSlide.addEvent(window, "load", CUSlide.onLoad);
CUSlide.addEvent(window, "unload", EventCache.flush);

