// RotateBlockAttitude
//
// Copyright (c) 2008 NuAttitude Ltd www.nuattitude.ee
// All rights reserved.
// 
// Redistribution and use of this effect in source form, with or without modification,
// are permitted provided that the following conditions are met:
// 
// * USE OF SOURCE ON COMMERCIAL (FOR-PROFIT) WEBSITE REQUIRES ONE-TIME LICENSE FEE PER DOMAIN.
//   Reasonably priced! Email: info@nuattitude.ee for licensing instructions. Thanks!
//
// * Redistribution of source code must retain the above copyright notice,
//   this list of conditions and the following disclaimer.
//
// * Redistribution of source code and derived works cannot be sold without specific
//   written prior permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

function rotateBlocks(pictureUrls, blockElements, parentElement)
{
	this.rotateNext = function()
	{
		if (this.pausePerformed == false)
		{
			this.pausePerformed = true;
			setTimeout(function(){thisObj.rotateNext()}, this.pauseLength);
		}
		else
		{
			this.pausePerformed = false;
			
			this.currentPictureNumber = this.nextPictureNumber;
			
			if (this.topLayer.childNodes.length > 0)
			{
				sourceLayer = document.getElementById(this.blockElements[this.currentPictureNumber]);
				while (this.bottomLayer.firstChild) 
				{
					this.bottomLayer.removeChild(this.bottomLayer.firstChild);
				}
			
				for (var index = 0; index < this.topLayer.childNodes.length; index++)
				{
					if (this.topLayer.childNodes[index].tagName == 'div' || this.topLayer.childNodes[index].tagName == 'DIV')
					{
						var newNode = this.topLayer.childNodes[index].cloneNode(true);
						this.bottomLayer.appendChild(newNode);
					}
				}
			}
			
			if (this.nextPictureNumber < this.pictureUrls.length - 1)
			{
				this.nextPictureNumber = this.nextPictureNumber + 1;
			}
			else 
			{
				this.nextPictureNumber = 0;
			}
			
			if (typeof(this.pictureObjects[this.nextPictureNumber]) == 'undefined')
			{
				this.pictureObjects[this.nextPictureNumber] = new Image();
				this.pictureObjects[this.nextPictureNumber].src = this.pictureUrls[this.nextPictureNumber];
			}
			
			this.preLoadNextPicture();
		}
	}
	
	this.init = function()
	{
		this.pictureObjects[this.nextPictureNumber] = new Image();
		this.pictureObjects[this.nextPictureNumber].src = this.pictureUrls[this.nextPictureNumber];
		
		this.preLoadFirstPicture();
	}
	
	this.preLoadNextPicture = function()
	{
		if (!this.pictureObjects[this.nextPictureNumber].complete)
		{
			this.preloadTimeoutID = setTimeout(function(){thisObj.preLoadNextPicture()}, 100);
		}
		else
		{
			clearTimeout(this.preloadTimeoutID);
			clearTimeout(this.timeoutID);
			this.frameNumber = 0;
			
			var sourceLayer = document.getElementById(this.blockElements[this.nextPictureNumber]);
			while (this.topLayer.firstChild) 
			{
				this.topLayer.removeChild(this.topLayer.firstChild);
			}
			
			if (sourceLayer.childNodes.length > 0)
			{
				for (var index = 0; index < sourceLayer.childNodes.length; index++)
				{
					if (sourceLayer.childNodes[index].tagName == 'div' || sourceLayer.childNodes[index].tagName == 'DIV')
					{
						var newNode = sourceLayer.childNodes[index].cloneNode(true);
						this.topLayer.appendChild(newNode);
					}
				}
			}
			
			this.drawFrameState();
		}
	}
	
	this.preLoadFirstPicture = function()
	{
		if (!this.pictureObjects[0].complete)
		{
			this.preloadTimeoutID = setTimeout(function(){thisObj.preLoadFirstPicture()}, 100);
		}
		else
		{
			clearTimeout(this.preloadTimeoutID);
			clearTimeout(this.timeoutID);
			
			var sourceLayer = document.getElementById(this.blockElements[this.currentPictureNumber]);
			while (this.bottomLayer.firstChild) 
			{
				this.bottomLayer.removeChild(this.bottomLayer.firstChild);
			}
			
			if (sourceLayer.childNodes.length > 0)
			{
				for (var index = 0; index < sourceLayer.childNodes.length; index++)
				{
					if (sourceLayer.childNodes[index].tagName == 'div' || sourceLayer.childNodes[index].tagName == 'DIV')
					{
						var newNode = sourceLayer.childNodes[index].cloneNode(true);
						this.bottomLayer.appendChild(newNode);
					}
				}
			}
			
			this.rotateNext();
		}
	}
	
	this.drawFrameState = function()
	{
		newOpacity = this.frameNumber / this.framesCount;
		//~ newOpacity = newOpacity * newOpacity;
		
		this.opacity(this.topLayer, newOpacity);
		this.frameNumber++;
		
		if (this.frameNumber <= this.framesCount)
		{
			this.timeoutID = setTimeout(function(){thisObj.drawFrameState()}, this.AnimationSpeed);
		}
		else
		{
			clearTimeout(this.timeoutID);
			this.frameNumber = 0;
			this.rotateNext();
		}
	}
	
	this.opacity = function(object, opacity) 
	{
		object.style.opacity = opacity;
		object.style.MozOpacity = opacity;
		object.style.filter = "alpha(opacity=" + opacity*100 + ")";
	}

	try 
	{
		document.execCommand("BackgroundImageCache", false, true);
	} 
	catch(err) {}
	
	var thisObj = this;
	
	this.pausePerformed = false;
	this.timeoutID = null;
	this.preloadTimeoutID = null;
	this.frameNumber = 0;
	this.framesCount = 80;
	this.AnimationSpeed = 12;
	this.pauseLength = 2000;
	
	this.parentElement = parentElement;
	this.pictureObjects = new Array();
	this.pictureUrls = pictureUrls;
	this.blockElements = blockElements;
	this.currentPictureNumber = 0;
	this.nextPictureNumber = 0;
	
	this.bottomLayer = document.createElement('div');
	this.bottomLayer.style.position = 'absolute';
	this.bottomLayer.style.width = this.parentElement.offsetWidth + 'px';
	this.bottomLayer.style.height = this.parentElement.offsetHeight + 'px';
	this.bottomLayer.style.zIndex = '1';
	parentElement.appendChild(this.bottomLayer);

	this.topLayer = document.createElement('div');
	this.topLayer.style.position = 'absolute';
	this.topLayer.style.width = this.parentElement.offsetWidth + 'px';
	this.topLayer.style.height = this.parentElement.offsetHeight + 'px';
	this.topLayer.style.zIndex = '2';
	parentElement.appendChild(this.topLayer);

	this.init();
}
function startRotateBlock(pictureUrls, blockElements, parentElement)
{
	if (typeof(parentElement.rotationBlocksObject) == 'undefined')
	{
		parentElement.rotationBlocksObject = new rotateBlocks(pictureUrls, blockElements, parentElement);
	}
}
