// ============= USEAGE EXAMPLE
/*

Simply call any UL object with the .jslide method, with an object containing the options for that slider

$(function(){
	$("#dss1").jslide({
		width: 240,
		height: 120,
		items: 8,
		direction: "vertical",
		next: ".dss1Next",
		prev: ".dss1Prev",
		loop: true,
		paginate: {paginate:true, object: "#pages"}
	});
});


//============ OPTIONS LIST:

width  		(total width of the slider)

height 		(total height of the slider)

items  		(number of items to show per page)

direction 	(will it scroll horizontal or vertical? Default is horizontal)

next   		(object(s) that will be used as next button (use a class to have more than one object))

prev   		(object(s) that will be used as the previous button (use a class to have more than one object))

loop   		(will the slider go back to the first object when it reaches the end? Default is false)

paginate 	(adds pagination for the slider.  Default is false. To enable, pass an object with two options in it,
	 	  	paginate: and object: where paginate is if you want it to paginate, and object is a css selector 
		  	of the object to place the pagination in)

speed 		(time in milliseconds that the animation should last. Default is 600)

easing 		(name of the type of easing to use. Requires jquery easing library. Default is swing.)

*/


(function($){
	$.fn.jslide = function(options){
		
		var args = arguments
		
		if(typeof args[0]==="object"){
			options = $.extend({
				speed: 600,
				easing: "swing",
				direction: "horizontal",
				loop: false,
				paginate: {paginate: false}
			}, options);
		}else{
			var ret;
			this.each(function(){
				ret=$(this).data('jslide')[args[0]](args[1]);
			});
			return ret;
		};
		
		
		
		return this.each(function(){
			
			// ============== SCRIPT VARIABLES
				var $that = $(this);
				var $slider = $that.children('ul')
			
				var pages = Math.ceil($slider.children('li').length / options.items) - 1;
			
				var swidth = (options.direction==="horizontal")? (options.width * (pages+1)) : options.width;
				var sheight = (options.direction==="horizontal")? options.height : (options.height * (pages+1)) ;
			
			
			// ============== STARTING POSITION INITIALIZATION
				if(options.page > -1 && options.page <= pages){
					var curpos = options.page;
				}else {
					var curpos = 0;
				};
				
			// ============== PAGINATION INITIALIZATION
				if(options.paginate.paginate===true){
					for(var i=0, j=pages; i<(j+1); i++){
						(function(i){
							$('<a href="#">' + (i+1) + '</a>')
								.appendTo(options.paginate.object)
								.bind('click', function(){
									api.goTo(i)
									return false;
								})
							;
						})(i);
					};
				}
			
			
			// ============== CSS INITIALIZATION
				$that.css({
					overflow: "hidden",
					position: "relative",
					width: options.width,
					height: options.height
				});
			
				$slider.css({
					position: "absolute",
					top: (options.direction==="horizontal")?0:-(curpos*options.height),
					left: (options.direction==="horizontal")?-(curpos*options.width):0,
					listStyle: "none",
					padding: 0,
					margin: 0,
					width: swidth,
					height: sheight
				});
			
				$slider.children('li').css({
					float: "left"
				});
				
				if (options.loop===false && curpos===0){
					$(options.prev).css({visibility: "hidden"});
				};
				
				if (options.loop===false && curpos===pages){
					$(options.next).css({visibility: "hidden"});
				};
				
				
				
			// ============== ANIMATION / EVENT CODE
				var anim = function(){
					if(options.direction==="horizontal"){
						$slider.stop(true).animate({
							left: -(options.width * curpos)
						}, options.speed, options.easing);
					}else {
						$slider.stop(true).animate({
							top: -(options.height * curpos)
						}, options.speed, options.easing);
					};
				};
				
				var prevFn = function(){
					if(curpos > 0){
						curpos --;
					}else if (curpos === 0 && options.loop === true){
						curpos = pages;
					};
					
					if(options.loop === false && curpos <= 0){
						$(options.prev).css({visibility: "hidden"});
					};
					
					if(options.loop === false){
						$(options.next).css({visibility:"visible"});
					};
					
					anim();
					
					return false
				};
				
				var nextFn = function(){
					if(curpos < pages){
						curpos ++;
					}else if (curpos === pages && options.loop === true){
						curpos = 0;
					};
					
					if(options.loop === false && curpos >= pages){
						$(options.next).css({visibility: "hidden"});
					};
					
					if(options.loop === false){
						$(options.prev).css({visibility:"visible"});
					};
					
					anim();
					
					return false
				};
				
				$(options.prev).bind('click', prevFn);
				$(options.next).bind('click', nextFn);
				
				
			
			// ============== PUBLIC API
				var api = {};
				
				api.next = nextFn;
				api.prev = prevFn;
				
				api.goTo = function(n){
					if(curpos > n){
						curpos = n + 1;
						prevFn();
					}else {
						curpos = n-1;
						nextFn();
					};
				};
				
				api.get = function(){
					return {
						max: pages,
						page: curpos,
						options: options
					};
				};
				
				$that.data('jslide', api)
				
		})
	}
})(jQuery);
