var MAP_TYPE_MAP = 'r';
var MAP_TYPE_SATELLITE = 'a';
var MAP_TYPE_HYBRID = 'h';

function IMap(mapName){
	this.map = new VEMap(mapName);
	this.mode = MAP_TYPE_MAP;
	this.centrePoint = new IPoint(45,0);
	this.zoom = 1;
}

IMap.prototype.show = function(isStatic){
	var mp = this.centrePoint._getGeoCode();
	//	this.map.SetCenterAndZoom(mp, (this.zoom)?this.zoom:4);
	this.map.LoadMap(mp, (this.zoom)?this.zoom:4, VEMapStyle.Road, isStatic, VEMapMode.Mode2D, false);
};

IMap.prototype.terminate = function () {
	//Nothing required for VEarth maps
};

IMap.prototype.addPin = function(marker) {
	var pin = new VEShape(VEShapeType.Pushpin, marker.point._getGeoCode());
	pin.SetCustomIcon(marker.icon);
	this.map.AddShape(pin);
	marker.isVisible = true;
	marker.pin = pin;
	pin.SetTitle(marker.title);
	pin.SetDescription(marker.description);
};

IMap.prototype.removePin = function(point) {
	this.map.DeleteShape(point.shape);
	point.isVisible = false;
	point.pin = null;
};

IMap.prototype.setCentreZoom = function (centre, zoom) {
	this.centrePoint = centre;
	this.zoom = zoom;
	this.map.SetCenterAndZoom(centre._getGeoCode(), zoom);
};

IMap.prototype.hideControl = function () {
	this.map.HideDashboard();
};

IMap.prototype.setControlSize = function (size) {
	if(size == 'tiny'){
		this.map.SetDashboardSize(VEDashboardSize.Tiny);
	}else if(size == 'small'){
		this.map.SetDashboardSize(VEDashboardSize.Small);
	}else{
		this.map.SetDashboardSize(VEDashboardSize.Normal);
	}
};

IMap.prototype.setView = function(markers) {
	try{
		var vePts = new Array();
		var count = 0;
		for(marker in markers){
			if(markers[marker].point.lat != 0 && markers[marker].point.long != 0 && markers[marker].isVisible){
				vePts[count++] = markers[marker].point._getGeoCode();
			}
		}
		this.map.SetMapView(vePts);
		if(this.getZoomLevel() > 17){
			this.setZoomLevel(17);
		}
	}catch(e){
	}
};

IMap.prototype.getCentre = function(){
	var centre = this.map.GetCenter();
	return new IPoint(centre.Latitude, centre.Longitude);
};

IMap.prototype.getZoomLevel = function(){
	return this.map.GetZoomLevel();
};

IMap.prototype.setZoomLevel = function(zoomLevel){
	this.map.SetZoomLevel(zoomLevel);
};

IMap.prototype.setMapStyle = function(style){
	if(style == 'aerial'){
		this.map.SetMapStyle(VEMapStyle.Aerial);
	}else if(style == 'hybrid'){
		this.map.SetMapStyle(VEMapStyle.Hybrid);
	}else{
		this.map.SetMapStyle(VEMapStyle.Road);
	}
};

IPoint.prototype._getGeoCode = function() {
	return new VELatLong(this.lat, this.long);
};

IMarker.prototype.togglePin = function() {
	try{
		if(this.isVisible){
			this.pin.Hide();
		}else{
			this.pin.Show();
		}
		this.isVisible = !this.isVisible;
	}catch(e){
	}
};

IMap.prototype.zoomIn = function(){
	var zoomLevel = this.getZoomLevel() + 1;
	this.setZoomLevel(zoomLevel);
};

IMap.prototype.zoomOut = function(){
	var zoomLevel = this.getZoomLevel() - 1;
	this.setZoomLevel(zoomLevel);
};

IMap.prototype.addEventHandler = function(evt, fnc){
	this.map.AttachEvent(evt, fnc);
};

IMap.prototype.isDefaultZoomLevel = function(){
	return this.getZoomLevel()=='1'
};

IMap.prototype.setScaleSystem = function(unitSystem){
	if(unitSystem == 'imperial')
		this.map.SetScaleBarDistanceUnit(VEDistanceUnit.Miles);
	else
		this.map.SetScaleBarDistanceUnit(VEDistanceUnit.Kilometers);
};

IMap.prototype.showInfoBox = function(iMarker){
	try{
		this.map.ShowInfoBox(iMarker.pin);
	}catch(e){}
};

IMap.prototype.hideInfoBox = function(iMarker){
	try{
		this.map.HideInfoBox(iMarker.pin);
	}catch(e){}
};
IMap.prototype.setPrintable= function() {
	try {
		var printOpt = new VEPrintOptions(true);
    	this.map.SetPrintOptions(printOpt);
    } catch (e) {}
};

