(function($) {

	$.fn.jFlow = function(options) {
		var opts = $.extend({}, $.fn.jFlow.defaults, options);
		var cur = 0;
		var timer;
		var pause; // current slideshow state i.e. paused or not paused
		var selected_class = "jFlowSelected";
		var maxi = $(".jFlowControl").length;
		
		
		$(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(selected_class);

		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();
		});
		
		$("img.btnPrev").click(function(){
		
				// if ($(':animated').length)
					//return false;  //prevents rapid clicks being acknlowledged while animation is in progress

					if (pause) {
						pause = false;
						clktopause();
					}
					
					doprev();
					dotimer();

			return false;
				});
		

		$("img.btnNext").click(function(){
		
				// if ($(':animated').length)
					//return false;  //prevents rapid clicks being acknlowledged while animation is in progress

					if (pause) {
						pause = false;
						clktopause();
					}
					
					donext();
					dotimer();

			return false;
				});


		$("a.jFlowNext").click(function(){
		
				// if ($(':animated').length)
					//return false;  //prevents rapid clicks being acknlowledged while animation is in progress

				if (cur == maxi -1) {
					cur = -1;
					pause = false;
					clktopause();
					donext();
					dotimer();
				}
				else {

					if (pause) {
						pause = false;
						clktopause();
						donext();
						dotimer();
					}
					else {
						pause = true;
						clktoplay();
						if(timer != null) 
							clearInterval(timer);
					}
				}
				return false;
				});

// DONEXT
		var donext = function (){
	
			if (!pause) {
				if (cur < maxi - 1)
					cur++;
				else {
				cur = -1;  
				}
			};
			
			if (!pause) { 
				$(".jFlowControl").removeClass(selected_class); 

				if (opts.flyback) {
					$(opts.slides).animate({marginLeft: "-" + (cur * $(opts.slides).find(":first-child").width() + "px")}, opts.duration);
				}
				else {
					if (cur > 0) { // this condition checks that next event will not be to rewind back to the first slide
						$(opts.slides).animate({marginLeft: "-" + (cur * $(opts.slides).find(":first-child").width() + "px")}, opts.duration);
						$(".jFlowControl").eq(cur).addClass(selected_class);
					}
					else {  // this condition means that cur == 0 and the next event will be to rewind back to the first
						$(opts.slides).animate({marginLeft: "-0"}, 1);
					}
				} 
				$(".jFlowControl").eq(cur).addClass(selected_class);
				
				//if (cur == maxi -1) {
				//  if(timer != null) 
				//			clearInterval(timer);  // stop show after final slide
				//$("a.jFlowNext").css({background: 'url(' + opts.playbtn + ') no-repeat'}).attr({title: opts.playtitle});
				//}
			}
		}
	
// DOPREV
		var doprev = function (){
	
			if (!pause) {
				if (cur >= 0)
					cur--;
				else {
				cur = -1;  
				}
			};
			
			if (!pause) { 
				$(".jFlowControl").removeClass(selected_class); 

				if (opts.flyback) {
					$(opts.slides).animate({marginLeft: "-" + (cur * $(opts.slides).find(":first-child").width() + "px")}, opts.duration);
				}
				else {
					if (cur >= 0) { // this condition checks that next event will not be to rewind back to the last slide
						$(opts.slides).animate({marginLeft: "-" + (cur * $(opts.slides).find(":first-child").width() + "px")}, opts.duration);
						$(".jFlowControl").eq(cur).addClass(selected_class);
					}
				} 
				$(".jFlowControl").eq(cur).addClass(selected_class);
				
				//if (cur == maxi -1) {
				//  if(timer != null) 
				//			clearInterval(timer);  // stop show after final slide
				//$("a.jFlowNext").css({background: 'url(' + opts.playbtn + ') no-repeat'}).attr({title: opts.playtitle});
				//}
			}
		}
		
// DOTIMER
		var dotimer = function (){
			if(timer != null) 
			    clearInterval(timer);
	         timer = setInterval(function() {
	         donext();
    	     }, opts.interval);
    	}
		
// CLKTOPLAY
		var clktoplay = function () {
			$("a.jFlowNext").css({background: 'url(' + opts.playbtn + ') no-repeat'}).attr({title: opts.playtitle});
		}

// CLKTOPAUSE
		var clktopause = function () {
			$("a.jFlowNext").css({background: 'url(' + opts.pausebtn + ') no-repeat'}).attr({title: opts.pausetitle});
		}
		
// DOSTART
		var dostart = function () {
			pause = opts.initpause;
			if (pause) {
				$("a.jFlowNext").css({background: 'url(' + opts.playbtn +') no-repeat'}).attr({title: opts.playtitle});
			}
			else {
				$("a.jFlowNext").css({background: 'url(' + opts.pausebtn + ') no-repeat'}).attr({title: opts.pausetitle});
				dotimer();
				}
		}
		
	dostart();
	
	};


	$.fn.jFlow.defaults = {
		easing: "swing",
		duration: 500,
		width: "100%",
		initpause: false,      				// pause slide show on initial load
		interval: 3500,  	   				// interval between slide animations
		flyback: false,         			// true = animate fly-back to beginning of show, false = instant return without animation
		playbtn: '/images/play.gif',  	// string.  Path to a.jFlowNext background image for play button 
		playtitle: 'Click to Play', 		// string.  Title attribute for play button image
		pausebtn: '/images/pause.gif', // string.  Path to a.jFlowNext background image for pause button 
		pausetitle: 'Click to Pause'		// string.  Title attribute for pause button image
	};

})(jQuery);