var mapservice;
var locale = null;
var instanceid = null;

function adminGeocode(instanceid){
	window.instanceid = instanceid;
	var address = document.getElementById("street_address_" + instanceid).getElementsByTagName("div")[0].innerHTML + ", " + document.getElementById("post_office_" + instanceid).getElementsByTagName("div")[0].innerHTML;
	var geocoder = new GClientGeocoder();
	
	alert(address);
	
	geocoder.getLocations(address, fillGeocodeSlot);
}

function fillGeocodeSlot(response){
	if (!response || response.Status.code != 200){
		alert("Can't geocode!");
	}
	else{
		place = response.Placemark[0];
		document.getElementById("geocode_" + window.instanceid).getElementsByTagName("div")[0].innerHTML = "lat : " + place.Point.coordinates[1] + ", lng : " + place.Point.coordinates[0];
	}
}




function initMap(instanceid, useDI){
	if(document.getElementById('geocode_' + instanceid).innerHTML != ''){		
		var hotelCoords = eval("({" + document.getElementById('geocode_' + instanceid).innerHTML + "})");
		var hotelPoint = new GLatLng(hotelCoords.lat, hotelCoords.lng);
		
		mapservice = new MapService('mapView');
		if(locale == null){
			locale = "fi_FI";
		}
		mapservice.setLocale(locale);
		mapservice.init();
		mapservice.showControls();
		
		if(useDI == true){
			var address = document.getElementById("street_address_" + instanceid).innerHTML + ", " + document.getElementById("post_office_" + instanceid).innerHTML;
			mapservice.getDirections(document.getElementById("diFrom").value, address, "directions");
		}
		else{
			mapservice.gotoPos(hotelPoint, true);
		}
	}
}


function mapView(instanceid, geoCode, emtId, showDIQuery){
	window.instanceid = instanceid;
	var position = findPos(document.getElementById("maplinks_" + instanceid));
	document.getElementById(emtId).style.left = (position[0] - 180) + "px";
	document.getElementById(emtId).style.top = (position[1] - 300) + "px";
	document.getElementById(emtId).style.display = "block";
	if(showDIQuery == true){
		document.getElementById(emtId).getElementsByTagName("form")[0].style.display = "block";
	}
	
	initMap(instanceid, false);
}

function updateDI(){
	if(document.getElementById("diFrom").value != ''){
		document.getElementById("diTo").value = document.getElementById("street_address_" + instanceid).innerHTML + ", " + document.getElementById("post_office_" + instanceid).innerHTML;
		document.getElementById("diHotel").value = document.getElementById("content_head_" + instanceid).innerHTML;
		return true;
	}
	else{
		return false;	
	}
}

function closeMap(emtId){
	this.instanceid = null;
	document.getElementById(emtId).style.display = "none";
}

function printMap(emtId){
	var address = document.getElementById("street_address_" + instanceid).innerHTML + ", " + document.getElementById("post_office_" + instanceid).innerHTML;
	var newWin = window.open("/stc/matkat/jsp/maps.jsp?from=" + urlEncode(document.getElementById("diFrom").value) + "&to=" + urlEncode(address) + "&hotel=" + urlEncode(document.getElementById("content_head_" + instanceid).innerHTML) + "&locale=" + mapservice.locale);
}





// URL encode strings
function urlEncode(clearString) {
  var output = '';
  var x = 0;
  clearString = clearString.toString();
  var regex = /(^[a-zA-Z0-9_.]*)/;
  while (x < clearString.length) {
    var match = regex.exec(clearString.substr(x));
    if (match != null && match.length > 1 && match[1] != '') {
    	output += match[1];
      x += match[1].length;
    } else {
      if (clearString[x] == ' ')
        output += '+';
      else {
        var charCode = clearString.charCodeAt(x);
        var hexVal = charCode.toString(16);
        output += '%' + ( hexVal.length < 2 ? '0' : '' ) + hexVal.toUpperCase();
      }
      x++;
    }
  }
  return output;
}

// Find element position
function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}



