﻿

//var arrFeatureImage = new Array();
var lNewImage = 0;
var lNewStart = 0;
var lFade = 500;

var tid = setInterval('RotateFeature(true);', 5000);

//var txt = 'Photo-Home-1.jpg,Photo-Home-2.jpg,Photo-Home-3.jpg,Photo-Home-4.jpg,Photo-Home-5.jpg,Photo-Home-6.jpg,Photo-Home-7.jpg,Photo-Home-8.jpg';
var txt;// = document.getElementById("hidImages").value;

var arrFeatureImage;// = txt.split(',');

//arrFeatureImage.push("Photo-Home-1.jpg")
//arrFeatureImage.push("Photo-Home-2.jpg")
//arrFeatureImage.push("Photo-Home-3.jpg")
//arrFeatureImage.push("Photo-Home-4.jpg")
//arrFeatureImage.push("Photo-Home-5.jpg")
//arrFeatureImage.push("Photo-Home-6.jpg")
//arrFeatureImage.push("Photo-Home-7.jpg")
//arrFeatureImage.push("Photo-Home-8.jpg")


// Preload the next image
//PreLoadNewImage()

// populate on body load
function GetImages() {
    txt = document.getElementById("hidImages").value;
    txt = txt.replace("&amp;","&")
    arrFeatureImage = txt.split(',');
    PreLoadNewImage()
    
}

// =====================================================================================

function PreLoadNewImage()
{

	// Calculate the next image
	SetNewImage()

	// Preload it
	var objImage = new Image(535, 248);
	//objImage.src = '/Images/Content/' + arrFeatureImage[lNewImage];
	objImage.src = arrFeatureImage[lNewImage];

	RecalculateStart();
}

// =====================================================================================

function SetNewImage()
{

	// Calculate the next image
	lNewImage = lNewImage + 1;

	// If it's past the end reset to 1
	if (lNewImage == arrFeatureImage.length) lNewImage = 0;

}

// =====================================================================================

function SetRandomNewImage()
{

	// Calculate the next image
	lNewImage = Math.round(Math.random() * (arrFeatureImage.length - 1));

	// See if it is the same as the one that we are already showing
	while ($("#FeatureImage").attr("src") == arrFeatureImage[lNewImage])
	{
		// It is, pick another
		lNewImage = Math.round(Math.random() * (arrFeatureImage.length - 1));
	}

}

// =====================================================================================

function RotateFeature(vbPreLoad)
{

	// Fade it out
	$("#FeatureImage").fadeOut(lFade, function () {
		// Change the image
		$("#FeatureImage").attr("src",  arrFeatureImage[lNewImage]);

		// And fade it in
		$("#FeatureImage").fadeIn(lFade);

		for (lIndex = lNewStart; lIndex <= (lNewStart + 4); lIndex++)
		{
			$("#FeatureLink" + (lIndex - lNewStart + 1)).html(lIndex + 1);

			if (lIndex == lNewImage)
			{
				$("#FeatureLink" + (lIndex - lNewStart + 1)).parent().attr("class", "Current");
			}
			else
			{
				$("#FeatureLink" + (lIndex - lNewStart + 1)).parent().attr("class", "");
			}
		}

		// Preload the next image
		if (vbPreLoad) PreLoadNewImage();
	});
}

// =====================================================================================

function ShowImage(vlIndex)
{
	lFade = 200;

	StopTheInterval();

	lNewImage = lNewStart + vlIndex - 1;

	RecalculateStart();

	RotateFeature(false)

	return false;
}

// =====================================================================================

function ChangeImage(vlBy)
{

	lFade = 200;

	StopTheInterval();

	AdjustImage(vlBy);

	RecalculateStart();

	RotateFeature(false)

	return false;
}

// =====================================================================================

function RecalculateStart()
{
	lAdjustment = 0;

	if (lNewImage - (lNewImage % 5) + 4 > arrFeatureImage.length)
	{
		lAdjustment = 1 + (lNewImage - (lNewImage % 5) + 4) - arrFeatureImage.length ;
	}

	lNewStart = (lNewImage - lAdjustment - (lNewImage % 5));

}

// =====================================================================================

function AdjustImage(vlBy)
{

	lNewImage = lNewImage + vlBy;

	if (lNewImage == -1) lNewImage = arrFeatureImage.length - 1;
	if (lNewImage == arrFeatureImage.length) lNewImage = 0;

}

// =====================================================================================

function StopTheInterval()
{

	if (tid)
	{
		clearInterval(tid);

		tid = null;

		AdjustImage(-1);
	}

}
