/******************************************************************
*	file		:		clientscript/dropdown.js
*
*	Functions for creating smooth dropdown-boxes
*	requires ./ajax.js for ajax support
*
*******************************************************************/
var dropdowns = new Array();

function ddAdd( trigger, container, do_ajax, default_height )
{
	dropdowns[trigger] = new dropDown( trigger, container, do_ajax, default_height );
}
/***
* Adds a single dropdown-element
*/
function dropDown( trigger, container, do_ajax, default_height )
{
	this.trigger = trigger;
	this.container = container;
	this.do_ajax = do_ajax;
	this.default_height = default_height;
	
	this.triggerObj = _(trigger);
	this.containerObj = _(container);
	
	this.containerObj.opened_by = 0;
	
	this.containerObj.maxh = this.containerObj.offsetHeight;
	
	this.triggerObj.onclick = dropDown.prototype.toggle;
	
	if( !this.default_height )
	{
		this.containerObj.open = false;
		this.containerObj.style.visibility = 'hidden';
		this.containerObj.style.height = '0px';
	}
	else
	{
		if( this.default_height !== true )
		{
			this.containerObj.style.height = this.default_height + 'px';
		}
		else
		{		
			this.containerObj.open = true;
		}
	}
}

dropDown.prototype.toggle = function(e)
{
	if( dropdowns[this.id].containerObj.open == undefined 	)
		dropdowns[this.id].containerObj.open = false; // Closed
	
	if( dropdowns[this.id].containerObj.open == false )
	{
		if( dropdowns[this.id].do_ajax )
		{
			do_ajax(dropdowns[this.id].containerObj, dropdowns[this.id].do_ajax);
			dropdowns[this.id].containerObj.maxh = dropdowns[this.id].containerObj.scrollHeight;
		}
		
		dropdowns[this.id].containerObj.style.visibility = 'visible';
		
		if( dropdowns[this.id].default_height )
		{
			var heightfrom = dropdowns[this.id].default_height;
			var opacityfrom = 1;
			var timer = 0.1;
			dropdowns[this.id].default_height = false;
		}
		else
		{
			var timer = 0.3;
			var heightfrom = 0;
			var opacityfrom = 0;
		} 
		
		var animopen = new YAHOO.util.Anim(dropdowns[this.id].containerObj, { opacity: { from: opacityfrom, to: 1 }, height: { from: heightfrom, to: dropdowns[this.id].containerObj.maxh } }, timer, YAHOO.util.Easing.easeBoth);
		animopen.animate();
		
		var ddid = this.id;
		animopen.onComplete.subscribe(
			function()
			{
				dropdowns[ddid].containerObj.style.height = '';
			}
		);
		dropdowns[this.id].containerObj.open = true;
		dropdowns[this.id].containerObj.opened_by = this.id;
	}
	else
	{
		var anim = new YAHOO.util.Anim(dropdowns[this.id].containerObj, { opacity: { from: 1, to: 0 }, height: { from: dropdowns[this.id].containerObj.maxh, to: 0 } }, .3, YAHOO.util.Easing.easeBoth);
		anim.animate();
		dropdowns[this.id].containerObj.open = false;
		
		var opened_by = dropdowns[this.id].containerObj.opened_by;
		var this_id = this.id;
		
		anim.onComplete.subscribe(
			function()
			{
				if( opened_by != this_id )
				{
					// Animate open again
					if( dropdowns[this_id].do_ajax )
					{
						do_ajax(dropdowns[this_id].containerObj, dropdowns[this_id].do_ajax);
						dropdowns[this_id].containerObj.maxh = dropdowns[this_id].containerObj.scrollHeight;
					}
					
					dropdowns[opened_by].containerObj.style.visibility = 'visible';
					var anim = new YAHOO.util.Anim(dropdowns[opened_by].containerObj, { opacity: { from: 0, to: 1 }, height: { from: 0, to: dropdowns[opened_by].containerObj.maxh } }, .3, YAHOO.util.Easing.easeBoth);
					anim.animate();
					dropdowns[opened_by].containerObj.open = true;
					dropdowns[opened_by].containerObj.opened_by = this_id;
				}
			}
		);
	}
	
	return false;
}
