/* ------------------------------------------------------------------------
	s3Slider
	
	Developped By: Boban Karišik -> http://www.serie3.info/
        CSS Help: Mészáros Róbert -> http://www.perspectived.com/
	Version: 1.0
	
	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 start		= 0;
		var klick       = 0;
		var currNo		= 0;
		var oldNo		= 0;
		var newitem		= 0;
		var inprogress	= false;
        var element     = this;
        var timeOut     = (vars.timeOut != undefined) ? vars.timeOut : 4000;
		var time		= timeOut;
        var current     = null;
        var timeOutFn   = null;
        var faderStat   = false;
        var mOver       = false;
        var items       = $("#" + element[0].id + "Content div." + element[0].id + "Image");
        var itemsBild   = $("#" + element[0].id + "buttons ." + element[0].id + "Bild");
        var itemsBildAnz   = $("#" + element[0].id + "buttons ." + element[0].id + "BildAnz");
        var itemsAnz    = $("#" + element[0].id + "Content div." + element[0].id + "Image img").length;
        
        items.each(function(n) {
        	if ($(this).html() != '' && $(this).find('div').length == 0) return;	
        		
        	$(itemsBild[n]).fadeOut(0);
        });
        
        if(itemsAnz <= 1) {
        	itemsBildAnz.fadeOut(0);
        	$(items[0]).fadeIn(1);
        	mOver = true;
        }
        
        itemsAnz++;
		
		//Versuch der klickbarkeit
		itemsBild.each(function(j) {
					
            $(itemsBild[j]).click(function() {
				klick = 1;
				newitem = j;
            });
         
        });
            
        items.each(function(i) {
    
            $(items[i]).mouseover(function() {
            	mOver = true;
            });
            
            $(items[i]).mouseout(function() {
				if(inprogress == false && klick == 0) {
	                mOver   = false;
	                if(itemsAnz <= 2) {
	                	mOver = true;
	                }
					klick = 1;
					newitem = (currNo+1);
					time = 1000;
				}
            });
            
        });
		
		var timerstart = function() {
			current = (current != null) ? current : items[0];
            currNo = jQuery.inArray(current, items);
			
           	$(items[currNo]).fadeIn((timeOut/6), function() {
			});
			oldNo = currNo;
			current = items[(currNo+1)];
			timer();
		}
		
		var timer = function() {
			time = time - 500;
			if(klick == 0) {
				if(time <= 0) {
					time = timeOut;
					current = items[(currNo+1)];
					oldNo = currNo;
					makeslide();
				} else {
					setTimeout(timer, 500);
				}
			} else {
				klick = 0;
				time = timeOut * 2;
				current = items[newitem];
				oldNo = currNo;
				makeslide();
			}
		}
		
		var makeslide = function() {
			inprogress = true;
           	if(!mOver) {
				current = (current != null) ? current : items[0];
	            currNo      = jQuery.inArray(current, items);
				currNo = (currNo == (itemsAnz - 1)) ? 0 : currNo;
               	$(itemsBild[oldNo]).fadeIn(0, function() {
              		$(itemsBildAnz[oldNo]).fadeOut(0, function() {
               			$(itemsBild[currNo]).fadeOut(0, function() {
               				$(itemsBildAnz[currNo]).fadeIn(0, function() {
                       			$(items[currNo]).fadeIn((timeOut/6), function() {
                       				$(items[oldNo]).fadeOut((timeOut/6), function() {
										inprogress = false;
										timer();
                   					});
                      			});
                   			});
               			});
           			});
               	});
           	} else {
				inprogress = false;
				timer();
			}
		}
		
		timerstart();
		
    };  

})(jQuery);  
