/* 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;
																		// SS > Simon Sarrasin ssarrasin@gmail.com
		var cur = parseInt(opts.vars_array['cur']);						// SS div cursor
		var total_count = parseInt(opts.vars_array['total_count']);		// SS items total
		var nts=parseInt(opts.vars_array['nts']);						// SS number of items to show. Used primarily for the count.
		
		var maxi = $(jFC).length;
		var mod_max=maxi%nts;
		var max_for_count=maxi/nts;
		max_for_count=mod_max ? parseInt(max_for_count)+1 : parseInt(max_for_count);
		// sliding function
		var slide = function (dur, i) {
			$(opts.slides).children().css({
				overflow:"hidden"
			});
			$(opts.slides + " iframe").hide().addClass("temp_hide");
			//alert($(opts.slides).find(":first-child").height() + "px - i: "+i);
			$(opts.slides).animate(
				(opts.direction=='horizontal' ?
					{marginLeft: "-" + (i * $(opts.slides).find(":first-child").width() + "px")}
				:
					{marginTop: "-" + getHeight(i)+"px"}
				), 
				opts.duration*(dur),
				opts.easing,
				function(){
					$(opts.slides).children().css({
						overflow:"auto"
					});
					$(".temp_hide").show();
				}
			);			
		}
		var getWidth=function(i) {
			data=updateCount(cur);
			var from=data[0];
			var to=data[1];
			var full_width=($(opts.slides).find(":first-child").width());
			var ret_width=0;
			var last_slide=opts.slides_class+":last";
			if(to==total_count) {
				var div=$(opts.slides).find(last_slide);
				var w=0;
				div.find(opts.slides_child).each(function() {
					alert(w);
					w+=$(this).width();
					alert(w);
				});
				ret_width=parseInt(w)+full_width;
			}
			else ret_width=i * full_width;
			//alert(ret);
			return ret_width;
		}
		var getHeight=function(i) {
			data=updateCount(cur);
			var from=data[0];
			var to=data[1];
			var full_height=($(opts.slides).find(":first-child").height());
			var ret_height=0;
			var last_slide=opts.slides_class+":last";
			if(to==total_count) {
				var slid=$(opts.slides).find(last_slide);
				var h=0;
				slid.find(opts.slides_children).each(function() {
					//alert(h);
					h+=$(this).height();
					//alert(h);
				});
				ret_height=parseInt(h)+full_height;
			}
			else ret_height=i * full_height;
			//alert(ret);
			return ret_height;
		}
		$(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());
		});
		var updateCount = function updateCount(c) { // added by Simon Sarrasin 29/12/08
			nts>total_count ? nts=total_count : "";
			var c=c==0 ? 1 : c*nts+1;
			var to_num=c==1 ? nts : parseInt(c+nts-1);
			to_num>total_count ? to_num=total_count : to_num;
			//alert('maxi: '+maxi+'\ncur: '+cur+'\nc: '+c+'\nto_num: '+to_num+'\ntotal_count: '+total_count);
			c=="" ? c=1 : "";
			return Array(c,to_num);
		}
		var writeCount=function(data) {	
			$(opts.from).html(data[0]);
			$(opts.to).html(data[1]);
		}
		//initialize the controller
		$(jFC).eq(cur).addClass(jSel);		
		var resize = function (x){
			$(jFS).css({
				position:"relative",
				width: opts.width,
				height: opts.height,
				overflow: "hidden"
			});
			//opts.slides or #mySlides container
			if(opts.direction!='vertical') {
				$(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")
				});
			}//
			else {
				$(opts.slides).css({
					marginTop: "-" + (cur * $(opts.slides).find(":eq(0)").height() + "px")
				});
			}
		}
		// sets initial size
		resize();
		// initial page count
		writeCount(updateCount(cur));
		// resets size
		$(window).resize(function(){
			resize();						  
		});
		$(opts.prev).click(function(){
			if ($(opts.slides).is(":not(:animated)")) {
				var dur = 1;
				if (cur > 0)
					cur--;
				else {
					cur = maxi -1;
					dur = cur;
				}
				$(jFC).removeClass(jSel);
				slide(dur,cur);
				$(jFC).eq(cur).addClass(jSel);
				writeCount(updateCount(cur));
				if(cur==0)$(opts.prev).fadeOut('fast');
				if(cur<max_for_count-1&&$(opts.next).css('display')=="none") $(opts.next).fadeIn('fast');
			}
		});		
		$(opts.next).click(function(){
			if ($(opts.slides).is(":not(:animated)")) {
				var dur = 1;
				if (cur < maxi - 1)
					cur++;
				else {
					cur = 0;
					dur = maxi -1;
				}
				$(jFC).removeClass(jSel);
				slide(dur, cur);
				$(jFC).eq(cur).addClass(jSel);
				writeCount(updateCount(cur));
				if(cur==max_for_count-1) $(opts.next).fadeOut('fast');
				if(cur>0 && $(opts.prev).css('display')=="none") $(opts.prev).fadeIn('fast');
			}
		});
	};	
	$.fn.jFlow.defaults = {
		controller: ".jFlowControl", // must be class, use . sign
		slideWrapper : "#jFlowSlide", // must be id, use # sign
		selectedWrapper: "jFlowSelected",  // just pure text, no sign
		slides_class:'.inner_wrap',
		slides_children:'.p-box',
		easing: "swing",
		duration: 250,
		width: "100%",
		direction: 'horizontal',
		numToShow: 3,
		prev: ".jFlowPrev", // must be class, use . sign
		next: ".jFlowNext", // must be class, use . sign
		from: ".jFlowFrom", // must be class, use . sign
		to:   ".jFlowTo" // must be class, use . sign
	};
})(jQuery);