/*
Copyright (c) 2007, Jorge Luis Dorta Palmero. All rights reserved.
Portions Copyright (c) 2007, 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 jldorta@gmail.com
 * @module slideshow
 * @namespace YAHOO.util
 * @requires yahoo, dom, animation
 */
YAHOO.namespace("Slideshow");

(function() {
	var $D = YAHOO.util.Dom;
	
	/**
     * Web Slideshow Constructor.
     * @method init
     * @param {String | HTMLElement} 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, el_container, fadeimages, fadewidth, fadeheight, delay, random) {
		if (el) {
	        this.init(el, el_container, fadeimages, fadewidth, fadeheight, delay, random); 
			_timer = null;
	    };
	};
	
	YAHOO.Slideshow.Slideshow.prototype = {

		init: function(el, el_container, fadeimages, fadewidth, fadeheight, delay, random) {			
			var _container = $D.get(el);
			
			this.imagesCount = fadeimages.length;
			if ((random != 'undefined') && (random)){
				//fadeimages.sort(function() {return 0.5 - Math.random();});
			}
			
			var _elem_container = $D.get(el_container);

			var elements = _elem_container.getElementsByTagName('span');
			
			var innerHTML = '<div id="master_'+el+'" style="margin-left:0px;position:relative;width:'+fadewidth+'px;height:'+fadeheight+'px;overflow:hidden;z-index:1;">';
			for (var i = (fadeimages.length-1); i >= 0; i--) {
				if (fadeimages[i][2] != '')
					innerHTML += '<div id="'+el+'_'+i+'" style="position:absolute;width:'+fadewidth+'px;height:'+fadeheight+'px;top:0;left:0;"><a href="'+fadeimages[i][2]+'" title="'+fadeimages[i][1]+'"><img src="'+fadeimages[i][0]+'" alt="'+fadeimages[i][1]+'" width="'+fadewidth+'" height="'+fadeheight+'" /></a></div>';
				else
					innerHTML += '<div id="'+el+'_'+i+'" style="position:absolute;width:'+fadewidth+'px;height:'+fadeheight+'px;top:0;left:0;"><img src="'+fadeimages[i][0]+'" alt="'+fadeimages[i][1]+'" width="'+fadewidth+'" height="'+fadeheight+'" /></div>';
				
			}
			innerHTML += '</div>';
			_container.innerHTML = innerHTML;
			for (var i = (fadeimages.length-1); i >= 0; i--) {
				var hide = new YAHOO.util.Anim(el+'_'+i, { opacity: { to: 0 } }, 0);
				hide.animate();
			}
			this.elements_container = _elem_container;
			this.fadeimages = fadeimages;
			this.container = el;
			this.image_container = _container;
			this.delay = delay;
			_image_active = 0;
			this.rotateimage();
			
		},
		
		/**
	     * Change the current image;
	     * @method rotateimage
	     * @param none
	     */ 
		rotateimage: function() {
			var slideshow = this;
			el = this.container;
			this._timer = setTimeout(function() {slideshow.rotateimage();}, this.delay*1000);
			var hide = new YAHOO.util.Anim(el+'_'+_image_active, { opacity: { to: 0 } }, 1);
			hide.animate();
			_next_image_active = (_image_active+1)%this.imagesCount;
			var show = new YAHOO.util.Anim(el+'_'+_next_image_active, { opacity: { to: 1 } }, 1);
			show.animate();
			
			x = $D.getX(this.image_container)+3;
			y = $D.getY(this.image_container)+this.fadeimages[_next_image_active][3];
			var move = new YAHOO.util.Motion(this.elements_container, { points: { to: [x, y] } }, 1, YAHOO.util.Easing.easeOut);
			move.animate();
			
			$D.setStyle(el+'_'+_image_active, 'z-index', '1');
			$D.setStyle(el+'_'+_next_image_active, 'z-index', '5');
			_image_active = _next_image_active;
		}
	};
})();

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