(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 diapoTimer = 6000;
		var cur = 0;
		var timer = null;
		var maxi = $(jFC).length;
		var onDiapo = 1;
		
		this.stop_jFlow = function() {
			clearInterval(timer);
		}
		this.doSlide = function(nb) {
			dotimer();
			cur =  nb;
			$(jFC).removeClass(jSel);
			slide(0, cur);
			$(jFC).eq(cur).addClass(jSel);
			checkPrevNext();
		}
		
		// sliding function
		var slide = function (dur, i) {
		
			$(opts.slides).children().css({

				overflow:"hidden"

			});

			$(opts.slides + " iframe").hide().addClass("temp_hide");

			$(opts.slides).animate({

				marginLeft: "-" + (i * $(opts.slides).find(":first-child").width() + "px")

				},

				opts.duration*(dur),

				opts.easing,

				function(){

					$(opts.slides).children().css({

						overflow:"hidden"

					});

					$(".temp_hide").show();

				}

			);	

		};

		$(this).find(jFC).each(function(i){
			$(this).click(function(){

				dotimer();

				if ($(opts.slides).is(":not(:animated)")) {
					$(jFC).removeClass(jSel);
					$(this).addClass(jSel);
					var dur = Math.abs(cur-i);
					slide(dur,i);
					cur = i;
					checkPrevNext();
				}
			});
		});	
		
		$(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());
		});

		//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

			$(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:"hidden"

			});

			

			$(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(){
			dotimer();
			doprev();
		});
		$(opts.next).click(function(){
			dotimer();
			donext();		
		});
		
		var doprev = function (x){
			if ($(opts.slides).is(":not(:animated)")) {
				var dur = 1;
				if (cur > 0) {
					cur--;
				} else {
					cur = maxi -1;
					dur = cur;
				}
				
				checkPrevNext();

				$(jFC).removeClass(jSel);
				slide(dur,cur);
				$(jFC).eq(cur).addClass(jSel);
			}
		}

		var donext = function (x){
			if ($(opts.slides).is(":not(:animated)")) {
				var dur = 1;
				if (cur < maxi - 1) {
					cur++;
				} else {
					cur = 0;
					dur = maxi -1;
				}
				
				checkPrevNext();
				
				$(jFC).removeClass(jSel);
				slide(dur, cur);
				$(jFC).eq(cur).addClass(jSel);

			}

		}
		
		var checkPrevNext = function () {
			if (cur > 0) {
				$(opts.prev).fadeTo(400, 1);
				$(opts.prev).click(function(){
					dotimer();
					doprev();
				});
			}
			if (cur < maxi - 1) {
				$(opts.next).fadeTo(400, 1);
				$(opts.next).click(function(){
					dotimer();
					donext();
				});
			}
			
			if (cur == maxi - 1) {
				$(opts.next).fadeTo(400, 0.4);
				$(opts.next).unbind('click');
			}
			if (cur == 0) {
				$(opts.prev).fadeTo(400, 0.4);
				$(opts.prev).unbind('click');
			}
		}

		var dotimer = function (x){
			if((opts.auto) == true) {
				if(timer != null) clearInterval(timer);
				timer = setInterval(donext, diapoTimer);
			}
		}

		//Pause/Resume function fires at hover

		
		$(opts.slides).hover(
			function() {
				clearInterval(timer);
			},
			function() {
				dotimer();
			}
		);
		
		cur =  Math.round(Math.random() * 1);
		$(jFC).removeClass(jSel);
		slide(0, cur);
		$(jFC).eq(cur).addClass(jSel);
		
		checkPrevNext();
		
		if (cur == 0 && intro_twinair == 0) {
			setTimeout(startIntro, 100);
		} else {
			dotimer();
		}
		
		return this;
	};			

	$.fn.jFlow.defaults = {
		controller: "#myController", // must be class, use . sign
		slideWrapper : "#mySlides", // must be id, use # sign
		selectedWrapper: ".jFlowSelected",  // just pure text, no sign
		auto: true,
		easing: "swing",
		duration: 400,
		width: "100%",
		prev: ".jFlowPrev", // must be class, use . sign
		next: ".jFlowNext" // must be class, use . sign
	};

})(jQuery);
