﻿var imgCount = 0, iCurrImg, evt;
var imagePrefix = "ssimg";
var iTotItems, iTopItem, iLastItem, iShowItems = 4;

function showAnimation()
{	
	var sImg1, sImg2;
	
	// if the current image is lower than the last image then we've gone too far, we need to loop back to the first image
	if(iCurrImg < 1)
	{
		iCurrImg = imgCount;
	}	
	
	// set the current image  (the one we're about to fade out)
	sImg1 = document.getElementById(imagePrefix + iCurrImg);
	
	
	// set the new image (the one we're about to fade in)
	if (document.getElementById(imagePrefix + (iCurrImg-1)))
	{
		sImg2 = document.getElementById(imagePrefix + (iCurrImg-1));
	}
	else
	{
		// if we get to the first image, go back to the first one
		sImg2 = document.getElementById(imagePrefix + imgCount);
	}
	
	// fade the images
	Effect.Fade(sImg1, { duration: 7.0});
	Effect.Appear(sImg2, { duration: 7.0});
		
	iCurrImg -= 1;		
			
	evt = setTimeout("showAnimation()", 7000);
}

function changeImg(url)
{
	clearTimeout(evt);
	
	var thisImg = document.getElementById(imagePrefix  + iCurrImg);

	// set the new image (the one we're about to fade in)
	if (document.getElementById(imagePrefix + (iCurrImg+1)))
	{
		sImg2 = document.getElementById(imagePrefix + (iCurrImg+1));
		iCurrImg++;
	}
	else
	{
	// if we get to the first image, go back to the first one
		sImg2 = document.getElementById(imagePrefix + 1);
		iCurrImg = 1;
	}
	
	if (thisImg.src.match(url) == null)	// If this is a new image then fade the old one out and fade the new one in
	{
		sImg2.src = url.replace("/thumbs", "");
		Effect.Fade(thisImg, { duration: 1.0});
		Effect.Appear(sImg2, { duration: 1.0});
	}		
}

function pauseAnimation()
{

}

function unpauseAnimation()
{

}

function imagePreloader()
{
     imageObj = new Image();

     var images = document.getElementById("ctl00_MainImageBox_loadimages").innerHTML;
	 var imageArray = images.split("|");
     
     for(i=1; i<imageArray.length; i++)
     {
          imageObj.src = "/uploadedimages/slideshow/" + imageArray[i];
     }
}

function StoreGalleryItems()
{
	var aItems = document.getElementById("ctl00_MainImageBox_imagelist").getElementsByTagName("li");
	
	/*if (document.getElementById("ctl00_contentPL_galitem1"))
	{
		document.getElementById("galleryimages").style.display = "block";
	}*/
	
	iTotItems = aItems.length;
	iTopItem = iTotItems;
	iLastItem = iTopItem - iShowItems;
		
	if (iTotItems > iShowItems)
	{
		document.getElementById("moveupbtn").style.display = "block";
		document.getElementById("movedownbtn").style.display = "block";
	}
	else
	{
	    document.getElementById("ctl00_MainImageBox_imagelist").style.marginTop = "16px";
	}
	/*else
    {
        document.getElementById("moveupbtn").href = "javascript:MoveUp();";
		document.getElementById("movedownbtn").href = "javascript:MoveDown();";
	}*/
	
	for (var i=4; i < iTopItem; i++) 
	{		
		aItems[i].style.display = 'none';		
	}
}

function MoveUp()
{		
	if (iTopItem > iShowItems)
	{			
		var firstItem = document.getElementById("image" + iTopItem);
		var lastItem = document.getElementById("image" + iLastItem);

		if (firstItem)
		{
			firstItem.style.display = 'none';
		}
		if (lastItem)
		{
			lastItem.style.display = 'block';
		}

		iTopItem--;
		iLastItem--;				
	}
}		

function MoveDown()
{	    
	if (iLastItem < (iTotItems - iShowItems))
	{	
		var firstItem = document.getElementById("image" + (iTopItem + 1));
		var lastItem = document.getElementById("image" + (iLastItem + 1));

		if (firstItem)
		{
			firstItem.style.display = 'block';
		}
		if (lastItem)
		{
			lastItem.style.display = 'none';
		}

		iTopItem++;
		iLastItem++;				
	}	
}

function AddOnClickEvents()
{
	var aLinks = document.getElementById("ctl00_MainImageBox_imagelist").getElementsByTagName("a");		
		
	for (var i=0; i<aLinks.length; i++) 
	{						
		var aTempImg = aLinks[i].getElementsByTagName("img");

		aLinks[i].href = "javascript:changeImg('" + aTempImg[0].src.replace('46x46/','') + "')";						
	}	
}