var infodivhalfwidth = 50;
var infodivhalfheight = 8;

var grnlinecolor = "#00FF00";
var sealinecolor = "#00FFFF";

var iconData = {
	"car": { width: 32, height: 32 },
	"port": { width: 32, height: 32 },
	"mansim": { width: 32, height: 32 },
	"manove": { width: 32, height: 32 },
	"adelvb": { width: 32, height: 32 },
	"adedlb": { width: 32, height: 32 },
	"iaaiaa": { width: 32, height: 32 },
	"copart": { width: 32, height: 32 },
	"tratra": { width: 32, height: 32 },
	"error": { width: 32, height: 32 }
};

var markerminzoom = 0;
var markermaxzoom = 17;

var posnlx = 0;
var posnly = 0;
var posnlxy = [posnlx, posnly];
var officeLayer = {};

var map;
var allmarkers = new Array();
var allpolylines = new Array();
var allrectangles = new Array();

var alrZoom = 7;
var cnt = { Lat: 35, Lng: -105, Zoom: 3 }

function vload() {
	var vOptions = {
		zoom: cnt.Zoom,
		center: new google.maps.LatLng(cnt.Lat, cnt.Lng),
		mapTypeControl: true,
		mapTypeControlOptions: {style: google.maps.MapTypeControlStyle.DROPDOWN_MENU},
		navigationControl: true,
		navigationControlOptions: {style: google.maps.NavigationControlStyle.SMALL},
		mapTypeId: google.maps.MapTypeId.HYBRID,
		scrollwheel:false
	}
	map = new google.maps.Map(document.getElementById("map"), vOptions);
	radioButtonChange();
	mapquickrefresh();
	singleajaxreqrate();
	try { if(addresslocation) unknownLocation(addresslocation.addr,addresslocation.lat,addresslocation.lng,addresslocation.auctionhouse); } catch(e){};
}

function getIcon(inam) {
	if(!inam || !iconData[inam]) inam = "car";
	var size = iconData[inam];
	var icon = new google.maps.MarkerImage(baseimgurl + inam + '.png', new google.maps.Size(size.width, size.height),new google.maps.Point(0,0), new google.maps.Point(16, 16));
	return icon;
}

function setupOfficeMarkers() {
	allmarkers.length = 0;
	for (var j in officeLayer) {
		var place = officeLayer[j];
		var icon = getIcon(place["icon"]);
		var title = place["name"];
		var posn = new google.maps.LatLng(place["posn"][0], place["posn"][1]);
		var marker = new google.maps.Marker({position:posn,map:map,title:title,icon:icon});
		marker.setMap(map);
		allmarkers.push(marker);
	}
}


function Rectangle(divcenterpoint, htmltxt, opt_color) {
	this.divcenterpoint_ = divcenterpoint;
	this.htmltxt_ = htmltxt || "rrrr";
	this.color_ = opt_color || grnlinecolor;
	this.div_ = null;
}

Rectangle.prototype = new google.maps.OverlayView();

Rectangle.prototype.onAdd = function() {
	var div = document.createElement('DIV');
	div.style.border = "0px";
	div.style.position = "absolute";
	div.style.color = "#000000";
	div.style.position = "absolute";
	div.style.background = this.color_;
	div.style.fontSize = "10px";
	div.innerHTML = "<B>" + this.htmltxt_ + "</B>";
	this.div_ = div;
	var panes = this.getPanes();
	panes.floatPane.appendChild(div);
}

Rectangle.prototype.draw = function() {
	var overlayProjection = this.getProjection();
	var divcntpnt = overlayProjection.fromLatLngToDivPixel(this.divcenterpoint_);
	this.div_.style.width = Math.round(infodivhalfwidth*2) + "px";
	this.div_.style.height = Math.round(infodivhalfheight*2) + "px";
	this.div_.style.left = (divcntpnt.x - infodivhalfwidth) + "px";
	this.div_.style.top = (divcntpnt.y - infodivhalfheight*this.htmltxt_.split("<br>").length) + "px";
}

Rectangle.prototype.remove = function() {
	this.div_.parentNode.removeChild(this.div_);
}

Rectangle.prototype.onRemove = function() {
	this.div_.parentNode.removeChild(this.div_);
	this.div_ = null;
}

function mapquickrefresh() {
	officeLayer = [];
	var iconforloc = "";
	var nameforloc = "";
	
	//1
	var elem = "";
	try { elem = auctData[document.forms['transportcalc'].auctionhouse.value]["locations"][document.forms['transportcalc'].shifrauc.value]; } catch(e) { }
	if(elem) {
		posnlxy = [elem[2], elem[3]];
		nameforloc = document.forms['transportcalc'].shifrauc.options[document.forms['transportcalc'].shifrauc.selectedIndex].text;
		iconforloc = document.forms['transportcalc'].auctionhouse.value;
		officeLayer["1"] = {"name": nameforloc,"posn": posnlxy,"icon": iconforloc};
	} else if(document.forms['transportcalc'].startzip.value){
		elem = document.forms['transportcalc'].startzip.value.split("|");
		if(elem[0] && elem[1] && elem[2] && elem[3]) {
			posnlxy = [elem[2], elem[3]];
			nameforloc = "Start Zip/Postal Code: " + elem[1];
			iconforloc = "";
			officeLayer["1"] = {"name":nameforloc,"posn":posnlxy,"icon":iconforloc};
		}
	}

	//2
	var elem = "";
	try { if(document.forms['transportcalc'].exitport.value) { for(var country in oceanports) if(oceanports[country]["ports"][document.forms['transportcalc'].exitport.value]) { elem = oceanports[country]["ports"][document.forms['transportcalc'].exitport.value]; break; } } } catch(e){ }
	if(elem) {
		posnlxy = [elem[2], elem[3]];
		nameforloc = "Port of " + document.forms['transportcalc'].exitport.options[document.forms['transportcalc'].exitport.selectedIndex].text;
		iconforloc = "port";
		officeLayer["2"] = {"name": nameforloc,"posn": posnlxy,"icon": iconforloc};
	} else if(document.forms['transportcalc'].endzip.value){
		elem = document.forms['transportcalc'].endzip.value.split("|");
		if(elem[0] && elem[1] && elem[2] && elem[3]) {
			posnlxy = [elem[2], elem[3]];
			nameforloc = "End Zip/Postal Code: " + elem[1];
			iconforloc = "";
			officeLayer["2"] = {"name":nameforloc,"posn":posnlxy,"icon":iconforloc};
		}
	}

	//3
	var elem = "";
	try { elem = oceanports[document.forms['transportcalc'].usercountry.value]["ports"][document.forms['transportcalc'].userport.value]; } catch(e){ }
	if(elem) {
		posnlxy = [elem[2], elem[3]];
		nameforloc = "Port of " + document.forms['transportcalc'].userport.options[document.forms['transportcalc'].userport.selectedIndex].text;
		iconforloc = "";
		officeLayer["3"] = {"name": nameforloc,"posn": posnlxy,"icon": iconforloc};
	}
	
	for (numbrcell in officeLayer) {
		officeLayer[numbrcell]["posn"][0] = officeLayer[numbrcell]["posn"][0] - 0;
		officeLayer[numbrcell]["posn"][1] = officeLayer[numbrcell]["posn"][1] - 0;
	}

	if(allmarkers.length>0) {
		for(var i=0; i<allmarkers.length; i++){ allmarkers[i].setMap(null); allmarkers[i] = null; }
		allmarkers.length = 0;
	}

	if(allpolylines.length>0) {
		for(var i=0; i<allpolylines.length; i++){ allpolylines[i].setMap(null); allpolylines[i] = null; }
		allpolylines.length = 0; 
	}
	
	if(allrectangles.length>0) {
		for(var i=0; i<allrectangles.length; i++){ allrectangles[i].setMap(null); allrectangles[i] = null; }
		allrectangles.length = 0; 
	}
	
	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 polylinesea = new google.maps.Polyline({ path: [ new google.maps.LatLng(officeLayer[2]["posn"][0],officeLayer[2]["posn"][1]), new google.maps.LatLng(officeLayer[3]["posn"][0],officeLayer[3]["posn"][1]) ], strokeColor: sealinecolor, strokeOpacity: 1.0, strokeWeight: 5 });
			polylinesea.setMap(map);
			allpolylines.push(polylinesea);

			var seafprice = "";
			var tmptmp = getRadioChecked(document.forms['transportcalc'].oceanratessel);
			if(!tmptmp) setRadioChecked(document.forms['transportcalc'].oceanratessel,"ocont40ft3carsconsol");
			if(tmptmp) seafprice = document.getElementById(tmptmp).innerHTML;
			
			if(seafprice) {
				var LatLngforDIV = new google.maps.LatLng((officeLayer[2]["posn"][0] + officeLayer[3]["posn"][0]) / 2, (officeLayer[2]["posn"][1] + officeLayer[3]["posn"][1]) / 2);
				var rectoc = new Rectangle(LatLngforDIV, seafprice, sealinecolor, map);
				rectoc.setMap(map);
				allrectangles.push(rectoc);
			}
		}
		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 polylineground = new google.maps.Polyline({ path: [ new google.maps.LatLng(officeLayer[1]["posn"][0],officeLayer[1]["posn"][1]), new google.maps.LatLng(officeLayer[2]["posn"][0],officeLayer[2]["posn"][1]) ], strokeColor: grnlinecolor, strokeOpacity: 1.0, strokeWeight: 5 });
			polylineground.setMap(map);
			allpolylines.push(polylineground);

			var grndprice = document.getElementById('groundrateidhtml').innerHTML;
			if(grndprice) {
				var LatLngforDIV = new google.maps.LatLng((officeLayer[1]["posn"][0] + officeLayer[2]["posn"][0]) / 2, (officeLayer[1]["posn"][1] + officeLayer[2]["posn"][1]) / 2);
				var rectgr = new Rectangle(LatLngforDIV, grndprice, grnlinecolor,map);
				rectgr.setMap(map);
				allrectangles.push(rectgr);
			}
		}
	}
	// ---
	var officeLayerBounds = new google.maps.LatLngBounds();
	var countrsrst=0
	for (numbrcell in officeLayer) { officeLayerBounds.extend(new google.maps.LatLng(officeLayer[numbrcell]["posn"][0],officeLayer[numbrcell]["posn"][1])); countrsrst++; }
	if(!officeLayerBounds.isEmpty()) {
		if(countrsrst<2) {	
			map.setZoom(cnt.Zoom); 
			map.panTo(officeLayerBounds.getCenter()); 
		} else {
			map.fitBounds(officeLayerBounds);
		}
	}
	if(officeLayerBounds.isEmpty()) {
		map.setZoom(cnt.Zoom);
		map.panTo(new google.maps.LatLng(cnt.Lat, cnt.Lng));
	}
	setupOfficeMarkers();
}

