var Accordion = Class.create({
	version: "1.0",
	initialize: function(options){
		// option defaults
		this.options = Object.extend({
			containerId: "accordion",
			tabSelector: "div#accordion div.tab",
			triggerSelector: "div#accordion div.trigger",
			drawerSelector: "div#accordion div.drawer",
			eventType: "click",
			defaultOpenTab: 0
		}, options || {});
		
		// accordion parts
		this.container = $(this.options.containerId);
		this.tabs = $$(this.options.tabSelector);
		this.triggers = $$(this.options.triggerSelector);
		this.drawers = $$(this.options.drawerSelector);
		this.accordionId = this.options.containerId;
		if (this.options.animator){
			this.animator = this.options.animator(this);
		}

		// set the state of all tabs to open
		this.tabs.each(function(currentTab){
			currentTab._open = true;
		});
		
		// check query string for default tab
		this.readQueryString();
				
		// quick close all tabs except default (no parallel event fired, therefore no animation)
		this.closeOtherTabs(this.tabs[this.options.defaultOpenTab], "quick");
	},
	readQueryString: function(){
		var originalDefaultOpenTab = this.options.defaultOpenTab;
		this.options.defaultOpenTab = window.location.search.toQueryParams()[this.accordionId];
		if(typeof(this.tabs[this.options.defaultOpenTab]) == "undefined"){
			this.options.defaultOpenTab	= originalDefaultOpenTab;
		}
	},
	openTab: function(tab){
		if(tab._open) { return; }
		tab.removeClassName("closed").addClassName("open");
		tab._open = true;
		tab.fire(this.accordionId+"_tab:opened");
	},
	closeTab: function(tab){
		if(!tab._open) { return; }
		tab.removeClassName("open").addClassName("closed");
		tab._open = false;
		tab.fire(this.accordionId+"_tab:closed");
	},
	closeOtherTabs: function(tabToOpen, type){
		// find all open tabs
		var currentlyOpenTabs = this.tabs.findAll(function(currentTab){
			return currentTab._open; 
		});
		
		// close each open tab
		currentlyOpenTabs.each(function(currentTab){
			currentTab.removeClassName("open").addClassName("closed");
			currentTab._open = false;
		});
		
		// set active tab to open
		tabToOpen.removeClassName("closed").addClassName("open");
		tabToOpen._open = true;
		
		// if type is quick, fire initialization event
		if (type == "quick"){
			this.container.fire(this.accordionId+"_accordion:initialized",{inactiveTabs: currentlyOpenTabs, activeTab: tabToOpen});			
			
		// if type is not quick, fire parallel event
		} else {
			tabToOpen.fire(this.accordionId+"_tab:parallel",{tabToClose: currentlyOpenTabs});
		}
	}
});

Accordion.Controller = Class.create(Accordion, {
	initialize: function($super, options){
		// call superclass's initialize
		$super(options);

		// add event handling
		this.triggers.invoke("observe", this.options.eventType, this.getActivatedTab.bindAsEventListener(this));			
	},
	getActivatedTab: function(e){
		var activatedTrigger = Event.findElement(e, this.options.triggerSelector);
		var activatedTab = this.tabs[this.triggers.indexOf(activatedTrigger)];
		this.manageActivatedTab(activatedTab);
	},
	manageActivatedTab: function(activatedTab){
		// if tab is closed, close other tabs and open it, if it is open, do nothing.
		if(!activatedTab._open){ this.closeOtherTabs(activatedTab); }	
	},
	getCurrentTab: function(){
		return this.tabs.find(function(currentTab){
			return currentTab._open;
		});
	},
	getNextTab: function(){
		var currentTab = this.getCurrentTab();
		var currentIndex = this.tabs.indexOf(currentTab);
		if (currentIndex < this.tabs.length-1){
			return this.tabs[currentIndex+1];
		} else {
			return "Currently open tab is the last tab.";
		}
	},
	getPreviousTab: function(){
		var currentTab = this.getCurrentTab();
		var currentIndex = this.tabs.indexOf(currentTab);
		if (!currentIndex == 0){
			return this.tabs[currentIndex-1];;					
		} else {
			return "Currently open tab is the first tab.";
		}
	}	
});
