/* Dan DeValve 5/16/11 */

/**
 * Turns the selected element into an accordian list.
 * @param element the element to apply the accordian styling to
 */
function activate_accordian(element, padding, onClass, overClass, dormentClass)
{
	// we're going to run through all the child divs in element
	// we assume that the divs are in title/content pairs
	// set the onclick to be accordian_activate (element, body_element)
	
	// loop all the accodrdian children...
	var element_array = element.getElementsByTagName("div");
	for(var i = 0; i < element_array.length-1; i+=2)
	{
		var title = element_array[i];
		var content = element_array[i+1];
		
		title.content_object = content;
		title.onclick = function() { accordian_activate(element, this.content_object); this.className = onClass;};

		content.style.display = "Block";
		content.accordian_content_height = content.offsetHeight - padding;
		content.style.display = "None";
		title.className = dormentClass;
		
		title.accordian_content_element = content;
		title.onmouseover = function () {if(this.accordian_content_element.accordian_is_active){this.className = onClass;}else{this.className = overClass;}};
		title.onmouseout = function () {if(this.accordian_content_element.accordian_is_active){this.className = onClass;}else{this.className = dormentClass;}};
		
	}
	
}

// 'clicks' an accordian element, making it bigified or smallified.
function accordian_activate(accordian, target)
{
	// shrink all the other content divs in the accordian
	// expand the target if it's display style is none
	// otherwise shrink the target...
	
	// loop all the accodrdian children...
	var element_array = accordian.getElementsByTagName("div");
	for(var i = 0; i < element_array.length-1; i+=2)
	{
		var title = element_array[i];
		var content = element_array[i+1];
		
		// ignore the title here...
		if(content.style.display == "None" || content.style.display == "none")
		{
			if(content == target)
			{
				// this is the one we want, expand it...
				set_accordian_expand(target);
			}
		}
		else
		{
			// smallify it...
			set_accordian_shrink(content);
			// change the class
			title.onmouseout();
		}
	}
	
}

// has the accordian element shrink until it's gone
function set_accordian_shrink(target)
{
	// if we're already shrinking, do nothing...
	if(target.accordian_is_shrinking)
	{
		return;
	}
	target.accordian_is_active = false;
	target.accordian_is_shrinking = true;
	
	// shrink until we have no more shrink to shrink
	window.clearInterval(target.accordian_interval);
	var interval = 25;
	var length = 200;
	var runs = length/interval;
	target.accordian_count = 0;

	
	// this is a crazyness that basically just calls itself over and over until we hit the number of runs we want.
	target.accordian_interval = window.setInterval(
			function()
				{
					
					
					if(target.accordian_count > runs)
					{
						window.clearInterval(target.accordian_interval);
						target.style.display = "None";
						target.style.maxHeight = "";
						target.style.overflow = "";
						target.accordian_is_shrinking = false;
					}
					else
					{
						// set height to be a fraction of the old height...
						target.style.display = "Block";
						
						var reset_height = target.accordian_content_height * (1-(target.accordian_count/runs));
						target.style.maxHeight = reset_height + "px";
						target.style.overflow = "hidden";
						
					}
					target.accordian_count++;
					
				},
				
			interval
			);
}

// expand the accordian element until it's normal size.
function set_accordian_expand(target)
{
	
	target.accordian_is_active = true;
	// expand until we have no more expand to expand
	window.clearInterval(target.accordian_interval);
	var interval = 25;
	var length = 200;
	var runs = length/interval;
	target.accordian_count = 0;
	
	// this is a crazyness that basically just calls itself over and over until we hit the number of runs we want.
	target.accordian_interval = window.setInterval(
			function()
				{
					
					
					if(target.accordian_count > runs)
					{
						window.clearInterval(target.accordian_interval);
						target.style.display = "Block";
						target.style.maxHeight = "";
						target.style.overflow = "";
					}
					else
					{
						// set height to be a fraction of the old height...
						target.style.display = "Block";
						
						var reset_height = target.accordian_content_height * (target.accordian_count/runs);
						target.style.maxHeight = reset_height + "px";
						target.style.overflow = "hidden";
						
					}
					target.accordian_count++;
					
				},
				
			interval
			);
}
