/* Copyright (c) 2008 Parsek (http://www.parsek.si)
 * Licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
 * Copyright notice and license must remain intact for legal use
 * jquery.vitlaj 0.1
 * Author: Rod Petrovic (petrovic@parsek.net)
 * Version: 0.2 (June 30, 2009)
 * Requires: jQuery 1.2+
 */
(function ($) {

    $.fn.vitlaj = function (config) {
        var default_config = {
            appear: 'fadeIn',
            disappear: 'fadeOut',
            speed: 1000,
            wait: 5000,
			manual_switch:''
        };
        config = $.extend({}, default_config, config);
        var to;
        var active_index;
		var effects = {
            'fadeIn': {opacity: 'show'},
            'fadeOut': {opacity: 'hide'},
            'slideDown': {height: 'show'},
            'slideUp': {height: 'hide'}
        };
        var appear = effects[config.appear] ? effects[config.appear] : config.appear;
        var disappear = effects[config.disappear] ? effects[config.disappear] : config.disappear;
        
        var objects = $(this).children().get();
        $(objects).hide();
        
        var transition = function (index, object, manual) {
			if(active_index == index) return;
            if(manual)$(object).parent().children().fadeOut();
			$(object).animate(appear, config.speed, function () {
                var next = index + 1 < objects.length ? index + 1 : 0;
				if(!manual){
				   to = setTimeout(function () {
						transition(next, objects[next]);
						$(object).animate(disappear, config.speed);
					}, config.wait);
				} 
            });
			active_index = index;
        };
		
		transition(0, objects[0]);
        
		if(config.manual_switch!=''){
			if($(config.manual_switch).length>0){
				$(config.manual_switch).click(function(){
					clearTimeout(to);
					var index = this.href.split('#')[1]-1;
					transition(index,objects[index],true);
				});
			}
		}
        
    }

})(jQuery);
