
function SiteBaseControl(opts_boxStyle, opts_other, opts_callbacks) {
 
}

SiteBaseControl.prototype = new GControl();
SiteBaseControl.showControls = false;

/**
 * Creates a new button to control gzoom and appends to map div.
 * @param {DOM Node} map The div returned by map.getContainer()
 */
SiteBaseControl.prototype.initButton_ = function(mapDiv) {

  var buttonDiv = document.createElement('div');
  buttonDiv.innerHTML ="<img src='sitebase.png' title='click here to access site editing functions' alt='click here to show the shoreline summary'/>";
  buttonDiv.id = 'site_control';
 buttonDiv.style.cursor='pointer';
  buttonDiv.style.zIndex=200;
  buttonDiv.style.width='24px';
    buttonDiv.style.height='24px';
  buttonDiv.style.border='none';
  buttonDiv.style.background= '#FFF';
  mapDiv.appendChild(buttonDiv);
  return buttonDiv;
};

function initSiteButtons_(mapDiv,type) {
	
	var buttonDiv = document.createElement('div');
	
	if(type == 1){
	 buttonDiv.innerHTML ="<img src='siteadd.png' title='click here to add a site' alt='click here to add a site'/>",
     buttonDiv.id = 'addsite_control';
	}
	else if(type == 2){
	 buttonDiv.innerHTML ="<img src='sitemove.png' title='click here to move a site' alt='click here to add a site'/>",
     buttonDiv.id = 'movesite_control';
	}
	else if(type == 3){
	 buttonDiv.innerHTML ="<img src='siteedit.png' title='click here to edit a site' alt='click here to edit a site'/>",
     buttonDiv.id = 'editsite_control';
	 }
	else if(type == 4){
	 buttonDiv.innerHTML ="<img src='sitedelete.png' title='click here to delete a site' alt='click here to delete a site'/>",
     buttonDiv.id = 'deletesite_control';
	}
 
 
 buttonDiv.style.cursor='pointer';
  buttonDiv.style.zIndex=200;
  buttonDiv.style.width='24px';
    buttonDiv.style.height='24px';
  buttonDiv.style.border='none';
  buttonDiv.style.background= '#FFF';
  mapDiv.appendChild(buttonDiv);
  return buttonDiv;
};



/**
 * Is called by GMap2's addOverlay method. Creates the zoom control
 * divs and appends to the map div.
 * @param {GMap2} map The map that has had this SiteBaseControl added to it.
 * @return {DOM Object} Div that holds the gzoomcontrol button
 */ 
SiteBaseControl.prototype.initialize = function(map) {
  var G = this.globals;
  var me = this;
  SiteBaseControl.showControls = false;
  var mapDiv = map.getContainer();
  //DOM:button
  var buttonDiv = this.initButton_(mapDiv);
  
  //DOM:map covers


  // add event listeners
  GEvent.addDomListener(buttonDiv, 'click', function(e) {
    me.buttonclick_(e);
  });


  return buttonDiv;
};

/**
 * Required by GMaps API for controls. 
 * @return {GControlPosition} Default location for control
 */
SiteBaseControl.prototype.getDefaultPosition = function() {
  return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(3, 120));
};

/**
 * Function called when the zoom button's click event is captured.
 */
SiteBaseControl.prototype.setButtonMode_ = function(mode){

}





/**
 * Function called when the zoom button's click event is captured.
 */
SiteBaseControl.prototype.buttonclick_ = function(){
  
//	this.intitCover_();
	if(SiteBaseControl.showControls == false){
		SiteBaseControl.showControls = true;
		document.getElementById("site_control").innerHTML ="<img src='sitebase2.png' title='click here to access site editing functions' alt='click here to show the shoreline summary'/>";
		this.showSiteControls_();
	}
	else{
		SiteBaseControl.showControls = false;
		document.getElementById("site_control").innerHTML ="<img src='sitebase.png' title='click here to access site editing functions' alt='click here to show the shoreline summary'/>";
		this.hideSiteControls_();
	}
	
	
  
};

SiteBaseControl.prototype.showSiteControls_ = function(){
  document.getElementById("site_control").style.border = '1 px solid #00FF00;';


  var oControl = new addSiteControl();
   aMapControls.push(oControl); 
  map.addControl(oControl,new GControlPosition(G_ANCHOR_TOP_LEFT,new GSize(25,150)));
  
 var oControl = new moveSiteControl();
   aMapControls.push(oControl); 
  map.addControl(oControl,new GControlPosition(G_ANCHOR_TOP_LEFT,new GSize(25,174)));
  
 var oControl = new editSiteControl();
   aMapControls.push(oControl); 
  map.addControl(oControl,new GControlPosition(G_ANCHOR_TOP_LEFT,new GSize(25,198)));
  
  var oControl = new deleteSiteControl();
   aMapControls.push(oControl); 
  map.addControl(oControl,new GControlPosition(G_ANCHOR_TOP_LEFT,new GSize(25,222)));



  

}


SiteBaseControl.prototype.hideSiteControls_ = function(){
 document.getElementById("site_control").style.border = 'none;';
//remove four site control.
	for(var a = 0;a<4;a++){
		var b = aMapControls.length-1;
		var oControl = aMapControls.pop();
		
		map.removeControl(oControl);
	//	alert(aMapControls.length);
	}
if(oSiteEventListener){
	GEvent.removeListener(oSiteEventListener)
}
}

SiteBaseControl.prototype.resetView_ = function(){
	 jg.clear();
	  map.setCenter(new GLatLng(58.077,-96.575),3,G_NORMAL_MAP);
	  
}

function subGPoints(a,b) {
        return new GPoint(a.x-b.x, a.y-b.y);
} 

function addSiteControl() {
}	  

addSiteControl.prototype = new GControl() 
  
addSiteControl.prototype.initialize = function(map) {
	var mapDiv = map.getContainer();
	var addSiteDiv = initSiteButtons_(mapDiv,1);
	
	GEvent.addDomListener(addSiteDiv, 'click', function(e) {
   siteClick_(1);
  });
  return addSiteDiv;
  
}

function moveSiteControl() {
}	  

moveSiteControl.prototype = new GControl() 
  
moveSiteControl.prototype.initialize = function(map) {
	var mapDiv = map.getContainer();
	var addSiteDiv = initSiteButtons_(mapDiv,2);
	
	GEvent.addDomListener(addSiteDiv, 'click', function(e) {
   siteClick_(2);
  });
  return addSiteDiv;
  
}

function editSiteControl() {
}	  

editSiteControl.prototype = new GControl() 
  
editSiteControl.prototype.initialize = function(map) {
	var mapDiv = map.getContainer();
	var addSiteDiv = initSiteButtons_(mapDiv,3);
	
	GEvent.addDomListener(addSiteDiv, 'click', function(e) {
    siteClick_(3);
  });
  return addSiteDiv;
  
}

function deleteSiteControl() {
}	  

deleteSiteControl.prototype = new GControl() 
  
deleteSiteControl.prototype.initialize = function(map) {
	var mapDiv = map.getContainer();
	var addSiteDiv = initSiteButtons_(mapDiv,4);
	
	GEvent.addDomListener(addSiteDiv, 'click', function(e) {
    siteClick_(4);
  });
  return addSiteDiv;
  
}

function resetFunctions(){
	if(oSiteEventListener){ //remove old event listener
		GEvent.removeListener(oSiteEventListener);
	}
	if(oMoveSiteListener){
		GEvent.removeListener(oMoveSiteListener);
	}
	if(addmarker){ //remove addsite marker
		map.removeOverlay(addmarker);
	}
	if(tipisup){ // remove polytip
		polyTooltip.style.visibility="hidden";
		jg.clear();
		tipisup = false;
	}

}

function siteClick_(type){
resetFunctions();



	if(type == 1){
		document.getElementById("addsite_control").innerHTML ="<img src='siteadd2.png' title='click here to add a site' alt='click here to add a site'/>";
document.getElementById("movesite_control").innerHTML =	"<img src='sitemove.png' title='click here to add a site' alt='click here to add a site'/>";
document.getElementById("editsite_control").innerHTML =	"<img src='siteedit.png' title='click here to add a site' alt='click here to add a site'/>";
document.getElementById("deletesite_control").innerHTML =	"<img src='sitedelete.png' title='click here to add a site' alt='click here to add a site'/>";	
		oSiteEventListener = GEvent.addDomListener(map, 'singlerightclick', function(point,src){
			if(point){
				
				
				point = map.fromContainerPixelToLatLng(point)
				var icon = new GIcon();	
			
				icon.shadow = "";
				icon.infoWindowAnchor = new GPoint(5, 1);
				icon.image = "mm_20_red.png";
				icon.iconSize = new GSize(12, 20);	
				icon.iconAnchor = new GPoint(6, 20);
			
			
				addmarker = new GMarker(point,icon);
				map.addOverlay(addmarker);
				map.setCenter(point,14);
					addmarker.openInfoWindowHtml('<div id="infowindow2">Add a new site here?<br>add the site name, type, and description</b><br> <p>Site Name:&nbsp;<input type="text" name="sitename" id="sitename" size="20"><br>Shoreline Type:<select name="select_shoreline" id="select_shoreline">				<option value="0">[Select Shoreline Type]</option><option value="1" >Lake or Pond</option><option value="2" >Ocean, Inlet or Bay</option><option value="3" >Other</option><option value="4" >River, Stream or Creek</option><option value="5" >Underwater (SCUBA)</option><option value="6" >Wetland, Marsh, or Swamp</option></select><br>Description:&nbsp;<textarea rows="3" cols="18" name="sitedescription" id="sitedescription"></textarea></p><div id = "error_text" style="color:#FF0000;"></div><a href ="#" onclick="addNewSite_('+ point.toUrlValue() + ',' + selectedid +')">add site</a> </div>' );
			}
		});

	
	}
	
	else if(type == 2){
		document.getElementById("addsite_control").innerHTML =	"<img src='siteadd.png' title='click here to add a site' alt='click here to add a site'/>";
		document.getElementById("movesite_control").innerHTML =	"<img src='sitemove2.png' title='click here to move a site' alt='click here to add a site'/>";
		document.getElementById("editsite_control").innerHTML =	"<img src='siteedit.png' title='click here to edit a site' alt='click here to add a site'/>";
		document.getElementById("deletesite_control").innerHTML =	"<img src='sitedelete.png' title='click delete to add a site' alt='click here to add a site'/>";
		//click on a marker and clone it.
			oSiteEventListener = GEvent.addListener(map, 'click', function(marker,point){
				if(marker){
					selectmoveid = marker.siteid;
					if(addmarker){
						selectmoveid = addmarker.siteid;
						map.removeOverlay(addmarker);
						for (var j = 0; j < types.length; j++) {
					//alert(types.length);
							var type = types[j];
							if(showMarker(type) === true){
								for(var i = 0;i<markerGroups[type].length;i++){
									if(markerGroups[type][i].siteid == selectmoveid){
										markerGroups[type][i].show();
										break;
									}
								}
							}
						}
					}
					//hide the overlay
					
					var newpoint = marker.getPoint();
					var type = marker.type;
					marker.hide();
					var icon = new GIcon();	
					
				icon.shadow = "";
				icon.infoWindowAnchor = new GPoint(5, 1);
				icon.image = "mm_20_red.png";
				icon.iconSize = new GSize(12, 20);	
				icon.iconAnchor = new GPoint(6, 20);
			
			
				addmarker = new GMarker(newpoint,{icon:icon,draggable:true});
				addmarker.siteid = selectmoveid;
				map.addOverlay(addmarker);	
				
				GEvent.addListener(addmarker, "dragstart", function() {
					map.closeInfoWindow();
				});

				oMoveSiteListener = GEvent.addListener(addmarker, "dragend", function() {
					var movePoint = addmarker.getPoint();
					addmarker.openInfoWindowHtml('<div id="infowindow3">Move site here?<br><a href ="#" onclick="moveSite_('+movePoint.toUrlValue() + ',' + selectmoveid +',true)">yes</a>&nbsp;|&nbsp;<a href ="#" onclick="moveSite_('+ movePoint.toUrlValue() + ',' + selectmoveid +',false)">no</a> </div>',{maxWidth:80});
				});

				
							
			}
			
			});
	//toggleDrag();
	}
	else if(type == 3){
document.getElementById("addsite_control").innerHTML =	"<img src='siteadd.png' title='click here to add a site' alt='click here to add a site'/>";
document.getElementById("movesite_control").innerHTML =	"<img src='sitemove.png' title='click here to move a site' alt='click here to add a site'/>";
document.getElementById("editsite_control").innerHTML =	"<img src='siteedit2.png' title='click here to edit a site' alt='click here to add a site'/>";
document.getElementById("deletesite_control").innerHTML =	"<img src='sitedelete.png' title='click delete to add a site' alt='click here to add a site'/>";
	}
	else if(type == 4){
document.getElementById("addsite_control").innerHTML =	"<img src='siteadd.png' title='click here to add a site' alt='click here to add a site'/>";
document.getElementById("movesite_control").innerHTML =	"<img src='sitemove.png' title='click here to move a site' alt='click here to add a site'/>";
document.getElementById("editsite_control").innerHTML =	"<img src='siteedit.png' title='click here to edit a site' alt='click here to add a site'/>";
document.getElementById("deletesite_control").innerHTML =	"<img src='sitedelete2.png' title='click delete to add a site' alt='click here to add a site'/>";
	}
	else if(type == 5){
	document.getElementById("addsite_control").innerHTML =	"<img src='siteadd.png' title='click here to add a site' alt='click here to add a site'/>";
	document.getElementById("movesite_control").innerHTML =	"<img src='sitemove.png' title='click here to move a site' alt='click here to add a site'/>";
	document.getElementById("editsite_control").innerHTML =	"<img src='siteedit.png' title='click here to edit a site' alt='click here to add a site'/>";
	document.getElementById("deletesite_control").innerHTML =	"<img src='sitedelete.png' title='click delete to add a site' alt='click here to add a site'/>";
	
	}

}
function moveSite_(x,y,id,move){
	if(move){
		resetFunctions();
		var point = x+" "+y;
		//call the database function
		var request = GXmlHttp.create();
		var urlstr = "movesiteajax.php?point="+point+"&siteid="+id;
		request.open('GET', urlstr , true);	// request XML from PHP with AJAX call
		request.onreadystatechange = function () {
			
			if (request.readyState == 4) {
				var latlng = new GLatLng(x,y);
				//refresh the markers position.
				for (var j = 0; j < types.length; j++) {
					//alert(types.length);
					var type = types[j];
					if(showMarker(type) === true){
						for(var i = 0;i<markerGroups[type].length;i++){
							if(markerGroups[type][i].siteid == selectmoveid){
								markerGroups[type][i].show();
								markerGroups[type][i].setLatLng(latlng);
								break;
							}
						}
					}
				}
					//alert(type);
			}
			//	map.addOverlay(createMarker(point1,name,type,pointid,gcsc));			
				//remove the addmarker maker
			if(addmarker){
				map.removeOverlay(addmarker);
			}
		}
	
		request.send(null);	
		//reset functions
		
		siteClick_(5);
		
	
	}
	else{
		//reset functions
		resetFunctions();
		//remove move overlay
		map.removeOverlay(addmarker);
		//cylce through array of sites and reset visibility
		
		 for (var j = 0; j < types.length; j++) {
					//alert(types.length);
			var type = types[j];
			if(showMarker(type) === true){
				for(var i = 0;i<markerGroups[type].length;i++){
					if(markerGroups[type][i].siteid == id){
						markerGroups[type][i].show();
						break;
					}
				}
			}
		}
		//go back to siteClick Calling Function
		siteClick_(2);
	}

}


function addNewSite_(x,y,id){
	var name = document.getElementById("sitename").value;
	var description = document.getElementById("sitedescription").value;
	var type = document.getElementById("select_shoreline").value;
	
	var filled = true;
	var text = "";
	if(type==0||type==""){
		var text = text + "<br>please select a type of shoreline"
		filled = false;
	}
	if(description == ""){
		var text = text + "<br>please describe the location"
		filled = false;
	}
	if(name == ""){
		var text = text + "<br>please provide a site name"
		filled = false;
	}
	var point = x+" "+y;
    var values = new Array(name,description,type,point,id);
	if(filled){
		
     //aha! 
		document.getElementById("error_text").innerHTML = "";
		var request = GXmlHttp.create();
		
		var urlstr = "addsiteajax.php";
		
		request.open('POST', urlstr , true);	// request XML from PHP with AJAX call
		request.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		request.send(formURLString(values, "p"));	
		request.onreadystatechange = function () {
			
			if (request.readyState == 4) {
			
				
			 var xmlDoc = request.responseXML;
			 sitepoints = xmlDoc.documentElement.getElementsByTagName("point");
				 // read each line
				for (var a = 0; a < sitepoints.length; a++) {
				var xmlDoc = request.responseXML;
				//add the pending site
				var coords = sitepoints[a].getAttribute("coords").split(",");
				var name = sitepoints[a].getAttribute("name")
				name = name.replace(/\*Y/g,"&");
				var type = sitepoints[a].getAttribute("type");
				var pointid = sitepoints[a].getAttribute("site_id");
				var gcsc = sitepoints[a].getAttribute("gcsc");
					
				var lat = coords[0];
				var lng = coords[1];
				var point1 = new GLatLng(lat,lng);
					//alert(type);
				}
				map.addOverlay(createMarker(point1,name,type,pointid,gcsc));			
				//remove the addmarker maker
				if(addmarker){
					map.removeOverlay(addmarker);
				}
			}
		}
		//request.send(null);	
	}
	else{
	document.getElementById("error_text").innerHTML = text;
	
	}
}

 function formURLString(obj, name)
{
  var str = new Array;
  name = name.replace(/&/g, '%26' );
  for(i in obj)
    str[str.length] = name + "[" + i.replace(/&/g, '%26' ) + "]=" +
obj[i].toString().replace(/&/g, '%26' );
  str = str.join("&");
  return str;
} 



