/*
 * TabPane script
 * 2007 by Martin Kleinhans
 *
 * handles tab panes
 */

var TabPane = {
	counter : 0,
	instances : new Array(),

	isManaged : function(id) {
		return (TabPane.getTabPaneById(id) ? true : false);
	},

	getTabPane : function(number) {
		return TabPane.instances[number];
	},

	getTabPaneById : function(id) {

		var tabPane = undefined;
		TabPane.instances.each(function(o) {
			if (o.getId() == id) {
				tabPane = o;
				throw $break;
			}
		});
		return tabPane;
	},

	create : function(id, options) {

		var div = $(id);
		if (!div || !(div.tagName == 'div' || div.tagName == 'DIV')) {
			return;
		}

		if (options) {
			options = new Hash(options);
		} else {
			options = $H();
		}

		TabPane.instances[TabPane.counter] = new TabPaneInstance(id, options);
		TabPane.counter++;

	},

	handleResponse : function(number, action, request) {
		TabPane.getTabPane(number).showContent(request);
	}
};

var TabPaneInstance = Class.create();

TabPaneInstance.prototype = {

	options_default : $H({
		id : '',
		currentTab : 0,
		tabContainer : '.tabpane_menu',
		boxContainer : '.tabpane_body',
		tabSelector : 'a',
		boxSelector : '> div',
		showtabcallback : false,
		hidetabcallback : false,
		showContentCallback : false,
		tabPrefix : '',
		showOnCreate : true,
		requestUrl : '',
		staticParameters : '',
		tabParameter : 'id',
		tabIdParameter : 'tab',
		initialized : false,
		activeClassName : 'active'
	}),

	getTabContainerSelector : function() {
		return "#" + this.getId() + ' > ' + this.options['tabContainer'];
	},

	getBoxContainerSelector : function() {
		return "#" + this.getId() + ' > ' + this.options['boxContainer'];
	},

	getTabSelector : function() {
		return "#" + this.getId() + ' > ' + this.options['tabContainer'] + ' '
				+ this.options['tabSelector'];
	},

	getBoxSelector : function() {
		return "#" + this.getId() + ' > ' + this.options['boxContainer'] + ' '
				+ this.options['boxSelector'];
	},

	options : $H({}),

	loading : false,

	getTabName : function(index) {
		return this.options['tabPrefix'] + index;
	},

	setOption : function(name, value) {
		this.options[name] = value;
	},

	getId : function() {
		return this.options['id'];
	},

	getNumber : function() {
		return this.options['number'];
	},

	initialize : function(id, opt) {
		this.options = Object.clone(this.options_default);
		this.options['id'] = id;
		this.options['number'] = TabPane.counter;
		if (opt.values().length > 0) {
			this.options.merge(opt);
		}

		var tabSelector = this.getTabSelector();
		var number = this.options['number'];
		var tabs = $$(tabSelector);

		for ( var i = 0; i < tabs.length; i++) {
			tabs[i].onclick = function() {
				var list = this.up('ul');
				var tabs = $$(tabSelector);
				for ( var i = 0; i < tabs.length; i++) {
					if (tabs[i] == this) {
						TabPane.getTabPane(number).showTab(i);
					}
				}
			};
		}

		if (this.options['showOnCreate']) {
			this.showTab(this.options['currentTab'], false, true);
		}

	},

	showContent : function(request) {
		var boxes = $$(this.getBoxSelector());

		boxes[0].innerHTML = request.responseText;

		var index = this.options['currentTab'];
		var tabs = $$(this.getTabSelector());
		if (this.options['showContentCallback']) {
			window[this.options['showContentCallback']](this.getTabName(index),
					tabs[index].id);
		}

		if (isLoadingEffectActive(this.getBoxContainerSelector())) {
			toggleLoadingEffect(this.getBoxContainerSelector());
		}
		this.loading = false;
	},

	showTab : function(index, forceRefresh, onCreate) {
		
		if (this.loading) {
			return false;
		}

		if (!forceRefresh && this.options['initialized']
				&& index == this.options['currentTab']) {
			return;
		}
		if (!this.options['initialized']) {
			this.options['initialized'] = true;
		}
		var tabs = $$(this.getTabSelector());

		if (index < 0 || index > tabs.length) {
			return;
		}
		for ( var i = 0; i < tabs.length; i++) {
			if (i == index) {
				tabs[i].addClassName(this.options['activeClassName']);
			} else {
				tabs[i].removeClassName(this.options['activeClassName']);
			}
		}

		// if a box selector is given, switch the visibility of the boxes
		if (this.options['requestUrl'] == ''
		&& this.options['boxSelector'] != '') {
			var actions = $$(this.getBoxSelector());
			if (actions.length < tabs.length) {
				actions[0].show();
				for ( var i = 1; i < actions.length; i++) {
					actions[i].hide();
				}
			} else {
				for ( var i = 0; i < actions.length; i++) {
					if (i == index) {
						actions[i].show();
					} else {
						actions[i].hide();
					}
				}
			}
		}
		// if an ajax request url is given, try to load the content of the box
		// using an ajax request
		else if (this.options['requestUrl'] != '') {
			var params = this.options['tabIdParameter'] + "=" + tabs[index].id
					+ "&" + this.options['tabParameter'] + "="
					+ this.getTabName(index);
			if (this.options['staticParameters'] != '') {
				params += '&' + this.options['staticParameters'];
			}

			var number = this.getNumber();
			this.loading = true;
			var myAjax = new Ajax.Request(this.options['requestUrl'], {
				method : 'post',
				parameters : params,
				onComplete : function(request, object) {
					TabPane.handleResponse(number, 'load', request);
				}
			});
		}

		if (this.options['hideTabCallback']) {
			var oldIndex = this.options['currentTab'];
			window[this.options['hideTabCallback']](+this.getTabName(oldIndex),
					tabs[oldIndex].id);
		}

		if (this.options['showTabCallback']) {
			window[this.options['showTabCallback']](this.getTabName(index),
					tabs[index].id);
		}

		this.options['currentTab'] = index;

		// trigger animation
		if (!onCreate) {
			if (this.options['requestUrl'] != '') {
				toggleLoadingEffect(this.getBoxContainerSelector());
			}
		}
	}

};
