/* 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) {
		
		// the options passed to the function
		var opts = $.extend({}, $.fn.jFlow.defaults, options);
		
		// random number
		var randNum = Math.floor(Math.random()*11);
		
		// the class given to the navigation controllers
		var navControllerClass = opts.navControllerClass;
		
		// the id given to the slides wrapper
		var slideWrapperID =  opts.slideWrapperID;
		
		// the class to be applied to selected controllers
		var navSelectedClass = opts.navSelectedClass;

		// the timer for periodical sliding
		var timer;

		// the current index
		var cur = 0;
		
		// the total number of navigation controllers
		var maxi = $(navControllerClass).length;
		
		
		// sliding function
		var slide = function (dur, i) {
			
			// move the slides left
			$(opts.slides).animate({
				marginLeft: "-" + (i * $(opts.slides).find(":first-child").width() + "px")}, 
				opts.duration*(dur),
				opts.easing,
				function(){
				}
			);
		};
		
		
		// for each navigation control, add a onclick action
		$(this).find(navControllerClass).each(function(i){
			$(this).click(function(){
				if ($(opts.slides).is(":not(:animated)")) {
					
					// stop any automatic scrolling
					stop();
					$(navControllerClass).removeClass(navSelectedClass);
					$(this).addClass(navSelectedClass);
					var dur = Math.abs(cur-i);
					slide(dur,i);
					cur = i;
				}
			});
		});	
				
		//initialize the controller
		$(navControllerClass).eq(cur).addClass(navSelectedClass);
		
		var resize = function (x){
			$(opts.slides).css({
				width: (opts.width * (maxi + 1)).toString() + "px",
				marginLeft: "-" + (cur * $(opts.slides).find(":eq(0)").width() + "px")
			});
		}
		
		// sets initial size
		resize();

		// resets size
		$(window).resize(function(){
			resize();						  
		});

		
		// captures the onclick event for the previous button
		$(opts.prev).click(function(){
			stop();
			movePrev();
		});
		
		// captures the onclick event for the next button
		$(opts.next).click(function(){
			stop();
			moveNext();
		});
		
		// slide to the previous slide
		var movePrev = function()
		{
			if ($(opts.slides).is(":not(:animated)")) {
				opts.onBeforeSlide(cur);
				var dur = 1;
				if (cur > 0)
					cur--;
				else {
					cur = maxi -1;
					dur = cur;
				}
				$(navControllerClass).removeClass(navSelectedClass);
				slide(dur,cur);
				$(navControllerClass).eq(cur).addClass(navSelectedClass);
			}
		};
		
		// slide to the next billboard slide
		var moveNext = function()
		{
			if ($(opts.slides).is(":not(:animated)")) {
				opts.onBeforeSlide(cur);
				var dur = 1;
				if (cur < maxi - 1)
					cur++;
				else {
					cur = 0;
					dur = maxi -1;
				}
				$(navControllerClass).removeClass(navSelectedClass);
				slide(dur, cur);
				$(navControllerClass).eq(cur).addClass(navSelectedClass);
			}
		};


		
		// stop the periodical slide
		var stop = function()
		{
			clearTimeout(timer);
		};

		// start the periodical sliding
		var start = function()
		{
			periodicallyUpdate();
		};

		// setup the periodical sliding
		var periodicallyUpdate = function()
		{ 
			if (timer != null) 
			{
				clearTimeout(timer);
			}
			timer = setInterval(function(){ moveNext(); }, opts.autoslideInterval);
		};
		
		
		// if the option to periodically slide has been set, do it
		if (opts.autoslide == true)
			start();
	};
	
		
	$.fn.jFlow.defaults = {
		navControllerClass: ".jFlowControl", // must be class, use . sign
		slideWrapperID : "#jFlowSlide", // must be id, use # sign
		navSelectedClass: "jFlowSelected",  // just pure text, no sign
		easing: "swing",
		duration: 400,
		width: 400,
		prev: ".jFlowPrev", // must be class, use . sign
		next: ".jFlowNext", // must be class, use . sign		
		autoslide: false,
		autoslideInterval: 5000,
		onBeforeSlide: function(){},
		onAfterSlide: function(){}
	};
	
})(jQuery);
