/*------------------------------------------------------------------
Generated by Shozam
------------------------------------------------------------------*/
var t;							// Declare timer variable
var timer = true;				// Declare timer variable
var slideShowSpeed = 1000;		// Set slideShowSpeed (milliseconds)
var crossFadeDuration = 1;		// Duration of crossfade (seconds)
var FadeStatus = "Off";			// Specify Fade On / Fade Off.

var slideTimer = 0;
var fadeTimer = 100;
var fade = "out";
var doneFirstSlide = false;
var started = false;
var slideDuration = 1;

var NextPressedCounter = -1;
var PrevPressedCounter = -1;

var PrevCacheImage = true;
var NextCacheImage = true;

var PrevCacheImageCounter = 0;
var NextCacheImageCounter = 0;

var onePrev = false;
var oneNext = false;


function On_Load()
{
	FadeStatus = "On";									//Initialise value;

	var imgIndexPrev = 0;
	if(j >= 1)
		imgIndexPrev = parseInt(j - 1);
	if(j < 1)
		imgIndexPrev = parseInt(parseInt(p-1) - j);
	
	PreparePrev(imgIndexPrev);

	var imgIndexNext = 0;
	imgIndexNext = parseInt(j + 1);
	if(imgIndexNext > (p-1))
	{
   		imgIndexNext=1;
	}
	
	PrepareNext(imgIndexNext);

   	runSlideShow();	//Slideshow run on load.
}


function PreparePrev(imgIndex)
{
	if(PrevCacheImage == true)
	{
		PrevCacheImage = false;
		
		if(imgIndex < 1)
			imgIndex = parseInt(p-1);

		if(IsImageCached[imgIndex] == false)
		{
			imgArray[imgIndex] = new Image();
			imgArray[imgIndex].src = Pic[imgIndex];
		}

		PrevCacheImageCounter = imgIndex;
		
		document.images.PreCache.src = imgArray[imgIndex].src;
	}
}

function PrepareNext(imgIndex)
{
	if(NextCacheImage == true)
	{
		NextCacheImage = false;
		
		if(imgIndex > (p-1))
		{
   			imgIndex = 1;
   		}

		if(IsImageCached[imgIndex] == false)
		{
			imgArray[imgIndex] = new Image();
			imgArray[imgIndex].src = Pic[imgIndex];
		}

		NextCacheImageCounter = imgIndex;
		
		document.images.PrePreCache.src = imgArray[imgIndex].src;
	}
}


function CacheNextImage()
{
	NextCacheImage = true;
	IsImageCached[NextCacheImageCounter] = true;
}
function CachePrevImage()
{
	PrevCacheImage = true;
	IsImageCached[PrevCacheImageCounter] = true;
}


function runSlideShow()
{
	if(oneNext == true)
	{
		if(NextPressedCounter == -1 && PrevPressedCounter == -1)
		{
			oneNext = false;
			StopNextPrev();
			return;
		}
	}
	else if(onePrev == true)
	{
		if(PrevPressedCounter == -1 && NextPressedCounter == -1)
		{
			onePrev = false;
			StopNextPrev();
			return;
		}
	}
	else if(timer == false)
	{
		return;
	}


	if( slideTimer >= (slideDuration*1000) )
	{
   		var tmp = 0;
   		if(PrevPressedCounter == -1)
		{
			tmp = parseInt(j + 1);
			if(tmp > (p-1))
			{
   				tmp=1;
   			}
   		}
   		else
   		{
   			if(j >= 1)
				tmp = parseInt(j - 1);
			if(j < 1)
				tmp = parseInt(parseInt(p-1) - j);
   		}
   		
   		
   				
		if (fade == "out")
		{
			if(FadeStatus == "On")
			{
				if(IsImageCached[tmp] == true)
				{
					transition(fadeTimer-=10);
				}
			}
			else
			{
				if(IsImageCached[tmp] == true)
				{
					fadeTimer-=10;
					transition(100);
				}
			}
		}
		if (fadeTimer <= 0)
		{
			if(IsImageCached[tmp] == true)
			{
				document.images.SlideShow.src = Blank.src;

   				if(PrevPressedCounter == -1)
				{
					j = parseInt(j + 1);
					if(j > (p-1))
					{
   						j=1;
   						
   						if(traverseAlbum == true)
   						{
   							clearTimeout(t);
							timer = false;
   							location.replace(strNextAmbumPage + '-' + document.getElementById(txtDuration).value);
   							return;
   						}
   					}
   				}
   				else
   				{
   				   	if(j >= 1)
						j = parseInt(j - 1);
					if(j < 1)
					{
						j = parseInt(parseInt(p-1) - j);
						
						if(traverseAlbum == true)
						{
							clearTimeout(t);
							timer = false;
							location.replace(strPrevAmbumPage + '-' + document.getElementById(txtDuration).value);
							return;
						}
					}
   				}
   				
   				if(document.getElementById(txtDuration) != null)
   				{
   					var slideDurationTemp = document.getElementById(txtDuration).value;
   					
   					if(isNaN(slideDurationTemp))
   					{
   						document.getElementById(txtDuration).value = slideDuration;
   					}
   					else
   					{
   						if(slideDurationTemp == 0)
   						{
   							slideDuration = 3;
   							document.getElementById(txtDuration).value = slideDuration;
   						}
   						else
   						{
   							if(NextPressedCounter == -1 && PrevPressedCounter == -1)
   							{
   								slideDuration = document.getElementById(txtDuration).value;
   							}
   						}
   					}
   				}
		   		
   				document.images.SlideShow.src = imgArray[j].src;
   				document.getElementById(spanCaption).innerHTML = Caption[j];
   				
   				if(parseInt(p-1) < 10)
   					document.getElementById('spanCounter').innerHTML = j + '/' + parseInt(p-1);
   				else if(parseInt(p-1) >= 10 && parseInt(p-1)< 100 )
   				{
   					if(j < 10)
   						document.getElementById('spanCounter').innerHTML = '0' + j + '/' + parseInt(p-1);
   					else
   						document.getElementById('spanCounter').innerHTML = j + '/' + parseInt(p-1);
   				}
   				else if(parseInt(p-1) >= 100 && parseInt(p-1)< 1000 )
   				{
   					if(j < 10)
   						document.getElementById('spanCounter').innerHTML = '00' + j + '/' + parseInt(p-1);
   					else if(j >= 10 && j < 100)
   						document.getElementById('spanCounter').innerHTML = '0' + j + '/' + parseInt(p-1);
   					else
   						document.getElementById('spanCounter').innerHTML = j + '/' + parseInt(p-1);
   				}
   				else if(parseInt(p-1) >= 1000 && parseInt(p-1)< 10000 )
   				{
   					if(j < 10)
   						document.getElementById('spanCounter').innerHTML = '000' + j + '/' + parseInt(p-1);
   					else if(j >= 10 && j < 100)
   						document.getElementById('spanCounter').innerHTML = '00' + j + '/' + parseInt(p-1);
   					else if(j >= 100 && j < 1000)
   						document.getElementById('spanCounter').innerHTML = '0' + j + '/' + parseInt(p-1);
   					else
   						document.getElementById('spanCounter').innerHTML = j + '/' + parseInt(p-1);
   				}
   				else
   					document.getElementById('spanCounter').innerHTML = j + '/' + parseInt(p-1);

   				PrepareNext(j+1);
   				PreparePrev(j-1);
	   			
				fade = "in";
			}
		}
		if (fade == "in")
		{
			if(FadeStatus == "On")
			{
				transition(fadeTimer+=10);
			}
			else
			{
				fadeTimer+=10;
				transition(100);
			}
		}
		if (fadeTimer >= 100)
		{
			slideTimer = 0;
			fade = "out";
			
			//For Reset Next Event
			if(NextPressedCounter != -1)
			{
				NextPressedCounter = -1;
			}
			//For Reset Prev Event
			if(PrevPressedCounter != -1)
			{
				PrevPressedCounter = -1;
			}
		}
	}
	else if (slideTimer < (slideDuration*1000) && fade == "in" && fadeTimer < 100)
	{
		if(FadeStatus == "On")
		{
			transition(fadeTimer+=10);
		}
		else
		{
			fadeTimer+=10;
			transition(100);
		}
	}
	else if (fadeTimer >= 100 && fade == "in")
	{
		slideTimer = 0;
		fade = "out";
	}
	else
	{
		slideTimer+=10;
	}
	
	t = window.setTimeout("runSlideShow()",10);
}


function transition(opacity) 
{
	if (opacity != 100) 
	{
		document.getElementById(slideImg).style.opacity = (opacity/100);
	}
	else 
	{
		document.getElementById(slideImg).style.opacity = (.99);
	}
	// only affects Win IE
	document.getElementById(slideImg).style.filter = "alpha(opacity="+opacity+")";
}

function detectKey(e)
{
    if (!e) {
        e = window.event;
    }
    if (e.keyCode == 37) { // arrow left
        Prev();
    }
    if (e.keyCode == 39) { // arrow right
        Next();
    }
    if (e.keyCode == 80 && NextPressedCounter == -1 && PrevPressedCounter == -1 ) { // 'p' or 'P'
        if(timer == true)
		{
			Stop();
		}
		else
		{
			Play();
		}
    }
    return false;
}


document.onkeyup = detectKey;

function Play()
{
	if(PrevPressedCounter == -1 && NextPressedCounter == -1)
	{
		if(showStop == 'True')
			document.getElementById(linkStop).innerHTML = "<a href='javascript: Stop();'>" +textStop+ "</a>";	// If stop then show option "play"
		if(showPlay == 'True')
			document.getElementById(linkPlay).innerHTML = textPlay;	// If playing then show option "stop"

		timer = true;
		runSlideShow();
	}
}


function StopNextPrev()
{
	if(timer == true)
	{
		slideTimer = 0;
		fadeTimer = 100;
		fade = "out";
		transition(100);
		
		clearTimeout(t);
		if(showStop == 'True')
			timer = false;
		if(showPlay == 'True')
			timer = true;
	}
}


function Stop()
{
	if(PrevPressedCounter == -1 && NextPressedCounter == -1)
	{
		if(timer == true)
		{
			slideTimer = 0;
			fadeTimer = 100;
			fade = "out";
			transition(100);
			
			clearTimeout(t);
			timer = false;

			if(showStop == 'True')
				document.getElementById(linkStop).innerHTML = textStop;	// If stop then show option "play"
			if(showPlay == 'True')
				document.getElementById(linkPlay).innerHTML = "<a href='javascript: Play();'>"+textPlay+"</a>";	// If playing then show option "stop"
		}
		else
		{
			clearTimeout(t);
			timer = false;
		}
	}
}

function Prev()
{
	if(PrevPressedCounter == -1 && NextPressedCounter == -1)
    {
		slideDuration = 1;

		PrevPressedCounter = j;
		NextPressedCounter = -1;
		
		if(timer == false)
		{
			oneNext = false;
			onePrev = true;
			runSlideShow();
		}
	}
}


function Next()
{
	if(NextPressedCounter == -1 && PrevPressedCounter == -1)
    {
		slideDuration = 1;

		NextPressedCounter = j;
		PrevPressedCounter = -1;
		
		if(timer == false)
		{
			oneNext = true;
			onePrev = false;
			runSlideShow();
		}
	}
}
