// Copyright Wondermill Webworks Inc. 2007
// Author: Edward Holets
// 
// Description: Dynamic.Tabs class. DHTML tabs.
// 
// 
// Requires: prototype.js

var Dynamic;

if (!Dynamic)
{
	Dynamic = Class.create();
	Dynamic.prototype = {
		initialize: function() {
			throw 'Dynamic root class should not be initalized!';
		}
	};
}

Object.extend(Dynamic, {
	Tabs: Class.create()
});

Object.extend(Dynamic.Tabs, {
	Constants: {
		selected_class: "selected"
	}
});

Dynamic.Tabs.prototype = {
	initialize: function(element, start_callback, finish_callback) {
		this.element = $(element);
		this.registeredEvents = {
			tabs: new Hash()
		};
		this.tabs = new Hash();
		this.start_callback = start_callback;
		this.finish_callback = finish_callback;
		this.old_value = this.element.getAttribute('value');
		this.is_toggeling = false;
	},
	getValue: function() {
		return this.element.getAttribute('value');
	},
	hasTabs: function() {
		if (this.tabs.values().length > 0)
		{
			return true;
		}
		
		return false;
	},
	addTabs: function(tabs) {
		tabs = $H(tabs);
		
		tabs.each(function(pair) {
			if (typeof pair.value.tab == 'undefined' || typeof pair.value.control == 'undefined' || !pair.value.tab || !pair.value.control)
			{
				return;
			}
			if (!this.registeredEvents.tabs[pair.key])
			{
				var event_handler = {
					fx: this.fxToggleTabs.bindAsEventListener(this, pair.key),
					element: $(pair.value.control),
					event: 'click'
				};
				this.registeredEvents.tabs[pair.key] = event_handler;
				Event.observe(event_handler.element, event_handler.event,event_handler.fx);
				this.tabs[pair.key] = $(pair.value.tab);
				if (pair.key != this.old_value)
				{
					$(pair.value.control).removeClassName(Dynamic.Tabs.Constants.selected_class);
					this.tabs[pair.key].hide();
				}
			}
		}.bind(this));
		
		if (this.tabs[this.old_value]) {
			this.registeredEvents.tabs[this.old_value].element.addClassName(Dynamic.Tabs.Constants.selected_class);
			this.tabs[this.old_value].show();
			if (typeof this.finish_callback == 'function')
			{
				this.finish_callback(this.old_value, this.tabs[this.old_value], this);
			}
		}
		
		//this.fxToggleTabs(null, this.old_value);
	},
	fxToggleTabs: function(e, selected_key) {
		if (!this.is_toggeling && this.old_value != selected_key)
		{
			this.is_toggeling = true;
			var can_toggle = true;

			if (this.start_callback)
			{
				can_toggle = this.start_callback(selected_key, this.tabs[selected_key], this);
			}
			
			if (can_toggle)
			{
				this.tabs.each(function(pair) {
					if (pair.key !== selected_key)
					{
						this.registeredEvents.tabs[pair.key].element.removeClassName(Dynamic.Tabs.Constants.selected_class);
						pair.value.hide();
					}
				}.bind(this));
				this.registeredEvents.tabs[selected_key].element.addClassName(Dynamic.Tabs.Constants.selected_class);
				this.tabs[selected_key].show();
				if (this.finish_callback)
				{
					this.finish_callback(selected_key, this.tabs[selected_key], this);
				}
				this.old_value = selected_key;
				this.element.value = selected_key;
			} else
			{
				this.element.value = this.old_value;
			}
			this.is_toggeling = false;
		}
		if (e)
		{
			Event.stop(e);
		}
	}
};