/* 
oSlider is a variation on easySlider 

oSlider
Version 1.1
by Martijn Schot

*/
var obj;
(function($) {

	$.fn.oSlider = function(options){
	  
		// default configuration properties
		var defaults = {	
			autoplay:true,
			buttons: false,
			prevId: 		'prevBtn',
			prevText: 		'Previous',
			nextId: 		'nextBtn',	
			nextText: 		'Next',
			controlsBefore:	'',
			controlsAfter:	'',
			speed: 800,
			pause: 2000
		}; 
		
		
		
		var options = $.extend(defaults, options);  
		var curmargin = 0;		
		this.each(function() {  
			obj = $(this); 	
			
			var list =  $("ul", obj);
			var firstPhoto = $("li", obj)[0];
			var fphoto = $(firstPhoto).clone()
			// add first photo to end of list
			list.append(fphoto);
			var s = $("li", obj).length;
			var w = $("li", obj).width(); 
			var h = $("li", obj).height(); 
			obj.width(w); 
			obj.height(h); 
			obj.css("overflow","hidden");
			
			$("li", obj).each(function(){
				var el = $(this);
				el.css("float", "left");					   
			});
			
			var margin = w;
			var newmargin = 0;
			
				
			var counter = 1;
			var totalPhotos = s-1;
			var counterDiv =  $(".counter", obj);
			counterDiv.css("position","absolute");
			counterDiv.html(counter+" of "+totalPhotos)
			
			
		    list.css('width',s*w);
			list.css('margin-left',-newmargin);
			
			
			if(options.buttons){
				var html = options.controlsBefore;
				html += ' <span id="'+ options.prevId +'"><a href=\"javascript:void(0);\">'+ options.prevText +'</a></span>';
				html += ' <span id="'+ options.nextId +'"><a href=\"javascript:void(0);\">'+ options.nextText +'</a></span>';
				html += options.controlsAfter;						
				$(obj).after(html);										
			};

			
			
			
			
			function setCounter(minus){
					
					if(minus){
						counter--;
					}else{
						counter++;	
					}
					
					
					
					if(counter == 0 ){
						counter = totalPhotos;
					}
					
					if(counter == s){
						counter = 1;
					}

				
				
					counterDiv.html(counter+" of "+totalPhotos)
					
			}
			
			
			function previous(){
					
					if(newmargin==0){
						newmargin = ((s-1)*margin)
						list.animate({  marginLeft: -newmargin}, 0);
						counter = 1;
					}
					
					newmargin = newmargin - margin;
					list.animate({  marginLeft: -newmargin}, options.speed, function(){
									setCounter(true, true) });
					
				
			}
			
			function next(){
					//alert(newmargin);
					if(newmargin == (s-1)*margin){
						newmargin = 0
						list.animate({  marginLeft: -newmargin}, 0);
						
					}
				
					newmargin = newmargin + margin;
					list.animate({  marginLeft: -newmargin}, options.speed, function(){
									setCounter(false, true)
																				 });
				
			}
			
			function runit(){
				next();
				
				setTimeout(function(){
						runit()
					},options.pause);
			}
			
			

			
			$("#"+options.prevId+" a").click(function(){ previous() });
			$("#"+options.nextId+" a").click(function(){ next() });
			
			
			
			if(options.autoplay){
				setTimeout(function(){
				runit() }, options.pause);
			}
		});
	  
	};

})(jQuery);


