/*
Copyright (c) 2008, Jorge Luis Dorta Palmero. All rights reserved.
Portions Copyright (c) 2008, Yahoo!, Inc. All rights reserved.
Code licensed under the BSD License:
http://developer.yahoo.net/yui/license.txt
*/
/**
 * The Slideshow module provides an dinamic images slideshow to your site.
 * @author Jorge Luis Dorta Palmero jldorta@gmail.com
 * @module slideshow
 * @namespace YAHOO.slideshow
 * @requires yahoo, dom, animation
 */
YAHOO.namespace("slideshow");

/**
 * Web Slideshow Constructor.
 * @method init
 * @param {String} el Reference to the element that will be container of the Slideshow.
 * @param {Array} fadeimages The array of images to show.  
 * @param {Number} fadewidth Width of the SlideShow content.
 * @param {Number} fadeheight Height of the SlideShow content.
 * @param {Number} delay Time in seconds.
 * @param {Boolean} random Specify if slideshow is random.
 */
YAHOO.slideshow.Slideshow = function(el, delay, random) {
	if (el) {
        this.init(el, delay, random); 
    };
};

YAHOO.slideshow.Slideshow.prototype = {
	_container : null,
	_container_name : '',
	_bd : null,
	
	_timer : null,
	
	_image_active : 0,
	_images_count : 0,
	_images : Array(),
	
	delay : 5,
	random : false,
	
	init: function(el, delay, random) {			
		this._container_name = el;
		
		this._container = YAHOO.util.Dom.get(el);
		YAHOO.util.Dom.addClass(this._container, 'slideshow_container');
		
		this._bd = YAHOO.util.Dom.getElementsByClassName('bd','',this._container)[0];
		
		this._images = YAHOO.util.Dom.getChildren(this._bd);
		this._images_count = this._images.length;
		
		YAHOO.util.Dom.setStyle(this._images, 'opacity', '0');
		
		if ((delay != 'undefined') && (delay > 0)){
			this.delay = delay;
		}
		if ((random != 'undefined') && (random > 0)){
			this.random = true;
		}
		
		this._image_active = 0;
		this.rotateImage();
		
	},
	
	/**
     * Change the current image;
     * @method rotateimage
     * @param none
     */ 
	rotateImage: function() {
		var slideshow = this;
		
		var _next_image_active = 0;
		if (this.random) {
			_next_image_active = this._image_active;
			while (_next_image_active == this._image_active) 
				_next_image_active = Math.round(Math.random()*(this._images_count-1));
		} else
			_next_image_active = (this._image_active+1)%this._images_count;
		
		var hide = new YAHOO.util.Anim(this._images[this._image_active], { opacity: { to: 0 } }, 1);
		var show = new YAHOO.util.Anim(this._images[_next_image_active], { opacity: { to: 1 } }, 1);
		
		hide.animate();
		show.animate();
		
		YAHOO.util.Dom.addClass(this._images[_next_image_active], 'active');
		YAHOO.util.Dom.removeClass(this._images[this._image_active], 'active');
		
		YAHOO.util.Dom.setStyle(this._bd, 'height', YAHOO.util.Dom.getStyle(this._images[_next_image_active], 'height'));
		YAHOO.util.Dom.setStyle(this._bd, 'width', YAHOO.util.Dom.getStyle(this._images[_next_image_active], 'width'));
		
		this._image_active = _next_image_active;
		
		this._timer = setTimeout(function() {slideshow.rotateImage();}, slideshow.delay*1000);
	}
};

YAHOO.register("slideshow", YAHOO.slideshow.Slideshow, {version: "2.5.0", build: "100"});