/* ------------------------------------------------------------------------
	s3Slider
	
	Developed By: Boban Karišik -> http://www.serie3.info/
        CSS Help: Mészáros Róbert -> http://www.perspectived.com/
	Version: 1.0
	
	Modified by: James Hallam -> http://www.tjs.co.uk/
	
	Copyright: Feel free to redistribute the script/modify it, as
			   long as you leave my infos at the top.
------------------------------------------------------------------------- */


(function($){  

    $.fn.s3Slider = function(vars) {       
        
        var element     = this;
        var timeOut     = (vars.timeOut != undefined) ? vars.timeOut : 4000;
        var current     = null;
        var timeOutFn   = null;
        var faderStat   = true;
        var items       = $("#" + element[0].id + "Content ." + element[0].id + "Image");
        var itemsSpan   = $("#" + element[0].id + "Content ." + element[0].id + "Image span.description");
        var animate_to_left		= 20;
        var animate_to_right		= 20;        
                   
        var fadeElement = function() {
            var thisTimeOut = timeOut;
            thisTimeOut = (faderStat) ? 10 : thisTimeOut;
            if(items.length > 0) {
                timeOutFn = setTimeout(makeSlider, thisTimeOut);
            } else {
                console.log("Poof..");
            }
        }
        
        var makeSlider = function() {
            current = (current != null) ? current : items[(items.length-1)];
            var currNo      = jQuery.inArray(current, items) + 1
            currNo = (currNo == items.length) ? 0 : (currNo - 1);
            if(faderStat == true) {
                $(itemsSpan[currNo]).css('display', 'block');
                var width = $(itemsSpan[currNo]).css('width');
                width = parseInt(width.substr(0, width.length-2));
                //alert(currNo+' Width: '+width);
                
                if($(itemsSpan[currNo]).hasClass('left')){
                	var animate = width+animate_to_left+'px';
					$(itemsSpan[currNo]).css('left', '-'+animate);
				} else if($(itemsSpan[currNo]).hasClass('right')){
					var animate = width+animate_to_right+'px';
					$(itemsSpan[currNo]).css('right', '-'+animate);
				}
				
				//alert(currNo+' Animate: '+animate);
				
                $(items[currNo]).fadeIn((timeOut/6), function() {
                    if($(itemsSpan[currNo]).hasClass('left')){
                        $(itemsSpan[currNo]).animate({ left: animate_to_left+'px' }, (timeOut/6), function(){
	                        appear(currNo);
                        });
					} else if($(itemsSpan[currNo]).hasClass('right')){
						$(itemsSpan[currNo]).animate({ right: animate_to_right+'px' }, (timeOut/6), function(){
	                        appear(currNo);
                        });
					}
                });
            } else {
				$(items[currNo]).fadeOut((timeOut/6));
				var width = $(itemsSpan[currNo]).width();
                if($(itemsSpan[currNo]).hasClass('left')){
                    var animate = width+animate_to_left+'px';
                    $(itemsSpan[currNo]).animate({ left: '-'+animate }, (timeOut/6), function(){
                        disappear(currNo);
                    });
				} else if($(itemsSpan[currNo]).hasClass('right')){
					var animate = width+animate_to_right+'px';
					$(itemsSpan[currNo]).animate({ right: '-'+animate }, (timeOut/6), function(){
	                    disappear(currNo);
                    });
				}                       
            }
        }
        
        var appear = function(currNo){
    		faderStat = false;
	        current = items[currNo];
            fadeElement();
		}
		
		var disappear = function(currNo){
			faderStat = true;
		    current = items[(currNo+1)];
	        fadeElement();
		}
        
        makeSlider();

    };

})(jQuery);  