google.load("jquery", "1");
google.load("maps", "2.x", {"locale" : "en_EN"});

var map = null;
var marker = null;
var geocoder = null;

function select_text() {
    el = document.getElementById('short_url');
    if (el.createTextRange) {
	    var oRange = el.createTextRange();
 		oRange.moveStart("character", 0);
	    oRange.moveEnd("character", el.value.length);
	    oRange.select();
    } else if (el.setSelectionRange) {
	    el.setSelectionRange(0, el.value.length);
    }
    el.focus();
}
function resize(){
  if (!$("#map")){ return; }
  //var height =  getWindowHeight() - ( $('#header').get(0).offsetHeight + $('#boxes').get(0).offsetHeight + 1);
  var height =  getWindowHeight() - ( $('#header').get(0).offsetHeight + 1);
  var width  = getWindowWidth();
  $('#map').css('height', height + 'px');  
}

function getWindowHeight() {
  var windowHeight=0;
  if (typeof(window.innerHeight) === 'number') {
    windowHeight=window.innerHeight;
  } 
  else if (document.documentElement && document.documentElement.clientHeight) {
    windowHeight=document.documentElement.clientHeight;
  } 
  else if (document.body && document.body.clientHeight) {
    windowHeight=document.body.clientHeight;
  }
  return windowHeight;
}

function getWindowWidth() {
    var windowWidth=0;
    if (typeof(window.innerWidth) === 'number') {
        windowWidth=window.innerWidth;
    }
    else if (document.documentElement && document.documentElement.clientWidth) {
        windowWidth=document.documentElement.clientWidth;
    } 
    else if (document.body && document.body.clientWidth) {
        windowWidth=document.body.clientWidth;
    }
    return windowWidth;
}

function setForm(){
  var point = marker.getPoint();
  document.getElementById('lat').value = point.lat();
  document.getElementById('lon').value = point.lng();
  document.getElementById('address').value = document.getElementById('q').value;
}

function getAddress(point){
  geocoder.getLocations(point, function(addresses) {
    if(addresses.Status.code != 200) {
      alert("reverse geocoder failed to find an address for " + point.toUrlValue());
    }
    else {
      address = addresses.Placemark[0];
      document.getElementById('address').value = address.address;
      document.getElementById('q').value = address.address;
      setForm();
    }
  });    	
}

function showAddress(address) {
  if (geocoder) {
    geocoder.getLatLng( address, plotMarker);
  }
}
    
function plotMarker (center) {
	if (center==null) {
		return false;
	}
  map.setCenter(center, 15);
  map.clearOverlays();
  marker = new google.maps.Marker(center, {draggable: true});
  map.addOverlay(marker);
  var myHtml = "<center>Drag &amp; Drop this marker<br /> to the exact location.</center>";
  marker.openInfoWindowHtml(myHtml);
  google.maps.Event.addListener(marker, "dragstart", function() {
    map.closeInfoWindow();
  });
  google.maps.Event.addListener(marker, "dragend", function () {
    getAddress(marker.getPoint());
  });
  google.maps.Event.addListener(marker, "click", function() {
      map.openInfoWindowHtml(marker.getPoint(), myHtml);
    });
  getAddress(marker.getPoint());
}

function setupGears() {
  // Make sure we have Gears. If not, tell the user.
  if (!window.google || !google.gears) {
    if (confirm("Plazzme works nicer with Google Gears. Install now?")) {
      // Use an absolute URL to allow this to work when run from a local file.
      location.href = "http://gears.google.com/?action=install&message=Plazz.me works better with Google Gears" + "&return=http://plazz.me";
    }
  }
}

function gearsloc(){
    setupGears();
    try{
      var geo = google.gears.factory.create('beta.geolocation');
      geo.getCurrentPosition(
        function(position) {
          var lat = position.latitude;
          var lon = position.longitude;
          var center = new google.maps.LatLng(lat, lon);
          showAddress(center);
        },            
        showAddress('barcelona'), // Error Handle 
        { enableHighAccuracy: true, gearsRequestAddress: true }
      );
    } catch(e) {
     showAddress('barcelona');
    }
    
}
window.onresize = resize;

window.onunload = function() { GUnload(); };

function load_home() {
    resize();
    map = new google.maps.Map2(document.getElementById("map"));
    map.addControl(new google.maps.MapTypeControl());        
    map.addControl(new google.maps.LargeMapControl());
    map.addControl(new google.maps.OverviewMapControl());
    geocoder = new google.maps.ClientGeocoder();

    google.maps.Event.addListener(map, "dragend", setForm);

    google.maps.Event.addListener(map, "zoomend", function(){
    	document.getElementById('zoom').value = map.getZoom();
    });

    google.maps.Event.addListener(map, "maptypechanged", function(){
    	var t = map.getCurrentMapType();
      document.getElementById('maptype').value = t === G_NORMAL_MAP ? 0 : t === G_SATELLITE_MAP ? 1 : 2;
    });
    
    showAddress(document.getElementById('q').value);
}

