/**
 * $Id$
 **/

/**
 * Neteor blocks slider
 */

var NeteorSlider = function (config) {
	this.defaultConfig = function (config, p, defaultValue) {
		if (config == undefined || config[p] == undefined || config[p] == '') {
			return defaultValue;
		} else {
			return config[p];
		}
	}

	var element = this; // création d'une référence vers l'objet courant
	this.debug				= false;
	this.element_id		=	this.defaultConfig(config, 'target', null);
	this.timeOut			= this.defaultConfig(config, 'timeout', 4000);
	this.imageTimeout	= this.defaultConfig(config, 'imageTimeout', 200);
	this.textTimeout	= this.defaultConfig(config, 'textTimeout', 150);
	this.current			= null;
	this.timeOutFn		= null;
	this.run					= true;
	this.items				= $("#" + this.element_id + "Content ." + this.element_id + "Image");
	this.itemsSpan		= $("#" + this.element_id + "Content ." + this.element_id + "Image span");
	// Effects config
	this.effects			= {image: {show: 'fadeIn', hide: 'fadeOut'}, text: {show: 'slideUp', hide: 'slideDown'}};
	if (config.effects != undefined && config.effects.image != undefined) {
		this.effects.image.show = this.defaultConfig(config.effects.image, 'show', 'fadeIn');
		this.effects.image.hide = this.defaultConfig(config.effects.image, 'hide', 'fadeOut');
	}
	if (config.effects != undefined && config.effects.image != undefined) {
		this.effects.text.show = this.defaultConfig(config.text.image, 'show', 'slideUp');
		this.effects.text.hide = this.defaultConfig(config.text.image, 'hide', 'slideDown');
	}
	

	// Check one items at least has been found
	if (this.items.length == 0) {
		alert('No items found. Please check target property.');
		return;
	}

	// Apply items events
	this.items.each(function(i) {
		$('#' + element.element_id + 'Link' + i).click(function () {
			// Remove timeout
			clearTimeout(element.timeOutFn);
			// Set pause mode
			element.pause();
			// Hide previous
			element.hide(element.current);
			// Show current
			element.show(i);
			// Set current
			element.current = i;
			// Resume timeout event handler
			element.main();
			// Enable run mode
			element.resume();
			if (element.debug) {
				console.log('manual selection ' + element.current);
			}
		});

		$(element.items[i]).mouseover(function() {
			// Pause events
			element.pause();
		});
		
		$(element.items[i]).mouseout(function() {
			// Resume events
			element.resume();
		});
	});

	/**
	 * Check items
	 */
	if (this.itemsSpan.length == 0) {
		alert('no items found');
	}

	/**
	 * Debug log function
	 */
	this.debugLog = function () {
		if (this.debug) {
			console.log('current : ' + this.current + ', run : ' + this.run + ', timeout : ' + this.timeOut);
		}
	}

	/**
	 * Image Animation event
	 */
	this.animate = function(el, anim, timeout, eventHandler) {
		if (typeof(anim) == 'object') {
			eval('el.animate(anim, timeout, eventHandler)');
		} else {
			eval('el.' + anim + '(timeout, eventHandler)');
		}
	}

	/**
	 * Show item
	 */
	this.show = function (index, showEvent) {
		if (element.debug) {console.log('show index : ' + index);}
		// Set text show callback
		showEvent = (typeof(showEvent) != 'function' ? function () {} : showEvent);
		// Set image show callback
		showImageCallback = function() {
			if($(element.itemsSpan[index]).css('bottom') == 0) {
				// Show UP span tag
				$(element.itemsSpan[index]).slideUp(element.textTimeout, showEvent);
			} else {
				// Show DOWN span tag
				$(element.itemsSpan[index]).slideDown(element.textTimeout, showEvent);
			}
		}
		// Call animate function
		this.animate($(this.items[index]), this.effects.image.show, this.imageTimeout, showImageCallback);
		// Add the "selected" CSS class to the link representing this item
		$('#' + element.element_id + 'Link' + index).addClass('selected');
	}

		/**
	 * Hide an item
	 */
	this.hide = function (index, hideEvent) {
		if (this.debug) {console.log('hide index : ' + index);}
		// Set image hide callback
		hideEvent = (typeof(hideEvent) != 'function' ? function () {} : hideEvent);
		if($(this.itemsSpan[index]).css('bottom') == 0) {
			$(this.itemsSpan[index]).slideDown(this.textTimeout, function() {
				element.animate($(element.items[index]), element.effects.image.hide, this.imageTimeout, hideEvent);
			});
		} else {
			$(this.itemsSpan[index]).slideUp(this.textTimeout, function() {
				element.animate($(element.items[index]), element.effects.image.hide, this.imageTimeout, hideEvent);
			});
		}
		// Remove the "selected" CSS class to the link representing this item
		$('#' + element.element_id + 'Link' + index).removeClass('selected');
	}

	/**
	 * Main function 
	 * this function will call itself with timeout
	 */
	this.main = function () {
		this.debugLog();
		if (this.run) {
			// Hide previous if defined
			if (this.current != null) {
				this.current++;
				// check current index
				this.current = (this.current == this.items.length ? 0 : this.current);
				// Were are already in the loop, do the "slide" effect
				this.slide(this.current);

			} else {
				// First time we enter in the loop
				this.current = 0;
				this.show(this.current);
			}
		}
		// Set timeout to next element
		this.timeOutFn = setTimeout ( function () { element.main.call(element, null); }, this.timeOut);
	}

	/**
	 * Slide function
	 * This function is called to slide items one by one
	 * hide previsous and show the next just after
	 */
	this.slide = function(index) {
		// Set hideEvent as show next slide function
		this.hide(((index == 0) ? (this.items.length - 1) : (index - 1)), function() { element.show(index)});
	}

	/**
	 * Set main function in "pause"
	 * So no changes are made
	 */
	this.pause = function () {
		this.run = false;
	}

	/**
	 * Resume main function (disable "pause" mode)
	 */
	this.resume = function () {
		this.run = true;
	}
}
