/** Configurable parameters **/

var numberOfCatalogsDisplayed = 5;		/* Number of catalogs displayed at any point in time */
var directionOfRotation = "horizontal";		/* Direction of rotation : horizontal, vertical*/
var scrollCount = 1;				/* Number of items shifted out of view for each navigation click */
var atEnd = "loop";                             /* What to do after reaching end? "loop", "startover", "nothing" */
var animationEffect = "scroll";			/* Specifies which animation to use  - "scroll', "none"*/
var animationSpeed = 1500;				/* Speed, in milliseconds, of the animation */
var autoRotationTimer = 000;			/* Autorotation timer, in milliseconds */
var autoRotationDir = "right";			/* Which direction to do autorotation? "right", "left", "top" or "bottom" */


/* Other JS variables */
var catalogCount = 0;				/* Total number of catalogs */
var itemObjects = [];				/* Array of all items in the carousel */
var indicesInCurrentView = [];			/* The list of indices in the current view */
var marginLeftForEachItem = 0; 			/* Margin by which items get shifted in the left-scroll */
var marginTopForEachItem = 0;			/* Margin by which items get shifted in the top -scroll  */
var baseValueForMarginLeft = 0;			/* MarginLeft for each item */
var autoScrollHalted = "false";			/* for halt-resume of auto scroll */
var showLogAsAlert = "true";


function browerChecks()
{
	if(!Array.indexOf){
		    Array.prototype.indexOf = function(obj){
		        for( i=0; i<this.length; i++){
		            if(this[i]==obj){
		                return i;
		            }
		        }
		        return -1;
		    }
	}

}

function Log(msg)
{
	if(showLogAsAlert == "true")
		alert(msg);
}

function checkConfigurableParams()
{


	if(numberOfCatalogsDisplayed > catalogCount)
	{
		Log("Number of catalogs to be displayed at one time must be at most the total number of catalogs");
		numberOfCatalogsDisplayed = catalogCount;
	}

	if((directionOfRotation != "vertical") && (directionOfRotation != "horizontal"))
	{
			Log("Incorrect direction of rotation. Assuming 'horizontal'");
			directionOfRotation = "horizontal";
	}
	if(scrollCount <= 0)
	{
		Log("scrollcount must be a positive integer > 0");
		scrollCount = 1;
	}
	if((animationEffect != "scroll") && (animationEffect != "none"))
	{
		Log("Supported animationEffects are 'scroll' and 'none'. Defaulting to none");
		animationEffect = "none";
	}
	if(animationSpeed < 0)
	{
		Log("Animation speed must be non-negative integer");
		animationSpeed = 0;
	}
	if(autoRotationTimer < 0)
	{
		Log("autorotation timer must be non-negative integer");
		autoRotationTimer = 0;
	}
	if((autoRotationDir != "top") && (autoRotationDir != "bottom") && (autoRotationDir != "left") && (autoRotationDir != "right"))
	{
		Log("autorotiationdir must be one of - top, bottom, left, right");
		autoRotationTimer = 0;
		autoRotationDir = "";
	}
	if((atEnd != "loop")&&(atEnd != "startover")&&(atEnd != "nothing"))
	{
		Log("valid values for atEnd are - loop, startover, nothing. defaulted to 'nothing'");
		atEnd = "nothing";

	}



	if((directionOfRotation == "horizontal") && (autoRotationDir != "left") && (autoRotationDir != "right"))
	{
		autoRotationDir = "";
		autoRotationTimer = 0;
	}


	if((directionOfRotation == "vertical") && (autoRotationDir != "top") && (autoRotationDir != "bottom"))
	{
		autoRotationDir = "";
		autoRotationTimer = 0;
	}


}




function setInitialStateOfIndices()
{
	//set the initial set of visible indices
	var cindex = 0;
	indicesInCurrentView = [];
	for(var i = 0; i < numberOfCatalogsDisplayed; ++i)
	{
		if(cindex == catalogCount) { if(atEnd == "loop") cindex = 0; else break; }
		indicesInCurrentView.push(cindex);
		cindex+=1;
	}

}


function setInitialDisplay()
{


	$("#catalog > ul").empty();

	for(var i = 0; i < indicesInCurrentView.length; ++i)
		$("#catalog > ul").append(itemObjects[indicesInCurrentView[i]]);

}



function haltAutoScroll()
{
	autoScrollHalted = "true";
}

function resumeAutoScroll()
{

	autoScrollHalted = "false";
}


function checkForAutoRotation()
{

	if(autoRotationTimer != 0)
	{
		switch(autoRotationDir)
		{
			case "right":
				setTimeout(scrollRight, autoRotationTimer);
				break;
			case "left":
				setTimeout(scrollLeft, autoRotationTimer);
			case "top":
				setTimeout(scrollTop, autoRotationTimer);
				break;
			case "bottom":
				setTimeout(scrollBotton, autoRotationTimer);
			break;
		}
	}

}


function init()
{
	//Record the HTML Objects for all the list items



	itemObjects = $("#catalog > ul > li").get();
	catalogCount = itemObjects.length;


	if(catalogCount == 1) return;
	checkConfigurableParams();
	marginLeftForEachItem = $(itemObjects[0]).css("marginLeft");
	marginTopForEachItem = $(itemObjects[0]).css("marginTop");

	checkForAutoRotation();





	//initial state of indices
	setInitialStateOfIndices();

	//initialize the display
	setInitialDisplay();



}


function animateToLeft(indicesToBeRemoved, indicesToBeAdded, effect)
{


	while(indicesToBeAdded.length > 0)
	{
		var ind = indicesToBeAdded.pop();

		var marginLeft =  -200;
		$("#catalog > ul").	prepend(itemObjects[ind]);
		$(itemObjects[ind]).css("marginLeft", marginLeft);

		if(effect == "scroll")
		{
			$("#catalog > ul").find("li").each(
				function () {

					$(this).animate({marginLeft: marginLeftForEachItem}, animationSpeed);

			});

			setTimeout(removeNodes,animationSpeed);

		}
		else if(effect == "none")
		{
				$("#catalog > ul").find("li").each(
						function () {
							$(this).css("marginLeft",marginLeftForEachItem);
						});


				removeNodes();
		}


	}


	function removeNodes()
	{
		clearTimeout();

		while(indicesToBeRemoved.length > 0)
		{
			var ind = indicesToBeRemoved.pop();
			$(itemObjects[ind]).remove();
		}
	}

}






function animateToRight(arr, effect)
{

	if(arr.length == 0) return;

	var ind = arr.shift();

	if(effect == "scroll")
	{
		var origValue = $(itemObjects[ind]).css("marginLeft");

		var marginLeftValue = - 132;
		$(itemObjects[ind]).animate({marginLeft: marginLeftValue}, animationSpeed, function() {
				$(itemObjects[ind]).css("marginLeft", origValue);
				$(itemObjects[ind]).remove();
				animateToRight(arr, effect);
		});
	}
	else if(effect == "none")
	{
		$(itemObjects[ind]).remove();
		animateToRight(arr, effect);
	}

}


function animateToBottom(arr,effect)
{


	if(arr.length == 0) return;

		var ind = arr.shift();

		if(effect == "scroll")
		{
			var origValue = $(itemObjects[ind]).css("marginTop");

			var marginTopValue = -132;
			$(itemObjects[ind]).animate({marginTop: marginTopValue}, animationSpeed, function() {
					$(itemObjects[ind]).css("marginTop", origValue);
					$(itemObjects[ind]).remove();
					animateToBottom(arr, effect);
			});
		}
		else if(effect == "none")
		{
			$(itemObjects[ind]).remove();
			animateToBottom(arr, effect);
	}



}


function animateToTop(indicesToBeRemoved, indicesToBeAdded, effect)
{


	while(indicesToBeAdded.length > 0)
	{
		var ind = indicesToBeAdded.pop();

		var marginTop =  -130;
		$("#catalog > ul").	prepend(itemObjects[ind]);
		$(itemObjects[ind]).css("marginTop", marginTop);

		if(effect == "scroll")
		{
			$("#catalog > ul").find("li").each(
				function () {

					$(this).animate({marginTop: marginTopForEachItem}, animationSpeed);

			});

			setTimeout(removeNodes,animationSpeed);

		}
		else if(effect == "none")
		{
				$("#catalog > ul").find("li").each(
						function () {
							$(this).css("marginTop",marginTopForEachItem);
						});


				removeNodes();
		}


	}


	function removeNodes()
	{
		clearTimeout();

		while(indicesToBeRemoved.length > 0)
		{
			var ind = indicesToBeRemoved.pop();
			$(itemObjects[ind]).remove();
		}
	}

}




function scrollLeft(who)
{
	var indicesInNewView = [];


	if(who == "click")
	{
		autoRotationTimer = 0;
	}
	else if(autoRotationTimer == 0)
	{

		return;
	}

	clearTimeout();
	checkForAutoRotation();

	if(autoScrollHalted == "true")
		return;

	if((indicesInCurrentView[0] == 0) && (atEnd != "loop"))
		return;

	else
	{
		for(var i = 0; i <indicesInCurrentView.length ; ++i)
		{
			var ind = indicesInCurrentView[i];
			ind = ind - scrollCount;
			while(ind < 0) ind += catalogCount;
			if((ind == catalogCount) && (atEnd != "loop")) break;
			indicesInNewView.push(ind);

		}
	}

	var indicesToBeRemoved = [];

	for(var i = 0; i < indicesInCurrentView.length; ++i)
	{
		var ind = indicesInCurrentView[i];
		if(indicesInNewView.indexOf(ind) == -1)
		//if(contains(indicesInNewView, ind) == false)										//1
		{
			indicesToBeRemoved.push(ind);
		}
	}


	//the last index in this array should be added first
	var indicesToBeAdded = [];

	for(var i = 0; i < indicesInNewView.length-1; ++i)
	{
		var ind = indicesInNewView[i];
		if(indicesInCurrentView.indexOf(ind) == -1)
		//if(contains(indicesInCurrentView, ind) == false)									//2
		{

			indicesToBeAdded.push(ind);
		}
	}



	animateToLeft(indicesToBeRemoved, indicesToBeAdded, animationEffect);
	indicesInCurrentView = indicesInNewView;

}




function scrollRight(who)
{

	var indicesInNewView = [];



	if(who == "click")
	{
		autoRotationTimer = 0;
	}
	else if(autoRotationTimer == 0)
	{

			return;
	}


	clearTimeout();
	checkForAutoRotation();

	if(autoScrollHalted == "true")
		return;

	if((indicesInCurrentView[indicesInCurrentView.length-1] == catalogCount -1) && (atEnd == "nothing"))
		return;

	if((indicesInCurrentView[indicesInCurrentView.length-1] == catalogCount -1) && (atEnd == "startover"))
	{
		setInitialStateOfIndices();

		setInitialDisplay();
		return;
	}
	else
	{
		for(var i = 0; i < indicesInCurrentView.length; ++i)
		{
			var ind = indicesInCurrentView[i];
			ind = ind + scrollCount;
			while(ind >= catalogCount) ind -= catalogCount;
			if((ind == 0) && (atEnd != "loop")) break;
			indicesInNewView.push(ind);

		}
	}

	var indicesToBeRemoved = [];
	for(var i = 0; i < indicesInNewView.length; ++i)
	{
		var ind = indicesInNewView[i];


		if(indicesInCurrentView.indexOf(ind) == -1)
		//if(contains(indicesInCurrentView, ind) == false)									//3
		{

				$("#catalog > ul").append(itemObjects[ind]);

		}


	}

	for(var i = 0; i < indicesInCurrentView.length; ++i)
	{
		var ind = indicesInCurrentView[i];
		if(indicesInNewView.indexOf(ind) == -1)
		//if(contains(indicesInCurrentView, ind) == false)									//4
		{
			indicesToBeRemoved.push(ind);
		}
	}




	animateToRight(indicesToBeRemoved, animationEffect);
	indicesInCurrentView = indicesInNewView;




}





function scrollTop(who)
{
	var indicesInNewView = [];


	if(who == "click")
	{
		autoRotationTimer = 0;
	}
	else if(autoRotationTimer == 0)
	{

		return;
	}

	clearTimeout();
	checkForAutoRotation();

	if(autoScrollHalted == "true")
		return;

	if((indicesInCurrentView[0] == 0) && (atEnd != "loop"))
		return;

	else
	{
		for(var i = 0; i <indicesInCurrentView.length ; ++i)
		{
			var ind = indicesInCurrentView[i];
			ind = ind - scrollCount;
			while(ind < 0) ind += catalogCount;
			if((ind == catalogCount) && (atEnd != "loop")) break;
			indicesInNewView.push(ind);

		}
	}

	var indicesToBeRemoved = [];

	for(var i = 0; i < indicesInCurrentView.length; ++i)
	{
		var ind = indicesInCurrentView[i];
		if(indicesInNewView.indexOf(ind) == -1)
		//if(contains(indicesInNewView, ind) == false)										//1
		{
			indicesToBeRemoved.push(ind);
		}
	}


	//the last index in this array should be added first
	var indicesToBeAdded = [];

	for(var i = 0; i < indicesInNewView.length-1; ++i)
	{
		var ind = indicesInNewView[i];
		if(indicesInCurrentView.indexOf(ind) == -1)
		//if(contains(indicesInCurrentView, ind) == false)									//2
		{

			indicesToBeAdded.push(ind);
		}
	}



	animateToTop(indicesToBeRemoved, indicesToBeAdded, animationEffect);
	indicesInCurrentView = indicesInNewView;

}




function scrollBottom(who)
{

	var indicesInNewView = [];



	if(who == "click")
	{
		autoRotationTimer = 0;
	}
	else if(autoRotationTimer == 0)
	{

			return;
	}


	clearTimeout();
	checkForAutoRotation();

	if(autoScrollHalted == "true")
		return;

	if((indicesInCurrentView[indicesInCurrentView.length-1] == catalogCount -1) && (atEnd == "nothing"))
		return;

	if((indicesInCurrentView[indicesInCurrentView.length-1] == catalogCount -1) && (atEnd == "startover"))
	{
		setInitialStateOfIndices();

		setInitialDisplay();
		return;
	}
	else
	{
		for(var i = 0; i < indicesInCurrentView.length; ++i)
		{
			var ind = indicesInCurrentView[i];
			ind = ind + scrollCount;
			while(ind >= catalogCount) ind -= catalogCount;
			if((ind == 0) && (atEnd != "loop")) break;
			indicesInNewView.push(ind);

		}
	}

	var indicesToBeRemoved = [];
	for(var i = 0; i < indicesInNewView.length; ++i)
	{
		var ind = indicesInNewView[i];


		if(indicesInCurrentView.indexOf(ind) == -1)
		//if(contains(indicesInCurrentView, ind) == false)									//3
		{

				$("#catalog > ul").append(itemObjects[ind]);

		}


	}

	for(var i = 0; i < indicesInCurrentView.length; ++i)
	{
		var ind = indicesInCurrentView[i];
		if(indicesInNewView.indexOf(ind) == -1)
		//if(contains(indicesInCurrentView, ind) == false)									//4
		{
			indicesToBeRemoved.push(ind);
		}
	}




	animateToBottom(indicesToBeRemoved, animationEffect);
	indicesInCurrentView = indicesInNewView;




}



function Main()
{
	$("document").ready(
		function()
		{
			browerChecks();

			init();
			$("#catalog-next").click(
				function() {

					if(directionOfRotation == "horizontal")
						scrollRight("click");
					else if(directionOfRotation == "vertical")
						scrollBottom("click");

					return false;
				});

			$("#catalog-previous").click(

				function() {

					clickInProgress = "true";
					if(directionOfRotation == "horizontal")
						scrollLeft("click");
					else if(directionOfRotation == "vertical")
						scrollTop("click");

					return false;
				});

		$(itemObjects).each(function()
			{
				$(this).find("img").each(
				function()
				{
					$(this).hover( function() { haltAutoScroll(); }, function() { resumeAutoScroll(); });
				}
				);
			});

	});
}


Main();

