// JavaScript Document
var baseIcon = new GIcon();
        baseIcon.image = "http://maps.google.com/mapfiles/kml/pal5/icon13.png";
		baseIcon.shadow = "http://www.google.com/mapfiles/kml/pal5/icon13s.png";
        baseIcon.iconSize = new GSize(25, 35);
        baseIcon.shadowSize = new GSize(25, 35);
        baseIcon.iconAnchor = new GPoint(9, 34);
        baseIcon.infoWindowAnchor = new GPoint(9, 2);
        baseIcon.infoShadowAnchor = new GPoint(18, 25);
var marker_updating = false;

function createMarker(point, sometext, ajax_var){
   		//Create marker geo,icon,tooltip
   		newmarker = new GMarker(point, {icon: baseIcon, title: sometext});
   		GEvent.addListener(newmarker, "click", function() {ajaxFunction(ajax_var) } );
  		return newmarker;
		}		

//-------------------------   Part I   MoveEnd and ZoomEnd functions -------------------------------------------------
function updateMarkers(){	
	if(marker_updating)
	{return}
	else{
	map.clearOverlays();
	//var moveZoom_bounds = new GLatLngBounds();
	var moveZoom_bounds = map.getBounds();
	//moveZoom_bounds.getBounds();
	var southWest = moveZoom_bounds.getSouthWest();
	var northEast = moveZoom_bounds.getNorthEast();
	var getVars = 'ne=' + northEast.toUrlValue() + '&sw=' + southWest.toUrlValue();
	var ajaxRequest3;  // The variable that makes Ajax possible
	
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest3 = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest3 = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest3 = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser is having trouble supporting Ajax objects!");
				return false;
				}
				}
				}
	document.getElementById("loading").className="spinner_map";
	ajaxRequest3.onreadystatechange = function()
		{
		if(ajaxRequest3.readyState == 4)
			{
			var xmlobject3 = ajaxRequest3.responseXML;
						
			// get the XML root item
            root3 = xmlobject3.getElementsByTagName("peakset")[0];
			var rows3 = root3.getElementsByTagName("row");
			map.clearOverlays();
			document.getElementById("loading").className="spinner_off";
	
	
				for (var i = 0 ; i < rows3.length ; i++)
					{
					// get one item after another
					var item = rows3[i];
			
					// get the names
					var mname = item.getElementsByTagName("peak")[0].firstChild.nodeValue;
					var mlat = item.getElementsByTagName("lat")[0].firstChild.nodeValue;
					var mlong = item.getElementsByTagName("long")[0].firstChild.nodeValue;
					var ajax_var = item.getElementsByTagName("id")[0].firstChild.nodeValue;
			
					var point = new GLatLng(mlat,mlong);
					var marker = createMarker(point, mname,ajax_var);
					
					map.addOverlay(marker);
					}
		}
	}
	ajaxRequest3.open("GET", "update_markers.php?" + getVars, true);
	ajaxRequest3.send(null);
	}
}
//-----------------------------------PART II Get Selected Results    ---------------------------------------------------

function getMarkers(markerset, column){
	var ajaxRequest2;  // The variable that makes Ajax possible
	marker_updating = true;
	document.getElementById("infoboxpic").innerHTML = "Click on flags for more info";
	document.getElementById("peakdata").style.visibility = "hidden";
	document.getElementById("picsub").innerHTML = "";
	document.getElementById("caption").innerHTML = "";
	
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest2 = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest2 = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest2 = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser is having trouble supporting Ajax objects!");
				return false;
				}
				}
				}
	document.getElementById("loading").className="spinner_map";
	ajaxRequest2.onreadystatechange = function(){
		if(ajaxRequest2.readyState == 4){
			var xmlobject2 = ajaxRequest2.responseXML;
						
			// get the XML root item
            root2 = xmlobject2.getElementsByTagName("peakset")[0];
			var rows2 = root2.getElementsByTagName("row");
			map.clearOverlays();
			document.getElementById("loading").className="spinner_off";
	
	var bounds = new GLatLngBounds;		
	
	for (var i = 0 ; i < rows2.length ; i++) {
		// get one item after another
		var item = rows2[i];
		
		// get the names
		var mname = item.getElementsByTagName("peak")[0].firstChild.nodeValue;
		var mlat = item.getElementsByTagName("lat")[0].firstChild.nodeValue;
		var mlong = item.getElementsByTagName("long")[0].firstChild.nodeValue;
		var ajax_var = item.getElementsByTagName("id")[0].firstChild.nodeValue;
		
		var point = new GLatLng(mlat,mlong);
		var marker = createMarker(point, mname,ajax_var);
		bounds.extend(point);
		map.addOverlay(marker);
		}
		
		// back map out from max zooms when small marker ser
		mpzoom = map.getBoundsZoomLevel(bounds);
		if (mpzoom > 17) 
			{
			map.setZoom(17);
			}
			else	
			{
			map.setZoom(map.getBoundsZoomLevel(bounds));
			}
		map.setCenter(bounds.getCenter());
		document.forms[0].reset();
		document.forms[1].reset();
		document.forms[2].reset();
		marker_updating = false;
		}
	}
	ajaxRequest2.open("GET", "get_markers.php?markerset="+markerset+"&column="+column+"", true);
	ajaxRequest2.send(null);
	}

//-----------         Part III      Infobox    -------------------------------------------------------------

function ajaxFunction(ajax_var){
	var ajaxRequest;  // The variable that makes Ajax possible
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser is having trouble supporting Ajax objects!");
				return false;
			}
		}
	}
	document.getElementById("loading").className="spinner_infobox";
	// receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			var xmlobject = ajaxRequest.responseXML;
						
			// get the XML root item
            rows = xmlobject.getElementsByTagName("row");
			document.getElementById("loading").className="spinner_off";
			
			for (var i = 0 ; i < rows.length ; i++) {
		// get one item after another
			var item = rows[i];	
			var pname = item.getElementsByTagName("peak")[0].firstChild.nodeValue;
			document.getElementById("pname").innerHTML = pname;
		
			var elev = item.getElementsByTagName("elevation")[0].firstChild.nodeValue;
			document.getElementById("elevation").innerHTML = elev;
		
			var member = item.getElementsByTagName("member")[0].firstChild.nodeValue;
			document.getElementById("member").innerHTML = member;
			}
			// check if photo exists...
			var picsub = item.getElementsByTagName("picsub")[0].firstChild.nodeValue;
			if (picsub == "not_set"){
				//set boxes and text for no photo
				document.getElementById("picsub").innerHTML = "";
				document.getElementById("caption").innerHTML = "";
				document.getElementById("infoboxpic").innerHTML = "<p size='16' align='center'><a href='http://www.socalpeakbagger.com/images/uploads/addphoto.php?member=" + member + "&peak=" + pname + "'>Upload Photo</a></p><p align='center' >&nbsp;</p><p align='center'>and get the credit you deserve!</p><p align='center'>Your photo displays instantly , with  your &quot;name&quot; and caption. </p>";
				document.getElementById("peakdata").style.visibility = "visible";
				}else{
				//set boxes and text IF photo
				//append pic "headsot"
					document.getElementById('infoboxpic').innerHTML="";
					var setpic = document.createElement('img');
					setpic.src = "../../images/uploads/" + member + "/" + pname + "/" + "headshot_220_" + pname + ".jpg";
					document.getElementById('infoboxpic').appendChild(setpic); 
					
				//turn on values
					document.getElementById("picsub").innerHTML = "Submitted By: " + picsub;
					var caption = item.getElementsByTagName("caption")[0].firstChild.nodeValue;
					document.getElementById("caption").innerHTML = caption;
					document.getElementById("peakdata").style.visibility = "visible";
					}
			}
		}	
	ajaxRequest.open("GET", "info_box.php?ajaxid=" + ajax_var, true);
	ajaxRequest.send(null); 
}