(function() {

  var newMethods = {
  
    fader_init : function(time) {
    	if (!time)time=2000;
    	$(this).attr("var:time", time);
    	$(this).addClass("fader_target");
    	$(this).fader_start();
    },
    
    fader_start : function() {

	  	var controller = $(this);
	   	current = controller.children(".current").get(0);

    	if(!current) {
    		// first time.
    		// show first image without fade 
	    	first = controller.children().get(0);
	    	$(first).addClass("current").css("display","block");
    		
    		// set timeout
		  	tid = setTimeout(function() {
		  		$(controller).fader_next();
		  	}, $(this).attr("var:time"));    		
    		
    	}
    },
    
    fader_next : function() {
    	var controller = $(this);
    	
	   	current = controller.children(".current").get(0);
	   	next = $(current).next().get(0);
	   	//console.log(next);
	   	
			if(!next) {
				next = controller.children().get(0);
			}
	   	
			$(current).addClass("outgoing").removeClass("current").removeClass("incoming").fadeOut("slow");
			
			$(next).addClass("current").addClass("incoming").show();
	
    	// repeat timeout
    	tid = setTimeout(function() {
    		$(controller).fader_next();
    	}, $(this).attr("var:time"));    	
    }
    
  };

  
  jQuery.each(newMethods, function(i) {
    jQuery.fn[i] = this;
  });
  
})();

