/*
 *
 *	jQuery Timer plugin v0.1
 *		Matt Schmidt [http://www.mattptr.net]
 *
 *	Licensed under the BSD License:
 *		http://mattptr.net/license/license.txt
 *
 */
 
 jQuery.timer = function (interval, callback)
 {
 /**
  *
  * timer() provides a cleaner way to handle intervals  
  *
  *	@usage
  * $.timer(interval, callback);
  *
  *
  * @example
  * $.timer(1000, function (timer) {
  * 	alert("hello");
  * 	timer.stop();
  * });
  * @desc Show an alert box after 1 second and stop
  * 
  * @example
  * var second = false;
  *	$.timer(1000, function (timer) {
  *		if (!second) {
  *			alert('First time!');
  *			second = true;
  *			timer.reset(3000);
  *		}
  *		else {
  *			alert('Second time');
  *			timer.stop();
  *		}
  *	});
  * @desc Show an alert box after 1 second and show another after 3 seconds
  *
  * 
  */

	var interval = interval || 100;

	if (!callback)
		return false;
	
	_timer = function (interval, callback) {
		this.stop = function () {
			clearInterval(self.id);
		};
		
		this.internalCallback = function () {
			callback(self);
		};
		
		this.reset = function (val) {
			if (self.id)
				clearInterval(self.id);
			
			var val = val || 100;
			this.id = setInterval(this.internalCallback, val);
		};
		
		this.interval = interval;
		this.id = setInterval(this.internalCallback, this.interval);
		
		var self = this;
	};
	
	return new _timer(interval, callback);
 };
 


/* 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.0 (May 13, 2008)
 * Requires: jQuery 1.2+
 */
 
(function($) {
	$.fn.jFlow = function(options) {
		var opts = $.extend({}, $.fn.jFlow.defaults, options);
		var cur = 0;
		var stopRotate = 0; // added to allow automatic rotation of slides
		var maxi = $(".jFlowControl").length;
		$(this).find(".jFlowControl").each(function(i){
			$(this).click(function(){
				stopRotate = 1;
				$(".jFlowControl").removeClass("jFlowSelected");
				$(this).addClass("jFlowSelected");
				var dur = Math.abs(cur-i+1);
				$(opts.slides).animate({
					marginLeft: "-" + (i * $(opts.slides).find(":first-child").width() + "px")
				}, opts.duration*(dur));
				cur = i;
			});
		});
		
		$(opts.slides).before('<div id="jFlowSlide"></div>').appendTo("#jFlowSlide");
		
		$(opts.slides).find("div").each(function(){
			$(this).before('<div class="jFlowSlideContainer"></div>').appendTo($(this).prev());
		});
		
		//initialize the controller
		$(".jFlowControl").eq(cur).addClass("jFlowSelected");
		
		var resize = function (x){
			$("#jFlowSlide").css({
				position:"relative",
				width: opts.width,
				height: opts.height,
				overflow: "hidden"
			});
		
			$(opts.slides).css({
				position:"relative",
				width: $("#jFlowSlide").width()*$(".jFlowControl").length+"px",
				height: $("#jFlowSlide").height()+"px",
				overflow: "hidden"
			});
		
			$(opts.slides).children().css({
				position:"relative",
				width: $("#jFlowSlide").width()+"px",
				height: $("#jFlowSlide").height()+"px",
				"float":"left"
			});
			
			$(opts.slides).css({
				marginLeft: "-" + (cur * $(opts.slides).find(":first-child").width() + "px")
			});
		}
		
		resize();
		
		$(window).resize(function(){
			resize();
		});
		
		$(".jFlowPrev").click(function(){
			if (cur > 0)
				cur--;
			else
				cur = maxi -1;
			
			$(".jFlowControl").removeClass("jFlowSelected");
			$(opts.slides).animate({
				marginLeft: "-" + (cur * $(opts.slides).find(":first-child").width() + "px")
			}, opts.duration);
			$(".jFlowControl").eq(cur).addClass("jFlowSelected");
		});
		
		$(".jFlowNext").click(function(){
			if (cur < maxi - 1)
				cur++;
			else
				cur = 0;
				
			$(".jFlowControl").removeClass("jFlowSelected");
			$(opts.slides).animate({
				marginLeft: "-" + (cur * $(opts.slides).find(":first-child").width() + "px")
			}, opts.duration);
			$(".jFlowControl").eq(cur).addClass("jFlowSelected");
		});
		// added to allow automatic rotation of slides
		if (opts.autoFlow == "true") {
			$(document).ready(function() {
				$.timer(8000, function (timer) {
					if (stopRotate == 0) {
						if (cur < maxi - 1)
							cur++;
						else
							cur = 0;
						$(".jFlowControl").removeClass("jFlowSelected");
						$(opts.slides).animate({
							marginLeft: "-" + (cur * $(opts.slides).find(":first-child").width() + "px")
						}, opts.duration);
						$(".jFlowControl").eq(cur).addClass("jFlowSelected");
					} else {
						timer.stop();
					}
				});
			});
		}
		// end auto rotate
	};
	
	$.fn.jFlow.defaults = {
		easing: "swing",
		duration: 400,
		width: "100%",
		autoFlow: "true" // added to allow automatic rotation of slides
	};
	
})(jQuery);
