// JavaScript Document
// this first method is an all purpose class updating utility for the DOM
/*it has four parameters:
-  a defines the action you want the function to perform (swap: replaces c1 with c2 in object o; 
													   add: adds class c1 to the object o; 
													   remove: removes class c1 from the object o; 
													   check: tests if class c1 is already applied to the oject o and returns true or 
													   false)
-  o the object in question
-  c1 the name of the first class
-  c2 the name ofthe second class
*/
function jscss(a,o,c1,c2)
{
  //alert('a:' + a + ',o:' + o + ',c1:' + 'c2:' + c2);
  switch (a){
    case 'swap':
      o.className=!jscss('check',o,c1)?o.className.replace(c2,c1): o.className.replace(c1,c2);
    break;
    case 'add':
      if(!jscss('check',o,c1)){o.className+=o.className?' '+c1:c1;}
    break;
    case 'remove':
      var rep=o.className.match(' '+c1)?' '+c1:c1;
      o.className=o.className.replace(rep,'');
    break;
    case 'check':
      return new RegExp('\\b'+c1+'\\b').test(o.className)
    break;
  }
}
// this sets the ul div class to show or hide depending on it's current state. It also sets everything to closed the first time thru, and places the onClick funtion on the anchor tag 
function listBlindAction(){
	// check if DOM is available, return if not - this takes us out of the function since there is nothing else we can do here
	if(!document.createTextNode) {return;}
	
	// loop over all the <h2> tags
	var listBlindHeads = document.getElementsByName('listBlindHeader');
	//var listBlindHeads = document.getElementsByTagName('h2');
	//alert("listBlindHeads found are: " + listBlindHeads.length);
	for(var i=0; i<listBlindHeads.length; i++){
	   
		var hideIt = listBlindHeads[i].nextSibling;
		while(hideIt.nodeType!=1)
		{
			hideIt = hideIt.nextSibling;
		}
		//alert(hideIt.className);
		if(hideIt != null)
		{
		    if (hideIt.className == 'listBlind hide')
		    {
		        jscss('add',hideIt,'hide');
		    }
		    else
		    {
		        jscss('add',hideIt,'show');
		    }
			// store the element to be hidden in an attribute
			listBlindHeads[i].hideIt=hideIt;
			listBlindHeads[i].onclick=function()
			{
			    var image = null;
			    for(var i = 0 ; i < this.childNodes.length && image == null; i++)
			    {
			        if(this.childNodes[i].nodeName.toLowerCase() == 'img')
			        {
			            image = this.childNodes[i];
			        }
			    }
			
				// test if the class 'hidden' is already applied to the 
				// next sibling
				if(jscss('check',this.hideIt,'hide'))
				{
				    //alert(this.hideIt.className);
					// if that is the case, replace it with shown and 
					// the headline class with open     
			        if(image != null)
			        {
			            image.src = "../Resources/Images/minusIcon.gif";
			        }
        			jscss('swap',this.hideIt,'hide','show');      
        		} else {
					// and vice versa
					//alert(this.hideIt.className);
					if(image != null)
					{     
			            image.src = "../Resources/Images/plusIcon.gif";
			        }
        			jscss('swap',this.hideIt,'show','hide');      
        		}
				return false;
      		}
		}
	}
}