/* 
	Tab Controller
	Initilise with:
		new TabController(htmlelement | string id, object options)
		exclude_list example: {tab_selector: 'div.tab', hover: true} (see options list in initialize function)
*/
TabController = Class.create({
	
	initialize: function(tab_list_container, options) {
		
		this.tabs = [];
		this.options = {
			tab_selector: 'li', // css target of tab within the tab list 
			use_class_on_link: false, // set to true to set the active class name on the link instead of the tab
			link_selector: 'a', // css target of the link within each tab
			active_class_name: 'selected', // class name of active class
			default_tab: 'first', // either 'first', 'last', the tab number (starting from 0), or the css id / html element
			hover: false, // switch tab on hover state instead of clicked state?
			allow_bookmark: true // allow the tabs to be bookmarkable
		};
							
		Object.extend(this.options, options, options || {});
		
		$(tab_list_container).select(this.options.tab_selector).each(function(tab) {
			this.addTab(tab);
		}, this);
		
		if (this.options.allow_bookmark && location.hash) {
			var found = false;
			if (found = (this.tabs.find(function(tab) { return tab.container.id == location.hash.replace('#', '') }))) {
				this.options.default_tab = found.tab;
				window.scrollTo(0,0);
				if (Prototype.Browser.IE) {
					var temp_id = found.container.id;
					location.hash = '';
				}
			}
		}
		
		if (this.options.default_tab == 'first') {
			this.setActive(this.tabs.first());
		} else if (this.options.default_tab == 'last') {
			this.setActive(this.tabs.last());
		} else if (typeof(this.options.default_tab) === 'number') {
			this.setActive(this.tabs[this.options.default_tab]);	
		} else {
			var found_elem = false;
			if (found_elem = this.tabs.find(function(tab) {
				return tab.tab === $(this.options.default_tab);
			},this)) {  this.setActive(found_elem); } //note different syntax for enumerator... same as bind(this)
		}
	},
	
	addTab: function(chosen_tab) {
		var link, href;
		if (link = chosen_tab.select(this.options.link_selector)) {
			if ((href = link[0].getAttribute('href')) && (href = href.split('#')[1]) ) {
				if ($(href)) {
					link[0][this.options.hover ? 'onmouseover' : 'onclick'] = function(e) { 
						Event.stop(e);
						this.processAction(Event.element(e)); 
					}.bindAsEventListener(this);
					this.tabs.push({tab:chosen_tab, container: $(href)});
				}
			}
		}
	},
	
	setActive: function(chosen_tab, clicked) {
		this.tabs.each(function(tab) {	
					
			if (tab === chosen_tab) {
				if (this.options.use_class_on_link) {
					tab.tab.select(this.options.link_selector)[0].addClassName(this.options.active_class_name);
				} else {
					tab.tab.addClassName(this.options.active_class_name);
				}
				tab.container.addClassName(this.options.active_class_name);
				if (clicked && this.options.allow_bookmark) {
					this.setBookmarkable(tab.container);
				}
			} else {
				if (this.options.use_class_on_link) {
					tab.tab.select(this.options.link_selector)[0].removeClassName(this.options.active_class_name);
				} else {
					tab.tab.removeClassName(this.options.active_class_name);
				}
				tab.container.removeClassName(this.options.active_class_name);
			}
			
			if (typeof(PNGBehavior) !='undefined') { // IE 6 fix for tabs with png backgrounds
				if (this.options.use_class_on_link && tab.tab.select(this.options.link_selector)[0].getAttribute('png_replaced')) {
					tab.tab.select(this.options.link_selector)[0].style.backgroundImage = '';
					PNGBehavior.replace(tab.tab.select(this.options.link_selector)[0]);
				} else if (tab.tab.getAttribute('png_replaced')) {
					tab.tab.style.backgroundImage = '';
					PNGBehavior.replace(tab.tab);
				}
			}
								
		}, this);
	},
	
	processAction: function(elem, has) {
		this.setActive(this.tabs.find(function(tab) {
			return tab.tab.select(this.options.link_selector)[0] === elem;
		}, this), true);
	},
	
	setBookmarkable: function(elem) {
			var tmp_id = elem.id;
			elem.id = '';
			location.hash = tmp_id;
			elem.id = tmp_id;
	}
	
});