/**
 * Plugin: TAO Slider
 * author: roman.weinberger@tao.at
 */
(function($) {
    /** 
	 * one slider for each matched element
	 */
    $.fn.tao_slider = function(options) {
        return this.each(function() { new $.tao_slider(this, options); });
    };
	
	// the defaults
	var default_options = {
		change_cb : false, // callback changed(new_id, old_id)
		contents_enabled : true,
		contents_element : false, // alternative - additional source element
		images_enabled : true,
		image_effects: {
			show: {
				type: 'alpha',
				effect_options: {},
				effect_duration: 1800
			},
			hide: {
				type: 'alpha',
				effect_options: {},
				effect_duration: 1800
			}
		},
		content_effects: {
			hide : {
				type: 'alpha',
				effect_options: {},
				effect_duration: "slow",
				timout: 0
			},
			show : {
				type: 'slide',
				effect_options: {direction:'down'},
				effect_duration: "slow",
				timeout: 120
			}
		}
	};

	/**
	 * create a slider
	 * options:
	 *  - 
	 */
	$.tao_slider = function(element, options) {
		this.options = $.extend({}, default_options, options || {});
		this.images = [];
		this.contents = [];
		this.current_idx = 0;
		
		this.element = element;
		var pass_this = this;
		
		if( this.options.images_enabled ) {
			// prepare images
			$(element).find('div.image').each(function(idx, img) {
				if( idx != pass_this.current_idx ) $(img).hide();
				else $(img).show();
				pass_this.images[idx] = img;
			});
		}
		
		var celement = options.contents_element || element;
		
		if( this.options.contents_enabled ) {
			// prepare contents
			$(celement).find('div.content').each(function(idx, content) {
				if( idx != pass_this.current_idx ) {
					$(content).hide();
				} else {
					$(content).show();
				}
				pass_this.contents[idx] = content;
			});
		}
		
		this.button_next = $(element).find('a.next');
		this.button_prev = $(element).find('a.prev');

		this.button_next.bind('click', function(e) { e.target.blur(); pass_this.button_next_click(); return false; });
		this.button_prev.bind('click', function(e) { e.target.blur(); pass_this.button_prev_click(); return false; });
		return this;
	};
	
	$.tao_slider.fn = $.tao_slider.prototype = { tao_slider: '0.0.1' };
    $.tao_slider.fn.extend = $.tao_slider.extend = $.extend;

    $.tao_slider.fn.extend({
		button_next_click : function() {
			var next_idx = this.current_idx + 1;
			if( typeof this.images[next_idx] == "undefined" ) { next_idx = 0; }
			this.do_switch(this.current_idx, next_idx);
			this.current_idx = next_idx;
		},
		button_prev_click : function() {
			var prev_idx = this.current_idx - 1;
			if( prev_idx < 0 ) { prev_idx = this.images.length-1; }
			this.do_switch(this.current_idx, prev_idx);
			this.current_idx = prev_idx;
			
		},
		switch_to : function(new_idx) {
			if( new_idx != this.current_idx ) {
				this.do_switch(this.current_idx, new_idx);
				this.current_idx = new_idx;
				return true;
			} else {
				return false;
			}
		},
		do_switch : function(old_idx, new_idx) {
			if( this.options.images_enabled ) {
				var ie = this.options.image_effects;
				this.perform_effect($(this.images[old_idx]), ie.hide, "hide")
				this.perform_effect($(this.images[new_idx]), ie.show, "show")
				var ce = this.options.content_effects;
			}
			
			if( this.options.contents_enabled ) {
				var pass_this = this;
				var ele = $(this.contents[old_idx]);
				pass_this.perform_effect(ele, ce.hide, "hide");

				ele = $(this.contents[new_idx]);
				if( ce.show.timeout == 0 ) 
					pass_this.perform_effect(ele, ce.show, "show");
			 	else
					setTimeout(
						function() {
							pass_this.perform_effect(ele, ce.show, "show");
						}, 
						ce.show.timeout
					);				
			}
			
			if( this.options.change_cb ) {
				this.options.change_cb(new_idx, old_idx);
			}
		},
		perform_effect : function(element, options, showhide) {
			if( showhide == "hide" ) {
				if( options.type == 'alpha' ) { 
					element.fadeOut(options.effect_duration)
				} else {
					element.hide(options.type, options.effect_options, options.effect_duration);
				}
			} else {
				if(options.type == 'alpha' ) {
					element.fadeIn(options.effect_duration)
				} else {
					$(element).show(options.type, options.effect_options, options.effect_duration);
				}
			}
		}
		
		
	});
	
	
	
})(jQuery);