/*
 * 	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
 *
 */
(function($) {

	$.fn.easySlider = function(options){
	  
		// default configuration properties
		var defaults = {			
			prevId: 		'prevBtn',
			prevText: 		'Previous',
			nextId: 		'nextBtn',	
			nextText: 		'Next',
			controlsFade:	true,
			firstId: 		'firstBtn',
			firstText: 		'First',
			firstShow:		false,
			lastId: 		'lastBtn',	
			lastText: 		'Last',
			lastShow:		false,				
			vertical:		true,
			speed: 			500,
			auto:			false,
			pause:			5000,
			continuous:		false,
			cols:			'',
			thebt:			'',
			thebtsub:		'',
			chgtype:		'a',
			chkclass:		'',
			likclass:		'',
			overact:		'false'
		};
		var options = $.extend(defaults, options);  
				
		this.each(function() {  
			var obj = $(this); 				
			var s,w,h,ts,t,oldt;
			t = 0;
			oldt=0;
			if (options.chgtype=="a")
			{	
				s = $("li", obj).length;
				w = $("li", obj).width(); 
				h = $("li", obj).height(); 
				//obj.width(w); 
				//obj.width(h); 
				ts= s-1;
				$("ul", obj).css('width',w);	
				$("li", obj).css('float','left');
				$("li", obj).css('display','none');
				$($("li",obj).get(oldt)).css('display','block');
				$($("li","#"+options.thebt).get(oldt)).addClass("slidermenubox1")
			}
			if (options.chgtype=="b")
			{
				s = $("li", obj).length;
				w = $("li",obj).width(); 
				h = $("li",obj).height(); 
				if (options.cols!=''){
					obj.height(h*options.cols); 
					s=(s-options.cols)>0?(s-options.cols+1):1;}
				else{
					obj.height(h); }
				//alert(w+","+h);
				obj.css("overflow","hidden");
				ts= s-1;
				$("ul", obj).css('width',w);		
				if(!options.vertical) $("li", obj).css('float','left');
			}
			if (options.chgtype=="c")
			{
				s = $(".textbox", obj).length;
				ts= s-1;
				$(".textbox", obj).css('display','none');
				$($(".textbox",obj).get(oldt)).css('display','block');
				$($(options.thebtsub,"#"+options.thebt).get(oldt)).addClass(options.chkclass);
			}
			
			$("a","#"+options.nextId).click(function(){		
				animate("next",true);
			});
			$("a","#"+options.prevId).click(function(){		
				animate("prev",true);				
			});	
			$("a","#"+options.firstId).click(function(){		
				animate("first",true);
			});				
			$("a","#"+options.lastId).click(function(){		
				animate("last",true);				
			});	
			if (options.chgtype=="a")
			{
				if (options.thebt!='')
				{
					$("li","#"+options.thebt).each(function(i){
						$($("li","#"+options.thebt).get(i)).click(function(){		
						animate(i,true);				
						});	
					});
				}	
			}
			if (options.chgtype=="c" || options.chgtype=="b")
			{
				if (options.thebt!='')
				{
					$(options.thebtsub,"#"+options.thebt).each(function(i){
						$($(options.thebtsub,"#"+options.thebt).get(i)).click(function(){		
						animate(i,true);				
						});	
					});
				}
			}
			if ((options.chgtype=="c" || options.chgtype=="b") && options.overact=="true")
			{
				if (options.thebt!='')
				{
					$(options.thebtsub,"#"+options.thebt).each(function(i){
						$($(options.thebtsub,"#"+options.thebt).get(i)).mouseover(function(){		
						animate(i,true);				
						});	
					});
				}
			}
			function animate(dir,clicked){
				var ot = t;				
				switch(dir){
					case "next":
						t = (ot>=ts) ? (options.continuous ? 0 : ts) : t+1;						
						break; 
					case "prev":
						t = (t<=0) ? (options.continuous ? ts : 0) : t-1;
						break; 
					case "first":
						t = 0;
						break; 
					case "last":
						t = ts;
						break; 
					default:
						t=dir;
						break; 
				};	
				
				var diff = Math.abs(ot-t);
				var speed = diff*options.speed;	
				if (options.thebtsub!='' && options.thebt!='')
				{
					$(options.thebtsub,"#"+options.thebt).removeClass(options.chkclass);
					$(options.thebtsub,"#"+options.thebt).addClass(options.likclass);
					$($(options.thebtsub,"#"+options.thebt).get(t)).removeClass(options.likclass);
					$($(options.thebtsub,"#"+options.thebt).get(t)).addClass(options.chkclass);
				}
				if (options.chgtype=="b")
				{
					if(!options.vertical) {
						p = (t*w*-1);
						$("ul",obj).animate(
							{ marginLeft: p }, 
							speed
						);				
					} else {
						p = (t*h*-1);
						$("ul",obj).animate(
							{ marginTop: p }, 
							speed
						);					
					};
				}
				if (oldt!=t && options.chgtype=="a")
				{
					$("li","#"+options.thebt).removeClass("slidermenubox1");
					$($("li","#"+options.thebt).get(t)).addClass("slidermenubox1");
					$($("li",obj).get(t)).fadeTo("fast","0.5",function(){
						//$($("li",obj).get(t)).css('display','block');
						//$($("li",obj).get(t)).css('z-index','-1');
						obj.css("background-image","url('"+$("img",$("li",obj).get(oldt)).attr("src")+"')");
						$($("li",obj).get(oldt)).fadeTo("fast","0.5",function(){
							$($("li",obj).get(oldt)).css('display','none');
							//$($("li",obj).get(oldt)).css("z-index","-1");
							$($("li",obj).get(t)).css('display','block');
							//$($("li",obj).get(t)).css('z-index','1');
							$($("li",obj).get(t)).fadeTo("slow","1");
							oldt=t;
						});
					});
					
					if (options.chkclass!=''&&options.likclass!='')
					{
						$("li","#"+options.thebt).removeClass(options.chkclass);
						$("li","#"+options.thebt).addClass(options.likclass);
						$($("li","#"+options.thebt).get(t)).removeClass(options.likclass);
						$($("li","#"+options.thebt).get(t)).addClass(options.chkclass);
					}
				}	
				if (oldt!=t && options.chgtype=="c")
				{
					$(".textbox",obj).css('display','none');
					$($(".textbox",obj).get(t)).css('display','block');
					oldt=t;
				}

				
				if(!options.continuous && options.controlsFade){					
					if(t==ts){
						$("a","#"+options.nextId).hide();
						$("a","#"+options.lastId).hide();
					} else {
						$("a","#"+options.nextId).show();
						$("a","#"+options.lastId).show();					
					};
					if(t==0){
						$("a","#"+options.prevId).hide();
						$("a","#"+options.firstId).hide();
					} else {
						$("a","#"+options.prevId).show();
						$("a","#"+options.firstId).show();
					};					
				};				
				
				if(clicked) clearTimeout(timeout);
				if(options.auto){;
					timeout = setTimeout(function(){
						animate("next",false);
					},diff*options.speed+options.pause);
				};
				
			};
			// init
			var timeout;
			if(options.auto){;
				timeout = setTimeout(function(){
					animate("next",false);
				},options.pause);
			};		
		
			if(!options.continuous && options.controlsFade){					
				$("a","#"+options.prevId).hide();
				$("a","#"+options.firstId).hide();				
			};				
			
		});
	  
	};

})(jQuery);




