/*
 * S3slideshow modified from the original one. 
 * written by Andrew Goldis as part of www.heseg-zioni.com project. 
 * agoldis@gmail.com
 * 
 */


(function($){  

    $.fn.s3Slider = function(vars) {       
        
        var element     = this;
        var timeOut     = (vars.timeOut != undefined) ? vars.timeOut : 4000;
        var currentItemNum = null;
        var items       = $("#"+element[0].id+"Content ."+element[0].id+"Image");
        var itemsSpan   = $("#" + element[0].id + "Content ." + element[0].id + "Image span");
        var maxNum = items.length-2;
        var animationGoing = false;
        var effSpeed = 2000;
        var effSpeedSpan = 500;
        var spanInitialBottom = "-70px";
        var isPlaying = true;
		
        var  timeOutFn = setTimeout('$("#s3Slider").slideNext()', timeOut);

        function endOfAnimation (nextItemNum) {
        	animationGoing = false;
        	currentItemNum = nextItemNum;
        }
        
        $.fn.pause = function () {
        	$("#pausebtn").hide();
        	$("#playbtn").show();
        	clearTimeout(timeOutFn);
        	isPlaying = false;
        }
        
        $.fn.play = function () {
        	$("#pausebtn").show();
        	$("#playbtn").hide();
        	isPlaying = true;
        	$("#s3Slider").slideNext();
        }
        
        $.fn.slidePrev = function () {
        	if(!animationGoing) {
	            if(currentItemNum == null) {
	            		currentItemNum = maxNum;
	            		switchOn(currentItemNum);
	            		return 0;
	            }
	            var nextItemNum  = (currentItemNum == 0) ? maxNum : (currentItemNum-1);
	            makeSwitch(nextItemNum);
        	}
        }
        
        $.fn.slideNext = function () {
        	if(!animationGoing) {
	            if(currentItemNum == null) {
	            		currentItemNum = 0;
	            		switchOn(currentItemNum);
	            		return 0;
	            }
	
	            var nextItemNum  = (currentItemNum == maxNum) ? 0 : (currentItemNum+1);
	            makeSwitch(nextItemNum);
        	}
			if(maxNum == 0) {
				$("#s3Slider").pause();
			}
        	if(isPlaying){
        		timeOutFn = setTimeout('$("#s3Slider").slideNext()', timeOut);
        	}
			

        }
        
        function switchOn (itemNum) {
        	animationGoing = true;
        	$(items[itemNum]).fadeIn(effSpeed, function () {
        		$(itemsSpan[itemNum]).show();
        		$(itemsSpan[itemNum]).animate({
        		bottom: "0px"
        		}, effSpeedSpan, function () {
        			endOfAnimation(itemNum);
        			});
        	});
        	
        	
        }
        
        function makeSwitch (nextItemNum) {
        	animationGoing = true;
        	$(itemsSpan[currentItemNum]).slideUp((effSpeedSpan), function () {
        		$(items[currentItemNum]).fadeOut(effSpeed, function () {
        			$(itemsSpan[currentItemNum]).css('bottom',spanInitialBottom);
        			$(items[currentItemNum]).hide(effSpeed);
        			$(itemsSpan[currentItemNum]).hide(effSpeedSpan, function () {
                		switchOn(nextItemNum); });	
        		});
            });
        }
        
        $("#s3Slider").slideNext();

    };  

})(jQuery);  
