/*
	FILENAME: jsweld.templateRotator.js
	AUTHOR: Philip Siedow-Thompson (psiedow@co.weld.co.us)
	WRITTEN: 7/7/2009
	REVISIONS: none
	DEPENDENCIES: prototype.js v.1.6.0 - (DO NOT USE ANY OTHER VERSION UNTIL TESTED!)
				  jsweld.wcDOM
				  jsweld.ajax
				  jsweld.animation
				  jsweld.rotator
*/


var jsweld;
	jsweld = (jsweld)?jsweld:{};
	jsweld.rotator;
	jsweld.rotator = (jsweld.rotator)?jsweld.rotator:{};

	/*------------------------------------------------------------------------------------
		JSWELD.rotator.baseRotator - base class of all calendars.
	--------------------------------------------------------------------------------------*/
	jsweld.rotator.TemplateRotator = Class.create(jsweld.calendar.baseRotator,
	{
		/* 
			FUNCTION: constructor
			DESCRIPTION: base constructor
			ARGUMENTS: a_node - the rotator will be built in this node clearing any nodes.
						a_time - the time |b| rotations IN SEC!
		*/
		initialize: function($super,a_node, a_sec, a_url)
		{
			$super(a_node,a_sec);
			
			this.url = a_url;
			this._imgs = new Array();
			this._height = 0;
			this._width = 0;
			this._loading = false;
			
			this.loadXML();
		},
		
		randomizePanes: function()
		{
			var randomPanes = new Array();
			
			while(this.panes.length>0)
			{
				randomPanes.push(this.panes.splice(Math.floor(Math.random()*this.panes.length),1)[0]);
			}
			
			this.panes = randomPanes;
		},
		
		loadXML: function()
		{
			var thisRef = this;
			
			var xmlLoader = new jsweld.ajax.xmlDownload(this.url, function(a_document)
			{
				if(a_document!=null)
				{
					var imagesNode = a_document.firstChild;
					if(imagesNode.nodeName.toLowerCase()=="xml")imagesNode = imagesNode.nextSibling;
					
					for(var i=0; i<imagesNode.childNodes.length; i++)
					{
						var picNode = imagesNode.childNodes[i];
						if(picNode.nodeType==1 && picNode.nodeName.toLowerCase()=='image')
						{
							var picObj = {};
							
							picObj.image = picNode.firstChild.nodeValue;
							thisRef.addPane(picObj);
						}
					}
					
					thisRef.randomizePanes();
					thisRef.buildHTML();
				}
				else throw new Error("The server did not return valid XML.");
			});
			
			xmlLoader.send();
		},
		
		/* 
			FUNCTION: next
			DESCRIPTION: incraments and displays the rotator.
		*/
		next: function($super)
		{
			if(!this._loading)$super();
		},
		
		/* 
			FUNCTION: previous
			DESCRIPTION: decraments and displays the rotator.
		*/
		previous: function($super)
		{
			if(!this._loading)$super();
		},
		
		createImg: function(a_href,a_callback)
		{
			var tNode = new Element('div').setStyle({position:'absolute', overflow:'hidden', width:(this._width+'px'), height:(this._height+'px')});
			var iNode = new Element('img');
			
			if(a_callback)iNode.onload = a_callback;
			iNode.src = a_href;
			
			tNode.appendChild(iNode);
			
			return tNode
		},
		
		/* 
			FUNCTION: buildHTML (private-ish)
			DESCRIPTION:
		*/
		buildHTML: function()
		{
			var parentDimensions = this.node.getDimensions();
			this._height = parentDimensions.height;
			this._width = (parentDimensions.width/2);
		
			this._imgs.push(new Element('div'));
		
			for(var i=0; i<2; i++)
			{	
				this.index = this.changeMethod(this.panes.length-1,this.index,1);
				
				var onLoadCallback = function()
				{
					var animate = new jsweld.animation.animate(this,jsweld.animation.interpolate.exponential,30);
						animate.animate({opacity:0},{opacity:1},1);
				}

				var tNode = this.createImg(this.panes[this.index].image, onLoadCallback);
					tNode.setStyle({left:(this._width*i)+'px'});
					
				this.node.appendChild(tNode);
				this._imgs.push(tNode);
			}
			
			this.start();
		},
		
		draw: function()
		{
			var thisRef = this;
			
			if(!this._loading)
			{
				this._loading = true;
				
				var rNode = this._imgs.shift();
				if(rNode && rNode.parentNode)
				{
					rNode.parentNode.removeChild(rNode);
				}
				
				var nNode = this.createImg(this.panes[this.index].image).hide();
				//var onLoadCallback = function()
				//{
					nNode.setStyle({left:(thisRef._width+'px')});
					thisRef.node.appendChild(nNode);
				
					for(var i=0; i<thisRef._imgs.length; i++)
					{
						thisRef.node.appendChild(thisRef._imgs[i]);
					}
					
					thisRef._imgs.push(nNode);
					nNode.show();
					
					var animate = new jsweld.animation.animate(thisRef._imgs[thisRef._imgs.length-2],jsweld.animation.interpolate.roughExponential,60);
						animate.animate({left:thisRef._width},{left:0},2, function(){thisRef._loading = false;});
				//}
				
				//var nNode = this.createImg(this.panes[this.index].image, onLoadCallback).hide();
			}
		}
		
	});