YAHOO.namespace('PHORK');

(function () {

	//local shortcuts to the YUI objects
	var Dom = YAHOO.util.Dom,
		Event = YAHOO.util.Event;
		
	
	/*---------------------------- OBJECT REGISTRY ----------------------------*/
	
	
	/**
	 * Registers objects so they can be accessed from the
	 * global scope.
	 */
	var Registry = {
		objects: {},
		
		get: function(id) {
			if (this.objects[id]) {
				return this.objects[id];
			}
		},
		
		set: function(id, object) {
			this.objects[id] = object;
		}
	};
	
	//make the object public
	YAHOO.PHORK.Registry = Registry;
	
	
	/*---------------------------- TOGGLER ELEMENT ----------------------------*/
	
	
	/**
	 * Sets up an element to toggle another element or
	 * group of elements from hidden to visible or vice
	 * versa.
	 *
	 * - required -
	 *
	 * config.elements - an object containing the elements to toggle
	 *  	elements: {
	 *			id: {
	 *				element: Dom.get('element'),
	 *				height: 42,
	 *				animate: true
	 *			}
	 *		} 
	 *
	 * - optional -
	 *
	 * config.register - whether to add the object to the registry
	 */
	var TogglerElement = function(element, config) {
 		config = config || {};
 		
 		this.elements = {};
 		this.elements.active = element;
 		this.elements.passive = config.elements;
 		
 		if (config.register) {
 			Registry.set(Dom.generateId(element), this);
 		}
 		
 		this.init();
	};
	
	//make the class public
	YAHOO.PHORK.TogglerElement = TogglerElement;
	
	//adds the event listener to the toggler element and sets up the animation when the library has loaded
	TogglerElement.prototype.init = function() {
		Event.addListener(this.elements.active, 'click', function(e) {
			this.toggle(e);
		}, this, true);
		
		Registry.get('loaded').subscribe(function() {
			for (var i in this.elements.passive) {
				if (this.elements.passive[i].animate) {
					var attributes = {};
					if (this.elements.passive[i].width) {
						attributes.width = { 
							to: this.elements.passive[i].width
						};
					}
					if (this.elements.passive[i].height) {
						attributes.height = { 
							to: this.elements.passive[i].height
						};
					}
					this.elements.passive[i].show = new YAHOO.util.Anim(this.elements.passive[i].element, attributes, .5, YAHOO.util.Easing.easeOut);
					this.elements.passive[i].show.onStart.subscribe(function() {
						this.show(this.elements.passive[i].element);
					}, this, true);
					
					var attributes = {};
					if (this.elements.passive[i].width) {
						attributes.width = { 
							to: 0
						};
					}
					if (this.elements.passive[i].height) {
						attributes.height = { 
							to: 0
						};
					}
					this.elements.passive[i].hide = new YAHOO.util.Anim(this.elements.passive[i].element, attributes, .5, YAHOO.util.Easing.easeIn);
					this.elements.passive[i].hide.onComplete.subscribe(function() {
						this.hide(this.elements.passive[i].element);
					}, this, true);
					
					if (Dom.getStyle(this.elements.passive[i].element, 'display') == 'none') {
						Dom.setStyle(this.elements.passive[i].element, 'height', 0);
					}		
				}
			}
		}, this, true);
	};
	
	//toggles the elements to hidden or visible
	TogglerElement.prototype.toggle = function(e) {
		Event.stopEvent(e);
		
		for (var i in this.elements.passive) {
			var region = Dom.getRegion(this.elements.passive[i].element)[0];
			
			if (!region || region.width == 0 || region.height == 0) {
				if (this.elements.passive[i].animate) {
					this.elements.passive[i].show.animate();
				} else {
					this.show(this.elements.passive[i]);
				}
			} else {
				if (this.elements.passive[i].animate) {
					this.elements.passive[i].hide.animate();
				} else {
					this.hide(this.elements.passive[i]);
				}
			}
		}
	};
	
	//sets an element to visible
	TogglerElement.prototype.show = function(element) {
		if (element.width) {
			Dom.setStyle(element.element, 'width', element.width + 'px');
		}
		if (element.height) {
			Dom.setStyle(element.element, 'height', element.height + 'px');
		}
	};
	
	//sets an element to hidden
	TogglerElement.prototype.hide = function(element) {
		if (element.width) {
			Dom.setStyle(element.element, 'width', 0);
		}
		if (element.height) {
			Dom.setStyle(element.element, 'height', 0);
		}
	}
})();


/*---------------------------- IMPLEMENTATION ----------------------------*/


YAHOO.util.Event.onDOMReady(function() {

	//local shortcuts to the YUI objects
	var Dom = YAHOO.util.Dom,
		Event = YAHOO.util.Event,
		Registry = YAHOO.PHORK.Registry;

	//register a custom event to fire when all libraries have been loaded
	Registry.set('loaded', new YAHOO.util.CustomEvent('onLoadGlobal'));		

	//load the yui javascript libraries
	new YAHOO.util.YUILoader({
		base: '',
		require: ['animation'],
		loadOptional: false,
		combine: true,
		filter: 'MIN',
		allowRollup: true,
		onSuccess: function() {
			Registry.get('loaded').fire();
		}
	}).insert(null, 'js');

	//set up the table of contents link to expand and collapse
	var tocToggler;
	if (tocToggler = Dom.getElementsByClassName('toggler', 'a', 'tab')) {
		new YAHOO.PHORK.TogglerElement(tocToggler, {
			elements: {
				toc: {
					element: Dom.getElementsByClassName('toc_wrapper'),
					height: 415,
					animate: true
				}
			},
			register: false
		});
	}
	
	var tocCloser;
	if (tocCloser = Dom.getElementsByClassName('close', 'div')) {
		new YAHOO.PHORK.TogglerElement(tocCloser, {
			elements: {
				toc: {
					element: Dom.getElementsByClassName('toc_wrapper'),
					height: 415,
					animate: true
				}
			},
			register: false
		});
	}
});