/*
	Uses the Google map API
*/
var map = null;

function createMap(x,y,type,zl){
  if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map"));
		var mapType = type || G_NORMAL_MAP; // default mapType
		var zoomLevel = zl || 6;
		
		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
		map.setCenter(new GLatLng(x, y), zoomLevel, mapType);		
		map.addControl(new GScaleControl());
		//map.addOverlay(new GMarker(map.getCenter()));
  }
}

/* Place a symbol on the map. Default placement is map center */
function addSymbol(coord){
		var symbolCoord = coord || map.getCenter();
		map.addOverlay(new GMarker(symbolCoord));
}

/*
	Returns a valid wgs84 decimal degree coordinate 	
	We have a bunch of different formats in our DB. 
	So we must wash them before using the together with a map.
	Example -> 
	Input: 59 22,8N
	Output: 59.36888888888889
*/
function formatCoordinate(coord){
	coord = coord.replace(/[- a-zA-z]/g,''); // Remove some junk chars, leave digits, ',' and '.' ('.' will flag that we have decimal degrees)
	//if(coord.indexOf('.') == -1){
		return convertDegMinSecToDecProdVersion(coord);
	//}
	return coord;
}

/*	
	Convert coordinates
	Input: Coordinate -> Degrees Minutes Seconds format (example: 59228)
	Output: Coordinate -> Decimal degrees (Example 59.36888888888889) 
*/

function convertDegMinSecToDecProdVersion(coord){
  var degrees = coord.split('.')[0];
  var tempCoord = coord.split('.')[1];

  if(!tempCoord || degrees.length > 2){
    return "";
  }
  if(tempCoord.indexOf(',') != -1){
    var minutes = tempCoord.split(',')[0];
    var seconds = tempCoord.split(',')[1];
  }
  else{
    var minutes = tempCoord;
    var seconds = 0;
  }


  if(minutes)
  //Debug with firebug...
  //console.log('Deg='+degrees+' min='+minutes+' sec='+seconds);

  // Convert string to int
  degrees = parseInt(degrees,10);
  minutes = parseInt(minutes,10);
  seconds = parseInt(seconds,10);

  var decDegrees = degrees + (minutes * (1.0 / 60.0)) + (seconds * (1.0 / 3600.0));
 
  return decDegrees;
}

function convertDegMinSecToDec(coord){
 var degrees = coord.substr(0,2); 					// We will only be dealing with scandinavian coordinates so there should be a 2 digit value
 var minutes;
 var seconds;
 
 // do we have 2934,6723 ?
 if(coord.indexOf(',') != -1){
 	minutes = coord.split(',')[0];
	minutes = minutes.substr(2,minutes.length);
	seconds = coord.split(',')[1] || 0;	
 }
 else{
 	var minutes = coord.substr(2,4); 					// Extract 2 more digits
	var seconds = coord.substr(4,coord.length) || 0;	// The rest is seconds
 }
 //Debug with firebug...
 //console.log('Deg='+degrees+' min='+minutes+' sec='+seconds);
  
 // Convert string to int
 degrees = parseInt(degrees);
 minutes = parseInt(minutes);
 seconds = parseInt(seconds);

 var decDegrees = degrees + (minutes * (1.0 / 60.0)) + (seconds * (1.0 / 3600.0));

 return decDegrees;
}
