var infodivhalfwidth = 50;
var infodivhalfheight = 10;

var grnlinecolor = "#00FF00";
var sealinecolor = "#00FFFF";

var iconData = {
	"car": { width: 48, height: 48 }
};

var markerminzoom = 0;
var markermaxzoom = 17;

var posnlx = 0;
var posnly = 0;
var posnlxy = [posnlx, posnly];
var nameforloc = "";
var officeLayer = {};

var map;
var mgr;
var icons = {};
var allmarkers = [];

var baseimgurl = "im/gmaps/";
var baseimgw = 32;
var baseimgh = 32;
var baseimgsw = 16;
var baseimgsh = 32;

var alrZoom = 7;
var cntZoom = 3;
var cntLat = 35;
var cntLng = -105;

var lat_zoom = new Array(4);
lat_zoom[1] = 100;
lat_zoom[2] = 80;
lat_zoom[3] = 60;
lat_zoom[4] = 30;

var lng_zoom = new Array(4);
lng_zoom[1] = 450;
lng_zoom[2] = 230;
lng_zoom[3] = 125;
lng_zoom[4] = 65;

var calibratelngarr = {
	"0": [0, 50],
	"5": [0, 100],
	"10": [0, 150],
	"15": [0, 200],
	"20": [0, 250],
	"25": [0, 300]
};

var calibratelatarr = {
	"0": [0, 25],
	"5": [0, 50],
	"10": [0, 75],
	"15": [0, 100],
	"20": [0, 125],
	"25": [0, 150]
};


function zoomfromlatlng(diffLat,diffLng) {

	var rZoom = 0;
	var rZoomLat = 0;
	var rZoomLng = 0;
	for (var curclt in lat_zoom) {
		if (diffLat < lat_zoom[curclt]) rZoomLat = curclt;
	}
	for (var curcln in lng_zoom) {
		if (diffLng < lng_zoom[curcln]) rZoomLng = curcln;
	}
	if (rZoomLat < rZoomLng) rZoom = rZoomLat;
	else rZoom = rZoomLng;
	return rZoom;
};

function load() {
	if (GBrowserIsCompatible()) {
		map = new google.maps.Map2(document.getElementById("map"));
		map.setMapType(G_HYBRID_MAP);
		map.addControl(new google.maps.SmallMapControl());
		map.addControl(new google.maps.MapTypeControl());
		map.setCenter(new GLatLng(cntLat, cntLng), cntZoom);
		map.enableDoubleClickZoom();
		mgr = new MarkerManager(map, {trackMarkers:true});
		//window.setTimeout(setupOfficeMarkers, 0);

		// Calibrate LAT
		for (var callat in calibratelatarr) {
			var polyOptions = {};
			var polyline = new GPolyline([
				new GLatLng(calibratelatarr[callat][0]-0,callat-0),
				new GLatLng(calibratelatarr[callat][1]-0,callat-0)
			], "#ff0000", 5, 1, polyOptions);
			//map.addOverlay(polyline); // !!!
		}

		// Calibrate LNG
		for (var callat in calibratelngarr) {
			var polyOptions = {};
			var polyline = new GPolyline([
				new GLatLng(callat-0,calibratelngarr[callat][0]-0),
				new GLatLng(callat-0,calibratelngarr[callat][1]-0)
			], "#ff0000", 5, 1, polyOptions);
			//map.addOverlay(polyline); // !!!
		}
	}
}

function getIcon() {
	var icon = null;
		icon = new GIcon();
		icon.image = baseimgurl + "car.png";
		var size = iconData["car"];
		icon.iconSize = new GSize(size.width, size.height);
		icon.iconAnchor = new GPoint(size.width >> 1, size.height >> 1);
	return icon;
}

function setupOfficeMarkers() {
	allmarkers.length = 0;
	var markers = [];
	for (var j in officeLayer) {
		var place = officeLayer[j];
		var icon = getIcon();
		var title = place["name"];
		var posn = new GLatLng(place["posn"][0], place["posn"][1]);
		var marker = createMarker(posn,title,icon); 
		markers.push(marker);
		allmarkers.push(marker);
	  }
	mgr.addMarkers(markers, markerminzoom, markermaxzoom);
	mgr.refresh();
}
  
function createMarker(posn, title, icon) {
	var marker = new GMarker(posn, {title: title, icon: icon, draggable:false });
	//GEvent.addListener(marker, 'dblclick', function() { mgr.removeMarker(marker) } ); 
	return marker;
}

function Rectangle (divcenterpoint, htmltxt, opt_color) {
	this.divcenterpoint_ = divcenterpoint;
	this.htmltxt_ = htmltxt || "rrrr";
	this.color_ = opt_color || grnlinecolor;
}
Rectangle.prototype = new GOverlay();
Rectangle.prototype.initialize = function(map) {
	var div = document.createElement("div");
	div.style.border = "0px";
	div.style.background = this.color_;
	div.style.color = "#000000";
	div.style.position = "absolute";
	div.innerHTML = "<B>" + this.htmltxt_ + "</B>";
	div.style.width = (infodivhalfwidth * 2) + "px";
	div.style.height = (infodivhalfheight * 2) + "px";
	map.getPane(G_MAP_MARKER_PANE).appendChild(div);
	this.map_ = map;
	this.div_ = div;
}
Rectangle.prototype.remove = function() {
	this.div_.parentNode.removeChild(this.div_);
}
Rectangle.prototype.redraw = function(force) {
	if (!force) return;
	var divcntpnt = this.map_.fromLatLngToDivPixel(this.divcenterpoint_);
	this.div_.style.left = (divcntpnt.x - infodivhalfwidth) + "px";
	this.div_.style.top = (divcntpnt.y - infodivhalfheight) + "px";
}

function refreshonmap () {

	officeLayer = {};

	var mrpnt1 = document.forms['transportcalc'].shifrauc.value;
	if (mrpnt1) {
		if (artu[mrpnt1]) {
			posnlx = (artu[mrpnt1]).split('|')[8];
			posnly = (artu[mrpnt1]).split('|')[9];
			posnlxy = [posnlx, posnly];
			nameforloc = (artu[mrpnt1]).split('|')[0];
			officeLayer["1"] = {"name": nameforloc,"posn": posnlxy};
		}
	}

	var mrpnt2 = document.forms['transportcalc'].exitport.value;
	if (mrpnt2) {
		if (arepne[mrpnt2]) {
			posnlx = (arepne[mrpnt2]).split('|')[2];
			posnly = (arepne[mrpnt2]).split('|')[3];
			posnlxy = [posnlx, posnly];
			nameforloc = (arepne[mrpnt2]).split('|')[0];
			officeLayer["2"] = {"name": nameforloc,"posn": posnlxy};
		}
	}

	var mrpnt3 = document.forms['transportcalc'].usercountry.value;
	if (mrpnt3) {
		if (arto[mrpnt3]) {
			var mrpnt32 = document.forms['transportcalc'].userport.value;
			if (mrpnt32) {
				var arrportList = (arto[mrpnt3]).split(';');
				for (i = 0; i < arrportList.length; i++) {
					portname = (arrportList[i]).split('|')[0];
					if (portname == mrpnt32) {
						posnlx = (arrportList[i]).split('|')[1];
						posnly = (arrportList[i]).split('|')[2];
						posnlxy = [posnlx, posnly];
						nameforloc = (arto[mrpnt3]).split('|')[0];
						officeLayer["3"] = {"name": nameforloc,"posn": posnlxy};
					}
				}
			}
		}
	}

	for (numbrcell in officeLayer) {
		officeLayer[numbrcell]["posn"][0] = officeLayer[numbrcell]["posn"][0] - 0;
		officeLayer[numbrcell]["posn"][0] = officeLayer[numbrcell]["posn"][0] - 0;
		officeLayer[numbrcell]["posn"][1] = officeLayer[numbrcell]["posn"][1] - 0;
		officeLayer[numbrcell]["posn"][1] = officeLayer[numbrcell]["posn"][1] - 0;
	}

	map.clearOverlays();
	mgr.clearMarkers();

	if (officeLayer) {
		if ( (officeLayer[2]) && (officeLayer[3]) ) {

			if (officeLayer[2]["posn"][1] > officeLayer[3]["posn"][1]) {
				if ( (officeLayer[2]["posn"][1] - officeLayer[3]["posn"][1]) > 180) {
					officeLayer[3]["posn"][1]+=360;
				}
			} else if (officeLayer[3]["posn"][1] > officeLayer[2]["posn"][1]) {
				if ( (officeLayer[3]["posn"][1] - officeLayer[2]["posn"][1]) > 180) {
					officeLayer[2]["posn"][1]+=360;
				}
			}

			var polyOptions = {};
			var polyline = new GPolyline([
				new GLatLng(officeLayer[2]["posn"][0],officeLayer[2]["posn"][1]),
				new GLatLng(officeLayer[3]["posn"][0],officeLayer[3]["posn"][1])
			], sealinecolor, 5, 1, polyOptions);
			map.addOverlay(polyline);

			var seafprice = eeparseFloatTh(document.getElementById('priceseafld').innerHTML);
			if (!isNaN(seafprice)) seafprice = "$ " + document.getElementById('priceseafld').innerHTML;
			else seafprice = document.getElementById('priceseafld').innerHTML;

			var LatLngforDIV = new GLatLng((officeLayer[2]["posn"][0] + officeLayer[3]["posn"][0]) / 2, (officeLayer[2]["posn"][1] + officeLayer[3]["posn"][1]) / 2);
			map.addOverlay(new Rectangle(LatLngforDIV, seafprice, sealinecolor));

		}
		if ( (officeLayer[1]) && (officeLayer[2]) ) {

			if (officeLayer[1]["posn"][1] > officeLayer[2]["posn"][1]) {
				if ( (officeLayer[1]["posn"][1] - officeLayer[2]["posn"][1]) > 180) {
					officeLayer[2]["posn"][1]+=360;
				}
			} else if (officeLayer[2]["posn"][1] > officeLayer[1]["posn"][1]) {
				if ( (officeLayer[2]["posn"][1] - officeLayer[1]["posn"][1]) > 180) {
					officeLayer[1]["posn"][1]+=360;
				}
			}

			var polyOptions = {};
			var polyline = new GPolyline([
				new GLatLng(officeLayer[1]["posn"][0],officeLayer[1]["posn"][1]),
				new GLatLng(officeLayer[2]["posn"][0],officeLayer[2]["posn"][1])
			], grnlinecolor, 5, 1, polyOptions);
			map.addOverlay(polyline);

			var grndprice = eeparseFloatTh(document.getElementById('priceland').innerHTML);
			if (!isNaN(grndprice)) grndprice = "$ " + document.getElementById('priceland').innerHTML;
			else grndprice = document.getElementById('priceland').innerHTML;

			var LatLngforDIV = new GLatLng((officeLayer[1]["posn"][0] + officeLayer[2]["posn"][0]) / 2, (officeLayer[1]["posn"][1] + officeLayer[2]["posn"][1]) / 2);
			map.addOverlay(new Rectangle(LatLngforDIV, grndprice, grnlinecolor));
		}
	}

	var cntLatNew = cntLat;
	var cntLngNew = cntLng;
	var minLat = "";
	var maxLat = "";
	var minLng = "";
	var maxLng = "";

	for (numbrcell in officeLayer) {
		if (minLat === "") minLat = officeLayer[numbrcell]["posn"][0];
		if (maxLat === "") maxLat = officeLayer[numbrcell]["posn"][0];
		if (minLng === "") minLng = officeLayer[numbrcell]["posn"][1];
		if (maxLng === "") maxLng = officeLayer[numbrcell]["posn"][1];

		if (minLat > officeLayer[numbrcell]["posn"][0]) minLat = officeLayer[numbrcell]["posn"][0];
		if (maxLat < officeLayer[numbrcell]["posn"][0]) maxLat = officeLayer[numbrcell]["posn"][0];
		if (minLng > officeLayer[numbrcell]["posn"][1]) minLng = officeLayer[numbrcell]["posn"][1];
		if (maxLng < officeLayer[numbrcell]["posn"][1]) maxLng = officeLayer[numbrcell]["posn"][1];
	}

	if (minLat === "") minLat = cntLat - 0;
	if (maxLat === "") maxLat = cntLat - 0;
	if (minLng === "") minLng = cntLng - 0;
	if (maxLng === "") maxLng = cntLng - 0;

	cntLatNew = (minLat + maxLat) / 2;
	cntLngNew = (minLng + maxLng) / 2;

	var FcntZoom = zoomfromlatlng((maxLat - minLat),(maxLng - minLng));

	if (FcntZoom > map.getZoom()) {
		while (FcntZoom > map.getZoom()) {
			map.zoomIn();
		}
	} else if (FcntZoom < map.getZoom()) {
		while (FcntZoom < map.getZoom()) {
			map.zoomOut();
		}
	}

	setupOfficeMarkers();
	map.panTo(new GLatLng(cntLatNew, cntLngNew));

	//document.getElementById('tempfordebug').value = ""; // Debugging field
}
