/*
Copyright (c) 2007, Caridy Patino. All rights reserved.
Portions Copyright (c) 2007, Yahoo!, Inc. All rights reserved.
Code licensed under the BSD License:
http://www.bubbling-library.com/eng/licence
version: 2.0.0
*/
(function() {
  var $L = YAHOO.lang,
	  $E = YAHOO.util.Event,
	  $D = YAHOO.util.Dom,
	  $ =  YAHOO.util.Dom.get;

	/**
	* @singleton WizardManager - Use this object to management your wizards...
	* Apply dynamic functionality to a wizard widget
	* @constructor
	*/
    YAHOO.widget.SlideShow = function() {
		var obj = {},
			_areas = {},
			_className = 'yui-cms-slideshow',
			_defConf   = {
							timer: 3, // seconds
							caption: null, // top or bottom
							align: null, // right or left
							xy: null, // position
							ds: [],
							width: null, 
							height: null
						 };		
		function setInnerHTML (area) {
			var p = area.position;
			p++;
			if (p >= area.ds.length) {
				p = 0;
			}
			var i = area.ds[p];
			area._fake.innerHTML = '<a href="'+i.uri+'" title="'+i.caption+'"><img src="'+i.image+'" /></a>';
		}
		function next (id) {
			var el, vp, i, area;
			area = _areas[id];
			if ($L.isObject(area)) {
			    window.clearTimeout(area.handle);
			    area.position++;
			    if (area.position >= area.ds.length) {
					area.position = 0;
			    }
				$D.setStyle(area._caption, 'display', 'none');
				el = area._fake;
				i = area.ds[area.position];
				el.innerHTML = '<a href="'+i.uri+'" title="'+i.caption+'"><img src="'+i.image+'" /></a>';
                if ((area.anim) && (area.anim.isAnimated())) { area.anim.stop(); }
    	        vp = $D.getRegion (area.element);
			    vp.height = vp.bottom-vp.top;vp.width = vp.right-vp.left;
			    $D.setStyle(el, 'display', 'none');
			    $D.setStyle(el, 'opacity', 0);
			    $D.setXY(el, [vp.left, vp.top]);
				$D.setStyle(el, 'display', 'block');
				// starting a new animation
				area.anim = new YAHOO.util.Anim(el, {opacity: { from: 0, to: 1} }, 1.5, YAHOO.util.Easing.easeIn);
        	    area.anim.animate();
			    area.anim.onComplete.subscribe(function(){

					// switching the contents
					area.element.innerHTML = '';
					var items = area._fake.childNodes, k;
					// Loop through each result
					for(k = 0; k < items.length; k++) {
						area.element.appendChild(items[k]);
					}
					
					window.setTimeout(function() {
						$D.setStyle(area._fake, 'display', 'none');
						setInnerHTML (area);
					}, Math.abs(area.timer*500));
					
					if (area.caption && area.ds[area.position].caption) {
					  // possitioning the caption
					  area._caption.innerHTML = area.ds[area.position].caption;
					  $D.setStyle(area._caption, 'display', '');
					  var cp = $D.getRegion (area._caption);
			    	  cp.height = cp.bottom-cp.top;cp.width = cp.right-cp.left;
					  if (area.caption == 'bottom') {
						$D.setXY(area._caption, [vp.left, vp.bottom]);
					  } else {
						$D.setXY(area._caption, [vp.left, vp.top-cp.height]);  
					  }
					  if (area.align == 'right') {
						$D.setX(area._caption, vp.right-cp.width);
					  }
					}
					window.clearTimeout(area.handle);
					area.handle=null;
					if ($L.isNumber(area.timer) && (area.timer > 0)) {
					  area.handle = window.setTimeout(function() {
						next( id );
					  }, Math.abs(area.timer*1000));
					}					
				});
			}
		}				 
		function reset (area) {
			if ($L.isObject(area) && $L.isObject(area.element = $(area.id)) && area.ds) {
			  // applying the styles
			  if (area.width) {
				  $D.setStyle (area.element, 'width', area.width);
			  }
			  if (area.height) {
				  $D.setStyle (area.element, 'height', area.height);
			  }
			  if (area.xy) {
				  $D.setStyle (area.element, 'left', area.xy[0]+'px');
				  $D.setStyle (area.element, 'top', area.xy[1]+'px');
			  }			  
			  // start the animation
			  area.position = -1;
			  window.clearTimeout(area.handle);
			  area.handle = null;
			  // creating the fake element
			  area._fake = area.element.cloneNode(true);
              var el = new YAHOO.util.Element(area._fake, {
                innerHTML: '',
				id: area.id+'-fake',
				display: 'none'
              });
			  $D.setStyle(area._fake, 'display', 'none');
			  setInnerHTML (area);
			  el.appendTo(area.element.parentNode);	
			  $D.setStyle(area._fake, 'zIndex', parseInt($D.getStyle(area.element, 'zIndex'), 10)+1);
			  // caption area
			  area._caption = document.createElement('div');
              el = new YAHOO.util.Element(area._caption, {
                innerHTML: '',
				id: area.id+'-caption',
				display: 'none'
              });
			  el.appendTo(area.element.parentNode);
			  $D.addClass(area._caption, _className+'-caption');
			  next (area.id);
			}
		}
		function check (area) {
			var items = [], ds = [], k, a, i;
			if ($L.isObject(area) && $L.isObject(area.element)) {
			      // analysing the content of the area...
			  	  items = [];
			  	  // searching for li elements
				  items.merge(area.element.getElementsByTagName('li'));
	              // Loop through each result
	              for(k = 0; k < items.length; k++) {
					a = items[k].getElementsByTagName('a')[0];
					i = a.getElementsByTagName('img')[0];
					if (a && i) {
						ds.push({image: i.getAttribute('src'), uri: a.getAttribute('href',2), caption: a.getAttribute('title')});
					}
				  }
				  // everything was fine here...
				  if (ds.length > 0) {
				    area.status = true;
				    area.ds = ds;
	                reset(area);
				  }
			}
		}
	    var onFinish = function ( area ) {
			if ($L.isFunction(area.onFinish)) {
				area.onFinish.apply ( area, [area.values] );
			}
	    };
		var onStart = function ( area ) {
			if ($L.isFunction(area.onReady)) {
				area.onReady.apply ( area, [area.values] );
			}
		};
		// public vars
		// public methods
		/**
		* * get the current status of the area
		* @public
		* @param {string} id   area Id
		* @return string
		*/
		obj.getStatus = function ( id ) {
			if ($L.isObject(_areas[id])) {
				return _areas[id].status;
			}
			return false;
		};
		/**
		* * get the datasource for the current area
		* @public
		* @param {string} id   area Id
		* @return boolean
		*/
		obj.getDataSource = function ( id ) {
			if (this.getStatus(id)) {
				return _areas[id].ds;
			}
			return false;
		};
		/**
		* * set the datasource for the current area
		* @public
		* @param {string} id   area Id
		* @param {string} ds   json object
		* @return Object
		*/
		obj.setDataSource = function ( id, ds ) {
			if (this.getStatus(id)) {
				var area = _areas[id];
				try {
                  area.ds = $L.JSON.parse(ds);
                }
                catch (e) {
                  // invalid json datasource
                }
				return area.ds;
			}
			return null;
		};
		/**
		* * configure the area properties
		* @publicf
		* @param {string} id
		* @param {object} userConfig
		* @return Object
		*/
		obj.init = function (id, userConfig) {
			var c = {};
			$L.augmentObject(c, _defConf, true);
			$L.augmentObject(c, userConfig, true);
			if ($L.isObject(id)) {
				id = $E.generateId(id);
			}
			// recompiling the properties of the current area...
			if ($L.isObject(_areas[id])) {
				$L.augmentObject(c, _areas[id], true);
			}
			$D.addClass(c.id, _className);
			_areas[c.id] = c;
			return c;
		};
		/**
		* * add a new area to the list
		* @public
		* @param {string} id
		* @param {object} userConfig
		* @return Object
		*/
		obj.add = function ( id, userConfig ) {
			var c = userConfig || {},
				el = c.id || id ||  c.element;
			if ($L.isString(el) && (el !== '')) {
				el = $(el);
			}
			if ($L.isObject(el) && $L.isObject(c.ds)) {
				c.id = $E.generateId(el);
				c.element = el;
				c.status = true;
				this.remove(c.id);
				this.init(id, c);
				if (_areas[id].ds.length > 0) {
				  reset ( _areas[id] );
				}
			}
			return _areas[id];
		};
		/**
		* * adopt an area with the content inside and add to the list
		* @public
		* @param {string} id
		* @param {object} userConfig
		* @return Object
		*/
		obj.adopt = function ( id, userConfig ) {
			var c = userConfig || _defConf,
				el = c.id || id ||  c.element;
			if ($L.isString(el) && (el !== '')) {
				el = $(el);
			}
			if ($L.isObject(el)) {
				c.id = $E.generateId(el);
				c.element = el;
				c.status = false;
				c.ds = {};
				this.remove(c.id);
				this.init(id, c);
				// process the current area's content...
				check ( _areas[c.id] );
			}
			return _areas[id];
		};
		/**
		* * Remove an area from the stock...
		* @public
		* @param {object} id	className
		* @return void
		*/
		obj.remove = function ( id ) {
			if ($L.isObject(id)) {
				id = $E.generateId(id);
			}
			if (id && (_areas[id])) {
				_areas[id].handle = null; // resetting the handle
				_areas[id] = null; // discarding the area...
			}
			_areas[id] = [];
		};
		return obj;
    }();

})();
YAHOO.register("slideshow", YAHOO.widget.SlideShow, {version: "2.0.0", build: "224"});
/*
Markup definition...
<ul>
  <li><a href="/path/to/link.html" title="image caption here..."><img src="/path/to/image.jpg" /></a></li>
  <li><a href="/path/to/link.html" title="image caption here..."><img src="/path/to/image.jpg" /></a></li>
  <li><a href="/path/to/link.html" title="image caption here..."><img src="/path/to/image.jpg" /></a></li>
</ul>
*/