/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 * 
 *  Usage: Rotator.load()
 * 
 *  All items to rotate give class name "rotate"
 * 
 */

var Rotator = {};
Rotator._container = null;
Rotator._stillTimer = 5000;		//default
Rotator._fadeTimer = 1000;		//default
Rotator._activeItem = 0;
Rotator._nextItem = 1;
Rotator._interval = null;
Rotator.play = false;
Rotator._hasSwitcher = false;

Rotator.load = function(_containerId, _stillTimer, _fadeTimer, _createSwitcher) {
	// hide content if javascript is on
	Rotator._container = $("#" + _containerId);
	Rotator._stillTimer = _stillTimer;
	Rotator._fadeTimer = _fadeTimer;
	Rotator._hasSwitcher = _createSwitcher;
	$(Rotator._container)
		.css("position","relative")
		.children(".rotate").css({
			"position": "absolute",
			"display": "none"
		})
		.eq(0).css("display","block");
		
	if (Rotator._hasSwitcher == true) {
		Rotator.createSwitcher();
		$("#switcherId_0").addClass("active");
	}
	
	Rotator.play = true;
	Rotator._interval = setInterval("Rotator.rotate()", Rotator._stillTimer);
}

Rotator.rotate = function() {
	$(Rotator._container).children(".rotate").eq(Rotator._activeItem).fadeOut(Rotator._fadeTimer);
	$(Rotator._container).children(".rotate").eq(Rotator._nextItem).fadeIn(Rotator._fadeTimer,
		function() {
			var cnt = $(Rotator._container).children(".rotate").length - 1;
			Rotator._activeItem = Rotator._nextItem;
			if (Rotator._activeItem == cnt) {
				Rotator._nextItem = 0;
			}
			else {
				Rotator._nextItem++;
			}
			if (Rotator._hasSwitcher == true) {
				$(".switcher.active").removeClass("active");
				$("#switcherId_" + Rotator._activeItem).addClass("active");
			}
		}
	);
}

Rotator.jump = function(jumpToIndex) {
	if (jumpToIndex == Rotator._activeItem) {
		return;
	}
	Rotator._nextItem = jumpToIndex;
	clearInterval(Rotator._interval);
	Rotator.rotate();
	Rotator._interval = setInterval("Rotator.rotate()", Rotator._stillTimer);
}

Rotator.createSwitcher = function() {
	switcherContainer = $("<div class='switcherContainer'></div>");
	var cnt = $(Rotator._container).children(".rotate").length - 1;
	for (var i=0; i <= cnt; i++) {
		$(switcherContainer).append("<div id='switcherId_" + i + "' class='switcher' onclick='Rotator.jump(" + i + ")'>&nbsp;</div>");
	}
	$(Rotator._container).append(switcherContainer);
}





