
if (Drupal.jsEnabled) {
	jQuery(document).ready(function() {
																	
		//build an array of areas ['r##'] to area_id
		var area_to_tid = [];
		
		//MANHATTAN
		area_to_tid['r1'] = 25;		//battery park
		area_to_tid['r2'] = 24;		//harlem
		area_to_tid['r3'] = 12;		//chelsea
		area_to_tid['r4'] = 2;		//soho
		area_to_tid['r5'] = 59;		//east harlem
		area_to_tid['r6'] = 9;		//east village
		area_to_tid['r7'] = 16;		//financial district
		area_to_tid['r8'] = 4;		//gramercy Park
		area_to_tid['r9'] = 31;		//greenwich village
		area_to_tid['r10'] = 34;	//upper manhattan
		area_to_tid['r11'] = 22;	//lower east side
		area_to_tid['r12'] = 6;		//midtown east
		area_to_tid['r13'] = 13;	//midtown west
		area_to_tid['r14'] = 32;	//morningside heights
		area_to_tid['r15'] = 28;	//kips bay
		area_to_tid['r16'] = 35;	//roosevelt island
		area_to_tid['r17'] = 1;		//tribeca
		area_to_tid['r18'] = 7;		//upper ease side
		area_to_tid['r19'] = 14;	//upper west side
		area_to_tid['r20'] = 11;	//west village
		
		//BROOKLYN
		area_to_tid['r21'] = 46;	//bedford stuyvesant
		area_to_tid['r22'] = 53;	//boerum hill
		area_to_tid['r23'] = 52;	//brooklyn heights
		area_to_tid['r24'] = 57;	//caroll gardens
		area_to_tid['r25'] = 47;	//clinton hill
		area_to_tid['r26'] = 54;	//cobble hill
		area_to_tid['r27'] = 51;	//downtown
		area_to_tid['r28'] = 44;	//east williamsburg
		area_to_tid['r29'] = 48;	//fort greene
		area_to_tid['r30'] = 56;	//gowanus
		area_to_tid['r31'] = 43;	//greenpoint
		area_to_tid['r32'] = 55;	//park slope
		area_to_tid['r33'] = 50;	//prospect heights
		area_to_tid['r34'] = 58;	//redhook
		area_to_tid['r35'] = 49;	//binegar hill
		area_to_tid['r36'] = 45;	//williamsburg
		
		//QUEENS
		area_to_tid['r37'] = 38;	//astoria
		area_to_tid['r38'] = 41;	//hunters point
		area_to_tid['r39'] = 40;	//long island city
		area_to_tid['r40'] = 39;	//ravenswood
		area_to_tid['r41'] = 37;	//stainway
		
		//use this function to swap keys in array
		function array_flip( trans ) { 
			var key, tmp_ar = {};
			for( key in trans ) { tmp_ar[trans[key]] = key;}
			return tmp_ar;
		}
		
		//build an array of area_id to areas ['r##']
		var tid_to_area = [];
		tid_to_area = array_flip(area_to_tid);
		
		
		var area_classes_string = '';
		for (var index in area_to_tid) {
			area_classes_string += ',#' + index;
		}
		area_classes_string = area_classes_string.substring(1);
		
		$("#brooklyn, #queens").show();
		//$("#boroughs_map img").show();
		$("#boroughs_map img").attr("class","png_search_map");
		$(area_classes_string + ", #brooklyn, #queens").hide();
		//$("#boroughs_map img").hide();
		//$("#mh_map, #spacer_mh, #br_map, #spacer_br, #qu_map, #spacer_qu").show();
		//$("#mh_map, #spacer_mh").show();
		/*if ($("#manhattan img").attr("id") != 'spacer_mh') {
	    	$("#manhattan img").attr("class","png_search_map");
	    	
		}*/
		
		//show Queens
		function show_queens(sel) {
			$("#manhattan").hide();
			$("#brooklyn").hide();
			if (sel == 0) { $("#mh_tbl th a:eq(0)").click(); }
			$("#queens").show("slow");
			
			//$("#queens img").show().attr({width: "277", height: "618"}).removeAttr("class").attr("class","png_search_map").hide();
			//$("#qu_map, #spacer_qu").show();
			
			return false;
		}
		//show Brooklyn
		function show_brooklyn(sel) {
			$("#manhattan").hide();
			$("#queens").hide();
			if (sel == 0) { $("#mh_tbl th a:eq(1)").click(); }
			$("#brooklyn").show("slow");
			$("#brooklyn img").attr("class","png_search_map");
			
			return false;
		}
		//show Manhattan
		function show_manhattan(sel) {
			$("#brooklyn").hide();
			$("#queens").hide();
			if (sel == 0) { $("#mh_tbl th a:eq(2)").click();}
			$("#manhattan").show("slow");
			$("#manhattan img").attr("class","png_search_map");
			
			return false;
		}
		
		function countMhChecked() {
	      var n = $("#Manhattan input:checked").length;
	      $("#mh_tbl th .current").text("Manhattan (" + n + ")");
	      $("#manhattan_map area[class=" + tid_to_area[$(this).attr('value')] + "]").click();
	    }
	    function countBrChecked() {
	      var n = $("#Brooklyn input:checked").length;
	      $("#mh_tbl th .current").text("Brooklyn (" + n + ")");
	      $("#brooklyn_map area[class=" + tid_to_area[$(this).attr('value')] + "]").click();
	    }
	    function countQuChecked() {
	      var n = $("#Queens input:checked").length;
	      $("#mh_tbl th .current").text("Queens (" + n + ")");
	      $("#queens_map area[class=" + tid_to_area[$(this).attr('value')] + "]").click();
	    }
	    $(area_classes_string).hide();
	    
	    countBrChecked();
	    countQuChecked();
	    countMhChecked();
	    $("#Manhattan :checkbox").click(countMhChecked);
	    $("#Brooklyn :checkbox").click(countBrChecked);
	    $("#Queens :checkbox").click(countQuChecked);
	    
	    $("#mh_tbl th a").click(function() {
	    	$("#mh_tbl th a").removeAttr('class');
	    	$(this).attr('class','current');
	    	
	    	var bor_name = $(this).text().substring(0, $(this).text().indexOf("(") - 1);
	    	
	    	$("#mh_tbl th strong").text(bor_name + " Locations");
	    	$("#Manhattan, #Brooklyn, #Queens").attr('style','display:none;');
	    	$("#" + bor_name).removeAttr('style');
	    	//alert(bor_name);
	    	switch (bor_name) {
	    		case 'Manhattan':
	    			show_manhattan(1);
	    			//$(".r103").click();
	    			break;
	    		case 'Brooklyn':
	    			show_brooklyn(1);
	    			//$(".r102").click();
	    			break;
	    		case 'Queens':
	    			show_queens(1);
	    			//$(".r101").click();
	    			break;
	    	}
	    	return false;
	    });
		
		
		
		$("#manhattan_map area, #brooklyn_map area, #queens_map area").bind("mouseover", function(e){
	  		$("#" + this.className).show();
	  		var div_text = $("#mh_tbl :checkbox[value=" + area_to_tid[this.className] + "]").parent().text();
	  		//alert(div_text);
	  		if (this.className != 'r101' && this.className != 'r102' && this.className != 'r103') {
		  		$("#tip").fadeIn("slow").text(div_text).css({ 
		          'left': e.pageX + 10,
		          'top': e.pageY - 50
	        	});
	  		}
	  		
	  		
	  	}).bind("mouseout", function(){
	  		$("#" + this.className).hide();
	  		$("#tip").hide();
	  	}).toggle(function(){
				$("#" + this.className).show();
				$(this).unbind("mouseover").unbind("mouseout");
				$("#tip").fadeOut("slow");
			
			$("#mh_tbl :checkbox[value=" + area_to_tid[this.className] + "]").attr("checked", "checked");
			//alert(this.className, "selected");
			//alert($("#boroughs_map div:visible").attr("id"));
			switch ($("#boroughs_map div:visible").attr("id")) {
				case 'manhattan':
					countMhChecked();
					break;
				case 'brooklyn':
					countBrChecked();
					break;
				case 'queens':
					countQuChecked();
					break;
	    	}
			return false;
	    },function(){
			$("#" + this.className).hide();
			$(this).bind("mouseover", function(e){
				$("#" + this.className).show();
				var div_text = $("#mh_tbl :checkbox[value=" + area_to_tid[this.className] + "]").parent().text();
		  		//alert(div_text);
		  		if (this.className != 'r101' && this.className != 'r102' && this.className != 'r103') {
			  		$("#tip").fadeIn("slow").text(div_text).css({ 
			          'left': e.pageX + 10,
			          'top': e.pageY - 50
		        	});
	        	}
				
			}).bind("mouseout", function(){
				$("#" + this.className).hide();
				$("#tip").hide();	
			});
			$("#mh_tbl :checkbox[value=" + area_to_tid[this.className] + "]").removeAttr("checked");
			//alert(this.className, "unselected");
			switch ($("#boroughs_map div:visible").attr("id")) {
				case 'manhattan':
					countMhChecked();
					break;
				case 'brooklyn':
					countBrChecked();
					break;
				case 'queens':
					countQuChecked();
					break;
	    	}
			return false;
	    });
		//show Queens
		$(".r101").click(function() {
			show_queens(0);
		});
		
		//show brooklyn
		$(".r102").click(function() {
			show_brooklyn(0);
		});
		
		//show manhattan
		$(".r103").click(function() {
			show_manhattan(0);
		});
	});
}
