/**
 * @author mpeccini
 */
 
var G = google.maps;
var initialExtent = new G.LatLng(50,-120);
var initialZoom = 3;
var gMap;
var mgr = null;
var pointIcon;
var smPointIcon;
var smShipIcon;
var largeShipIcon;
var mgr;
var titleWindow;
var titleOn = true;

function removeTitle(where){
	menu.hide();
	gMap.removeControl(titleWindow);
	document.getElementById("mapTitleContent").style.display = "none";
	titleOn = false;
	//alert(menu.items.get('titleCheckID').checked)
	if (where=="fromX"){
		menu.items.get('titleCheckID').setChecked(false);
	}
}

function addTitle(){
	//gMap.removeControl(titleWindow);
	var newCenter = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(theWidth/2-250, 0));
	gMap.addControl(titleWindow,newCenter);
	document.getElementById("mapTitleContent").style.display = "block";
	titleOn = true

}

function resetTitleOnResize(){
	if (titleOn == true){
		//gMap.removeControl(titleWindow);
		var newCenter = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(theWidth/2-250, 0));
		gMap.addControl(titleWindow,newCenter);
	}
}


var zoomControl;
function initializeGoogle(){
	//gMap = new G.Map2(document.getElementById("googleMap"),{draggableCursor:"crosshair",draggingCursor:"crosshair"} );
	var gSearchOptions = {
		resultList : document.getElementById('results'),
		suppressZoomToBounds : true,
		showOnLoad : true
	};
	
	gMap = new G.Map2(document.getElementById("googleMap"), {googleBarOptions: gSearchOptions});
	gMap.setCenter(initialExtent, initialZoom);
	gMap.enableContinuousZoom();
	var scaleBarPos = new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(85,10)); 
	var controlPosition = new GControlPosition(G_ANCHOR_TOP_RIGHT, new G.Size(googleZoomRight,googleZoomTop))
	zoomControl = new GLargeMapControl3D;
	gMap.addControl(zoomControl,controlPosition);
	//gMap.addControl(new GSmallZoomControl3D,controlPosition)
	gMap.addControl(new GScaleControl(),scaleBarPos);
	gMap.addControl(new GOverviewMapControl());
	//gMap.addControl(new G.MapTypeControl());
	gMap.enableScrollWheelZoom();
	gMap.enableDoubleClickZoom();

	//G_PHYSICAL_MAP
	//G_SATELLITE_3D_MAP
	//gMap.setMapType(G_SATELLITE_3D_MAP)
	//gMap.setMapType(G_SATELLITE_MAP)
	gMap.setMapType(G_PHYSICAL_MAP); 
	//gMap.setMapType(G_HYBRID_MAP)
	//gMap.enableGoogleBar();
	GEvent.addListener(gMap, "click", function() {
		menu.hide();
	});
	
	GEvent.addListener(gMap, "zoomend", function(oldZ,newZ) {
		//alert(oldZ+"-"+newZ)
		//tests zoom scale and resizes markers if needed
		
if(mapTypeVar=="all"){	
		if (newZ > 6 && oldZ<=6){
			gMap.clearOverlays()
			if(menu.items.get('restorationCheckID').checked){
				sendToCreateMarker("large")
			}
			if(menu.items.get('surveyCheckID').checked){
				sendToCreateMarkerHydro("large")
			}
			markerSize = "large"
			markerSizeHydro = "large"
			//changeMarkers();
		}else if(newZ < 7 && oldZ >=7){
			gMap.clearOverlays()
			if(menu.items.get('restorationCheckID').checked){
				sendToCreateMarker("small")
			}
			if(menu.items.get('surveyCheckID').checked){
				sendToCreateMarkerHydro("small")
			}
			markerSize = "small"
			markerSizeHydro = "small"
		}
}else if (mapTypeVar=="Restoration"){
			if (newZ > 6 && oldZ<=6){
			gMap.clearOverlays()
			sendToCreateMarker("large")
			markerSize = "large"
			markerSizeHydro = "large"
			//changeMarkers();
		}else if(newZ < 7 && oldZ >=7){
			gMap.clearOverlays()
				sendToCreateMarker("small")
			markerSize = "small"
			markerSizeHydro = "small"
		}
}else if (mapTypeVar=="nos"){	
				if (newZ > 6 && oldZ<=6){
			gMap.clearOverlays()
			sendToCreateMarkerHydro("large")
			markerSize = "large"
			markerSizeHydro = "large"
			//changeMarkers();
		}else if(newZ < 7 && oldZ >=7){
			gMap.clearOverlays()
				sendToCreateMarkerHydro("small")
			markerSize = "small"
			markerSizeHydro = "small"
		}
}
		
	if (autoMapType=="on") {
		if (newZ <= 11 ){
			gMap.setMapType(G_PHYSICAL_MAP);
		}else if(newZ >= 12 ){
			gMap.setMapType(G_HYBRID_MAP);
		}
	} 


	});
	
	function autoMapSwitch(){

	}
	
	var tempMap = document.getElementById("googleMap")
	var CopyrightDiv = tempMap.firstChild.nextSibling;
	//CopyrightDiv.style.position= "absolute";
	//CopyrightDiv.style.top = "10px";
	//CopyrightDiv.style.right = "10px";
	//CopyrightDiv.style.zvalue= "500";
	//CopyrightDiv.style.bottom = "-10px"
	//CopyrightDiv.style.left = "20px";
	//CopyrightDiv.style.color = "gray";
	//alert("write xml")

//--------------------------------------------------------------------------------
///creates map title as a GControl. This is needed so that info windows won't open under the title.
function mapTitle() {
}

mapTitle.prototype = new GControl();

mapTitle.prototype.initialize = function(gMap) {
  var titleContainer = document.createElement("div");

  var titleDiv = document.createElement("div");
  
  this.setButtonStyle_(titleDiv);
  titleContainer.appendChild(titleDiv);
  //titleDiv.appendChild(document.createTextNode("NOAA Marine and Coastal Habitat Restoration Projects Funded Under the American Recovery and Reinvestment Act"));
  titleDiv.appendChild(document.createTextNode(""));
  
  gMap.getContainer().appendChild(titleContainer);
  return titleContainer;
}  
  
  mapTitle.prototype.getDefaultPosition = function() {
  return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(theWidth/2-250, 0));
}
mapTitle.prototype.setButtonStyle_ = function(button) {
  button.style.color = "#003986";
  button.style.backgroundColor = "#F0F8FE";
  button.style.font = "medium Times";
  button.style.fontWeight = "bold";
  button.style.width = "400px";
  button.style.height = "40px";
  button.style.marginBottom = "3px";
  button.style.paddingLeft = "10px";
  button.style.filter="alpha(opacity=80)";
  button.style.opacity="0.8";
  button.style.backgroundImage="url(images/recovery_logo_med.gif)";
  button.style.backgroundRepeat="no-repeat";
  button.style.backgroundPosition="10px center";
  button.style.border="solid";
  button.style.borderWidth="1px";
  button.style.borderColor="#A7DAF5";
}

titleWindow = new mapTitle()

gMap.addControl(titleWindow);
document.getElementById("mapTitleContent").style.display = "block";

//end map title code-------------------------------------------------------------

///Start drag zoom code	
	        /* first set of options is for the visual overlay.*/
        var boxStyleOpts = {
          opacity: .2,
          border: "2px solid red"
        }

        /* second set of options is for everything else */
        var otherOpts = {
          buttonHTML: "<img src='images/zoom-button.gif' alt='map tool used to zoom to a specific area' title='zoom to area'/>",
          buttonZoomingHTML: "<img src='images/zoom-button-activated.gif' alt='map tool used to zoom to a specific area - tool is activated' title='zoom to area tool is activated'/>",
          buttonStartingStyle: {width: '24px', height: '24px'},
		  backButtonStartingStyle: {width: '9px', height: '9px',position: 'absolute', top: backTopPosition+'px', right: backRightPosition+'px'}
        };

        /* third set of options specifies callbacks 
        var callbacks = {
          buttonclick: function(){GLog.write("Looks like you activated DragZoom!")},
          dragstart: function(){GLog.write("Started to Drag . . .")},
          dragging: function(x1,y1,x2,y2){GLog.write("Dragging, currently x="+x2+",y="+y2)},
          dragend: function(nw,ne,se,sw,nwpx,nepx,sepx,swpx){GLog.write("Zoom! NE="+ne+";SW="+sw)}
        };
        */
//end drag zoom code
  
        gMap.addControl(new DragZoomControl(boxStyleOpts, otherOpts));
	//thePostForm.ArcXMLRequest.value=""
	//thePostForm.document.location="PostForm.htm";
	
	mgr = new GMarkerManager(gMap);
//alert(mgr)
//var markers = [];


smPointIcon = new GIcon();
smPointIcon.image = "images/shovelIcon2_15.png";
smPointIcon.iconSize = new GSize(15, 15);
smPointIcon.iconAnchor = new GPoint(7, 7);
smPointIcon.infoWindowAnchor = new GPoint(13, -5);

medPointIcon = new GIcon();
medPointIcon.image = "images/shovelIcon2_20.png";
medPointIcon.iconSize = new GSize(20, 20);
medPointIcon.iconAnchor = new GPoint(10, 10);
medPointIcon.infoWindowAnchor = new GPoint(20, -1);

largePointIcon = new GIcon();
largePointIcon.image = "images/shovelIcon2_30.png";
largePointIcon.iconSize = new GSize(30, 30);
largePointIcon.iconAnchor = new GPoint(15, 15);
largePointIcon.infoWindowAnchor = new GPoint(30, -1);

greenPointIcon = new GIcon();
greenPointIcon.image = "images/green_point.png";
greenPointIcon.iconSize = new GSize(14, 14);
greenPointIcon.iconAnchor = new GPoint(7, 7);
greenPointIcon.infoWindowAnchor = new GPoint(13, -5);

redPointIcon = new GIcon();
redPointIcon.image = "images/red_point_3d.png";
redPointIcon.iconSize = new GSize(14, 14);
redPointIcon.iconAnchor = new GPoint(7, 7);
redPointIcon.infoWindowAnchor = new GPoint(13, -5);

greenSQ = new GIcon();
greenSQ.image = "images/smallSQGreenIcons/marker1.png";
greenSQ.iconSize = new GSize(15, 15);
greenSQ.iconAnchor = new GPoint(7, 7);
greenSQ.infoWindowAnchor = new GPoint(15, -5);

smShipIcon = new GIcon();
smShipIcon.image = "images/Ship_icon_15.png";
smShipIcon.iconSize = new GSize(15, 15);
smShipIcon.iconAnchor = new GPoint(7, 7);
smShipIcon.infoWindowAnchor = new GPoint(13, -5);

largeShipIcon = new GIcon();
largeShipIcon.image = "images/Ship_icon_30.png";
largeShipIcon.iconSize = new GSize(30, 30);
largeShipIcon.iconAnchor = new GPoint(15, 15);
largeShipIcon.infoWindowAnchor = new GPoint(30, -1);
/*
pointIcon = new GIcon();
pointIcon.image = "images/red_point2.png";
//pointIcon.image = "images/pointSM4.png";
pointIcon.iconSize = new GSize(10, 10);
pointIcon.iconAnchor = new GPoint(5, 5);
pointIcon.infoWindowAnchor = new GPoint(13, -5);

*/
	
	//getRCLayer();
	//writeQueryXML();
	
}




function openDialog(){
$("#dialogBox").dialog({ 
    buttons: { 
        "Ok": function() { 
            alert("Ok"); 
        }, 
        "Cancel": function() { 
            $(this).dialog("close"); 
        } 
    } 
});
}


function changeMap(type){
	//alert(type)
	if (type == "Physical"){
/*
		document.getElementById('physicalCheck').src = "images/checked.png"
		document.getElementById('hybridCheck').src = "images/unchecked.png"
		document.getElementById('streetCheck').src = "images/unchecked.png"
		document.getElementById('satelliteCheck').src = "images/unchecked.png"
*/
		gMap.setMapType(G_PHYSICAL_MAP)
	}else if (type == "Hybrid"){
/*
		document.getElementById('physicalCheck').src = "images/unchecked.png"
		document.getElementById('hybridCheck').src = "images/checked.png"
		document.getElementById('streetCheck').src = "images/unchecked.png"
		document.getElementById('satelliteCheck').src = "images/unchecked.png"
*/
		gMap.setMapType(G_HYBRID_MAP)
	}else if (type == "Street"){
/*
		document.getElementById('physicalCheck').src = "images/unchecked.png"
		document.getElementById('hybridCheck').src = "images/unchecked.png"
		document.getElementById('streetCheck').src = "images/checked.png"
		document.getElementById('satelliteCheck').src = "images/unchecked.png"
*/
		gMap.setMapType(G_NORMAL_MAP)		
	}else if (type == "Satellite"){
/*
		document.getElementById('physicalCheck').src = "images/unchecked.png"
		document.getElementById('hybridCheck').src = "images/unchecked.png"
		document.getElementById('streetCheck').src = "images/unchecked.png"
		document.getElementById('satelliteCheck').src = "images/checked.png"
*/
		gMap.setMapType(G_SATELLITE_MAP)
	}else if (type == "3D"){
/*
		document.getElementById('physicalCheck').src = "images/unchecked.png"
		document.getElementById('hybridCheck').src = "images/unchecked.png"
		document.getElementById('streetCheck').src = "images/unchecked.png"
		document.getElementById('satelliteCheck').src = "images/checked.png"
*/
		gMap.setMapType(G_SATELLITE_3D_MAP)
	}
}

var gSearchBar = "off"

function googleSearch() {
	if (gSearchBar == "off"){
		gMap.enableGoogleBar();
		gSearchBar = "on"
		//document.getElementById('gsearchCheck').src = "images/checked.png"
	}else if (gSearchBar == "on"){
		gMap.disableGoogleBar();
		gSearchBar = "off"
		//document.getElementById('gsearchCheck').src = "images/unchecked.png"
	}
}

function ZoomToLocation(idx){
	var tmpCoords = new GLatLng(recLat[idx],recLong[idx]);
	var tmpZoom = zoomLevel[idx];
	gMap.setCenter(tmpCoords, tmpZoom) 
}

function ZoomToLocationHydro(idx){
	var tmpCoords = new GLatLng(recLatHydro[idx],recLongHydro[idx]);
	var tmpZoom = zoomLevelHydro[idx];
	gMap.setCenter(tmpCoords, tmpZoom) 
}


function ZoomToMultiLocations(idx){
	//alert("zoom")
	var nameArray = new Array();
	var latArray = new Array();
	var longArray = new Array();
	var detailMarkersArray = new Array();
	
	var tempSplit = numLocs[idx].split(";")
	alert(tempSplit)
	for (i=0;i<tempSplit.length;i++){
		var tempSplitA = tempSplit[i].split("|");
		nameArray[i]= tempSplitA[0];
		latArray[i] = tempSplitA[1];
		longArray[i] = tempSplitA[2];
	}
	
	for (i=0;i<nameArray.length;i++){
		alert(latArray[i])
		var tmpCoords = new GLatLng(latArray[i],longArray[i]);
		detailMarkersArray[i] = new GMarker(tmpCoords, {icon:smPointIcon});
		//eval('GEvent.addListener(smMarkersArray['+i+'],"click", function(){projcetPopUp("'+i+'")})'); //end eval
		//eval('GEvent.addListener(smMarkersArray['+i+'],"mouseover", function(){projcetSmPopUp("'+i+'")})'); //end eval
		//eval('GEvent.addListener(smMarkersArray['+i+'],"mouseout", function(){closeSmPopUp()})'); //end eval
		//eval('GEvent.addListener(smMarkersArray['+i+'],"infowindowclose", function(){disableMouseOver = false})'); //end eval
		gMap.addOverlay(detailMarkersArray[i]);
	}
	
	var tmpCoords = new GLatLng(recLat[idx],recLong[idx]);
	var tmpZoom = zoomLevel[idx];
	gMap.setCenter(tmpCoords, tmpZoom) 
}

//openDialog()

//var progressbarOptions = {width: 150, loadstring: 'loading projects'};
//var progressbar = new ProgressbarControl(gMap, progressbarOptions);

//creates markers

noaaIcon = new GIcon();
noaaIcon.image = "images/noaa_marker.png";
noaaIcon.shadow = "images/shadow-noaa_marker.png";
noaaIcon.iconSize = new GSize(20, 34);
noaaIcon.shadowSize = new GSize(38, 34);
noaaIcon.iconAnchor = new GPoint(10, 34);
noaaIcon.infoWindowAnchor = new GPoint(5, 1);

ArrowIcon = new GIcon();
ArrowIcon.image = "http://maps.google.com/mapfiles/arrow.png";
ArrowIcon.shadow = "http://maps.google.com/mapfiles/arrowshadow.png";
ArrowIcon.iconSize = new GSize(39, 34);
ArrowIcon.shadowSize = new GSize(39, 34);
ArrowIcon.iconAnchor = new GPoint(10, 34);
ArrowIcon.infoWindowAnchor = new GPoint(15, 15);

XIcon = new GIcon();
XIcon.image = "images/x.png";
XIcon.iconSize = new GSize(10, 10);
XIcon.iconAnchor = new GPoint(5, 5);



