/**
 * WJAccordion handles the accordion display
 *
 * Changelog:
 *  - Mon Nov 24 2008 : Renamed to WJAccordion and added keyword argument and usage
 *  - Tue Okt 28 2008 : Added functionality for tweening the accordion with Scriptaculous Library | Leonard Visser
 *
 * @since Tue Sep 2 2008
 * @revision $Revision$
 * @author Giso Stallenberg
 * @package Windmill.Javascript.SBA
 **/
var WJAccordion = Class.create({
	/**
	 * initialize
	 *
	 * Creates a new WJAccordion
	 *
	 * @since Tue Sep 2 2008
	 * @access public
	 * @return WJAccordion
	 **/
	initialize: function(keyword) {
		this.keyword = keyword;
		this.elements = new Hash();
		Event.observe(window, 'load', this._showHrefItem.bind(this) );
	},

	/**
	 * register
	 *
	 * A way to register an item to this object
	 *
	 * @since Tue Sep 2 2008
	 * @access public
	 * @param integer barid
	 * @param integer itemid
	 * @return void
	 **/
	register: function(barid, itemid) {
		var block = $(barid + "_content");
		this.elements.set(itemid, block);
		this._addObserver(barid, itemid);
		block.hide(); // using hide here (and not through style, because it's unobstructive)
	},

	/**
	 * _addObserver
	 *
	 * Adds a click observer to the given bar
	 *
	 * @since Tue Sep 2 2008
	 * @access protected
	 * @param integer barid
	 * @param integer itemid
	 * @return void
	 **/
	_addObserver: function(barid, itemid) {
		$(barid).observe('click', this.toggle.bind(this, barid, itemid) );
	},

	/**
	 * toggle
	 *
	 * First check if a Effect is running, if not then Choose between show and hide
	 *
	 * @since Mon Sep 15 2008
	 * @access public
	 * @param integer barid
	 * @param integer itemid
	 * @return void
	 **/
	toggle: function(barid, itemid) {
		if(Effect.Queues.get('global').interval == null){
		if ($(barid).hasClassName("show") ) {
			this.hide(barid, itemid);
		}
		else {
			this.show(barid, itemid);
		}
		}
	},

	/**
	 * hide
	 *
	 * Hides the bar content
	 *
	 * @since Mon Sep 15 2008
	 * @access public
	 * @param integer barid
	 * @param integer itemid
	 * @return void
	 **/
	hide: function(barid, itemid) {
		if(Effect.Queues.get('Blindaction').length != 1){
			Effect.BlindUp($(barid + "_content"));
			$(barid).removeClassName.bind($(barid)).defer("show")
			$(barid).addClassName.bind($(barid)).defer("hide")
			document.location.hash = "#";
		}
	},
	
	/**
	 * show
	 *
	 * Shows the block of the clicked bar
	 *
	 * @since Tue Sep 2 2008
	 * @access public
	 * @param integer barid
	 * @param integer itemid
	 * @return void
	 **/
	show: function(barid, itemid) {
		this.elements.values().each(function(element) {
				if (element.id != barid + "_content") {
					Effect.BlindUp(element);
				}
			element.previous("h1").removeClassName("show");
			element.previous("h1").addClassName("hide");
		});
			Effect.BlindDown($(barid + "_content"));
		$(barid).removeClassName.bind($(barid)).defer("hide")
		$(barid).addClassName.bind($(barid)).defer("show")
		document.location.hash = "#" + this.keyword + itemid;
	},

	/**
	 * _showHrefItem
	 *
	 * Tries to find an item identified in the anchor of the url and if found shows it
	 *
	 * @since Tue Sep 2 2008
	 * @access protected
	 * @return void
	 **/
	_showHrefItem: function() {
		var id = document.location.hash.substring(this.keyword.length + 1);
		var element = this.elements.get(id);
		if (Object.isElement(element) ) {
			this.show(element.previous("h1").id, id);
		}
	}
});
