﻿function toggleRegionMap(suffix) {
	var container = document.getElementById("pc_region_map");
	if (container.style.display == "inline") {
		container.style.display = "none";
		document.getElementById("pc_mapview_img").src = "/docs/images/landing/map_view"+suffix+".jpg";
		document.body.removeChild(_cttcGmap);
	} else {
		document.getElementById("pc_mapview_img").src = "/docs/images/landing/map_back"+suffix+".jpg";
		toggleToRegionMap();
	}
}

function loadGoogleRegion(){
	_cttcGcontrols.style.display = 'block';
	_cttcGoogleMap();
}

var _cttcMapRegions = new Array('Bay_Area','Central_Coast','Central_Valley','Desert','Gold_Country','High_Sierra','Inland_Empire','Los_Angeles','North_Coast','Orange_County','San_Diego','Shasta_Cascade','Winter_Recreation');
function isPageOnRegion(){
	var l = (window.location.search).toLowerCase();
	var r = false;
	for(i = 0; i < _cttcMapRegions.length; i++){
		if( l.indexOf('section=' + (_cttcMapRegions[i]).toLowerCase()) > -1 ) r = true;
	}
	return r;
}
function identifyRegion(){
	var l = (window.location.search).toLowerCase();
	var r = '';
	for(i = 0; i < _cttcMapRegions.length; i++){
		if( l.indexOf('section=' + (_cttcMapRegions[i]).toLowerCase()) > -1 ){
			r = _cttcMapRegions[i];
			break;
		}
	}
	return r.toLowerCase();
}

var _imgActv, _cttcGmap;
function toggleToRegionMap(){
	//removeImageRollovers(); 
	_imgActv = document.getElementById("pc_region_map");
	_imgActv.style.display = "inline";
	
	var e = _imgActv;
	var l = getElementLeft(e.id);
	var t = getElementTop(e.id);
	var w = getElementWidth(e.id);
	var h = getElementHeight(e.id);

	// create map box
	var mapDiv = document.createElement('div');
	mapDiv.id = 'gMapDisplay';
	mapDiv.style.width = (w) + 'px'; //(w - 40) + 'px';
	mapDiv.style.height = (h) + 'px'; //(h - 40) + 'px';
	mapDiv.style.position = 'absolute';
	mapDiv.style.left = (l) + 'px'; //(l + 20) + 'px';
	mapDiv.style.top = (t) + 'px'; //t + 20) + 'px';
	mapDiv.style.zIndex = 3;
	// create controls box
	var ie6Fix1 = isExplorer() ? 'display:none;' : '';
	var ie6Fix2 = isExplorer() ? '5' : '15';

	// organize child nodes
	document.body.appendChild(mapDiv);

	// assign objects to variables
	_cttcGmap = mapDiv;
	// start expanding box
	var _kml_file = "";
	switch (_googleMapRegion) {
		case "bayarea": {_kml_file = "bay_area";break;}
		case "shastacascade": {_kml_file = "shasta_cascade";break;}
		case "northcoast": {_kml_file = "north_coast";break;}
		case "centralvalley": {_kml_file = "central_valley";break;}
		case "goldcountry": {_kml_file = "gold_country";break;}
		case "highsierra": {_kml_file = "high_sierra";break;}
		case "centralcoast": {_kml_file = "central_coast";break;}
		case "desert": {_kml_file = "desert";break;}
		case "losangeles": {_kml_file = "los_angeles";break;}
		case "orangecounty": {_kml_file = "orange_county";break;}
		case "inlandempire": {_kml_file = "inland_empire";break;}
		case "sandiego": {_kml_file = "san_diego";break;}
	}
	_cttcGoogleMap(_kml_file,_googleMapRegion);
}


/*-------------------------------------------------------------------------
Manages the google region maps
-------------------------------------------------------------------------*/

var _cttcGmapProto, _cttcGmapCoordinates, _cttcGmapPolygon, _cttcGmapIcon, _cttcGmapIconBase, _cttcGmapCurrentRegion;
var _cttcGmapControls = {
	// handles zoom in
	zoomIn: function(){
		_cttcGmapProto.zoomIn();
	},
	// handles zoom out
	zoomOut: function(){
		_cttcGmapProto.zoomOut();
	},
	// toggles to satellite view
	satelliteView: function(){
	
		var s = grabElem('viewToggleSatellite');
		var m = grabElem('viewToggleMap');
		s.style.background = 'url(http://www.visitcalifornia.com/_images/goverlay/viewIndicator.gif) center 5px no-repeat';
		m.style.background = '';
		
		_cttcGmapProto.removeOverlay(_cttcGmapPolygon);
		_cttcGmapProto.setMapType(G_HYBRID_MAP);
		_cttcGmapPolygon = new GGeoXml('http://www.visitcalifornia.com/pages/widgets/KML.aspx/' + _cttcGmapCurrentRegion + '.kml?linecolor=CCFFFFFF&polycolor=33FFFFFF');
		_cttcGmapProto.addOverlay(_cttcGmapPolygon);
	
		//		_cttcGmapProto.removeOverlay(_cttcGmapIcon);
		//		var tempIcon = new GIcon(_cttcGmapIconBase);
		//			tempIcon.image = 'http://www.visitcalifornia.com/_images/goverlay/' + _cttcGmapCurrentRegion + '1.png';
		//		_cttcGmapIcon = new GMarker(_cttcGmapViewOptions.center, {icon:tempIcon});
		//		_cttcGmapProto.addOverlay(_cttcGmapIcon);
	
	},
	// toggles to normal view
	normalView: function(){
		
		var s = grabElem('viewToggleSatellite');
		var m = grabElem('viewToggleMap');
		m.style.background = 'url(http://www.visitcalifornia.com/_images/goverlay/viewIndicator.gif) center 5px no-repeat';
		s.style.background = '';
		
		_cttcGmapProto.removeOverlay(_cttcGmapPolygon);
		_cttcGmapProto.setMapType(G_NORMAL_MAP);
		_cttcGmapPolygon = new GGeoXml('http://www.visitcalifornia.com/pages/widgets/KML.aspx/' + _cttcGmapCurrentRegion + '.kml?linecolor=CC965c00&polycolor=02965c00');
		_cttcGmapProto.addOverlay(_cttcGmapPolygon);
		
		//		_cttcGmapProto.removeOverlay(_cttcGmapIcon);
		//		var tempIcon = new GIcon(_cttcGmapIconBase);
		//			tempIcon.image = 'http://www.visitcalifornia.com/_images/goverlay/' + _cttcGmapCurrentRegion + '2.png';
		//		_cttcGmapIcon = new GMarker(_cttcGmapViewOptions.center, {icon:tempIcon});
		//		_cttcGmapProto.addOverlay(_cttcGmapIcon);
	
	},
	// pans the map to the center of region
	panMap: function(){
		var o = document.createElement('input');
		o.type = "text";
		o.style.position = 'absolute';
		o.style.top = '0px';
		o.style.left = '0px';
		o.style.width = '900px';
		o.value = 'o.center = new GLatLng' + _cttcGmapPolygon.getDefaultBounds().getCenter() + ';o.zoom = ' + _cttcGmapProto.getBoundsZoomLevel(_cttcGmapPolygon.getDefaultBounds()) + ';';
		document.body.appendChild(o);
		_cttcGmapProto.setCenter(_cttcGmapPolygon.getDefaultBounds().getCenter(), _cttcGmapProto.getBoundsZoomLevel(_cttcGmapPolygon.getDefaultBounds()));
	},
	// obtains the zoom level and center
	defaultOptions: function(reg){
		var o = new Object();
		switch(reg){
			case 'shastacascade': o.center = new GLatLng(40.673522, -121.815225);o.zoom = 7; break;
			case 'northcoast': o.center = new GLatLng(40.11400799999999, -123.339954);o.zoom = 6; break;
			case 'centralvalley': o.center = new GLatLng(37.30798299999999, -120.66925799999999);o.zoom = 6; break;
			case 'goldcountry': o.center = new GLatLng(38.427641, -120.45380800000001);o.zoom = 7; break;
			case 'highsierra': o.center = new GLatLng(37.483675, -119.19773299999999);o.zoom = 6; break;
			case 'centralcoast': o.center = new GLatLng(35.490717, -120.43011900000002);o.zoom = 7; break;
			case 'desert': o.center = new GLatLng(35.02199099999999, -116.502991);o.zoom = 6; break;
			case 'losangeles': o.center = new GLatLng(34.25417600000001, -118.451858);o.zoom = 8; break;
			case 'orangecounty': o.center = new GLatLng(33.629018, -117.85600599999998);o.zoom = 9;; break;
			case 'inlandempire': o.center = new GLatLng(34.053658000000006, -117.023458);o.zoom = 8; break;
			case 'sandiego': o.center = new GLatLng(32.986062, -116.83935800000002);o.zoom = 8; break;
			default: o.center = new GLatLng(37.566337, -122.264595);o.zoom = 8; //case 'bay_area':
		}
		return o;
	}
}
function _cttcGoogleMap(){
	if (arguments.length > 0) {
		_kmlFile = arguments[0];
		_cttcGmapCurrentRegion = arguments[1];
	}
	var trace = false;
	if(trace){_w = document.createElement('input');_w.type='text';_w.style.width='900px';_w.style.position='absolute';_w.style.left='0px';_w.style.top='0px';}
	
	if(!_cttcGmapCurrentRegion || _cttcGmapCurrentRegion == '') {
		_cttcGmapCurrentRegion = identifyRegion();
		_kmlFile = _cttcGmapCurrentRegion;
	}
	_cttcGmapViewOptions = _cttcGmapControls.defaultOptions(_cttcGmapCurrentRegion);
	
	if(trace) _cttcGmapProto = new GMap2(_cttcGmap, {draggableCursor:'crosshair',draggingCursor:'crosshair'});
	else _cttcGmapProto = new GMap2(_cttcGmap);
	_cttcGmapProto.enableScrollWheelZoom();
	_cttcGmapProto.setCenter(_cttcGmapViewOptions.center, _cttcGmapViewOptions.zoom);
	_cttcGmapProto.setMapType(G_NORMAL_MAP);
	
	_cttcGmapPolygon = new GGeoXml('http://www.visitcalifornia.com/pages/widgets/KML.aspx/' + _kmlFile + '.kml?linecolor=CC965c00&polycolor=02965c00');
	_cttcGmapProto.addOverlay(_cttcGmapPolygon);
	
	_cttcGmapIconBase = new GIcon();
		_cttcGmapIconBase.size = new GSize(200, 30);
		_cttcGmapIconBase.iconAnchor = new GPoint(100, 15);
		
	_cttcGmapProto.addControl(new GSmallMapControl());
	
	//	var tempIcon = new GIcon(_cttcGmapIconBase);
	//		tempIcon.image = 'http://www.visitcalifornia.com/_images/goverlay/' + _cttcGmapCurrentRegion + '2.png';
	//	_cttcGmapIcon = new GMarker(_cttcGmapViewOptions.center, {icon:tempIcon});
	//	_cttcGmapProto.addOverlay(_cttcGmapIcon);
	
	if(trace){
		_cttcGmapCoordinates = getRegionCoordinates();
		var newOverlay = new GPolygon(allCoordinates,"#000000",1,1,"#000000",0);
		function enterCoordinates(lat,lon){
			_w.value += (_w.value == '') ? lat + ',' + (lon * -1) : '|' + lat + ',' + (lon * -1);
			_cttcGmapProto.removeOverlay(newOverlay);
			newOverlay = new GPolygon(coordinates(_w.value),"#000000",1,1,"#000000",0);
			_cttcGmapProto.addOverlay(newOverlay);
		}
		_cttcGmapProto.addOverlay(newOverlay);
		GEvent.addListener(_cttcGmapProto, "click", function(o,l){
			if(!o) enterCoordinates(l.lat(), l.lng());
		});
		GEvent.addListener(_cttcGmapPolygon, "click", function(l){
			enterCoordinates(l.lat(), l.lng());
		});
	}
	
	//setTimeout(_cttcGmapControls.panMap, 5000);
}

function getRegionCoordinates(){
	var createPoints = false;
	if(!Coords){
		var Coords = new String();
		createPoints = true;
	}
	var cleanupCoords = function(v){
		if(v.indexOf('-') > -1){
			var sides = v.split('-');
			var left = parseFloat(sides[0]);
			var right = parseFloat(sides[1]);
			return left + (right / 60);
		}else{
			return parseFloat(v);
		}
	}
	var geoCoords = new Array();
	var geoCodes = Coords.split('|');
	for(i = 0; i < geoCodes.length; i++){
		
		cords = geoCodes[i].split(',');
		var point = new GLatLng( cleanupCoords(cords[0]), cleanupCoords(cords[1]) * -1 );
		
		if(createPoints && _w){
			var marker = new GMarker(point);
			marker.originalCoordinates = geoCodes[i];
			GEvent.addListener(marker,"click",function(){enterCoordinates(this.getLatLng().lat(), this.getLatLng().lng());});
			map.addOverlay(marker);
		}
		
		geoCoords.push(point);;
	}
	geoCoords.push(geoCoords[0]);
	return geoCoords;
}
