/*
 * 	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
 *
 */
 
/*
 *	markup example for $("#images").easySlider();
 *	
 * 	<div id="images">
 *		<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.easySlider = function(options){
	  
		// default configuration properties
		var defaults = {
			prevId: 		'prevBtn',
			prevText: 		'Previous',
			nextId: 		'nextBtn',	
			nextText: 		'Next',
			orientation:	'', //  'vertical' is optional;
			speed: 			800,
			widthS:			400,
			totalwidth:		0,
			paddingItem:0,
			marginItem:0,
			Items:0,
			WrapGalleryId:"wrapgallery"
			
		}; 
		
		var options = $.extend(defaults, options);  
		
		return this.each(function() {  
			obj = $(this); 		
			total = options.totalwidth + options.paddingItem * options.Items  + options.marginItem * options.Items ;			
			var s = 0
			var w = 400; 
			w = options.widthS;
			var h = obj.height(); 
			s = Math.ceil(total/w);
			var ts = s-1;
			var t = 0;
			// ts = Math.ceil(total /w);
			// s = ts + 1
			var vertical = (options.orientation == 'vertical');			
			$("#items",obj).css('width',total);

			$(obj).after('<div id="'+ options.nextId +'">'+ options.nextText +'</div>');		
			$(obj).before('<div id="'+ options.prevId +'">'+ options.prevText +'</div>');		
			
			$("#"+options.prevId, "#"+options.WrapGalleryId).hide();
			$("#"+options.nextId, "#"+options.WrapGalleryId).hide();
			$("#"+options.nextId, "#"+options.WrapGalleryId).click(function(){		
				animate("next");
				if (t>=ts) $(this).fadeOut();
					$("#"+options.prevId, "#"+options.WrapGalleryId).fadeIn();
			});
			$("#"+options.prevId, "#"+options.WrapGalleryId).click(function(){		
				animate("prev");
				if (t<=0) $(this).fadeOut();
					$("#"+options.nextId, "#"+options.WrapGalleryId).fadeIn();
			});		

			var container = $("#sliderWrap");
			var itsss = $("div#items", obj);			
			var itemsWidth = itsss.innerWidth() - 950;
			
			$(".hanlddiv", obj).slider({
				min: 0,
				max: itemsWidth,
				handle: ".handle",
				stop: function (event, ui) {
					itsss.animate({"left" : ui.value * -1}, 500);				
				},
				slide: function (event, ui) {
					itsss.css("left", ui.value * -1);
					
				}
				
			});				
			
			function animate(dir){
				if(dir == "next"){
					t = (t>=ts) ? ts : t+1;	
				} else {
					t = (t<=0) ? 0 : t-1;
				};								
				if(!vertical) {
					p = (t*w*-1);
					if(dir == "next" && t == (ts)){
						p = -1*(total - w);
					}						

					$("#items",obj).animate(
						{ left: p }, 
						options.speed
					);
					
					lhandle = Math.round(((p* -1) * 834/itemsWidth));
					if(lhandle <0)
						lhandle =0;
					if(lhandle >= itemsWidth)
						lhandle =itemsWidth;
						
					$(".handle",obj).animate(
						{ left:  lhandle }, 
						options.speed
					);
				} else {
					p = (t*h*-1);
					$("#items",obj).animate(
						{ marginTop: p }, 
						options.speed
					);					
				}
			};
			if(s>1) $("#"+options.nextId, "#"+options.WrapGalleryId).fadeIn();	
		});
			

	};

})(jQuery);