/// <reference path="lib/prototype.js" />

/*
 * TabbedPane.js v1.0.0 -- Dynamic AJAX Tabs for Prototype
 * Copyright (c) 2007-2008 Jesse Farmer <jesse@20bits.com>
 * Licensed under the MIT license.
 */
Hash.prototype.toObject = Hash.prototype.toObject || function() { return Object.clone(this); }

var TabbedPane = function(pane, page_urls, args) {
    var activeId
    var args = $H({ asynchronous: true, method: 'post', parameters: { load: 'true' }, evalScripts: true }).merge(args).toObject();	
	this.load_page = function(page_id) {
	    //alert(pane + " " + page_urls[page_id] + " " + page_id);
	    new Ajax.Updater(pane, page_urls[page_id], args);
	}
	
	this.getActiveId = function() { return activeId; }
	this.setActiveId = function(id) { activeId = id; }
	
	for (page_id in page_urls) {
		Event.observe(page_id, 'click', function(e) {
			if ('function' == typeof(args.onClick))
				args.onClick(e);

			for (page_id in page_urls) $(page_id).removeClassName('active').removeClassName('on');
			
			activeId = Event.element(e).id;
			this.load_page(Event.element(e).addClassName('active').id);
			Event.stop(e);
		}.bindAsEventListener(this));

		if ($(page_id).hasClassName('active')) { this.load_page(page_id); }
	}
}
