var acordeonList = {};

function AcordeonItem(pName, pState)
{
	this.itemName = pName;
	this.itemState = pState;
	
	this.slideUp = function(pStepCnt, pInterval, pStrength, fOnDone)
	{
		var step = 1;
		var pThis = this;
		var obj = document.getElementById(this.itemName);
		
		if (pStepCnt < 1)
		{
			if (fOnDone)
				fOnDone();
			return;
		}
		
		if (pStrength < 1)
			pStrength = 1;
		
		var dims = getElementDimensions(this.itemName);
		obj.className = 'acordhidden';
		obj.style.height = dims.height + 'px';
		var anim_int = setInterval(
			function()
			{
				if (step > pStepCnt)
				{
					clearInterval(anim_int);
					pThis.itemState = 'hidden';
					
					if (fOnDone)
						fOnDone();
						
				}
				else
				{
					var dt = dims.height;
					var val = Math.ceil((Math.pow(((1 / pStepCnt) * step), pStrength) * dt));
					step++;
					obj.style.height = (dims.height - val) + 'px';
				}
			}
			, pInterval);
		
	}
	
	this.slideDown = function(pMaximum, pStepCnt, pInterval, pStrength, fOnDone)
	{
		var step = 1;
		var pThis = this;
		var obj = document.getElementById(this.itemName);
		
		if (pStepCnt < 1)
		{
			if (fOnDone)
				fOnDone();
			return;
		}
		
		if (pStrength < 1)
			pStrength = 1;
		
		var anim_int = setInterval(
			function()
			{
				if (step > pStepCnt)
				{
					clearInterval(anim_int);
					pThis.itemState = 'visible';
					
					if (fOnDone)
						fOnDone();
						
					obj.className = 'acordvisible';
				}
				else
				{
					var dt = pMaximum;
					var val = Math.ceil((Math.pow(((1 / pStepCnt) * step), pStrength) * dt));
					step++;
					obj.style.height = val + 'px';
				}
			}
			, pInterval);
	}
}

function Acordeon()
{
	// Items array
	this.items = [];
	
	// Method to add a new item
	this.addItem = function(pName)
	{
		var pItem = document.getElementById(pName);
		
		if (!pItem)
			return false;
			
		var cnt = this.items.length;
		var pState;
		
		if (pItem.className == 'acordhidden')
			pState = 'hidden';
		else
			pState = 'visible';
			
		this.items[cnt] = new AcordeonItem(pName, pState);
	}
	
	this.output = function()
	{
		var cnt = this.items.length;
		
		for (i = 0; i < cnt; i++)
			alert('item: ' + this.items[i].itemName + ', state: ' + this.items[i].itemState);
	}
	
	this.getItemByName = function(pName)
	{
		for (i = 0; i < this.items.length; i++)
			if (this.items[i].itemName == pName)
				return this.items[i];
				
		return false;
	}
	
	this.getFirstItemWithState = function(pState)
	{
		for (i = 0; i < this.items.length; i++)
			if (this.items[i].itemState == pState)
				return this.items[i];
				
		return false;
	}
	
}

function Acordeons()
{
	/*
		Finds an acordeon, based on an item that belongs to it
	*/
	this.getAcordeonByItemName = function(pName)
	{
		for (prop in acordeonList)
		{
			for (i = 0; i < acordeonList[prop].items.length; i++)
			{
				if (acordeonList[prop].items[i].itemName == pName)
					return acordeonList[prop];
			}
		}
		
		return false;
	}
	
	/*
		Finds an item in the acordeons list by name
	*/
	this.getAcordeonItemByName = function(pName)
	{
		var acc = this.getAcordeonByItemName(pName);
		
		if (!acc)
			return false;
		
		return acc.getItemByName(pName);
	}
	
	this.getFirstAcordeonItemWithState = function(pAcordeon, pState)
	{
		var acc = this.getAcordeonByItemName(pAcordeon);
		
		if (!acc)
			return false;
		
		return acc.getFirstItemWithState(pState);
	}
	
}

function acordInit(pName, pPrefix, pFirst, pLast)
{
	acordeonList[pName] = new Acordeon();
	var acc = acordeonList[pName];
	
	for (i = pFirst; i <= pLast; i++)
	{
		acc.addItem(pPrefix + i);
	}
}

function acordToggle(pName, pUpInterval, pDownInterval, pStrength)
{
	var acs = new Acordeons();
	var acc = acs.getAcordeonByItemName(pName);
	var obj = document.getElementById(pName);
	var open_item = acc.getFirstItemWithState('visible');
	var aci = acc.getItemByName(pName);
	
	if (open_item)
	{
		if (open_item.itemName == pName)
		{
			open_item.slideUp(pUpInterval, 10, pStrength, null);
		}
		else
		{
			open_item.slideUp(pUpInterval, 10, pStrength, 
				function()
				{
					aci.slideDown(obj.scrollHeight, pDownInterval, 10, pStrength, null);
				}
			);
		}
	}
	else
	{
		aci.slideDown(obj.scrollHeight, pDownInterval, 10, pStrength, null);
	}

}
