jQuery.noConflict();
function print_r(theObj){if(theObj.constructor==Array||theObj.constructor==Object){document.write("<ul>");for(var p in theObj){if(theObj[p].constructor==Array||theObj[p].constructor==Object){document.write("<li>["+p+"] => "+typeof(theObj)+"</li>");document.write("<ul>");print_r(theObj[p]);document.write("</ul>");}else{document.write("<li>["+p+"] => "+theObj[p]+"</li>");}}document.write("</ul>");}}

var toggle=false;
jQuery(document).ready(function() {
	state_reps=new Array();
	rep_states=new Array();
	rep_logos=new Array();
	state_logos=new Array();
	
	jQuery("area").each(function(){ state_logos[jQuery(this).attr('title')] = new Array(); });
	
	jQuery.ajax({
		type: "GET",
		url: "regions_schools_reps2.xml",
		dataType: "xml",
		success: function(xml) {
			
			jQuery(xml).find('region').each(function(){
				var attr_states = jQuery(this).attr('states');
				var attr_name = jQuery(this).find('rep').attr('name');
				var attr_phone = jQuery(this).find('rep').attr('phone');
				var attr_email = jQuery(this).find('rep').attr('email');
				
				// ----- rep_logos of state_reps  ----- 
				rep_logos[attr_name]=new Array();					
				jQuery(this).find('logo').each(function(){
					var theState = jQuery(this).attr('state');	
					var theImg=(
						jQuery('<img></img>')
							.attr('src',"images/logos/"+jQuery(this).text())
							.css('display','none')
							.pngfix()
						.appendTo('#dynamic_logos')
					);	
					rep_logos[attr_name].push(theImg);									
					if (theState!=null) { state_logos[theState].push(theImg); } 			
				});
				

				// ----- rep_states of state_reps -----
				rep_states[attr_name]=new Array(); 
				jQuery("area").each(function(){					
					if (attr_states.indexOf(jQuery(this).attr('title')) != -1) {
						rep_states[attr_name].push(jQuery(this));
					}
				});				
				
				
				// ----- state_reps of rep_states ----- 
				theObj=jQuery('<li></li>')					
					.attr('class','rep_unselected')
					.attr('rep',attr_name)
					.attr('rep_states',attr_states)
					.html('<b class="attr_name">'+attr_name+'</b>' + '<br/><span class="phone">' + attr_phone + 
						  '</span><br/><span class="email">' + attr_email + '</span><br/><br/>')
					.mouseover(function(){
						var thisRep=jQuery(this).attr('rep');
						
						if (mouseIn == false) {
							mouseIn = true;							
							jQuery.each(rep_states[thisRep],function(){
								jQuery(this).trigger("mouseover");
							});		
							jQuery.each(rep_logos[thisRep],function(){
								jQuery(this).show();
							});					
						}
						jQuery(this).attr('class','rep_selected');
					})
					.mouseout(function(){
						mouseIn=false;
						
						var thisRep=jQuery(this).attr('rep');
						jQuery(this).attr('class','rep_unselected');
						
						//this needs to be optimized
						jQuery.each(rep_logos[thisRep],function(){
							jQuery(this).hide();
						});
						jQuery.each(rep_states[thisRep],function(){
								jQuery(this).trigger("mouseout");
							});
					})
				.appendTo('#dynamic_reps ul');
				
				jQuery("area").each(function(){
					var currState=jQuery(this).attr('title');
					if(state_reps[currState]==null){ state_reps[currState]=new Array();}					
					if(attr_states.indexOf(currState)!=-1){
						state_reps[currState].push(theObj);
					}
				});	
			}); //close each(			
		}
	}); //close jQuery.ajax(
	
	
	var mouseIn=false;
	jQuery("area").mouseover(function(){
		if(mouseIn==false){
			mouseIn=true;
			
			var currState=jQuery(this).attr('title');			
			jQuery.each(state_reps[currState], function(){
				jQuery(this).trigger('mouseover');	
			});		
			jQuery.each(state_logos[currState], function(){
				jQuery(this).show();	
			});
			
		}
	});
	jQuery('area').mouseout(function(){
		if(mouseIn==true){
			mouseIn=false;
			
			var currState=jQuery(this).attr('title');
			jQuery.each(state_reps[currState], function(){
				jQuery(this).trigger('mouseout');	
			});
		}
	});
	
	jQuery(".mapper").maphilight().pngfix();
	
}); //close jQuery( 

