/*
 * 	Ajax Slider 1.0 - jQuery plugin
 *  based on EasySlider 1.7 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.ajaxSlider = function(options){
	  
		// default configuration properties
		var defaults = {			
			prevId: 		'prevBtn',
			prevText: 		'Previous',
			nextId: 		'nextBtn',	
			nextText: 		'Next',
			firstId: 		'firstBtn',
			firstText: 		'First',
			firstShow:		false,
			lastId: 		'lastBtn',	
			lastText: 		'Last',
			lastShow:		false,				
			vertical:		false,
			speed: 			800,
			auto:			false,
			pause:			2000,
			continuous:		false
		}; 
		
		var options = $.extend(defaults, options);  
				
		this.each(function() {  
			if($("ul", this).length==0) {
			    $(this).wrapInner("<div id='slider'><ul><li></li></ul></div>");
			}
			var myId = this.id;
			var obj = $("div", $(this));
			var s = $("li:first", obj).length;
			var w = $("li:first", obj).width(); 
			var h = $("li:first", obj).height(); 
			var clickable = true;
			obj.width(w); 
			obj.height(h); 
			obj.css("overflow","hidden");
			var ts = s-1;
			var t = 0;
			$("div:first", obj).css('width',w);		
			$("ul:first", obj).css('width',s*w);			
			
			if(options.continuous){
				$("ul:first", obj).prepend($("ul li:last-child", obj).clone().css("margin-left","-"+ w +"px"));
				$("ul:first", obj).append($("ul li:nth-child(2)", obj).clone());
				$("ul:first", obj).css('width',(s+1)*w);
			};				
			
			if(!options.vertical) $("li", obj).css('float','left');
								
			
            $("."+options.nextId, obj).click(function(){
                return clickNext(obj);
            });
            
            $("."+options.prevId, obj).click(function(){
                return clickPrev(obj);
            });	
            /*
            $(obj,"#"+options.firstId).click(function(){		
                animate("first",true);
                return false;
            });				
            $(obj,"#"+options.lastId).click(function(){		
                animate("last",true);	
                return false;			
            });		
            */
            
            function clickNext(curObj) {

                var newLi = $("<li></li>");
                curObj.children("ul:first").append(newLi);
                var loadUrl = $("."+options.nextId, curObj).attr("href")+" #"+myId+"";
                //alert("obj.ul.li.length="+obj.children("ul").children("li").length+" loading "+loadUrl);
                newLi.load(loadUrl, function(responseText, textStatus, XMLHttpRequest){
                    ts=s;
                    s++;
                    $("div:first", $(this)).css('width',w);		
                    $("ul:first", $(this)).css('width',s*w);	
                    
                    var newPage = $(this);
                    //alert(options.nextId+".length="+$("."+options.nextId, $(this)).length);
                        
                    $("."+options.nextId, newPage).click(function(){
                        return clickNext(newPage);
                    });
                    $("."+options.prevId, newPage).click(function(){
                        return clickPrev(newPage);
                    });
                    
                    animate("next", true);
                });
                return false;
            
            }
            
            function clickPrev(curObj) {

                var newLi = $("<li></li>");
                curObj.children("ul:first").prepend(newLi);
                var loadUrl = $("."+options.prevId, curObj).attr("href")+" #"+myId+"";
                //alert("obj.ul.li.length="+obj.children("ul").children("li").length+" loading "+loadUrl);
                newLi.load(loadUrl, function(responseText, textStatus, XMLHttpRequest){
                    ts=s+1;
                    s--;
                    $("div:first", $(this)).css('width',w);		
                    $("ul:first", $(this)).css('width',s*w);	
                    $("ul:first", $(this)).css('margin-left',(-2*w));	
                    
                    var newPage = $(this);
                    alert(options.prevId+".length="+$("."+options.prevId, $(this)).length);
                        
                    $("."+options.nextId, newPage).click(function(){
                        return clickNext(newPage);
                    });
                    $("."+options.prevId, newPage).click(function(){
                        return clickNext(newPage);
                    });
                    
                    animate("prev", true);
                });
                return false;
            
            }
            
			function setCurrent(i){
				i = parseInt(i)+1;
				$("li:first", "#" + options.numericId).removeClass("current");
				$("li:first#" + options.numericId + i).addClass("current");
			};
			
			function adjust(){
				if(t>ts) t=0;		
				if(t<0) t=ts;	
				if(!options.vertical) {
					$("ul",obj).css("margin-left",(t*w*-1));
				} else {
					$("ul",obj).css("margin-left",(t*h*-1));
				}
				clickable = true;
				if(options.numeric) setCurrent(t);
			};
			
			function animate(dir,clicked){
				if (clickable){
					clickable = false;
					var ot = t;				
					switch(dir){
						case "next":
							t = (ot>=ts) ? (options.continuous ? t+1 : ts) : t+1;						
							break; 
						case "prev":
							t = (t<=0) ? (options.continuous ? t-1 : 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.vertical) {
						p = (t*w*-1);
					    //alert("obj.length="+$("ul",obj).length+" diff="+diff+" p="+p+" t="+t+" w="+w+" continuous="+options.continuous);
						$("ul:first",obj).animate(
							{ marginLeft: p }, 
							{ queue:false, duration:speed, complete:adjust }
						);				
					} else {
						p = (t*h*-1);
						$("ul:first",obj).animate(
							{ marginTop: p }, 
							{ queue:false, duration:speed, complete:adjust }
						);					
					};
					
					if(clicked) clearTimeout(timeout);
					if(options.auto && dir=="next" && !clicked){;
						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.numeric) setCurrent(0);
		
			
		});
	  
	};

})(jQuery);




