/*
	sd-collapse by Adam
*/
// YUI Namespace
YAHOO.namespace('sovrn');
YAHOO.sovrn.Collapse = {
	init:function(){
		//css classes used by this fn
		YAHOO.sovrn.Collapse.css = {
			triggerClass:'sd-collapse',
			hideClass:'x-hide',
			parentClass:'x-parent',
			openClass:'x-open'
		}
		var css = YAHOO.sovrn.Collapse.css;
		
		//console.log(typeof(css))
		
		// get possible bookmark
		var bookmark = window.location.hash.replace('#','');
		// get all elements with the correct class
		var elms = YAHOO.util.Dom.getElementsByClassName(css.triggerClass);
		// loop over all the elements
		for(var i=0,j=elms.length;i<j;i++){
		
			//console.log("this: " + elms[i].innerHTML)
		
			var t = YAHOO.sovrn.Collapse.getNext(elms[i]);
			if(t){
			
				//console.log("next: " + t.innerHTML)
				
				
				// get the element's ID or create a new one
				var newID = t.id || YAHOO.util.Dom.generateId();
				t.setAttribute('id',newID);
				
				// create a new target and replace the element's
				// content with this new element
				var a = document.createElement('a');
				a.setAttribute('href','#'+newID);
				var c = elms[i].innerHTML;
				a.innerHTML = elms[i].innerHTML;
				elms[i].innerHTML = '';
				elms[i].appendChild(a);
				
				// if the ID is not the bookmark add the parent class 
				// to the trigger element and hide the element by 
				// adding the hide class
				if(newID !== bookmark){
				  YAHOO.util.Dom.addClass(elms[i],css.parentClass);
				  YAHOO.util.Dom.addClass(t,css.hideClass);
				// otherwise remove the hide class and add the 
				// open class to the trigger
				} else {
				  YAHOO.util.Dom.addClass(elms[i],css.openClass);
				  YAHOO.util.Dom.removeClass(t,css.hideClass);
				};
				// add a click handler to the link pointing to toggle()
				YAHOO.util.Event.on(a, 'click', YAHOO.sovrn.Collapse.toggle);				
				
			
			}
  
		
		
		
		}
		
		
		
	}, //init
	
	// tool method to get the next sibling that is not a text node
	getNext:function(o){
	var t = o.nextSibling;
	if(t){
	  while(t.nodeType !== 1 && t.nextSibling){
		t = t.nextSibling;
	  }
	}
	return t;  
	}, //getnext
	
	// method to toggle the showing and hiding of the next element
	toggle:function(e){
	// shortcut for CSS object
	var css = YAHOO.sovrn.Collapse.css;
	// if the element has the trigger class it is a link, otherwise 
	// it is a generated link by init()
	var parent = YAHOO.util.Dom.hasClass(this,css.triggerClass) ? this : this.parentNode;
	// grab the ID the link points to from the href attribute and get the element
	var id = this.href.replace(/.*#/,'');
	var t = document.getElementById(id);
	if(t !== undefined){
	  // if the element is hidden (has the hide class) remove the hide 
	  // class and swap parent for open and vice versa
	  if(YAHOO.util.Dom.hasClass(t,css.hideClass)){
		YAHOO.util.Dom.removeClass(t,css.hideClass);
		YAHOO.util.Dom.replaceClass(parent,css.parentClass,css.openClass);
		YAHOO.util.Event.preventDefault(e);
	  } else {
		YAHOO.util.Dom.addClass(t,css.hideClass);
		YAHOO.util.Dom.replaceClass(parent,css.openClass,css.parentClass);
		// don't follow the link when you hide the element
		YAHOO.util.Event.preventDefault(e);
	  };
	};
	} 
	
  	
}
// Init sd-collapse when DOM is ready
YAHOO.util.Event.onDOMReady(YAHOO.sovrn.Collapse.init);