/* Copyright (c) 2010 Westin Produktion http://www.westinproduktion.se
 * Licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
 * jSlide
 * Version: 1.0 (Mars 1, 2010)
 * Requires: jQuery 1.2+
 */
 
(function($) {

	$.fn.jSlide = function(options) {
		
		var opt = $.extend({}, $.fn.jSlide.defaults, options);
		var theSlider = $(this);
		var singleSlide = theSlider.children();
		var sWidth = singleSlide.css('width');
		var sHeight = singleSlide.css('height');
		
		
		
		
		
		var opts = $.extend({}, $.fn.jSlide.defaults, options);
		var randNum = Math.floor(Math.random()*11);
		var jFC = opts.controller;
		var jFS =  opts.slideWrapper;
		var jSel = opts.selectedWrapper;
		var currentSlide = opt.currentSlide;
		
		var slideWrapper = $(theSlider);
		var singleSlides = $(theSlider).children();
		
		var slideCount = $(this).children().size();
		
		var cur = 0;
		var timer;
		var maxi = $(jFC).length;
		
		
		//add jSlideNav
		var jSlideNav = '<div id="jSlideNav"><span class="jSlidePrev">&lt;&lt;</span>';
		$(this).children().each(function (i) {
        	jSlideNav = jSlideNav+'<span class="jSlideControl">'+(i+1)+'</span>';
		});
		jSlideNav = jSlideNav+'<span class="jSlideNext">&gt;&gt;</span>';
		$(this).after(jSlideNav);
		
		//add nextSlide
		singleSlides.prepend('<div class="next_slide jSlideNext"></div>');
		
		// sliding function
		var slide = function (dur, i) {
			$(this).children().css({
				overflow:"hidden"
			});
			$(theSlider + " iframe").hide().addClass("temp_hide_iframe");//Temporary hide iframes
			$(theSlider).animate({
				marginLeft: "-" + (i * $(theSlider).find(":first-child").width() + "px")
				},
				opts.duration*(dur),
				opts.easing,
				function(){
					$(theSlider).children().css({
						overflow:"hidden"
					});
					$(".temp_hide_iframe").show();
				}
			);
			
		}
		$('.jSlideControl').each(function(i){
			$(this).click(function(){
				dotimer();
				if ($(theSlider).is(":not(:animated)")) {
					$('.jSlideControl').removeClass(currentSlide);
					$(this).addClass(currentSlide);
					var dur = Math.abs(cur-i);
					slide(dur,i);
					cur = i;
				}
			});
		});	
		
		theSlider.wrap('<div id="jSlideWrapper"></div>');
		
		theSlider.find("div").each(function(){
			$(this).before('<div class="jSlideSlideContainer"></div>').appendTo($(this).prev());
		});
		
		singleSlide.height( singleSlide.height()-2 ); //To show 1px border
		
		


		
		
		//initialize the controller
		$(jFC).eq(cur).addClass(jSel);
		
		var resize = function (x){
			$('#jSlideWrapper').css({
				position:"relative",
				width: sWidth,
				height: sHeight,
				overflow: "hidden"
			});
			//theSlider or #mySlides container
			$(theSlider).css({
				position:"relative",
				width: $('#jSlideWrapper').width()*slideCount+"px",
				height: $('#jSlideWrapper').height()+"px",
				overflow: "hidden"
			});
			// jSlideSlideContainer
			$(theSlider).children().css({
				position:"relative",
				width: $('#jSlideWrapper').width()+"px",
				height: $('#jSlideWrapper').height()+"px",
				"float":"left",
				overflow:"hidden"
			});
			
			$(theSlider).css({
				marginLeft: "-" + (cur * $(theSlider).find(":eq(0)").width() + "px")
			});
		}
		
		// sets initial size
		resize();

		// resets size
		$(window).resize(function(){
			resize();						  
		});
		
		$('.jSlidePrev').click(function(){
			dotimer();
			doprev();
			
		});
		
		$('.jSlideNext').click(function(){
			dotimer();
			donext();
			
		});
		
		var doprev = function (x){
			if ($(theSlider).is(":not(:animated)")) {
				var dur = 1;
				if (cur > 0)
					cur--;
				else {
					cur = slideCount -1;
					dur = cur;
				}
				$(jFC).removeClass(jSel);
				slide(dur,cur);
				$(jFC).eq(cur).addClass(jSel);
			}
		}
		
		var donext = function (x){
			if ($(theSlider).is(":not(:animated)")) {
				var dur = 1;
				if (cur < slideCount - 1)
					cur++;
				else {
					cur = 0;
					dur = slideCount -1;
				}
				$(jFC).removeClass(jSel);
				//$(jFS).fadeOut("fast");
				slide(dur, cur);
				//$(jFS).fadeIn("fast");
				$(jFC).eq(cur).addClass(jSel);
			}
		}
		
		var dotimer = function (x){
			if((opts.auto) == true) {
				if(timer != null) 
					clearInterval(timer);
			    
        		timer = setInterval(function() {
	                	$(opts.next).click();
						}, 3000);
			}
		}

		dotimer();
	};
	
	$.fn.jSlide.defaults = {
		slideSingle: ".slide",
		slideNav: ".jSlideNav",
		controller: ".jSlideControl", // must be class, use . sign
		slideWrapper : "#jSlideSlide", // must be id, use # sign
		selectedWrapper: "jSlideSelected",  // just pure text, no sign
		auto: false,
		easing: "swing",
		duration: 200,
		width: "100%",
		prev: ".jSlidePrev", // must be class, use . sign
		next: ".jSlideNext" // must be class, use . sign
	};
	
})(jQuery);
