/* Universal Messaging Control - Slide Animations */

var in_use = new Array();
var is_active = false;
var current = '';
var indicator_needed = false;

function UMCSlide(element, options) {
	this.element = $(element);
	this.duration = 1;
	this.height = parseInt(this.element.style.height);
	if (typeof options != 'undefined') {
		this.options = options;
	} else {
		this.options = {};
	}
	if (this.options.duration) {
		this.duration = this.options.duration;
	}
	this.up = function() {
		this.current_height = this.height;
		this.new_height = '1';
		if (in_use[element] != true) {
			is_active = false;
			current = '';
			var finish_time = this.animate();
			window.setTimeout("UMCSlide('" + element + "').cleanup(" + this.height + ");", finish_time);
		}
	}
	this.down = function() {
		this.new_height = this.height;
		this.current_height = '1';
		if (in_use[element] != true) {
			this.element.style.height = '1px';
			this.element.style.display = 'block';
			is_active = true;
			current = this.options.page;
			this.animate();
		}
	}
	this.resize = function(height) {
		this.new_height = height;
		this.current_height = this.height;
		if (in_use[element] != true) {
			current = this.options.page;
			this.animate();
		}
	}
	this.toggle = function(height, options) {
		this.options = options;
		if (this.options.page == current || this.options.page == "close") {
			Effect.Fade('dropdown_content', {duration:0.5});
			this.up();
		} else if (is_active == true && this.options.page != current) {
			Effect.Fade('dropdown_content', {duration:0.5});
			if (this.options.url != '') {
				
				indicator_needed = true;
				
				window.setTimeout("ajax_loadContent('dropdown', '" + options.url + "')", 500);
				window.setTimeout('Effect.Appear(\'dropdown_content\')', 800);
			}
			this.resize(height);
		} else if (is_active == false) {
			Element.hide('dropdown_content');
			this.height = height;
			if (this.options.url != '') {
				ajax_loadContent('dropdown', this.options.url);
			}
			this.down();
			window.setTimeout('Effect.Appear(\'dropdown_content\')', 800);
		}
		window.setTimeout('indicator_needed = false', 500);
	}	
	this.animate = function() {
		in_use[element] = true;
		var frames = 30 * duration;
		var time_increment = (duration * 1000) / frames;
		time_increment = Math.round(time_increment);
		var s_increment = (this.current_height - this.new_height) / frames;
		var frame_sizes = new Array();
		for (var i = 0; i < frames; i++) {
			if (i < frames / 2) {
				frame_sizes[i] = (s_increment * (i / frames)) * 4;
			} else {
				frame_sizes[i] = (s_increment * (1 - (i / frames))) * 4;
			}
		}
		for (var i = 0; i < frames; i++) {
			this.current_height = this.current_height - frame_sizes[i];
			window.setTimeout("document.getElementById('" + element + "').style.height='" + Math.round(this.current_height) + "px';", time_increment * i);
		}
		window.setTimeout("delete(in_use['" + element + "']);", time_increment * i);
		
		if (this.options.onComplete) {
			window.setTimeout(this.options.onComplete, time_increment * (i - 2));
		}
		return time_increment * i;
	}
	this.cleanup = function(height) {
		this.element.style.display = 'none';
		this.element.style.height = height + 'px';
	}
	return this;
}

Ajax.Responders.register({
	onCreate : function() {
		if ($('progress') && Ajax.activeRequestCount > 0) {
			if (indicator_needed == true) {
				Effect.Appear('progress', {duration: 0.25, queue: 'end'});
			}
		}
	},
	onComplete : function() {
		if ($('progress') && Ajax.activeRequestCount == 0) {
			Effect.Fade('progress', {duration: 0.25, queue: 'end'});
		}
	}
});