//the following function just ensures that the page loads before executing the rest
window.addEvent('domready', function(){
									 
	//check that the imagemap exists
	if ($('male-map')!=null) {
										 
		//set up the arrays and array lengths
		aBodyparts=$("haz-mod2").getElements("area");
		abLength=aBodyparts.length;	
		aWholeBody=new Array ('arm','blood','nerves','bones','muscles','circulation','hair','joints');
		awbLength=aWholeBody.length
		
		//derive base male and female images
		var maleSrc=$("male-image").getAttribute("src");
		var femaleSrc=$("female-image").getAttribute("src");
		
		//loop through one array to assign the event handlers - this array has terms which are both IDs and classes
		for (nCount=0; nCount < abLength ; nCount++) {
			targetID = aBodyparts[nCount].id;
			//process by ID
			$(targetID).addEvent('mouseover', function(){switcher(this,'mouseover')});
			$(targetID).addEvent('mouseout', function(){switcher(this,'mouseout')});
			//now by class
			document.getElementsBySelector('a.'+targetID).addEvent('mouseover', function(){switcher(this,'mouseover')});
			document.getElementsBySelector('a.'+targetID).addEvent('mouseout', function(){switcher(this,'mouseout')});
		}
		
		//loop through the other array to assign the event handlers - this array deals only with the remaining classes
		for (nCount=0; nCount < awbLength ; nCount++) {
			targetID = aWholeBody[nCount];
			document.getElementsBySelector('a.male-'+targetID).addEvent('mouseover', function(){switcher(this,'mouseover')});
			document.getElementsBySelector('a.male-'+targetID).addEvent('mouseout', function(){switcher(this,'mouseout')});
			document.getElementsBySelector('a.female-'+targetID).addEvent('mouseover', function(){switcher(this,'mouseover')});
			document.getElementsBySelector('a.female-'+targetID).addEvent('mouseout', function(){switcher(this,'mouseout')});
		}
		
		//the switch and reset function
		function switcher(obj,state)	{
			
			if (obj.id=='')	{ 							//if there is no ID...
				obj=obj.className.split(" ",2);			//pick the correct class name	
				obj=obj[1];								//and set the var to that
			}else{
				obj=obj.id								//if there is one, use the ID	
			}
			
			targetSplit=obj.split("-",3); 				//to derive the gender
			targetRaw=obj.replace(/[0-9]/,'1');                              //retain number 1 in arms
			targetFileName=obj.replace(/[0-9]/,''); 	//removes the numbers from the arm values	
			
			if (targetSplit[0]!='selected')   {
			    if (state=='mouseover')	{
				    $(targetSplit[0]+"-image").setAttribute("src","/img/healthaz/bodymap/"+targetFileName+".gif")
				    document.getElementsBySelector('a.'+targetRaw).addClass(targetSplit[0]+'-forced-hover'); //to make the link highlight
			    }else{
				    switch(targetSplit[0]) 					//finds the correct base image to use depending on gender
					    {
					    case "male":
					      var imgSrc=maleSrc;
					      break    
					    case "female":
					      var imgSrc=femaleSrc;
					      break
					    }				
    				
				    $(targetSplit[0]+"-image").setAttribute("src",imgSrc)
				    document.getElementsBySelector('a.'+targetRaw).removeClass(targetSplit[0]+'-forced-hover'); //to make the link highlight
			    }
			}
		}
		
	}

})