//<![CDATA[
var zipar = [ ] ;					// Zips Found
var chkar = [ ] ;					// County Found
var ctyar = [ ] ;					// County Found
var marar = [ ] ;					// Marker Array
var lastclick = "" ;					// Last Clicked Point
var map ;

var sel_counties = [];
var show_offices = false;

function LoadMap() {
	if (GBrowserIsCompatible()) { 			// Do Map if Compatible Browser only
	    map = new GMap2(document.getElementById("map"));
	    
		map.addControl(new MyCustomControl());
		map.addControl(new ButtonsControl());
		map.setCenter(new GLatLng(35.58, -82.550), 8);
		//map.setCenter(new GLatLng(center_latitude, center_longitude), 8);
		map.addMapType(G_PHYSICAL_MAP);
	    
		GEvent.addListener(map, 'click', function(overlay, point) {
			if (overlay) {
			} else if (point) {
				checkclick( point ) ;
			}
		});

		if ( location.search.length > 1 ) {
			zippoly( 0, location.search.substring(3,location.search.length) ) ;
		}   
		updateMap();
	} else {
		document.getElementById("map").innerHTML = "<h1>Browser not compatible with Google Maps. Sorry...</h1>" ;
	}
}

function MyCustomControl() {
}

//Default constructor for GControl
MyCustomControl.prototype = new GControl(false,false);

// Default location for the control
MyCustomControl.prototype.getDefaultPosition = function() {
	return new GControlPosition(G_ANCHOR_TOP_LEFT,new GSize(5,5));
};

MyCustomControl.prototype.initialize = function(gmapref) {
	var container=gmapref.getContainer();
	var c = document.createElement("div");
	c.style.width = "70px";
	c.style.height = "126px";
	c.style.position='relative';
	c.style.backgroundImage="url(/images/mapcontrols-sm.png)";
	c.innerHTML+="<div style='position: absolute; left: 22px; top: 0px; width: 18px; height: 18px; cursor: pointer;' title='Pan up' id='panu'/>";
	c.innerHTML+="<div style='position: absolute; left: 3px; top: 21px; width: 18px; height: 18px; cursor: pointer;' title='Pan left' id='panl'/>";
	c.innerHTML+="<div style='position: absolute; left: 39px; top: 21px; width: 18px; height: 18px; cursor: pointer;' title='Pan right' id='panr'/>";
	c.innerHTML+="<div style='position: absolute; left: 22px; top: 40px; width: 18px; height: 18px; cursor: pointer;' title='Pan down' id='pand'/>";
	c.innerHTML+="<div style='position: absolute; width: 18px; height: 18px; cursor: pointer; left: 21px; top: 69px;' title='Zoom In' id='zin'/>";
	c.innerHTML+="<div style='position: absolute; width: 18px; height: 18px; cursor: pointer; left: 21px; top: 96px;' title='Zoom Out' id='zout'/>";
	container.appendChild(c);

	GEvent.addDomListener(document.getElementById('zout'), "click", function() {map.zoomOut();});
	GEvent.addDomListener(document.getElementById('zin'), "click", function() {map.zoomIn();});
	GEvent.addDomListener(document.getElementById('panl'), "click", function() {map.panDirection('1',  '0');});
	GEvent.addDomListener(document.getElementById('panr'), "click", function() {map.panDirection(-1,  0);});
	GEvent.addDomListener(document.getElementById('panu'), "click", function() {map.panDirection(0,  1);});
	GEvent.addDomListener(document.getElementById('pand'), "click", function() {map.panDirection(0,  -1);});
	//GEvent.addDomListener(document.getElementById('srch'), "click", function() {showSrchWin();});
	return c;
}

function ButtonsControl() {
}

//Default constructor for GControl
ButtonsControl.prototype = new GControl(false,false);

// Default location for the control
ButtonsControl.prototype.getDefaultPosition = function() {
	return new GControlPosition(G_ANCHOR_TOP_RIGHT,new GSize(5,5));
};

ButtonsControl.prototype.initialize = function(gmapref) {
	var container=gmapref.getContainer();
	var c = document.createElement("div");
	return c;
}


// Find a zip code and cause it to be drawn on the map...
function findzip( county ) {
	if ( CheckZip( county.zip.value ) > -1 ) {
		alert("Zip Code Keyed Has Already Been Selected.") ;
	} else {
		zippoly( 0, county.zip.value ) ;
	}
}

// Clear overlays, Div area and restore map...
function clearmap( county ) {
	//county.zip.value = "" ;
	//document.getElementById("message").innerHTML = "" ;
	map.clearOverlays() ;
	//lastclick = "" ;
	//zipar = [ ] ;
	//chkar = [ ] ;
	//ctyar = [ ] ;
	//marar = [ ] ;
}

// Check for a double click...
function checkclick ( point ) {
	if ( lastclick != point ) {
		lastclick = point ;
		zippoly( point, '' ) ;
	}
}

// Open an Infowindow when the zip link is clicked in the message div...
function countyLink(county) {
	for (var i = 0; i < ctyar.length; i++) {
		if ( ctyar[i] == county ) {
			break ;
		} 
	}
	GEvent.trigger(marar[i], "click");
}

// Check to see if a County has already been selected...
function CheckCounty(county) {
	for (var i = 0; i < chkar.length; i++) {
		if ( chkar[i] == county ) {
			return(i) ;
		} 
	}
	return(-1) ;
}

// Check to see if a Zip has already been selected...
function CheckZip(zip) {
	for (var i = 0; i < zipar.length; i++) {
		if ( zipar[i] == zip ) {
			return(i) ;
		} 
	}
	return(-1) ;
}

// Find a zip code and return the polygon coordinates along with other information...
function zippoly( name ) {
	try {
		var count = lines[name];
		
		for (var j = 1; j <= count; j++) {
			map.addOverlay( new GPolygon(points[name][j],"#ff0000", 2, 1, '#00ff00',.2) ) ;
		}
	} catch(e) {
		alert("Some error occured during program processing:" + e) ;
	}
}

// Create a marker at a point with an infowindow...
function createMarker(point, html) {
	var marker = new GMarker(point);
	GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml(html);
	});
	return marker;
}

function updateMap() {
	map.clearOverlays() ;

	areasel = document.getElementById('areas');
	for (i=0; i<areasel.options.length; i++) {
		if (areasel.options[i].selected) {
			area = areasel.options[i].value.toLowerCase();
			if (areasel.options[i].getAttribute('type')=='county') {
				try {
					var count = lines[area];
			
					for (var j = 1; j <= count; j++) {
						map.addOverlay( new GPolygon(points[area][j],"#ff0000", 2, 1, '#00ff00',.2) ) ;
					}
				} catch(e) {
					//alert("Some error occured during program processing:" + e) ;
				}		
			} else {
				html = '<b>' + areasel.options[i].value + '</b>';
				map.addOverlay(createMarker(new GLatLng(parseFloat(area_lat[area]),parseFloat(area_long[area])), html));
			}
		}
	}
}



function submitForm() {
	//area_sel = document.getElementById('areas');
	//if (area_sel) {
		//j = area_sel.options.length;

		//for (i=0; i<j; i++) {
		//	area_sel.options[i].selected = true;
		//}
	//}
	
	document.search_form.submit();
	return false;
}

function resetForm() {
	map.clearOverlays();
	document.search_form.reset();
}
//]]>

