//<![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.innerHTML="<img id='mcontrol' style='border: 0px none ; margin: 0px; padding: 0px; position: absolute; left: 0px; top: 0px; width: 70px; height: 126px;' src='/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'/>";
	//c.innerHTML+="<div style='position: absolute; width: 145px; height: 27px; cursor: pointer; left: 60px; top: 15px;' title='Searcht' id='srch'/>";

	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 updateAreas() {
	var sel_counties = [];
	regionsel = document.getElementById('regions');
	regionsel.options.length = 0;
	var opts = [];

	for (i=0; i<document.getElementById('counties').options.length; i++) {
		if (document.getElementById('counties').options[i].selected) {
			county = document.getElementById('counties').options[i].value.toLowerCase();
			if (areas[county]) {
				for (j=0; j<areas[county].length; j++) {
					opts.push(areas[county][j]);
				}
			}
		}
	}

	opts.sort();
	opt_count = 0;
	opt_name = '';
	for (i=0; i<opts.length; i++) {
		if (opt_name!=opts[i]) {
			regionsel.options[opt_count] = new Option(opts[i], opts[i]);
			opt_count++;
			opt_name = opts[i];
		}
	}
	
}

function updateMap() {
	map.clearOverlays() ;

	areasel = document.getElementById('areas');
	if (areasel) {
		for (i=0; i<areasel.options.length; i++) {
			area = areasel.options[i].value.toLowerCase();
			if (areasel.options[i].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 moveSelected(obj, type) {
	area_sel = document.getElementById('areas');
	j = area_sel.options.length;

	add = true;	
	for (i=0; i<j; i++) {
		if (area_sel.options[i].value==obj.value) {
			add = false
			i=j+1;
		}
	}
	
	if (add) {
		area_sel.options[j] = new Option(obj.value, obj.value);
		area_sel.options[j].type=type;
		updateMap();
	}	
	
}

function removeSelected() {
	area_sel = document.getElementById('areas');
	j = area_sel.options.length;
	var area_list = [];
	var vals = [];

	val = false;
	for (i=0; i<j; i++) {
		if (! area_sel.options[i].selected) {
			vals.push(new Array(area_sel.options[i].text, area_sel.options[i].value, area_sel.options[i].type));
		}
	}	

	area_sel.options.length = 0;
	for (i=0; i<vals.length; i++) {
		area_sel.options[i] = new Option(vals[i][0], vals[i][1]);
		area_sel.options[i].type = vals[i][2];
	}
	updateMap();
}


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_exists = false;
	if (document.getElementById('areas')) {
		document.getElementById('areas').options.length=0;
		map_exists = true;
	}
	if (document.getElementById('regions')) {
		document.getElementById('regions').options.length=0;
		map_exists = true;
	}
	if (map_exists) {
		map.clearOverlays();
	}
	document.search_form.reset();
}

//]]>
