/*
 * 	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.
 *
 *	Built for jQuery library
 *	http://jquery.com
 *
 */ 
(function($) {
	$.fn.easySlider = function(options){
		// default configuration properties
		var defaults = {
			prevId: 		'prevBtn',
			prevText: 		' ',
			nextId: 		'nextBtn',	
			nextText: 		' ',			
			speed: 			500	
				}; 
		var options = $.extend(defaults, options);
		return this.each(function() {  
			obj = $(this);
			span = $("span", obj)[0];
			width = $("#sliderOutside").width();
			count = $("span", obj).length
			max = Math.floor(width / $(span).width());
			var slider = {
				c: count,
				m: max,
				w: width/max,
				h: $(obj).height(),
				ts: count - max,
				t: 0,
				vertical: (options.orientation == 'vertical'),
				obj: obj
			}
			$("span", obj).css('width', slider.w);
			$(span).css('marginLeft', 0);
			obj.css('width', slider.w * slider.c); 
			$("a","#"+options.prevId).hide();
			$("a","#"+options.prevId).css('visibility', 'visible');
			if (slider.c > slider.m) {
				$("a","#"+options.nextId).show();
			} else {
				$("a","#"+options.nextId).hide();
			}
			$("a","#"+options.nextId).css('visibility', 'visible');
			$("a","#"+options.nextId).click(function(){
				animate("next");
				if (slider.t>=slider.ts) $(this).fadeOut();
				$("a","#"+options.prevId).fadeIn();
			});
			$("a","#"+options.prevId).click(function(){
				animate("prev");
				if (slider.t<=0) $(this).fadeOut();
				$("a","#"+options.nextId).fadeIn();
			});	

			function animate(dir){
				if(dir == "next"){
					slider.t = (slider.t>=slider.ts) ? slider.ts : slider.t + slider.m;				
				} else {
					slider.t = (slider.t<=0) ? 0 : slider.t - slider.m;
				};								
				if(!slider.vertical) {
					p = (slider.t * slider.w * -1);
					slider.obj.animate(
						{ marginLeft: p }, 
						options.speed
					);				
				} else {
					p = (slider.t * slider.h * -1);
					slider.obj.animate(
						{ marginTop: p }, 
						options.speed
					);					
				}
			};
		});
	};
})(jQuery);
