(function(jQuery) {

	jQuery.fn.easySlider = function(options){
	  
		// default configuration properties
		var defaults = {			
			prevId: 		'prevBtn',
			prevText: 		'',
			nextId: 		'nextBtn',	
			nextText: 		'',
			controlsShow:	true,
			controlsBefore:	'',
			controlsAfter:	'',	
			controlsFade:	true,
			firstId: 		'firstBtn',
			firstText: 		'First',
			firstShow:		false,
			lastId: 		'lastBtn',	
			lastText: 		'Last',
			lastShow:		false,				
			vertical:		false,
			speed: 			800,
			auto:			false,
			pause:			6000,
			continuous:		false, 
			singleNav:		true,
			// thumbnail options
			singleNavWidth:	500,
			thumbs:			true,
			thumb_prevId: 	'prevThumb',
			thumb_prevText: '<<',
			thumb_nextId: 	'nextThumb',	
			thumb_nextText: '>>',				
			thumb_firstId: 	'firstThumb',
			thumb_firstText:'First',
			thumb_firstShow:false,
			thumb_lastId: 	'lastThumb',	
			thumb_lastText: 'Last',
			thumb_lastShow:	false,
			thumb_id:		'thumb',
			thumb_activeclass:'current'
		}; 
		
		var options = jQuery.extend(defaults, options);  
				
		this.each(function() {  
			var obj = jQuery(this); 				
			var s = jQuery("li.the_slide", obj).length;
			var w = jQuery("li.the_slide", obj).width(); 
			var h = jQuery("li.the_slide", obj).height(); 
			var ThumbLength = jQuery("li.thumbdo").length;
			var ThumbWidth = jQuery("li.thumbdo").width();
			var carouselWidth = jQuery("ul#thumb_slide").width();
			var carWidth = ((carouselWidth-ThumbWidth) - 30);
			var clickable = true;
			var qot = '';
			var thumbed = false;
			obj.width(w); 
			obj.height(h); 
			obj.css("overflow","hidden");
			var ts = s-1;
			var t = 0;
			var qot=0;
			var theAdvance = 0;
			var carNumberMax = Math.ceil(s/5);
			var carNumber = 1;
			jQuery("ul#the_slides", obj).css('width',s*w);			
			
			if(options.continuous){
				jQuery("ul", obj).prepend(jQuery("ul li:last-child", obj).clone().css("margin-left","-"+ w +"px"));
				jQuery("ul", obj).append(jQuery("ul li:nth-child(2)", obj).clone());
				jQuery("ul", obj).css('width',(s+1)*w);
			};				
			
			jQuery("div#slider").attr("rel", 0);
			jQuery("li", obj).css('float','left');
			
			if(options.controlsShow){
				var html = options.controlsBefore;				

				if(options.singleNav) {
					html += ' <div class="slider-single-nav" style="width: ' + options.singleNavWidth + 'px">';
					if(options.firstShow) html += '<span id="'+ options.firstId +'"><a href=\"javascript:void(0);\">'+ options.firstText +'</a></span>';
					html += ' <span id="'+ options.prevId +'"><a href=\"javascript:void(0);\"><!-- ie6 --></a></span>';
					html += ' <span id="'+ options.nextId +'"><a href=\"javascript:void(0);\"><!-- ie6 --></a></span>';
					if(options.lastShow) html += ' <span id="'+ options.lastId +'"><a href=\"javascript:void(0);\">'+ options.lastText +'</a></span>';	
					html += ' </div><div style="clear: both;"><!-- ie6 --></div> ';
					};

				jQuery("#prevThumbBtn").click(function() {
					if(carNumber<1) {
						jQuery("#thumb_slide").animate({ 
							marginLeft: "+="+carWidth+""
						})
						carNumber=carNumber+1;	
						jQuery("#prevThumb").show();
					} else {
			//			jQuery("#nextThumb").hide();
					}
						
				});	

				jQuery("#nextThumbBtn").click(function() {
					if(carNumber < carNumberMax) {
						jQuery("#thumb_slide").animate({ 
							marginLeft: "-="+carWidth+""
						})
						carNumber=carNumber-1;
						jQuery("#prevThumb").show();
					} else {
			//			jQuery("#prevThumb").hide();
					}			
				});					
				
				html += options.controlsAfter;						
				jQuery(obj).after(html);										
			};

				
				for(var i=0;i<s;i++){
					jQuery('#thumb'+i).attr('rel',''+i+'');
					jQuery('#thumb'+i).click(function(){							
						animate(jQuery("a.thumba",jQuery(this)).attr('rel'),true);
					});
				};							
			
			if(options.singleNav) {
				jQuery("a","#"+options.nextId).click(function(){	
					animate("next",true);
				});
				jQuery("a","#"+options.prevId).click(function(){	
					animate("prev",true);	
				});	
			};

			function setCurrent(i){
				i = parseInt(i);
				jQuery("ul#thumb_slide li").removeClass("current");
				jQuery("li#thumb" + i).addClass("current");
			};
			
			function adjust(){
				if(t>ts) t=0;		
				if(t<0) t=ts;
				jQuery("ul#the_slides",obj).css("margin-left",(t*w*-1));

				clickable = true;
				setCurrent(t);
			};
			
			function animate(dir,clicked){
				if (clickable){
					clickable = false;
					var ot = t;		
					switch(dir){
						case "next":
							if(t>0) { t=2; } else if(t<0) { t=-1; } else if (t>ts) { t=2 } else { t=1; }						
							break; 
						case "prev":
							if(t>0) { t=0; } else if(t<0) { t=-1; } else if (t>ts) { t=-2 } else { t=-1; }
							break; 
						default:
							t=dir;
							break; 
					};	
					
					var diff = Math.abs(ot-t);
					var speed = diff*options.speed;		
						jQuery("ul#thumb_slide").addClass(''+t+'');
						
						p = (t*w*-1);
						jQuery("ul#the_slides",obj).animate(
							{ marginLeft: p }, 
							{ queue:false, duration:speed, complete:adjust }
						);				
					
					if(options.controlsFade){					
						if(t==ts){
							jQuery("a","#"+options.nextId).hide();
							jQuery("a","#"+options.lastId).hide();
						} else {
							jQuery("a","#"+options.nextId).show();
							jQuery("a","#"+options.lastId).show();					
						};
						if(p==0) {
							jQuery("a","#"+options.prevId).hide();
							jQuery("a","#"+options.firstId).hide();
						} else if(t==0){
							jQuery("a","#"+options.prevId).hide();
							jQuery("a","#"+options.firstId).hide();
						} else {
							jQuery("a","#"+options.prevId).show();
							jQuery("a","#"+options.firstId).show();
						};					
					};				
					
					if(clicked) clearTimeout(timeout);
					if(options.auto && dir=="next" && !clicked){;
						timeout = setTimeout(function(){
							animate("next",false);
						},diff*options.speed+options.pause);
					};
			
				};
				
			};
			// init
			var timeout;
			if(options.auto){;
				timeout = setTimeout(function(){
					animate("next",false);
				},options.pause);
			};		
			
			setCurrent(0);
		
			if(!options.continuous && options.controlsFade){					
				jQuery("a","#"+options.prevId).hide();
				jQuery("a","#"+options.firstId).hide();				
			};	

			
		});
	  
	};

})(jQuery);
