/* Copyright (c) 2008 Kean Loong Tan http://www.gimiti.com/kltan
* Licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
* jFlow
* Version: 1.1 (May 22, 2008)
* Requires: jQuery 1.2+
*/

(function (A) {
	A.fn.jFlow = function (D) {
		var E = A.extend({}, A.fn.jFlow.defaults, D);
		var F = 0;
		var stopRotate = 0; // added to allow automatic rotation of slides
		var B = A(".jFlowControl").length;
		A(this).find(".jFlowControl").each(function (G) {
			A(this).click(function () {
				stopRotate = 1;
				A(".jFlowControl").removeClass("jFlowSelected");
				A(this).addClass("jFlowSelected");
				var H = Math.abs(F - G);
				A(E.slides).animate({
					marginLeft: "-" + (G * A(E.slides).find(":first-child").width() + "px")
				}, E.duration * (H));
				F = G
			})
		});
		A(E.slides).before('<div id="jFlowSlide"></div>').appendTo("#jFlowSlide");
		A(E.slides).find("div").each(function () {
			A(this).before('<div class="jFlowSlideContainer"></div>').appendTo(A(this).prev())
			//alert(A(this).find("img").height());
		});
		A(".jFlowControl").eq(F).addClass("jFlowSelected");
		var C = function (G) {
			A("#jFlowSlide").css({
				position: "relative",
				width: E.width,
				height: E.height,
				overflow: "hidden"
			});
			A(E.slides).css({
				position: "relative",
				width: A("#jFlowSlide").width() * A(".jFlowControl").length + "px",
				height: A("#jFlowSlide").height() + "px",
				overflow: "hidden"
			});
			A(E.slides).children().css({
				position: "relative",
				width: A("#jFlowSlide").width() + "px",
				height: A("#jFlowSlide").height() + "px",
				"float": "left"
			});
			A(E.slides).css({
				marginLeft: "-" + (F * A(E.slides).find(":first-child").width() + "px")
			})
		};
		C();
		A(window).resize(function () {
			C()
		});

		// Autorotation av slider var 8:e sekund
		if (E.autoFlow == "true") {
			$(document).ready(function() {
				$.timer(5000, function (timer) {
					if (stopRotate == 0) {
						(F < B - 1) ? F++ : F = 0 ;
/*						if(F < B - 1){
							F++;
						}else{
							F = 0;
						}*/
						$(".jFlowControl").removeClass("jFlowSelected");
						$(E.slides).animate({
							marginLeft: "-" + (F * $(E.slides).find(":first-child").width() + "px")
						}, E.duration);
						$(".jFlowControl").eq(F).addClass("jFlowSelected");
					} else {
						timer.stop();
					}
				});
			});
		}
	};
	A.fn.jFlow.defaults = {
		easing: "swing",
		duration: 400,
		width: "100%"
	}
})(jQuery);

