/**
 * (c) 2006 Vortex Webdesign
 * Vortex Scroller Object
 * Written by Mike De Smet
 * http://www.vortex-webdesign.be
 */

function vortexScroller(objName) 
{
	this.obj = objName;
	this.scrollerItems = [];
	this.currentItem = -1;
	this.width = 500;
	this.height = 500;
	this.random = false;
	this.cssClass = 'vortexScroller';
	this.shrinkamount = 5;
	this.opacitysteps = 20;
	this.speed = 50;
	this.effect = 'none';
	this.disappearEffect = 'none';
	this.waitForDisappear = true;
	this.hasDisappeared = true;
	this.currentOpacity = 0.0;
};

vortexScroller.prototype.add = function(content, hyperlink, duration, target) 
{
	this.scrollerItems[this.scrollerItems.length] = new scrollerItem(this.obj +"_"+ this.scrollerItems.length, content, duration, hyperlink, target);
};

function scrollerItem (name, content, duration, hyperlink, target) 
{
	this.name = name;
	this.content = content;
	this.duration = duration;
	this.hyperlink= hyperlink;
	this.target = target;
};

vortexScroller.prototype.removePx = function(width)
{
	return parseInt(width.substr(0,width.length-2));
}

vortexScroller.prototype.toString = function() 
{
	var str = '<div id="vortexScroller" style="width: '+this.width+'px; height: '+this.height+'px;">';
	
	for (var iCtr = 0; iCtr < this.scrollerItems.length; iCtr++)
	{
		str += '<div ';
		str += 'id="'+this.scrollerItems[iCtr].name+'" ';
		str += 'class="vortexScrollerHide" '
		str += 'style="width: '+this.width+'px; height: '+this.height+'px; overflow: hidden;" ';
		str += '>\n';

		if (this.scrollerItems[iCtr].hyperlink != "")
			str += '<a href="'+this.scrollerItems[iCtr].hyperlink+'" target="'+this.scrollerItems[iCtr].target+'">';

		str += this.scrollerItems[iCtr].content;
		
		if (this.scrollerItems[iCtr].hyperlink != "")
			str += '</a>';

		str += '</div>';
	}
	str += '<div>';
	return str;
}

vortexScroller.prototype.start = function()
{
	this.changeItem();
	var thisObj = this.obj;
	setTimeout(thisObj+".start()", this.scrollerItems[this.currentItem].duration * 1000);
}

vortexScroller.prototype.getRandomItem = function() 
{
	return Math.floor(Math.random() * this.scrollerItems.length);
}

vortexScroller.prototype.grow = function(itemindex)
{
	var thisObj = this.obj;
	var objItem = document.getElementById(this.scrollerItems[itemindex].name);
	var newheight = this.removePx(objItem.style.height);

	if (isNaN(newheight))
	{
		newheight = this.height;
	} else {
		newheight += this.shrinkamount;
	}
	
	if (newheight > this.height)
	{
		newheight = this.height;
	} else {
		var temp = setTimeout(thisObj+'.grow('+itemindex+')',this.speed);
	}
	
	objItem.style.height = newheight+"px";
}

vortexScroller.prototype.shrink = function(itemindex)
{
	var thisObj = this.obj;
	var objItem = document.getElementById(this.scrollerItems[itemindex].name);
	var newheight = this.removePx(objItem.style.height);

	if (isNaN(newheight))
	{
		newheight = 0;
	} else {
		newheight -= this.shrinkamount;
	}
	
	if (newheight <= 0)
	{
		newheight = this.height;
		this.cssHide(itemindex);
		this.hasDisappeared = true;
	} else {
		var temp = setTimeout(thisObj+'.shrink('+itemindex+')',this.speed);
	}
	
	objItem.style.height = newheight+"px";
}

vortexScroller.prototype.opacity = function(itemindex)
{
	var thisObj = this.obj;
	var objItem = document.getElementById(this.scrollerItems[itemindex].name);
	
	this.currentOpacity += (1/this.opacitysteps);
	objItem.style.opacity = this.currentOpacity;
	
	if (this.currentOpacity < 1)
		var temp = setTimeout(thisObj+'.opacity('+itemindex+')',this.speed);
	else
		this.currentOpacity = 0.0;
}

vortexScroller.prototype.reverseOpacity = function(itemindex)
{
	var thisObj = this.obj;
	var objItem = document.getElementById(this.scrollerItems[itemindex].name);
	
	this.currentOpacity -= (1/this.opacitysteps);
	
	if (this.currentOpacity > 0)
		var temp = setTimeout(thisObj+'.reverseOpacity('+itemindex+')',this.speed);
	else
	{
		this.currentOpacity = 1.0;
		this.cssHide(itemindex);
		this.hasDisappeared = true;
	}
	
	objItem.style.opacity = this.currentOpacity;
}

vortexScroller.prototype.cssShow = function(currentItem)
{
	document.getElementById(this.scrollerItems[currentItem].name).className = "vortexScrollerShow";
}

vortexScroller.prototype.cssHide = function(currentItem)
{
	document.getElementById(this.scrollerItems[currentItem].name).className = "vortexScrollerHide";
}

vortexScroller.prototype.showItem = function(currentItem)
{
	if (!this.hasDisappeared && this.waitForDisappear) 
	{
		var temp = setTimeout(this.obj+'.showItem('+currentItem+')',this.speed);
	} else {
		switch (this.effect)
		{
			case 'scroll':
				document.getElementById(this.scrollerItems[currentItem].name).style.height = '0px';
				this.cssShow(currentItem);
				this.grow(currentItem);
				break;
			case 'opacity':
				this.currentOpacity = 0;
				document.getElementById(this.scrollerItems[currentItem].name).style.opacity = 0;
				this.cssShow(currentItem);
				this.opacity(currentItem);
				break;
			case 'none':
			default:
				this.cssShow(currentItem);
				break;
		}
	}
}

vortexScroller.prototype.hideItem = function(prevItem)
{
	this.hasDisappeared = false;

	if (prevItem >= 0)
	{
		switch (this.disappearEffect)
		{
			case 'scroll':
				this.shrink(prevItem);
				break;
			case 'opacity':
				this.currentOpacity = 1;
				this.reverseOpacity(prevItem);
				break;
			case 'none':
			default:
				document.getElementById(this.scrollerItems[prevItem].name).className = "vortexScrollerHide";
				this.hasDisappeared = true;
		}
	} else {
		this.hasDisappeared = true;
	}
}


vortexScroller.prototype.changeItem = function()
{
	var prevItem = -1;
	
	if (this.currentItem < this.scrollerItems.length)
		prevItem = this.currentItem;

	if (!this.random)
	{
		if (this.currentItem < this.scrollerItems.length - 1)
			this.currentItem = this.currentItem + 1;
		else
			this.currentItem = 0;
			
	} else {
		this.currentItem = this.getRandomItem();
	}

	this.hideItem(prevItem);
	this.showItem(this.currentItem);
}