/*@FILE INFORMATION
----------------------------------------
Author:		Mark Wise 
File:		Expando_1.0.4.css 
Created:	12/4/09 10:50 AM
Updated:	2/8/10 1:04 PM

/*@END--------------------------------*/

var $Expando = {
	
	//PRIVATE
	registeredCollections: [],
	
	//PUBLIC METHOD
	register: function(arg){
		//Is there an argument
		if($defined(arg)){
			//What type of argument
			switch($type(arg)){
				case "string":
					//Is arg an id
					if($(arg)){
						this.registerCollectionFromId(arg);
					
					//Is arg a class
					}else if($$("." + arg).length){
						this.registerCollectionFromClass(arg);
					
					}
					break;
															
				case "object":
					this.registerCollectionFromHash(arg);
					break;
						
			}
					
		}
		
	},
	
	//PRIVATE METHOD
	isRegistered: function(arg){
		var i=0, a=this.registeredCollections, l=a.length;
		for(; i<l; i++){
			if(a[i] === arg){
				return true;
			
			}
				
		}
		return false;
	
	},
	
	//PRIVATE METHOD
	registerCollectionFromId:function(arg){
		//Create reference
		var ele = $(arg);
		//Has the element already been registered
		if(!this.isRegistered(ele)){
			//Iterate through even indexed child elements
			ele.getChildren(":even").each(function(item){
				//Does each trigger(even index) element have a matching target(odd index) element
				if(item.getNext()){	
					//Register click event on trigger elements
					item.addEvent("click", function(){
						//Toggle class on trigger and target elements
						this.toggleClass("show").getNext().toggleClass("show");
																	
					});
								
				}
						
			});
			//Add to registration array
			this.registeredCollections.push(ele);
			
		}
	
	},
	
	//PRIVATE METHOD
	registerCollectionFromClass:function(arg){
		//Iterate through class array
		$$("." + arg).each(function(arr){
			//Has the element already been registered
			if(!this.isRegistered(arr)){
				//Iterate through even indexed child elements
				arr.getChildren(":even").each(function(item){
					//Does each trigger(even index) element have a matching target(odd index) element
					if(item.getNext()){	
						//Register click event on trigger elements
						item.addEvent("click", function(){
							//Toggle class on trigger and target elements
							this.toggleClass("show").getNext().toggleClass("show");
																															
						});
													
					}
					
				});
				//Add to registration array
				this.registeredCollections.push(arr);
			
			}
									
		}.bind(this));
	
	},
	
	//PRIVATE METHOD
	registerCollectionFromHash:function(arg){
		//Iterate throught hash
		$each(arg, function(item, index){
			//Make sure index and item are not null or undefined
			if($(index) && $(item)){
				//Register click event with trigger element
				$(index).addEvent("click", function(){
					//Toggle class on trigger element
					this.toggleClass("show");
					//Toggle class on target element
					$(item).toggleClass("show");					
				
				});
							
			}
					
		});
	
	}

};


