/* 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.2 (July 7, 2008)
 * Requires: jQuery 1.2+
 */
 
(function($) {
		  
	$.fn.jFlow = function(options) {
		var opts = $.extend({}, $.fn.jFlow.defaults, options);
		var randNum = Math.floor(Math.random()*11);
		var jFC = opts.controller;
		var jFS =  opts.slideWrapper;
		var jSel = opts.selectedWrapper;
		var gc = opts.galcore;

		var cur = 0;
		var maxi = $(jFC).length;

		// sliding function
		var slide = function (dur, i, direction) {


				switch(direction){
					case 'next':
						// 1) включаем анимацию 
						// 2) в функции обратного вызова премещаем первый элемент в конц и обнуляем отступ!
						$(gc).animate({
							marginLeft: "-" + (i * $(gc).find(":first-child").width() + "px")}, 
							"slow", function(){
							    $("#myOwnCarousell li:first").insertAfter($("#myOwnCarousell li:last")); 
								$(gc).css('marginLeft', 0);
							}
						);
	      				break;
					case 'prev':
						// 1) втыкаем последний элемент перед первым 
						// 2) определяем поле (чтоб элемент был покачто не виден)
						// 3) включаем анимацию
					    $("#myOwnCarousell li:last").insertBefore($("#myOwnCarousell li:first"));
					    $(gc).css('marginLeft', "-" +  $(gc).find(":last-child").width())
						$(gc).animate({
							marginLeft: (0 + "px")}, 
							"slow"
						);
	      				break;
	      			default:
	      				break;
				}
					

		}
		$(this).find(jFC).each(function(i){
			$(this).click(function(){
				if ($(opts.slides).is(":not(:animated)")) {
					$(jFC).removeClass(jSel);
					$(this).addClass(jSel);
					var dur = Math.abs(cur-i);
					slide(dur,i);
					cur = i;
				}
			});
		});	

		$(opts.slides).before('<div id="'+jFS.substring(1, jFS.length)+'"></div>').appendTo(jFS);

		$(opts.slides).find("div").each(function(){
			//$(this).before('<div class="jFlowSlideContainer"></div>').appendTo($(this).prev());
		});

		//initialize the controller
		$(jFC).eq(cur).addClass(jSel);

		var resize = function (x){
			var divwidth = opts.itemwidth*$(jFC).length;
			
			$(jFS).css({
				position:"relative",
				width: opts.width,
				height: opts.height,
				overflow: "hidden"
			});
			//opts.slides or #mySlides container
			$(opts.slides).css({
				position:"relative",
				width: "100%", //$(jFS).width()+"px", //$(jFS).width()*$(jFC).length+"px",
				height: $(jFS).height()+"px",
				overflow: "hidden"
			});
			//#myOwnCarousell
			$(gc).css({
				position:"relative",
				width: divwidth+"px", //$(jFS).width()*$(jFC).length+"px",
				height: $(jFS).height()+"px",
				overflow: "visible"
			});
			//opts.slides_wrapper container
			$(opts.slides_wrapper).css({
				position:"relative",
				width: "100%", //$(jFS).width()+"px", //$(jFS).width()*$(jFC).length+"px",
				height: $(jFS).height()+"px",
				overflow: "visible"
			});
			// jFlowSlideContainer
			$(opts.slides).children().css({
				position:"relative",
				width: "100%", //$(jFS).width()+"px",
				height: $(jFS).height()+"px",
				"float":"left",
				overflow:"visible"
			});

			$(opts.slides).css({
				marginLeft: "-" + (cur * $(opts.slides).find(":eq(0)").width() + "px")
			});
		}

		// sets initial size
		resize();



		$(opts.prev).click(function(){
			if ($(opts.slides).is(":not(:animated)")) {
				var direction = 'prev';
				var dur = 1;
				if (cur > 0)
					cur--;
				else {
					cur = maxi -1;
					dur = cur;
				}
				$(jFC).removeClass(jSel);
				slide(dur,-1, direction);
				$(jFC).eq(cur).addClass(jSel);
			}
			//$("#myOwnCarousell li:last").insertBefore($("#myOwnCarousell li:first"));

		});
		
		$(opts.next).click(function(){
			if ($(opts.slides).is(":not(:animated)")) {			
				var direction = 'next';
				var dur = 1;
				if (cur < maxi - 1)
					cur++;
				else {
					cur = 0;
					dur = maxi -1;
				}
				$(jFC).removeClass(jSel);
				slide(dur, 1, direction);
				$(jFC).eq(cur).addClass(jSel);
			}
			//$("#myOwnCarousell li:first").insertAfter($("#myOwnCarousell li:last"));

		});
	};

	$.fn.jFlow.defaults = {
		controller: ".jFlowControl", // must be class, use . sign
		slideWrapper : "#jFlowSlide", // must be id, use # sign
		selectedWrapper: "jFlowSelected",  // just pure text, no sign
		easing: "swing",
		duration: 400,
		width: "100%",
		prev: ".jFlowPrev", // must be class, use . sign
		next: ".jFlowNext" // must be class, use . sign
	};

	var caru = $.fn.jFlow.prototype;
	
	$(window).resize(function(){
		$(caru).resize();
	});
})(jQuery);
