JQ = jQuery.noConflict();
/* 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(JQ) {

	JQ.fn.jFlow = function(options) {
		var opts = JQ.extend({}, JQ.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 = JQ(jFC).length;
		// sliding function
		var slide = function (dur, i) {
			JQ(opts.slides).children().css({
				overflow:"hidden"
			});
			JQ(opts.slides + " iframe").hide().addClass("temp_hide");
			JQ(opts.slides).animate({
				marginLeft: "-" + (i * JQ(opts.slides).find(":first-child").width() + "px")}, 
				opts.duration/**(dur)*/,
				opts.easing,
				function(){
					JQ(opts.slides).children().css({
						overflow:"hidden"
					});
					JQ(".temp_hide").show();
				}
			);	
		}
		activeAlpha = 0;
		JQ(this).find(jFC).each(function(i){
			JQ(this).click(function(){
				if (JQ(opts.slides).is(":not(:animated)")) {
					JQ(jFC).removeClass(jSel);
					//JQ(jFC).stop().fadeTo(500, 1); // Yorick
					JQ(this).addClass(jSel);
					//JQ(this).stop().fadeTo(500, 0.4); // Yorick 
					var dur = Math.abs(cur-i);
					slide(dur,i);
					cur = i;
					light();					
				}
			});
		});	
		function light(){
			JQ("#"+activeAlpha).stop().fadeTo(800, 0.15);
			JQ("#"+cur).stop().fadeTo(800, 1);
			activeAlpha = cur;
		}
		JQ(opts.slides).before('<div id="'+jFS.substring(1, jFS.length)+'"></div>').appendTo(jFS);
		
		JQ(opts.slides).find("div").each(function(){	
			JQ(this).before('<div class="jFlowSlideContainer"></div>').appendTo(JQ(this).prev());
			JQ(this).stop().fadeTo(0, 0.15);
			JQ("#0").stop().fadeTo(0, 1);
		});
		
		//initialize the controller
		JQ(jFC).eq(cur).addClass(jSel);
		
		var resize = function (x){
			JQ(jFS).css({
				position:"relative",
				width: opts.width,
				height: opts.height,
				overflow: "hidden"
			});
			//opts.slides or #mySlides container
			JQ(opts.slides).css({
				position:"relative",
				width: JQ(jFS).width()*JQ(jFC).length+"px",
				height: JQ(jFS).height()+"px",
				overflow: "hidden"
			});
			// jFlowSlideContainer
			JQ(opts.slides).children().css({
				position:"relative",
				width: /*JQ(jFS).width()*JQ(jFC).length*/606+"px",
				height: JQ(jFS).height()+"px",
				"float":"left",
				overflow:"hidden"
			});
			
			JQ(opts.slides).css({
				marginLeft: "-" + (cur * JQ(opts.slides).find(":eq(0)").width() + "px")
			});
		}
		
		// sets initial size
		resize();

		// resets size
		JQ(window).resize(function(){
			resize();						  
		});
		
		JQ(opts.prev).click(function(){
			if (JQ(opts.slides).is(":not(:animated)")) {
				var dur = 1;
				if (cur > 0)
					cur--;
				else {
					cur = maxi -1;
					dur = cur;
				}
				JQ(jFC).removeClass(jSel);
				slide(dur,cur);
				JQ(jFC).eq(cur).addClass(jSel);
			}
			light();
		});
		
		JQ(opts.next).click(function(){
			if (JQ(opts.slides).is(":not(:animated)")) {
				var dur = 1;
				if (cur < maxi - 1)
					cur++;
				else {
					cur = 0;
					dur = maxi -1;
				}
				JQ(jFC).removeClass(jSel);
				slide(dur, cur);
				JQ(jFC).eq(cur).addClass(jSel);
				
			}
			light();
		});
	};
	
	JQ.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
	};
	
})(jQuery);

