/*
 * 	Easy Slider - jQuery plugin
 *	written by Alen Grakalic	
 *	http://cssglobe.com/post/3783/jquery-plugin-easy-image-or-content-slider
 *	Copyright (c) 2009 Alen Grakalic (http://cssglobe.com)
 *	Dual licensed under the MIT (MIT-LICENSE.txt)
 *	and GPL (GPL-LICENSE.txt) licenses.
 */
(function($) {
	/* JQuery general element replace function : if useful, move out into global.
	*/
	$.fn.replace = function() {
		var stack = [];
		return this.domManip(arguments, true, 1, function(a){
			this.parentNode.replaceChild( a, this );
			stack.push(a);
		}).pushStack( stack );
	};
	
	$.fn.easySlider = function(options){
		// default configuration properties
		var defaults = {
			prevId: 		'prevBtn',
			prevText: 		'Previous',
			nextId: 		'nextBtn',	
			nextText: 		'Next',
			pageId:			'page',
			pageNumbers:	true,
			orientation:	'', //  'vertical'
			speed: 			800
		}; 
		
		var options = $.extend(defaults, options);  
		
		return this.each(function() {  
			obj = $(this);
			var s = $("li", obj).length;
			var w = obj.width(); 
			var h = obj.height(); 
			var ts = s;
			var t = 1;
			var vertical = (options.orientation == 'vertical');
			$("ul", obj).css('width',s*w);			
			if(!vertical) $("li", obj).css('float','left');
			$(obj).after('<span id="'+ options.prevId +'"><a href=\"javascript:void(0);\">'+ options.prevText +'</a></span> <span id="'+ options.nextId +'"><a href=\"javascript:void(0);\">'+ options.nextText +'</a></span>');
			$(obj).after('<span id="'+ options.pageId + '"></span>');
			/* do page numbering */
			if (options.pageNumbers) {
				for(i=1;i<s+1;i++){
					$('#'+options.pageId).append('<a rel="'+ i +'" id="'+options.pageId +'-'+ i +'" class="'+options.pageId+'-numbers" href="javascript:void(0);">'+ i +'</a>');
				}
				$('.'+options.pageId+'-numbers').click(function(){
					t=Number($(this).attr('rel'));
					$('.'+options.pageId+'-current').attr('class',options.pageId+'-numbers');
					$('#'+options.pageId+'-'+t).attr('class',options.pageId+'-current');
					animate(null);
				});
				/* set first page to current */
				$('#'+options.pageId+'-'+1).attr('class',options.pageId+'-current');
			}
			$("a","#"+options.nextId).click(function(){		
				if (options.pageNumbers) {
					p = (t>=ts) ? 1 : t+1;
					$('.'+options.pageId+'-current').attr('class',options.pageId+'-numbers');
					$('#'+options.pageId+'-'+p).attr('class',options.pageId+'-current');
				}
				animate("next");				
			});
			$("a","#"+options.prevId).click(function(){		
				if (options.pageNumbers) {
					p = (t<=1) ? ts : t-1;
					$('.'+options.pageId+'-current').attr('class',options.pageId+'-numbers');
					$('#'+options.pageId+'-'+p).attr('class',options.pageId+'-current');
				}
				animate("prev");				
			});
			
			function animate(val){
				if(val == "next"){
					t = (t>=ts) ? 1 : t+1;
				} else if(val == "prev") {
					t = (t<=1) ? ts : t-1;
				}
				if(!vertical) {
					p = ((t-1)*w*-1);
					
					$("ul",obj).animate(
						{ marginLeft: p }, 
						options.speed
					);				
				} else {
					p = ((t-1)*h*-1);
					$("ul",obj).animate(
						{ marginTop: p }, 
						options.speed
					);					
				}
			};
			if(s>1) $("a","#"+options.nextId).fadeIn();	
		});
	  
	};

})(jQuery);