
var nowDot;
var event= Array();
var c;
var now;

var centerX;
var centerY;
var width;
var height;
var radius=100;

var map;
var baseIcon;
var form;



function init() {

	//window.resizeTo(1024,768);
	now=new Date();
  	
  	width=window.innerWidth;
	height=window.innerHeight;
	//alert(width);
	form= new Dynamic_Object({"id":"form_01","width":""+width+"","height":""+height+"","top":"0","left":"0","color":"rgb(0,0,0)","backgroundColor":"black","opacity":".9","zIndex":"20","visibility":"hidden"});
	form.initialize();

	loadMap();
  	
  	makeHttpRequest("getMarkers.php",drawMarkers);	
  	setInterval(function(){makeHttpRequest("getMarkers.php",drawMarkers);},30000);
  	
	makeHttpRequest("entryForm.php",function(response){form.insertHTML(response)});

}



  function loadMap() {
      if (GBrowserIsCompatible()) {
	   map = new GMap2(document.getElementById("map_canvas"));
		
		baseIcon = new GIcon(G_DEFAULT_ICON);
		baseIcon.shadow = "http://cwwang.com/avdrop/images/sqshadow.png";
		baseIcon.iconSize = new GSize(40, 40);
		baseIcon.shadowSize = new GSize(66, 40);
		baseIcon.iconAnchor = new GPoint(40, 40);
		baseIcon.infoWindowAnchor = new GPoint(20, 20);
		baseIcon.transparent="http://cwwang.com/avdrop/images/icon.png";
		baseIcon.imageMap=Number[0,0,40,40];
		
		
		var copyright = new GCopyright(1,new GLatLngBounds(new GLatLng(-90, -180), new GLatLng(90, 180)), 0,"");
		var copyrightCollection = new GCopyrightCollection('');
		copyrightCollection.addCopyright(copyright);      
		var tilelayers = [new GTileLayer(copyrightCollection , 3, 3)];
		function CustomGetTileUrl(a,b) { var f = "blank.jpg";return f;}
		tilelayers[0].getTileUrl = CustomGetTileUrl;
		
		var custommap = new GMapType(tilelayers, new GMercatorProjection(12), "Chart", {errorMessage:"No data available"});
		
		map.addMapType(custommap);
		map.setMapType(custommap);
		//map.addControl(new GSmallMapControl());
		map.setCenter(new GLatLng(0,0), 3);
		//map.enableScrollWheelZoom();
		

 
      }

}

function deleteIcon(_id){
	//alert("delete");
	var _pass = prompt("Enter your password to delete this avatar.", "");
	makeHttpRequest("deleteIcon.php?id="+_id+"&pass="+_pass+"",init);
	//markerArray=[];
}

function updateCoordinates(_id,_lat,_lon){

	makeHttpRequest("updatePosition.php?id="+_id+"&lat="+_lat+"&lon="+_lon,nada);

}
    
    function createMarker(_id, _point, _image,_note) {
	  // Create a lettered icon for this point using our icon class
	  var avIcon = new GIcon(baseIcon);
	  avIcon.image = "http://cwwang.com/avdrop/images/"+_image;

	  markerOptions = { icon:avIcon , draggable:true , title: _id};
	  var marker = new GMarker(_point, markerOptions);


	
	GEvent.addListener(marker, "dragstart", function() {
	  map.closeInfoWindow();
	  });
	
	GEvent.addListener(marker, "dragend", function() {
	  //marker.openInfoWindowHtml("Thanks for moving me...");
	  var pos=marker.getLatLng();
	  var title=marker.getTitle();

	  updateCoordinates(title,pos.lat(),pos.lng());
	  
	  });

	  GEvent.addListener(marker, "click", function() {
	  
	    marker.openInfoWindowHtml("<b>"+_note+"</b><br/><small onClick='deleteIcon("+_id+");'>delete</small>");
	    //alert(_note);
	    
	  });
	  
	  return marker;
	}



//var markerArray=new Array();

    function drawMarkers(response){
    
    map.clearOverlays();
    
		var result = eval('(' + response + ')');	
		for (var i=0; i<result.length; i++)
		{
			//add negative to lng since all database entries are in western hemisphere
			var point = new GLatLng(result[i].lat,result[i].lon); 
			var image = result[i].image;
			//if(result[i].id!=markerArray[i]){
				map.addOverlay(createMarker(result[i].id,point,image, result[i].note) );
			//	markerArray[i]=result[i].id;
			//}
	
		}
	}
	
	
     // Monitor the window resize event and let the map know when it occurs
      if (window.attachEvent) { 
        window.attachEvent("onresize", function() {this.map.onResize()} );
      } else {
        window.addEventListener("resize", function() {this.map.onResize()} , false);
      }
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
function $style(ElementId, CssProperty)
{
	
	function $(stringId)
	{
	    return document.getElementById(stringId);
	}   
	
	if($(ElementId).currentStyle)
	{

	    var convertToCamelCase = CssProperty.replace(/\-(.)/g, function(m, l){return l.toUpperCase()});
	    return $(ElementId).currentStyle[convertToCamelCase];
	}
	else if (window.getComputedStyle)
	{
	    var elementStyle = window.getComputedStyle($(ElementId), "");
	    return elementStyle.getPropertyValue(CssProperty);
	}

}



window.onkeyup=function(e)
{
	//alert(e.keyCode);
	if(e.keyCode==192) // ~ (tilda)
	{
		//enterdata();

	}
	
	if(e.keyCode==27) //esc key
	{
	//enterdata();
	}
}


function enterdata()
{

	if(form.visibility=='hidden')
	{
		//alert("enterData");
		fadeIn(form,.85);
	}
	else
	{
		fadeOut(form,.85);
	}

}


function addEvent( _el, _trigger, _fn ) {
	if (_el.addEventListener)
		_el.addEventListener( _trigger, _fn, false );
	else if (_el.attachEvent) {
		_el["e"+_trigger+_fn] = _fn;
		_el[_trigger+_fn] = function() { _el["e"+_trigger+_fn]( window.event ); }
		_el.attachEvent( "on"+_trigger, _el[_trigger+_fn] );
	}
}

function removeEvent( _el, _trigger, _fn ) {
	if (_el.removeEventListener)
		_el.removeEventListener( _trigger, _fn, false );
	else if (_el.detachEvent) {
		_el.detachEvent( "on"+_trigger, _el[_trigger+_fn] );
		_el[_trigger+_fn] = null;
		_el["e"+_trigger+_fn] = null;
	}
}

 function mysqlDateToDate(date) {
    //function parses mysql datetime string and returns javascript Date object
    //input has to be in this format: 2007-06-05 15:26:02
    var regex=/^([0-9]{2,4})-([0-1][0-9])-([0-3][0-9]) ?$/;
    var parts=date.replace(regex,"$1 $2 $3 $4 $5 $6").split(' ');
    return new Date(parts[0],parts[1]-1,parts[2]);  }


function nada(){}
