﻿var _unitmarkers = [];
var _map = null;
var _officetype = 1;
var _ws = null;
var _vr = null;

function UnitMarker(id,marker) { 
   this.id = id;
   this.marker = marker;
   this.getPoint = function() { return this.marker.getPoint(); }
} 

function loadMap(vr, ws, lat, lng, zoom, type) {
	_ws = ws;
	_vr = vr;
	_officetype = type;
	if (GBrowserIsCompatible()) {
		_map = new GMap2(document.getElementById("gmap"));
		
		_map.setCenter(new GLatLng(lat, lng), zoom);
		_map.setMapType(G_HYBRID_MAP);
		//_map.addControl(new GSmallMapControl())
		_map.addControl(new GMapTypeControl());
	
		GEvent.addListener(_map, "moveend", function() {
			loadMarkers();
		});
		
		loadMarkers();
	}
}

function openMarker(id)
{
	loadContent(id);
}
//function openMarker(unit, latlng) {
//	var myHtml = getHtml(unit);
//	// 	map.openInfoWindowHtml(latlng, myHtml);
//	loadPopup(myHtml);
//}	

function createMarker(latlng, unit) {
	var iconurl = _vr + "/images/blue_sign.png";
	var icon = new GIcon(G_DEFAULT_ICON, iconurl);
	icon.iconSize = new GSize(20, 20);
	icon.iconAnchor = new GPoint(10, 10);
	icon.shadow = _vr + "/images/shadow.png";
	icon.shadowSize = new GSize(37, 17);
	
	var marker = new GMarker(latlng, {title: unit.name, icon: icon });
	
	// add to global collection
	addMarkerToCollection(unit.id, marker);
	
	GEvent.addListener(marker,"click", function() {
		openMarker(unit.id);
	});
	return marker;
}

function getMarker(id) {
	for(var i=0;i<_unitmarkers.length;i++) {
		var m = _unitmarkers[i];
		if (window.console) { window.console.log(m); }
		if (m.id = id) { 
			return m;
		}
	}
}

function addMarkerToCollection(id, marker) {
	for(var i=0;i<_unitmarkers.length;i++) {
		var m = _unitmarkers[i];
		if (m.getPoint().equals(marker.getPoint())) { 
			return;
		}
	}
	_unitmarkers[_unitmarkers.length] = new UnitMarker(id, marker);
}

function loadMarkers()
{
	var bounds = _map.getBounds();
	var sw = bounds.getSouthWest();
	var ne = bounds.getNorthEast();
    
	// load the units
	var j = _ws + "/json/units.aspx";
	$.getJSON(j, 
		{ latmin: sw.lat(), latmax: ne.lat(), lngmin: sw.lng(), lngmax: ne.lng(), type: _officetype },
		function(data) {
		
			for(index in data.units) {
				var u = data.units[index];
				var point = new GLatLng(u.lat, u.lng);
				_map.addOverlay(createMarker(point, u));
			}
		}
	);
}

function getHtml(unit)
{
	var ret = '<div class="infowindow">';
	ret += '<table cellpadding="4"><tr><td valign="top" align="left">';
	ret += '<img width="150" src="' + unit.thumbnail + '" alt="' + unit.name + '"/></td>';
	ret += '<td valign="top" align="left">';
	ret += '<div class="map_unit_code">' + unit.code + '</div><div class="map_unit_name">' + unit.name + '</div>';
	ret += '<p/>';
	ret += '<a href="' + _vr + '/units/detail/pictures/?o=' + unit.id + '">Pictures</a>';
	ret += '<br/>';
	ret += '<a href="' + _vr + '/units/detail/videos/?o=' + unit.id + '">Videos</a>';
	ret += '</div></td></tr></table>';
	return ret;
}