/*
 * 	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: 		'Prev',
			nextId: 		'nextBtn',	
			nextText: 		'Next',
			orientation:	'', //  'vertical' is optional;
			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 = Math.ceil(s/3) - 1;	//s-5;
			var t = 0;
			var vertical = (options.orientation == 'vertical');
			$("ul", obj).css('width',s*w);

			if(!vertical) $("li", obj).css('float','left');

			//number of pages
			var p = Math.ceil(s/3);
			var pages = '';
			//make page links
			for(var i = 1;i <= p;i++){
				pages += '<span class="page" id="p-'+i+'">'+i+'</span>';
			}

			if(p > 1)
				$(obj).after('<div id="pages"><span id="'+ options.prevId +'"><img src="/content/images/gallery-prev-gray-arrow.gif" alt=""/>'+ options.prevText +'</span> '+pages+' <span id="'+ options.nextId +'">'+ options.nextText +'<img src="/content/images/gallery-next-green-arrow.gif" alt=""/></span></div>');	

			//if click next
			$("#"+options.nextId).click(function(){
				if (t < ts) animate("next");
			});

			//if click prev
			$("#"+options.prevId).click(function(){
				if (t > 0) animate("prev");
			});

			//if click page number
			$("span.page").click(function(){
				var pg = $(this).attr("id");
				pg = pg.replace("p-","");
				//pg--;
				t = pg;
				animate("page");
			});

			//scroll images
			function animate(dir){
				if(dir == "next"){
					t = (t>=ts) ? ts : t+1;
				} else {
					t = (t<=0) ? 0 : t-1;
				};

				if(dir == 'page'){
					p = (w*t*-1);
				}
				else{
					p = (t*w*-1);
				}

				$("ul",obj).animate(
					{ marginLeft: p },
					options.speed
				);

				if(dir == "prev" && t <= 0){
					$("#"+options.prevId).addClass("active");
					$("#"+options.prevId+" img").attr("src","/content/images/gallery-prev-gray-arrow.gif");
					$("#"+options.nextId).removeClass("active");
					$("#"+options.nextId+" img").attr("src","/content/images/gallery-next-green-arrow.gif");
				}
				else if(dir == "prev" && t > ts){
					$("#"+options.prevId).removeClass("active");
					$("#"+options.prevId+" img").attr("src","/content/images/gallery-prev-green-arrow.gif");
					$("#"+options.nextId).addClass("active");
					$("#"+options.nextId+" img").attr("src","/content/images/gallery-next-gray-arrow.gif");
				}
				else if(dir == "prev"){
					$("#"+options.nextId).removeClass("active");
					$("#"+options.nextId+" img").attr("src","/content/images/gallery-next-green-arrow.gif");
					$("#"+options.prevId).removeClass("active");
					$("#"+options.prevId+" img").attr("src","/content/images/gallery-prev-green-arrow.gif");
				}

				if(dir == "next" && t <= 0){
					$("#"+options.nextId).removeClass("active");
					$("#"+options.nextId+" img").attr("src","/content/images/gallery-next-green-arrow.gif");
					$("#"+options.prevId).addClass("active");
					$("#"+options.prevId+" img").attr("src","/content/images/gallery-prev-gray-arrow.gif");
				}
				else if(dir == "next" && t >= ts){
					$("#"+options.nextId).addClass("active");
					$("#"+options.nextId+" img").attr("src","/content/images/gallery-next-gray-arrow.gif");
					$("#"+options.prevId).removeClass("active");
					$("#"+options.prevId+" img").attr("src","/content/images/gallery-prev-green-arrow.gif");
				}
				else if(dir == "next"){
					$("#"+options.nextId).removeClass("active");
					$("#"+options.nextId+" img").attr("src","/content/images/gallery-next-green-arrow.gif");
					$("#"+options.prevId).removeClass("active");
					$("#"+options.prevId+" img").attr("src","/content/images/gallery-prev-green-arrow.gif");
				}

				if(dir == "page" && t <= 0){
					$("#"+options.nextId).removeClass("active");
					$("#"+options.nextId+" img").attr("src","/content/images/gallery-next-green-arrow.gif");
					$("#"+options.prevId).addClass("active");
					$("#"+options.prevId+" img").attr("src","/content/images/gallery-prev-gray-arrow.gif");
				}
				else if(dir == "page" && t >= ts){
					$("#"+options.nextId).addClass("active");
					$("#"+options.nextId+" img").attr("src","/content/images/gallery-next-gray-arrow.gif");
					$("#"+options.prevId).removeClass("active");
					$("#"+options.prevId+" img").attr("src","/content/images/gallery-prev-green-arrow.gif");
				}
				else if(dir == "page"){
					$("#"+options.nextId).removeClass("active");
					$("#"+options.nextId+" img").attr("src","/content/images/gallery-next-green-arrow.gif");
					$("#"+options.prevId).removeClass("active");
					$("#"+options.prevId+" img").attr("src","/content/images/gallery-prev-green-arrow.gif");
				}
			};
			if(s>1){
				$("#"+options.prevId).addClass("active");
			}
		});

	};

})(jQuery);