/*
 * PhotoCube.js, Version 1 (11/16/2007)
 * Matthew Hazlett
 * Clarity Computers
 * hazlema@devclarity.com
 *
 * This uses the Ext 1.1 library for the visual effets
 */
 
	/* General Effect Settings */
 	var FadeOutSpeed  = 4;   // Speed in seconds
	var FadeInSpeed   = 4;   // Speed in seconds
	var WaitToStart   = 5;   // Start the fadeing in 5 seconds
	var MustStay      = 30;  // Cell Item wont change for 3 seconds
	var ChangeSeconds = 60;  // Random Value added to MustStay
	
	/* Don't change below here */
	var Containers = [];
	var Timeings   = [];
	var Content    = [];
	
	/* Randomize an array */
 	function ArrayShuffle(o)
	{
		for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
		return o;
	};

	/* Load pictures into cells (onInit) */
	function LoadCube(Container)
	{
		// Take content from the top of the array
		//
		var myContent = Content.shift();
		Content.push(myContent);
		
		// Set a timing for this container
		//
		Timeings.push(Math.round(ChangeSeconds * Math.random()) + MustStay);

		// Add the content to the container
		//
		Child = Container.createChild({tag: 'div', className:'content'});
		Child.dom.innerHTML = myContent.innerHTML;
		Child.fadeIn({duration: FadeInSpeed, easing: 'easeIn'});
	}	

	/* Process a Cell, cell's timer is up */
	function ProcessCube(Container)
	{
		// Get Container
		//
		if (! Container) return false;
  	var Child      = Ext.get(Container.dom.childNodes[0]);
		
		// Create job
		//
		Job = function()
		{
			Child.fadeOut(
			{
				duration: FadeOutSpeed, 
				easing: 'easeOut', 
				remove: true,
				callback: function()
				{
					var myContent  = Content.shift();
					Content.push(myContent);
				
					Child = Container.createChild({tag: 'div', className:'content'});
					Child.dom.innerHTML = myContent.innerHTML;
					Child.fadeIn({duration: FadeInSpeed, easing: 'easeIn'});	
					ProcessCube();
				}
			})};

		// Do it
		//
		Job();
	}
	
	/* This initalizes the component */
	function cubeInit(config)
	{
		// Any config paramiters?
		//
		if (config != null)
		{
		 	if (config.fadeout)    FadeOutSpeed  = config.fadeout;
			if (config.fadein)     FadeInSpeed   = config.fadein;
			if (config.start)  WaitToStart   = config.start;
			if (config.stay)   MustStay      = config.stay;
			if (config.change) ChangeSeconds = config.change;
		}

		// Init Object Arrays
		//
		Containers = ArrayShuffle(Ext.DomQuery.select("#photoCube td"));
		Content    = ArrayShuffle(Ext.DomQuery.select("#photoCube .content"));
		
		// Preload Cells
		//
		Ext.each(Containers, function(ele){LoadCube(Ext.get(ele))}, this);
		
		// Cycle the timer array and do a job if its time has passed
		//
		Job = function()
		{
			for (counter=0; counter<=Containers.length-1; counter++)
			{
				if (Timeings[counter] <= 0)
				{
					Timeings[counter] = Math.round(ChangeSeconds * Math.random()) + MustStay;
					ProcessCube(Ext.get(Containers[counter]));
				}
			else { Timeings[counter]--; }
			}
		};

		eval(function(){setInterval(Job, 1000)}).defer(WaitToStart * 1000);
	}
		