/*  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 = {
			defaultPanel:		'',
			ajaxUrls: 			{},
			elementFocus:		'',
			onOpenPanel:		Prototype.emptyFunction,
			ajaxLoadingText: 	'Loading...'	
		};
		this.options = Object.extend(this.options, options || {});
		this.currentTab = '';
		
		this.element = $(element);
		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');
			tabLI.itemId = itemLinks[0].href.split("#")[1];
			tabLI.linkedPanel = $(tabLI.itemId);
			tabLI.linkedPanel.setStyle({clear:'both'});		//firefox hack
			
			//check for the intially active tab
			if ((this.options.defaultPanel != '') && (this.options.defaultPanel == tabLI.itemId)) {
				this.openPanel(tabLI);
			} else {
				$($(tabLI).linkedPanel).hide();
			}
			
			// 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));
		}
		
	},
	
	openPanel: function(tab){
		if (tab == this.currentTab) {
			return;
		}
		
		tab = $(tab); // ie hack
		this.options.onOpenPanel(tab);
		// alert(tab.innerHTML);
		var test = "blabla"+Math.random()*100000;
		if(this.currentTab != ''){
			if (tab.hasClassName('selected')) {
				if (this.options.defaultPanel != '') {
					tab = $(this.listElements[0]);
				}
			}
			this.currentTab.linkedPanel.fade({duration : 0.1, queue : { position : 'front', scope : test}});
			this.currentTab.removeClassName('selected');
		}
		
		//set the currently open panel to the new panel
		this.currentTab = tab;
		
		tab.linkedPanel.appear({duration : 0.1, queue : { position : 'end', scope : test}});
		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);
				}
			});
		}
		
		if (this.options.elementFocus != '') {
			$(this.options.elementFocus).focus();
		}
	},
	
	next: function() {
		var li=this.currentTab;
		var nextLi = li.next();
		if (nextLi == null) {
			nextLi = li.previousSiblings().last();
		}
		if(nextLi != null) {
			this.openPanel(nextLi);
		}
	},
	
	prev: function() {
		var li=this.currentTab;
		var nextLi = li.previous();
		if (nextLi == null) {
			nextLi = li.nextSiblings().last();
		}
		if(nextLi != null) {
			this.openPanel(nextLi);
		}
	},
	
	setOnOpenPanel: function(fn) {
		this.options.onOpenPanel = fn;
	}
};
