/*  Prototabs
 *  (c) 2007 James Starmer
 *
 *  Prototabs is freely distributable under the terms of an MIT-style license.
 *  For details, see the web site: http://www.jamesstarmer.com/prototabs
 *
/*--------------------------------------------------------------------------*/
var ProtoTabs = Class.create();
ProtoTabs.prototype = {
	
	initialize: function(element, options) {
		this.options = Object.extend({
			defaultPanel: '',
			ajaxUrls: 			{},
			ajaxLoadingText: 	'Loading...',
			onChange: null
		}, options || {});
		
		this.currentTab = '';
		
		this.element = $(element);
		if(Object.isUndefined(this.element) || this.element == null){
			var errorMessage =
				"There is no container element for tab html with Id: " + element + ".\n" +  
				"Prototabs need to wait dom:load event. \n" +
				"So let try to initialize prototabs inside : Event.observe(window,'load',function(){"
			alert(errorMessage);
			return;
		}
		
		this.listElements = $A(this.element.getElementsByTagName('LI'));
		//loop over each list element
		for(i = 0; i < this.listElements.length; i++) {	
			
			//get the tabs
			tabLI = this.listElements[i];
			var itemLinks = tabLI.getElementsByTagName('A');
			
			//if there is <li> without inline <a>, we may face a separator <li>
			if(Object.isUndefined(itemLinks)){
				continue;
			} else if(itemLinks.length == 0){
				continue;
			}
			
			
			tabLI.itemId = itemLinks[0].href.split("#")[1];
			tabLI.linkedPanel = $(tabLI.itemId);
			
			
			
			// watch for clicked
			$(itemLinks[0]).observe('click', function(event){
					element = Event.findElement(event, 'LI');
					this.openPanel(element);					
					Event.stop(event); // like return false;
			}.bind(this));
			
			//check for the intially active tab
			if((this.options.defaultPanel != '') && (this.options.defaultPanel == tabLI.itemId)){
				this.openPanel(tabLI);
			}else{
				$($(tabLI).linkedPanel).hide();
			}
		}
		
	},
	
	openPanel: function(tab){
		tab = $(tab); // ie hack

		this._beforeOpenPanel(tab.itemId);
		
		if(this.currentTab != ''){
			this.currentTab.linkedPanel.hide();
			this.currentTab.removeClassName('selected');
		}
		
		//set the currently open panel to the new panel
		this.currentTab = tab;
		
		tab.linkedPanel.show();
		tab.addClassName('selected');
		
		var url = this.options.ajaxUrls[tab.itemId];
		
		// if there is an ajax url defined update the panel with ajax
		if(url != undefined){
			tab.linkedPanel.update(this.options.ajaxLoadingText);
			new Ajax.Request(url,{
				onComplete: function(transport) {
					tab.linkedPanel.update(transport.responseText);
				}
			});
		} else if(this.options.onChange){
			this.options.onChange(tab.itemId);
		}
		
		this._afterOpenPanel(tab.itemId);
	},
	
	/**
	 * Create a chance so derived class can do something it wants.
	 */
	_afterOpenPanel: function(tab){
		//let override.
	},
	
	/**
	 * Create a chance so derived class can do something it wants.
	 */
	_beforeOpenPanel: function(tab){
		//let override.
	}	
};
