/*
 * 	Easy Slider 1.5 - jQuery plugin
 *	written by Alen Grakalic	
 *	http://cssglobe.com/post/4004/easy-slider-15-the-easiest-jquery-plugin-for-sliding
 *
 *	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
 *
 */
 
/*
 *	markup example for $("#slider").easySlider();
 *	
 * 	<div id="slider">
 *		<ul>
 *			<li><img src="images/01.jpg" alt="" /></li>
 *			<li><img src="images/02.jpg" alt="" /></li>
 *			<li><img src="images/03.jpg" alt="" /></li>
 *			<li><img src="images/04.jpg" alt="" /></li>
 *			<li><img src="images/05.jpg" alt="" /></li>
 *		</ul>
 *	</div>
 *
 */

(function($) {

	$.fn.easySlider1 = function(optionsGallery){
	  
		// default configuration properties
		var defaultsGallery = {			
			prevId: 		'prevBtnG',
			prevText: 		'',
			nextId: 		'nextBtnG',	
			nextText: 		'',
			controlsShow:	true,
			controlsBefore:	'',
			controlsAfter:	'',	
			controlsFade:	true,
			firstId: 		'firstBtn',
			firstText: 		'First',
			firstShow:		false,
			lastId: 		'lastBtn',	
			lastText: 		'Last',
			lastShow:		false,				
			vertical:		false,
			speed: 			800,
			auto:			false,
			pause:			2000,
			continuous:		true
		}; 
		
		var optionsGallery = $.extend(defaultsGallery, optionsGallery);  
				
		this.each(function() {  
			var objGallery = $(this);
			var teste = $("li", objGallery).length;
			//alert($("li", objGallery).width());
			if(!teste%2){
				var acrescenta = 2;
				var acr = 1;
			}else{
				var acrescenta = 3;
				var acr = 2;
			}
				
			var sGallery = Math.ceil (($("li", objGallery).length));
			var wGallery = (($("li", objGallery).width()+10)*3); 
			var hGallery = $("li", objGallery).height();
			var positionGallery =  objGallery.position();
			//alert(wGallery);
			
			/*objGallery.width(wGallery); 
			objGallery.height(hGallery);*/ 
			objGallery.css("overflow","hidden");
			var tsGallery = Math.ceil (sGallery/3)-1;
			//alert(tsGallery);
			var tGallery = 0;
			$("ul", objGallery).css('width',((sGallery*wGallery)/3), 'overflow', 'hidden');				
			if(!optionsGallery.vertical) $("li", objGallery).css('float','left');
			
			if(optionsGallery.controlsShow){
				var html = optionsGallery.controlsBefore;
				if(optionsGallery.firstShow) html += '<div id="'+ optionsGallery.firstId +'"><a href=\"javascript:void(0);\">'+ optionsGallery.firstText +'</a></div>';
				html += ' <div class="rollLeft" style="left:'+(positionGallery.left-0)+'px;top:'+positionGallery.top+'px;"  id="'+ optionsGallery.prevId +'"><a href=\"javascript:void(0);\">'+ optionsGallery.prevText +'</a></div>';
				html += ' <div class="rollRight" style="left:'+(positionGallery.left+441)+'px;top:'+(positionGallery.top)+'px;" id="'+ optionsGallery.nextId +'"><a href=\"javascript:void(0);\">'+ optionsGallery.nextText +'</a></div>';
				if(optionsGallery.lastShow) html += ' <div id="'+ optionsGallery.lastId +'"><a href=\"javascript:void(0);\">'+ optionsGallery.lastText +'</a></div>';
				html += optionsGallery.controlsAfter;						
				$(objGallery).after(html);										
			};
	
			$("a","#"+optionsGallery.nextId).click(function(){		
				animate("next",true);
				$("ul", objGallery).css('margin-left','35px');
			});
			$("a","#"+optionsGallery.prevId).click(function(){		
				animate("prev",true);
				$("ul", objGallery).css('margin-left','35px');
							
			});	
			$("a","#"+optionsGallery.firstId).click(function(){		
				animate("first",true);
			});				
			$("a","#"+optionsGallery.lastId).click(function(){		
				animate("last",true);				
				
			});		
			
			function animate(dir,clicked){
				var otGallery = tGallery;
				//alert(tGallery);
				if(tsGallery>=1){
					switch(dir){
						case "next":
							tGallery = (otGallery>=tsGallery) ? (optionsGallery.continuous ? 0 : tsGallery) : tGallery+1;						
							break; 
						case "prev":
							tGallery = (tGallery<=0) ? (optionsGallery.continuous ? (tsGallery > 0 ? tsGallery : 0) : 0) : tGallery-1;
							break; 
						case "first":
							tGallery = 0;
							break; 
						case "last":
							tGallery = tsGallery;
							break; 
						default:
							break; 
					};	
				}
				//alert(tGallery);
				var diff = Math.abs(otGallery-tGallery);
				var speed = diff*optionsGallery.speed;						
				if(!optionsGallery.vertical) {
					//alert(optionsGallery.continuous);
					if(dir=='next' && tGallery>0)
						p = (((tGallery*wGallery)-35)*-1);
					else if(tGallery>0)
							p = (((tGallery*wGallery)-35)*-1);
						else
							p = (tGallery*wGallery*-1)+35;
					$("ul",objGallery).animate(
						{ marginLeft: p }, 
						speed
					);				
				} else {
					p = (tGallery*hGallery*-1);
					$("ul",objGallery).animate(
						{ marginTop: p }, 
						speed
					);					
				};
				
				if(!optionsGallery.continuous && optionsGallery.controlsFade){					
					if(tGallery==tsGallery){
						$("#"+optionsGallery.nextId).hide();
						$("#"+optionsGallery.lastId).hide();
					} else {
						$("#"+optionsGallery.nextId).show();
						$("#"+optionsGallery.lastId).show();					
					};
					if(tGallery==0){
						$("#"+optionsGallery.prevId).hide();
						$("#"+optionsGallery.firstId).hide();
					} else {
						$("#"+optionsGallery.prevId).show();
						$("#"+optionsGallery.firstId).show();
					};					
				};				
				
				if(clicked) clearTimeout(timeout);
				if(optionsGallery.auto && dir=="next" && !clicked){;
					timeout = setTimeout(function(){
						animate("next",false);
					},diff*optionsGallery.speed+optionsGallery.pause);
				};
				
			};
			// init
			var timeout;
			if(optionsGallery.auto){;
				timeout = setTimeout(function(){
					animate("next",false);
				},optionsGallery.pause);
			};		
		
			if(!optionsGallery.continuous && optionsGallery.controlsFade){					
				$("#"+optionsGallery.prevId).hide();
				$("#"+optionsGallery.firstId).hide();				
			};				
			
		});
	  
	};

})(jQuery);




