/* 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 cur = 0;
		var maxi = $(jFC).length;
         		
		// sliding function
		var slide1 = function (dur, i, cur) {
            if (cur == i) return;
			$(opts.slides).children().css({overflow:"hidden"});
			$(opts.slides + " iframe").hide().addClass("temp_hide");

			var marL = "-0px";
			if (cur > i) marL = "-" + ($(opts.slides).find(":first-child").width() + "px");		
			$(opts.slides).css({ marginLeft:marL });
			
            $(jFC).each( function(index) { 
                if ((cur == index) || (i == index)) {
                   $(opts.slides).find('.jFlowSlideContainer').get(index).style.display = 'block';
                } else {
                   $(opts.slides).find('.jFlowSlideContainer').get(index).style.display = 'none';
                }
            }); 			
            
            marL = "-0px";
			if (cur < i) marL = "-" + ($(opts.slides).find(":first-child").width() + "px");
			
	        $(opts.slides).animate({
                marginLeft: marL}, 
		        opts.duration,
		        opts.easing,
		        function(){
			        $(opts.slides).children().css({ overflow:"auto" });
			        $(".temp_hide").show();
		        }
	        );
		}	

		$(this).find(jFC).each(function(i){
			$(this).click(function(){
				if ($(opts.slides).is(":not(:animated)")) {
                    $(jFC).each( function(index) { 
                        if ($(jFC).get(index).className == "jfc " + jSel) cur = index
		            });
					$(jFC).removeClass(jSel);
					$(this).addClass(jSel);
					var dur = Math.abs(cur-i);
					slide1(dur,i, cur);
					cur = i;
				}
			});
		});	
			
		$(opts.slides).before('<div id="'+jFS.substring(1, jFS.length)+'"></div>').appendTo(jFS);
		$(opts.slides).find("div#flow").each(function(){$(this).before('<div class="jFlowSlideContainer"></div>').appendTo($(this).prev());});
		
		//initialize the controller
		$(jFC).eq(cur).addClass(jSel);
											
       $(this).find(jFC).each( function(index) { 
            if($(this).attr("name")== opts.file){
                $('#slide').find('.jfc').get(0).className = 'jfc';
                $(this).addClass(jSel);
                slide1(1,index,0);
            }
        });

		var resize = function (x){
			$(jFS).css({
				position:"relative",
				width: opts.width,
				height: opts.height,
				overflow: "hidden"
			});
			//opts.slides or #mySlides container
			$(opts.slides).css({
				position:"relative",
				width: $(jFS).width()*$(jFC).length+"px",
				height: $(jFS).height()+"px",
				overflow: "hidden"
			});
			// jFlowSlideContainer
			$(opts.slides).children().css({
				position:"relative",
				width: $(jFS).width()+"px",
				height: $(jFS).height()+"px",
				"float":"left",
				overflow:"auto"
			});
			
			$(opts.slides).css({
				marginLeft: "-" + (cur * $(opts.slides).find(":eq(0)").width() + "px")
			});
		}
		
		// sets initial size
		resize();

		// resets size
		$(window).resize(function(){ resize(); });
		
		$(opts.prev).click(function(){
			if ($(opts.slides).is(":not(:animated)")) {
		        $(jFC).each( function(index) { 
                    if ($(jFC).get(index).className == "jfc " + jSel) cur = index
		        });			
				$(jFC).removeClass(jSel);
				var dur = 1;
				if (cur > 0) {
					var i = cur-1;
				    slide1(dur,i, cur);
				    cur--;
				} else {
					dur = cur;
					slide1(dur, maxi -1, cur);
					cur = maxi -1;
				}
				$(jFC).eq(cur).addClass(jSel);
			}
		});
		
		$(opts.next).click(function(){
			if ($(opts.slides).is(":not(:animated)")) {
		        $(jFC).each( function(index) { 
                    if ($(jFC).get(index).className == "jfc " + jSel) cur = index
		        });			
				$(jFC).removeClass(jSel);
				var dur = 1;
				if (cur < maxi - 1) {
					var i = cur+1;
				    slide1(dur,i,cur);
				    cur++;
				} else {
					dur = maxi -1;
					slide1(dur, 0, cur);
					cur = 0;
				}
				$(jFC).eq(cur).addClass(jSel);
			}
		});
	};
	
	$.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
		file: "noQueryString"
	};

})(jQuery);

