/////////////////////////////////////////////////////////////
// Globals
/////////////////////////////////////////////////////////////
var xSlideshow = 0;

/////////////////////////////////////////////////////////////
// Browser Sniffing
/////////////////////////////////////////////////////////////
var ua = navigator.userAgent;
var dom = (document.getElementById) ? true : false;
var ie4 = (document.all && !dom) ? true : false;
var ie5_5 = ((ua.indexOf("MSIE 5.5")>=0 || ua.indexOf("MSIE 6")>=0 || ua.indexOf("MSIE 7")>=0) && ua.indexOf("Opera")<0) ? true : false;
var ns4 = (document.layers && !dom) ? true : false;

// Document.all is deprecated but without it we fail on 
// IE4 or earlier. This mocks up the get element by ID.
if (document.all && !document.getElementById) {
	document.getElementById = function(id) {
		return document.all[id];
    }
}


/////////////////////////////////////////////////////////////
// OnLoad - Single function does all the necessary init work.
/////////////////////////////////////////////////////////////
window.onload = function()
{
	// Construct the objects
	xSlideshow = new Slideshow();
	if (xSlideshow) {
		xSlideshow.SetAbsolutePhoto(0);
	}
};


/////////////////////////////////////////////////////////////
// Snaffling key presses to move the pictures
/////////////////////////////////////////////////////////////
if (!document.all) {
	window.captureEvents(Event.KEYDOWN);
	window.onkeydown = onKeyDown;
} else {
	document.onkeydown = onKeyDown;
} 
function onKeyDown(e)
{
	var nKeyCode = 
	    document.layers ? e.which :
	    document.all ? window.event.keyCode :
	    document.getElementById ? e.keyCode : 0;

	switch(nKeyCode) {
		case 37:
			xSlideshow.SetRelativePhoto(-1);
			break;

		case 39:
			xSlideshow.SetRelativePhoto(+1);
			break;
	}
}


/////////////////////////////////////////////////////////////
// SetPhoto
/////////////////////////////////////////////////////////////
function SetActivePhoto(nIndex)
{
	if (xSlideshow) {
		xSlideshow.SetAbsolutePhoto(nIndex);
	}
}

function SetRelativePhoto(nDelta)
{
	if (xSlideshow) {
		xSlideshow.SetRelativePhoto(nDelta);
	}
}

function PlaySlideshow()
{
	if (xSlideshow) {
		xSlideshow.goSlideshow(true, false);
	}
}

function StopSlideshow()
{
	if (xSlideshow) {
		xSlideshow.goSlideshow(false, true);
	}
}




/////////////////////////////////////////////////////////////
// Slideshow Image Class
/////////////////////////////////////////////////////////////
function SlideshowImage(sSrc, sDesc)
{
	this.m_sDesc = sDesc;
	this.m_xImage = new Image();
	if (this.m_xImage) {
		this.m_xImage.src = sSrc;
	}
}


/////////////////////////////////////////////////////////////
// Slideshow Class
/////////////////////////////////////////////////////////////
function Slideshow()
{
	// Initialise Objects
	this.m_nCurrent = -1;
	this.m_bSlideShow = false;
	this.m_nTimerID = 0;
	this.m_nTimerPeriod = 5000;
	this.m_nSlideshowTransitionPeriod = 3.0;
	this.m_nDefaultTransitionPeriod = 0.3;
	this.m_wndFrame = document.getElementById('PhotoFrame');
	this.m_wndImg = document.getElementById('PhotoImg');
	this.m_wndDesc = document.getElementById('PhotoDesc');

	// ------------------------------------------------------
	// Pseudo Message Handler - Moving to new Photos
	// ------------------------------------------------------
	this.SetAbsolutePhoto = function(nIndex)
	{
		this.onChangePhoto(nIndex);
	}

	this.SetRelativePhoto = function(nDelta)
	{
		this.onChangePhoto((this.m_nCurrent + nDelta + xImage.length) % xImage.length);
	}

	this.goSlideshow = function(bPlayButton, bStopButton)
	{
		var xButton = document.getElementById('sstoggle');

		// Pressing the stop button pauses then resets the slideshow
		if (bStopButton) {
			if (!this.m_bSlideShow) {
				this.SetAbsolutePhoto(0);
			}
			this.StopTimer();
			this.m_bSlideShow = false;
		}

		// Pressing the play button will pause or restart the slideshow
		if (bPlayButton) {
			if (this.m_bSlideShow) {
				this.StopTimer();
				this.m_bSlideShow = false;
			} else {
				this.StartTimer();
				this.m_bSlideShow = true;
			}
		}

		// Update the button image
		if (this.m_bSlideShow) {
			xButton.src = '_share/img/sspause.jpg';
		} else {
			xButton.src = '_share/img/ssplay.jpg';
		}
	}


	// ------------------------------------------------------
	// Support Function - onChangePhoto
	// ------------------------------------------------------
	this.onChangePhoto = function(nIndex)
	{
		//Range check the index into the preload images
		if ((nIndex < 0) || (nIndex >= xImage.length)) {
			return false;
		} 

		// No action if this is the same picture
		if (this.m_nCurrent == nIndex) {
			return false;
		}

		// Update the description
		if (this.m_wndDesc) {
			this.m_wndDesc.innerHTML = xImage[nIndex].m_sDesc;
		}

		// Update the image source
		if (this.m_wndFrame && this.m_wndImg) {
			if (ie5_5) {
				if (this.m_bSlideShow) {
					this.m_wndFrame.style.filter="progid:DXImageTransform.Microsoft.Fade(duration=" + this.m_nSlideshowTransitionPeriod + ");";
				} else {
					this.m_wndFrame.style.filter="progid:DXImageTransform.Microsoft.Fade(duration=" + this.m_nDefaultTransitionPeriod + ");";
				}
				this.m_wndFrame.filters[0].Apply();
				this.m_wndImg.src = xImage[nIndex].m_xImage.src;
				this.m_wndFrame.filters[0].Play();	
			} else {
				this.m_wndImg.src = xImage[nIndex].m_xImage.src;
			}
		}

		this.m_nCurrent = nIndex;
	}


	// ------------------------------------------------------
	// Protected Member - Start Timer
	// ------------------------------------------------------
	this.StartTimer = function()
	{
		if (this.m_nTimerID == 0) {
			var self = this;
			var nPeriod = (this.m_bSlideShow) ? (this.m_nTimerPeriod + (this.m_nSlideshowTransitionPeriod * 1000)) : (this.m_nTimerPeriod);
			this.m_nTimerID = setTimeout(function() {self.onTimer();}, nPeriod);
		}
	}

	// ------------------------------------------------------
	// Protected Member - Stop Timer
	// ------------------------------------------------------
	this.StopTimer = function()
	{
		if (this.m_nTimerID != 0) {
			clearTimeout(this.m_nTimerID);
			this.m_nTimerID = 0;
		}
	}
	
	// ------------------------------------------------------
	// Protected Member - onTimer
	// ------------------------------------------------------
	this.onTimer = function()
	{
		this.StopTimer();
		this.SetRelativePhoto(+1);
		this.StartTimer();
	}
}


