/** 
 * @fileoverview
 * @author			Michael Ord <michael.ord@think.eu>
 * @version			0.1
 * @class
 * @requires		YAHOO.util.Dom
 * @requires		YAHOO.util.Event
 * 
 * @file			init.js
 * @description		
 */

/**
 *
 */
ThinkCo.namespace ('ThinkCo.component');

/**
 * Method used to implement a show/hide behaviour on DOM elements; based on classes.
 * @method Collapse
 * @usage
 *
 * 			<div class="jsCollapsor jsOpen">
 *				<h3><span class="jsToggle">jsCollapsor</span></h3>
 *				<div class="jsCollapsee">
 *					<p>jsCollapsee content</p>
 *				</div>
 *			</div>
 */
ThinkCo.component.Collapse = function () {
	/**
	 * Private method used to apply a click event to links to trigger the show/hide
	 * behaviour.
	 * @method _toggle
	 * @param {Object} ev
	 * @param {Object} obj
	 */
	var _toggle	= function (ev, obj) {
		// get the reference to the parent container (jsCollapsor)
		var targ	= YAHOO.util.Dom.get (this.hash.substr (1));

		if (targ){
			if ( YAHOO.util.Dom.hasClass (targ, 'jsToggleClosed') ) {
				YAHOO.util.Dom.replaceClass ( targ, 'jsToggleClosed', 'jsToggleOpen' );
			} else if ( YAHOO.util.Dom.hasClass (targ, 'jsToggleOpen') ) {
				YAHOO.util.Dom.replaceClass ( targ, 'jsToggleOpen', 'jsToggleClosed' );
			}
		};
		// if an event was passed, stop the event
		if (ev) {
			YAHOO.util.Event.stopEvent (ev);
		};
	};

	//
	var tmp_location	= window.location.hash.substr (1);

	if (tmp_location) {
		var tmp_el		= YAHOO.util.Dom.get (tmp_location);
		if (tmp_el) {
			if (YAHOO.util.Dom.hasClass (tmp_el, 'jsCollapsor')) {
				YAHOO.util.Dom.addClass (tmp_el, 'jsOpen');
			};
		};
	};

	// get a list of elements that have the jsCollapsor class
	var els		= YAHOO.util.Dom.getElementsByClassName ('jsCollapsor');

	// loop through all the elements
	for (var i = 0; i < els.length; i++) {
		//
		var el		= YAHOO.util.Dom.get (els [ i ]);
		var id		= YAHOO.util.Dom.generateId (el);
		var counter	= 0;

		//
		var tgs	= YAHOO.util.Dom.getElementsByClassName ('jsToggle', 'span', el);



		for (var j = 0; j < tgs.length; j++) {
			var tg			= YAHOO.util.Dom.get (tgs [ j ]);

			// the following check is necessary to get nested collapsors to work as expected
			// get the first parent collapsor of the link/span
			var p			= YAHOO.util.Dom.getAncestorsByClass (tg, 'jsCollapsor', null, null, true);

			// if an ancestor has been found
			if ( p ) {
				// if the ancestor (jsCollapsor) is not the direct parent jsCollapsor of the link, then stop the current loop
				if ( p != el ) {
					continue;
				};
			};

			var a			= YAHOO.util.Dom.create ('a', { title:title, href:'#' + id, className : tg.className, listener: [ 'click', _toggle ] });
				a.innerHTML	= tg.innerHTML;

			tg.parentNode.replaceChild (a, tg);

			counter++;
		};

		if ( counter ) {

			var title	= 'Open'

			// by default the toggle will be closed
			var cls	= 'jsToggleClosed';
			// if the element has the class of 'jsOpen' apply a class that will keep the element open
			if (YAHOO.util.Dom.hasClass (el, 'jsOpen')) {
				cls		= 'jsToggleOpen';
				title	= 'Close'
			};

			YAHOO.util.Dom.addClass (el, cls);
		};
	};
};