/*
 * Easy Slider - jQuery plugin
 *	written by Alen Grakalic	
 * Modified by Thijs de Mooij
 *	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: 		'Previous',
			nextId: 		'nextBtn',	
			nextText: 		'Next',
			orientation:	'', //  'vertical' is optional;
			speed: 			800			
		}; 
		
		var options = $.extend(defaults, options);  
		
		return this.each(function() {  
			obj = $(this);			
			var s = $(obj).find("ul.screenshots li").length;
			var w = $(obj).width(); 
			var h = $(obj).height(); 
			var ts = s-1;
			var t = 0;
			var vertical = (options.orientation == 'vertical');
			$(obj).find("ul.screenshots").css('width',s*w);			
			if(!vertical) $("ul.screenshots li", obj).css('float','left');
			$(obj).append('<span id="'+ options.prevId +'"><a href=\"javascript:void(0);\" title=\"\"></a></span> <span id="'+ options.nextId +'"><a href=\"javascript:void(0);\" title=\"\"></a></span>');		
			$(obj).find("#"+options.prevId+" a").hide();
			$(obj).find("#"+options.nextId+" a").hide();
			$(obj).find("#"+options.nextId+" a").click(function(){	
				animate("next",$(this).parent().parent());
				if (t>=ts) $(this).fadeOut();
				$(this).parent().parent().find("#"+options.prevId+" a").fadeIn();
			});
			$(obj).find("#"+options.prevId+" a").click(function(){	
				animate("prev",$(this).parent().parent());
				if (t<=0) $(this).fadeOut();
				$(this).parent().parent().find("#"+options.nextId+" a").fadeIn();
			});	
			function animate(dir,element){
				if(dir == "next"){
					t = (t>=ts) ? ts : t+1;	
				} else {
					t = (t<=0) ? 0 : t-1;
				};								
				if(!vertical) {
					p = (t*w*-1);
					$(element).find("ul.screenshots").animate(
						{ marginLeft: p }, 
						options.speed
					);				
				} else {
					p = (t*h*-1);
					$(element).find("ul.screenshots").animate(
						{ marginTop: p }, 
						options.speed
					);					
				}
			};
			if(s>1) $(obj).find("#"+options.nextId+" a").fadeIn();	
		});
	  
	};

})(jQuery);