
catchClicks();
displaySelection();
loadChecklist();



/* universal event attacher */
    function addEvent( obj, type, fn ) {
      if ( obj.attachEvent ) {
        obj['e'+type+fn] = fn;
       obj[type+fn] = function(){obj['e'+type+fn]( window.event );}
        obj.attachEvent( 'on'+type, obj[type+fn] );
      } else
        obj.addEventListener( type, fn, false );
    }
    function removeEvent( obj, type, fn ) {
     if ( obj.detachEvent ) {
       obj.detachEvent( 'on'+type, obj[type+fn] );
       obj[type+fn] = null;
     } else
       obj.removeEventListener( type, fn, false );
   }
   
   	
function loadChecklist(){
				if (document.getElementById) {
				// Get all unordered lists
				var lists = document.getElementsByTagName("ul");
				
				for (i = 0; i < lists.length; i++) {
					var theList = lists[i];
					
					// Only work with those having the class "ulchecklist"
					if (theList.className.indexOf("ulchecklist") > -1) {
						var labels = theList.getElementsByTagName("label");
						
						// Assign event handlers to labels within
						for (var j = 0; j < labels.length; j++) {
							var theLabel = labels[j];
							if (labels[0].getElementsByTagName("input")[0].checked){ theLabel.getElementsByTagName("input")[0].checked="true"; }
							if(theLabel.getElementsByTagName("input")[0].checked){
									theLabel.className="checked";
								}
							theLabel.onmouseover = function() { this.className += " hover"; };
							theLabel.onmouseout = function() { this.className = this.className.replace("hover", "");};
							theLabel.getElementsByTagName("input")[0].onclick=function(){	
								if(this.getAttribute('id')==="allloc"){
									if(this.checked){checkAll(true); this.parentNode.className="";}
									else{checkAll(false); this.parentNode.className="checked";}
								}
								else{
									document.getElementById("allloc").checked=false;
									document.getElementById("allloc").parentNode.className="";
								}
								
															
								if(this.parentNode.className.indexOf("checked")<=-1){this.parentNode.className+=" checked";}
								else if(this.parentNode.className.indexOf("hover")>-1){this.parentNode.className=" hover";}
								else{this.parentNode.className="";}	
								}	
						}
					}
				}
			}
}




function displaySelection(){
	var checks = document.getElementById("topics-list").getElementsByTagName("input");
	var selections="";
	var isall=false;
	if(document.getElementById("allloc").checked){
		selections="All Locations";
	}	
	else{
		for (var k=0; k<checks.length; k++){
				if (checks[k].checked){
				var stringdata=checks[k].parentNode.innerHTML;
				var stringdata=stringdata.substring(stringdata.indexOf(">")+1, stringdata.length);
				selections += stringdata+ ", ";
				}
			}
		if(selections.substr(selections.length-2, 2)===", "){selections=selections.substring(0, selections.length-2);}
		if(selections.length==0){selections="Select a Location";}
	}
	document.getElementById("currenttopic").innerHTML=selections;
}

function checkAll(torf){
	var checks = document.getElementById("topics-list").getElementsByTagName("input");
	for (var k=0; k<checks.length; k++){
		checks[k].checked=torf;
		if (torf == true){checks[k].parentNode.className="checked";}
		else{checks[k].parentNode.className="";}
	}
}

		
		
function catchClicks(){	
		addEvent(document, 'click', closeMenu);

		
	//register a show-list event to the topics image
		addEvent(document.getElementById("currenttopic"), 'click', showList); 
		
		document.getElementById("currenttopic").onmouseover=function(){ document.getElementById("currenttopic").className="focus"; };
		document.getElementById("topics-list").onmouseover=function(){ document.getElementById("currenttopic").className="focus"; document.getElementById("topics-list").className+=" focus"};
		document.getElementById("currenttopic").onmouseout=function(){ document.getElementById("currenttopic").className="";};
		document.getElementById("topics-list").onmouseout=function(){ document.getElementById("currenttopic").className=""; document.getElementById("topics-list").className=document.getElementById("topics-list").className.replace("focus", "");};
		document.getElementById("currenttopic").onmousedown=function(){document.getElementById("currenttopic").className=" mousedown";};
		document.getElementById("currenttopic").onmouseup=function(){document.getElementById("currenttopic").className=document.getElementById("currenttopic").className.replace("mousedown","");}
}

function hideList(){
	document.getElementById("topics-list").className="hideList";
}

function showList(){
		if(document.getElementById("topics-list").className=="showList") hideList();
		else document.getElementById("topics-list").className="showList";
}

function isDescendant(descendant,ancestor){
 			     return ((descendant.parentNode==ancestor)  || (descendant.parentNode!=document) &&  isDescendant(descendant.parentNode,ancestor));	}	
				 
function getParentId(target){
		return((target.id) || (target.parentNode!=document)&& getParentId(target.parentNode));
	}

function closeMenu(event){
	var el;
	var targ;
	ts=document.getElementById('topics-select');
	if (event == null){
	  event = window.event;
	}
	var targ = event.target != null ? event.target : event.srcElement;
	
	// follow links; otherwise, go one or two levels up to find a parent element with an ID
				if(targ.tagName=="a" || targ.tagName=="A"){
					return true;
					}
				el=getParentId(targ);
				

	if(el && isDescendant(document.getElementById(el), ts)){
    	return;	
		}
	else{hideList();}
	
	displaySelection();
	
}

