/*
 * Written by Per Larsson 2011-01-12
 */
jQuery.fn.rotateImages = function() {
	var div = this;
	this.timeout;
	/*Passed arguments*/
	var args = arguments[0] || {};
	var imageClass = args.imageClass;
	var interval = args.interval;
	var random = args.random;
	var showNumbers = args.showNumbers;
	
	if (imageClass == null)
		imageClass = "rotImage";
	if (interval == null)
		interval = 2500;
	if (random == null)
		random = false;
	if (showNumbers == null)
		showNumbers = false;

	var imgLength = $(this).find("." + imageClass).size();
	
	if (showNumbers && imgLength > 1) {
		var w = $(this).width();
		var h = $(this).height();
		$(this).css({
			position: "relative"
		});
		$t = $(this);
		
		for (var i = 0; i < imgLength; i++)
			$("<div class=\"imgNumber\"><div style=\"margin: 1px 3px;\">" + (i + 1) + "</div></div>").css({
				position: "absolute",
				left: w - imgLength * 20 + i * 20,
				top: h - 20,
				background: i != 0 ? "white" : "#febd10",
				border: "1px solid black",
				fontSize: 10,
				fontWeight: "bold",
				cursor: "pointer",
				color: "black"
			}).click(function() {
				if (!$(this).hasClass("img-active") && !$(this).parent().find(".rotImage:visible").is(":animated")) {
					clearTimeout(div.timeout);
					div.timeout = setTimeout(function() {
						startRotate([div, imageClass, interval, random, showNumbers]);
					}, interval);
					div.doRotation({
						imageClass: imageClass,
						interval: interval,
						random: random,
						showNumbers: showNumbers,
						imgNumber: parseInt($(this).text()) - 1
					});
				}
			}).appendTo(this);
		$(this).find(".imgNumber:first").addClass("img-active");
	}
	if (imgLength > 1 && interval > 0)
		div.timeout = setTimeout(function() {
			startRotate([div, imageClass, interval, random, showNumbers]);
		}, interval);
};

jQuery.fn.doRotation = function(args) {
	
	
	var args = arguments[0] || {};
	
	var imageClass = args.imageClass;
	var interval = args.interval;
	var random = args.random;
	var showNumbers = args.showNumbers;
	var imgNumber = args.imgNumber;
	
	if (imageClass == null)
		imageClass = "rotImage";
	if (interval == null)
		interval = 2500;
	if (random == null)
		random = false;
	if (showNumbers == null)
		showNumbers = false;
	
	var l = $(this).find("." + imageClass).size();
	var eq = 0;
	
	eq = $(this).find("." + imageClass).index($(this).find("." + imageClass + ":visible"));
	var lastEq = eq;
	if (eq == -1)
		eq = 0;
	
	$(this).find(".imgNumber").css({
		zIndex: 3
	}).removeClass("img-active");
	if (l > 1 && eq != -1 && imgNumber == null) {
		if (random) {
			eq = Math.round(Math.random() * (l - 1));
			//$(this).closest(".innerContent").append(eq);
		}
		if (lastEq != eq + 1) {
			$imgNumber = $(this).find(".imgNumber");
			$imgNumber.css({
				background: "white"
			});
			if ((eq + 1) < l) {
				$imgNumber.filter(":eq(" + (eq + 1) + ")").css({
					background: "#febd10"
				}).addClass("img-active");
				$(this).find("." + imageClass + ":eq(" + (eq + 1) + ")").css({
					zIndex: 1
				}).show();
			}
			else {
				$imgNumber.filter(":eq(0)").css({
					background: "#febd10"
				}).addClass("img-active");
				$(this).find("." + imageClass + ":eq(0)").css({
					zIndex: 1
				}).show();
			}
			$(this).find("." + imageClass + ":eq(" + lastEq + ")").css({
				zIndex: 2
			}).fadeOut(500);
		}
	}
	else if (imgNumber != null) {
		$imgNumber = $(this).find(".imgNumber");
		$imgNumber.css({
			background: "white"
		});
		if (imgNumber < l) {
			//alert(imgNumber+" "+l+" "+$imgNumber.size());
			$imgNumber.filter(":eq(" + imgNumber + ")").css({
				background: "#febd10"
			}).addClass("img-active");
			$(this).find("." + imageClass + ":eq(" + imgNumber + ")").css({
				zIndex: 1
			}).show();
		}
		else {
			$imgNumber.filter(":eq(0)").css({
				background: "#febd10"
			}).addClass("img-active");
			$(this).find("." + imageClass + ":eq(0)").css({
				zIndex: 1
			}).show();
		}
		$(this).find("." + imageClass + ":eq(" + lastEq + ")").css({
			zIndex: 2
		}).fadeOut(500);
	}
};

function startRotate(args) {
	var obj			= args[0];
	var imageClass	= args[1];
	var interval	= args[2];
	var random		= args[3];
	var showNumbers = args[4];
	obj.doRotation({
		imageClass: imageClass,
		interval: interval,
		random: random,
		showNumbers: showNumbers
	});
	if (interval > 0)
		obj.timeout = setTimeout(function() {
			startRotate(args);
		}, interval);
}
