var _mSvgForced = true; 
var _mSvgEnabled = true;


var rule;
var strokeWidth;
var strokeColor;
var strokeOpacity;


var fillColor;
var fillOpacity;


var mouseOverColor;

var cityBounds;

var neighborhoods = [];

var ne_neighborhoods = [];

var hasInited = false;	

function initCityView(){

rule = getCSSRule(getStyleSheet("test"), ".polygon-stroke");
strokeWidth = rule.style.borderWidth.split(" ")[0].substring(0,rule.style.borderWidth.split(" ")[0].length-2);
strokeColor = rule.style.color;
strokeOpacity = 1;

rule = getCSSRule(getStyleSheet("test"), ".polygon-fill");
fillColor = rule.style.color;
fillOpacity = rule.style.opacity;

rule = getCSSRule(getStyleSheet("test"), ".polygon-mouseover");
mouseOverColor = rule.style.color;

map.clearOverlays();
//map.setCenter(new GLatLng(45.53,-122.65), 11);
if(!hasInited){



hasInited = true;	

cityBounds = new GLatLngBounds();

}


}

		function clearMap(){
			if(hasInited){
				
				
	   		map.clearOverlays();
	   		
	   		
	   		
	   		for(i in neighborhoods){
	   			polys = neighborhoods[i];
	   			for(t=0;t<polys.length;t++){
		   			map.removeOverlay(polys[t]);
		   			GEvent.clearInstanceListeners(polys[t])
		   			GEvent.clearListeners(polys[t], "click");
		   			GEvent.clearListeners(polys[t], "mouseover");
		   			GEvent.clearListeners(polys[t], "mouseout");
		   		}
	   		}
	   	}
   	}
   	
   	function addQuadrant(quadrant){
   		GEvent.addListener(quadrant, "click", function() {
   			
					neighborhoodView(quadrant);
				
			});
			
			GEvent.addListener(quadrant, "mouseover", function() {
				quadrant.setFillColor(mouseOverColor);
			});
			GEvent.addListener(quadrant, "mouseout", function() {
				quadrant.setFillColor(fillColor);
			});
			
			map.addOverlay(quadrant);
   	}
   	
   	function cityView(){
   		if(!hasInited){
   			initCityView();
   		}
   		else{
   			
	   		clearMap();
	   		changeTabs('neighborhoodsearchdiv');
	   		
				
	   	}
	   	//alert("here2");
	   	
	  	//map.setCenter(cityBounds.getCenter(), map.getBoundsZoomLevel(cityBounds)	);
   	}
   	
   	function neighborhoodView(quadrant){
   		YAHOO.example.container.wait.show();
			clearMap();
			changeTabs('neighborhoodsearchdiv');
			currentQuadrant = quadrant;
			document.getElementById("backToCity").style.display = "inline";
			var url = "";
			
			
			
			if(!neighborhoods[quadrant.name]){
				GDownloadUrl(url, loadQuadrantDetail);
			}
			else{
				var polys = neighborhoods[quadrant.name];
				for(i=0;i<polys.length;i++){
					var neighborhood = polys[i];
					var neighborhood = addEvents(neighborhood);
					map.addOverlay(neighborhood);
				}
				map.disableDragging();
				map.hideControls();
				YAHOO.example.container.wait.hide();
				map.setCenter(currentQuadrant.getBounds().getCenter(), map.getBoundsZoomLevel(currentQuadrant.getBounds())	);
			}
			
   	}
   	
   	function addEvents(neighborhood){
   		GEvent.addListener(neighborhood, "click", function() {
							if(neighborhood.id != ""){
								YAHOO.example.container.wait.show();
								//var getdataurl = 'http://maps.hs1.com/dev/getneighborhooddata.html?nid=' +neighborhood.id ;
								clearMap();
								setNeighborhood(neighborhood.id);
								document.getElementById("backToNeighborhood").style.display = "inline";
								map.enableDragging();
								map.showControls();
								//GDownloadUrl(getdataurl, loadNeighborhoodDetail);
							}
							else{
								alert("you are searching: "+neighborhood.name);
							}
						});
						GEvent.addListener(neighborhood, "mouseover", function() {
							neighborhood.setFillColor(mouseOverColor);
						});
						GEvent.addListener(neighborhood, "mouseout", function() {
							neighborhood.setFillColor(fillColor);
						});
			return neighborhood;
   	}
   	
   	function loadQuadrantDetail(data){
   			var xml = GXml.parse(data);
				var polygons = xml.documentElement.getElementsByTagName("polygon");
				if(polygons && polygons.length > 0){
					
					for (var i = 0; i < polygons.length; i++) {
						var neighborhood = createNeighborhood(polygons[i]);
						
						
						map.addOverlay(neighborhood);
		      }
		      
		      
		    }
				
				map.disableDragging();
				map.hideControls();
				YAHOO.example.container.wait.hide();
				map.setCenter(currentQuadrant.getBounds().getCenter(), map.getBoundsZoomLevel(currentQuadrant.getBounds())	);
			
   	}
   	
   	function clearNeighborhoods(){
   		ne_neighborhoods = [];
   		
   	}
   	
   	function createNeighborhood(polygon){
   		var coords = polygon.getAttribute("coordinates").split("|");
			var pairs = [];
			for(p=0;p<coords.length;p++){
				var coord = coords[p].split(",");
				pairs[p] = new GLatLng(parseFloat(coord[0]),parseFloat(coord[1]));
			}
   		var neighborhood = new BDCCPolygon(pairs, strokeColor, strokeWidth, strokeOpacity, fillColor, fillOpacity, polygon.getAttribute("name"));
			neighborhood.name = polygon.getAttribute("name");
			neighborhood.id = polygon.getAttribute("id");
			GEvent.addListener(neighborhood, "click", function() {
							if(neighborhood.id != ""){
								YAHOO.example.container.wait.show();
								clearMap();
								setNeighborhood(neighborhood.id);
								document.getElementById("backToNeighborhood").style.display = "inline";
								map.enableDragging();
								map.showControls();
								//GDownloadUrl(getdataurl, loadNeighborhoodDetail);
							}
							else{
								alert("you are searching: "+neighborhood.name);
							}
						});
						GEvent.addListener(neighborhood, "mouseover", function() {
							neighborhood.setFillColor(mouseOverColor);
						});
						GEvent.addListener(neighborhood, "mouseout", function() {
							neighborhood.setFillColor(fillColor);
						});
			return neighborhood;
   	}
   	
   	
    function showCityMode(){
    	//YAHOO.example.container.wait.show();
    	//cityView();
    	//document.getElementById("backToCity").style.display = "none";
    	//document.getElementById("backToNeighborhood").style.display = "none";
    	//map.enableDragging();
			//map.showControls();
			
			//YAHOO.example.container.wait.hide();
		}
    
    function showNeighborhoodMode(){
    	//YAHOO.example.container.wait.show();
    	//neighborhoodView(currentQuadrant);
    	//document.getElementById("backToNeighborhood").style.display = "none";
    	//map.disableDragging();
			//map.hideControls();
			
			//YAHOO.example.container.wait.hide();
			map.setCenter(currentQuadrant.getBounds().getCenter(), map.getBoundsZoomLevel(currentQuadrant.getBounds())	);
    }