
//Create a new GTileLayer and point to the ParkInfo tile server and layer desired
//greenbelt_infill2 is defined in /var/www/tilecache-2.03/tilecache.cfg (pointing to layer in mapfile)
var tile_bayarea = new GTileLayer(null,null,null,{
	tileUrlTemplate: 'http://www.parkinfo.org/tilecache/tilecache.py/1.0.0/bayarea_background/{Z}/{X}/{Y}.png?type=google',
	isPng:true});
tile_bayarea.getOpacity = function() { return 1.0; }
/*
var tile_bayarea2 = new GTileLayer(null,null,null,{
	tileUrlTemplate: 'http://www.parkinfo.org/tilecache/tilecache.py/1.0.0/bayarea_background/{Z}/{X}/{Y}.png?type=google',
	isPng:true});
tile_bayarea2.getOpacity = function() { return 0.5; }
*/
var tile_bayarea3 = new GTileLayer(null,null,null,{
	tileUrlTemplate: 'http://www.parkinfo.org/tilecache/tilecache.py/1.0.0/nonbayarea_background2/{Z}/{X}/{Y}.png?type=google',
	isPng:true});
//tile_bayarea3.getOpacity = function() { return 1; }

var bayareaNoLabels=[tile_bayarea,tile_bayarea3];
var googMapsSmartSpots=[G_NORMAL_MAP.getTileLayers()[0],tile_bayarea3];
var bayareaBackground = new GMapType(bayareaNoLabels, G_NORMAL_MAP.getProjection(), "SmartGrowth0", {errorMessage:""});
var googMapSS = new GMapType(googMapsSmartSpots, G_NORMAL_MAP.getProjection(), "Google background", {errorMessage:""});

var boundaries = new GLatLngBounds(new GLatLng(36.9,-123.5), new GLatLng(38.8,-121));
//var boundaries = new GLatLngBounds(new GLatLng(36,-124), new GLatLng(39.5,-120));

var kmlHotSpots;
var a = [];

var smartLabels = {"smarts":[
	{"lat":37.78,"lon":-122.44,"label":"<a href=http://www.growsmartbayarea.org/its_possible/map_gallery4.html>San<br>Francisco</a>","style":"smartSpotLeft"},
	{"lat":38.4274,"lon":-122.735,"label":"<a href=http://www.growsmartbayarea.org/its_possible/map_gallery7.html>SMART<br>Corridor</a>","style":"smartSpotLeft"},
	{"lat":37.9,"lon":-122.31,"label":"<a href=http://www.growsmartbayarea.org/its_possible/map_gallery3.html>Inner<br>East<br>Bay</a>","style":"smartSpotRight"},
	{"lat":37.45,"lon":-122.2,"label":"<a href=http://www.growsmartbayarea.org/its_possible/map_gallery2.html>El Camino<br>Real</a>","style":"smartSpotLeft"},
	{"lat":37.40,"lon":-121.85,"label":"<a href=http://www.growsmartbayarea.org/its_possible/map_gallery1.html>Northeast<br>Santa Clara<br>County</a>","style":"smartSpotRight"},
	{"lat":37.65,"lon":-122.08,"label":"<a href=http://www.growsmartbayarea.org/its_possible/map_gallery5.html>Southern<br>Alameda<br>County</a>","style":"smartSpotRight"},
	{"lat":37.9,"lon":-122.01,"label":"<a href=http://www.growsmartbayarea.org/its_possible/map_gallery6.html>Central<br>Contra Costa</a>","style":"smartSpotRight"}
]}

var myObject = {"myCities":[
	//begin 0
	//{"lat":37.774929,"lon":-122.419408,"label":"San Francisco","style":"city4"},
	//begin 1
	{"lat":37.339383,"lon":-121.89496,"label":"San Jose","style":"city4"},
	{"lat":37.804368,"lon":-122.270809,"label":"Oakland","style":"cityLeft"},
	//begin 2
	//{"lat":37.368831,"lon":-122.03635,"label":"Sunnyvale","style":"cityLeft"},
	{"lat":37.354116,"lon":-121.955242,"label":"Santa Clara","style":"cityLeft"},
	//begin 3
	{"lat":38.44047,"lon":-122.714428,"label":"Santa Rosa","style":"city4"},
	{"lat":37.668819,"lon":-122.080792,"label":"Hayward","style":"cityLeft"},
	{"lat":37.831314,"lon":-122.285254,"label":"Emeryville","style":"cityLeft"},
	{"lat":37.441875,"lon":-122.143009,"label":"Palo Alto","style":"city4"},
	//{"lat":37.428267,"lon":-121.906633,"label":"Milpitas","style":"city4"},
	{"lat":37.548264,"lon":-121.988569,"label":"Fremont","style":"city4"},
	{"lat":37.871598,"lon":-122.272753,"label":"Berkeley","style":"cityLeft"},
	{"lat":37.386048,"lon":-122.083852,"label":"Mountain View","style":"city4"},
	{"lat":37.724934,"lon":-122.156077,"label":"San Leandro","style":"cityLeft"},
	//begin 4
	{"lat":38.232417,"lon":-122.636659,"label":"Petaluma","style":"city4"},
	{"lat":38.297136,"lon":-122.285524,"label":"Napa","style":"city4"},
	{"lat":38.104095,"lon":-122.256643,"label":"Vallejo","style":"city4"},
	{"lat":37.93575,"lon":-122.347759,"label":"Richmond","style":"cityLeft"},
	{"lat":38.027982,"lon":-121.884682,"label":"Pittsburg","style":"city4"},
	{"lat":37.977987,"lon":-122.031076,"label":"Concord","style":"city4"},
	{"lat":37.906311,"lon":-122.064961,"label":"Walnut<br>Creek","style":"city4"},
	//begin 5
	{"lat":37.681869,"lon":-121.768006,"label":"Livermore","style":"city4"},
	{"lat":37.485219,"lon":-122.236357,"label":"Redwood City","style":"cityLeft"},
	{"lat":37.005771,"lon":-121.568278,"label":"Gilroy","style":"city4"},
	{"lat":38.356581,"lon":-121.98775,"label":"Vacaville","style":"city4"},
	{"lat":37.973532,"lon":-122.531089,"label":"San Rafael","style":"cityLeft"},
	{"lat":38.249355,"lon":-122.039959,"label":"Fairfield","style":"city4"},
	{"lat":38.799059,"lon":-123.017316,"label":"Cloverdale","style":"city4"}
]}

var iconRed = new GIcon(); 
iconRed.image = 'img/grey999Dot.png';
iconRed.shadow = '';
//iconRed.iconSize = new GSize(8, 8);
iconRed.iconSize = new GSize(0, 0);
iconRed.shadowSize = new GSize(22, 20);
iconRed.iconAnchor = new GPoint(30,30);
iconRed.infoWindowAnchor = new GPoint(5, 1);
var customIcons = [];
customIcons["city1"] = iconRed;
var markerGroups = { "city0": [], "city1": [], "city2": [], "city3": [], "city4": [], "city5": [], "smartSpot": [], "smartSpotLeft": [], "smartSpotRight": [], "cityLeft": []};			
var icon;
var opts;

var mySmartLabels = [];
var myMarkers = [];
var citiesState = true;
var map;

function init(){
  map =
    new GMap2(
      document.getElementById("map_canvas"),
      {
	    backgroundColor: "#ffffff",
        mapTypes:
        [
          bayareaBackground,
		  googMapSS//,
			/*
			G_PHYSICAL_MAP,
			G_HYBRID_MAP
		        //G_SATELLITE_3D_MAP
		  */
        ]
      }
	);
	var m = map.getMapTypes();
	for (var j = 0; j < m.length; j++) {
		m[j].getMinimumResolution = function() {return 8};
		m[j].getMaximumResolution = function() {return 12};
	}
	/*
	for (var i = 0; i < hotpolys.length; i++) {
		for (var k = 0; k < 1; k++) {
			//alert(hotpolys[i].length);
			//hotpolys[i][k].weight = 10;
			//hotpolys[i][k].color = "#0000ff";
		}
		addHotPoly(hotpolys[i]);
	}
	*/
	function addHotPoly(poly){
		var hotpolygon = new GPolygon();
		hotpolygon = poly;
		//alert(hotpolygon.strokeWeight);
		hotpolygon.setFillStyle({opacity:0.8});
		//hotpolygon.strokeWeight = 2;
		//hotpolygon.strokeColor = hotpolygon.fillColor;
		map.addOverlay(hotpolygon);

		return hotpolygon;
	}
	
		
	GEvent.addListener(map, 'maptypechanged', function(){
		if (map.getCurrentMapType() == bayareaBackground) {
			toggleOverlay(mySmartLabels);
			/*
			document.getElementById("citiesBox").disabled = false;
			document.getElementById("hwysBox").disabled = false;
			if (citiesState == false) {
				toggleGroup('city0');
				toggleGroup('city1');
				toggleGroup('city2');
				toggleGroup('city3');
				toggleGroup('city4');
				toggleGroup('city5');
				toggleGroup('cityLeft');
				citiesState = true;
			}
			*/
		}
		else if (map.getCurrentMapType() != bayareaBackground) {
			toggleOverlay(mySmartLabels);
			/*
			document.getElementById("citiesBox").disabled = true;
			document.getElementById("hwysBox").disabled = true;
			if (citiesState == true) {
				toggleGroup('city0');
				toggleGroup('city1');
				toggleGroup('city2');
				toggleGroup('city3');
				toggleGroup('city4');
				toggleGroup('city5');
				toggleGroup('cityLeft');
				citiesState = false;
			}
			*/
		}
		
		else {
		}

	
	}); 

	/*
	var hierarchy = new GHierarchicalMapTypeControl();
	hierarchy.addRelationship(bayareaBackground, myMapGoogLabels, "More labels", false);
	hierarchy.addRelationship(bayareaBackground, normalPlus, "Even more", false);

	//map.addControl(hierarchy, new GControlPosition(G_ANCHOR_BOTTOM_LEFT, new GSize(5,40)));
	*/
	//map.setCenter(new GLatLng(37.9, -121.75), 9); //alcatraz = 37.83, -122.42
 	kmlSpots = new GGeoXml("http://www.greeninfo.org/download/greenbelt/web2/scripts/ss.kml");
	map.addOverlay(kmlSpots);
	
 	kmlHwy = new GGeoXml("http://www.greeninfo.org/download/greenbelt/web2/kml/hwys.kmz");
	//map.addOverlay(kmlHwy);
	//kmlHwy.hide();

	map.setCenter(new GLatLng(37.9, -122.2), 9);

   map.addMapType(bayareaBackground); 
   map.enableScrollWheelZoom();
   map.addControl(new GLargeMapControl(), new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(5,110)));
   
	var zoomOpts = { 
		buttonStartingStyle: {background: 'transparent'},
		buttonHTML: '<img title="Drag Zoom In" src="img/zoomin_off.png">',
		buttonStyle: {background: 'transparent', padding:'0px', width:'30px', height:'30px',border:'none'},
		buttonZoomingHTML: 'Drag a region on the map (click here to reset)',
		buttonZoomingStyle: {background:'#EBEBEB',width:'80px',height:'100%',padding:'5px',fontFamily:'arial',fontSize:'75%',border:'1px solid #666666'},
		backButtonHTML: '<img title="Zoom Back Out" src="img/zoomout_off.png">',  
		backButtonStyle: {display:'none',marginTop:'5px',width:'30px', height:'30px'},
		backButtonEnabled: true, 
		overlayRemoveTime: 1500}  
   
   map.addControl(new DragZoomControl({}, zoomOpts, {}), new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(20,270)));

	addSmartLabels();
	//addLabel();
	//checkCities();
	a["city1"] = iconRed;
	
	

	GEvent.addListener(map, "move", function() {
		checkBounds();
		checkHwys();
	});

	for (var j = 0; j < countylines.length; j++) {
		countylines[j].color = "#c7ac9f";
		countylines[j].weight = 0.5;
		map.addOverlay(countylines[j]);
	}
	for (var j = 0; j < coast.length; j++) {
		coast[j].color = "#c7ac9f";
		coast[j].weight = 0.8;
		map.addOverlay(coast[j]);
	}
	/*
	for (var j = 0; j < hwys.length; j++) {
		hwys[j].color = "#888";
		map.addOverlay(hwys[j]);
		hwys[j].hide;
	}
	toggleOverlay(hwys);
	*/
} 

function checkBounds() {
	// Perform the check and return if OK
	//alert(map.getCenter());
	if (boundaries.contains(map.getCenter())) {
	  return;
	}
	// It`s not OK, so find the nearest allowed point and move there
	var C = map.getCenter();
	var X = C.lng();
	var Y = C.lat();

	var AmaxX = boundaries.getNorthEast().lng();
	var AmaxY = boundaries.getNorthEast().lat();
	var AminX = boundaries.getSouthWest().lng();
	var AminY = boundaries.getSouthWest().lat();

	if (X < AminX) {X = AminX;}
	if (X > AmaxX) {X = AmaxX;}
	if (Y < AminY) {Y = AminY;}
	if (Y > AmaxY) {Y = AmaxY;}
	//alert ("Restricting "+Y+" "+X);
	map.setCenter(new GLatLng(Y,X));
}

function detectMapType() {
  var myMapType = map.getCurrentMapType();
  var myTypeName = myMapType.getName();
  return myTypeName;
}

function addSmartLabels(){
	opts = {
	  //"icon": icon,
	  "clickable": true,
	  "title": "",
	  "labelText": "",
	  "labelOffset": new GSize(1, -10),
	  "labelClass": "city"
	};
	for (var i = 0; i < smartLabels.smarts.length; i++) {
		var label = smartLabels.smarts[i].label;
		var type = smartLabels.smarts[i].style;
		var point = new GLatLng(smartLabels.smarts[i].lat, smartLabels.smarts[i].lon);
		mySmartLabels[i] = createMarkerJK(point, label, type);
		map.addOverlay(mySmartLabels[i]);
	
	}
}

function addLabel(){
	var latlng = new GLatLng(37.83, -122.42);
	var icon = new GIcon();
	//icon.image = 'http://gmaps-samples.googlecode.com/svn/trunk/markers/circular/greencirclemarker.png';
	icon.image = 'img/grey999Dot.png';
	icon.iconSize = new GSize(0, 0);
	icon.iconAnchor = new GPoint(0,0);
	icon.infoWindowAnchor = new GPoint(25, 7);

	opts = {
	  //"icon": icon,
	  "clickable": true,
	  "title": "",
	  "labelText": "",
	  "labelOffset": new GSize(1, -10),
	  "labelClass": "city"
	};
	var marker = new LabeledMarker(latlng, opts);
	var markerLabelText = opts.labelText;


	/*
	for (var i = 0; i < myObject.myCities.length; i++) {
		var label = myObject.myCities[i].label;
		var type = myObject.myCities[i].style;
		var point = new GLatLng(myObject.myCities[i].lat, myObject.myCities[i].lon);
		myMarkers[i] = createMarkerJK(point, label, type);
		map.addOverlay(myMarkers[i]);
	}
	*/
}

function createMarkerJK(point, label, type) {
		if (type == "smartSpotLeft") {
			opts.labelOffset = new GSize(-70,-12);
		}
		else if (type == "smartSpotRight") {
			opts.labelOffset = new GSize(20,-20);
		}
		else if (type == "cityLeft") {
			opts.labelOffset = new GSize(-40,-12);
		}		
		else if (label == "San Francisco") {
			opts.labelOffset = new GSize(-90,-12);
		}
		else {
			opts.labelOffset = new GSize(2,-12);
		}
	    //var marker = new LabeledMarker(point, {labelClass: type, icon: customIcons["city1"], labelText: label, labelOffset:opts.labelOffset});
		var marker = new LabeledMarker(point, {clickable: true, labelClass: type, icon: customIcons["city1"], labelText: label, labelOffset:opts.labelOffset});
		/*
		GEvent.addListener(marker, "click", function() {
			marker.openInfoWindowHtml(label);
		});
		*/
	    markerGroups[type].push(marker);
		return marker;
}

function toggleOverlay(myArray) {
	//var showHotspots = document.getElementById("checkHotspots").checked;
	//if (showHotspots) {
		for (var j = 0; j < myArray.length; j++) {
			var apoly = myArray[j];
			if (apoly.isHidden()) {
				apoly.show();
			} else {
				apoly.hide();
			}
		}
	//}
}

function resetMap(){
	map.setCenter(new GLatLng(37.9, -122.2), 9);
}

function checkCities(){
				toggleGroup('city0');
				toggleGroup('city1');
				toggleGroup('city2');
				toggleGroup('city3');
				toggleGroup('city4');
				toggleGroup('city5');
				toggleGroup('cityLeft');
				citiesState = citiesState * -1;
}


function checkHwys(){
	if (map.getZoom() >= 10) {
		show(kmlHwy);
	} else {
		kmlHwy.hide();
	}
}

function showTransit(){
	if (document.getElementById("railBox").checked == true) {
		show(kmlRail);
	} else {
		kmlRail.hide();
	}
}
function googMap(){
	if (detectMapType() != "SmartGrowth0") {
		map.setMapType(bayareaBackground);
		//document.getElementById("citiesBox").checked = -1 * document.getElementById("citiesBox").checked;
	} else {
		map.setMapType(googMapSS);
		//document.getElementById("citiesBox").checked = -1 * document.getElementById("citiesBox").checked;
	}
	
}

function show(kml) { 
	if (kml.IsAdded) { 
		kml.show(); 
	} else { 
		map.addOverlay(kml);
		kml.IsAdded = true;
	} 
} 
