//Requires: Google Maps js api

////////////////////////////
//PMap class
function PMap(container,zoom,lat,lng,size)
{
	if (GBrowserIsCompatible()) {
		this.map = new GMap2(document.getElementById(container));
		if (size == 'small') {
			this.map.addControl(new GSmallMapControl());
		}
		else {
			
			this.map.addControl(new GLargeMapControl());
			this.map.enableContinuousZoom();
			this.map.enableScrollWheelZoom();
			this.map.addControl(new GMenuMapTypeControl());
			
		}
		this.map.setCenter(new GLatLng(lat, lng), zoom);
		this.geocode = new GClientGeocoder();   	
	}
	else {
		this.map = null;
		this.geocode = null;
	}
	locationMarker = null;
	this.OnDragDrop = OnDragDrop;
	this.OnZoom = OnZoom;
	this.moveEvent = null;
	this.zoomEvent = null;
	this.markerSets = [];
	this.gDir = null;
	this.addMarkerSet = addMarkerSet;
	this.deleteMarkerSet = deleteMarkerSet;
	this.flushMarkerSet = flushMarkerSet;
	this.gDirections = gDirections;
	this.moveToLocation = moveToLocation;
	
}

function OnDragDrop(func) {
	if (func)
	{
		this.moveEvent = GEvent.addListener(this.map, "moveend", func);
	}
	else
		GEvent.removeListener(this.moveEvent);
}

function OnZoom(func) {
	if (func)
	{
		this.zoomEvent = GEvent.addListener(this.map, "zoomend", func);
	}
	else
		GEvent.removeListener(this.zoomEvent);
}

function addMarkerSet(name,icon,max) {
	this.markerSets[name]=new PMarkerSet(this.map,icon,max);
}

function deleteMarkerSet(name) {
	if (this.markerSets[name]) {
		for (var i = 0; i < this.markerSets[name].markers.length; i++) {
			this.map.removeOverlay(this.markerSets[name].markers[i].marker);
		}
		this.markerSets[name].markers = [];
		this.markerSets[name]=null;
	}
	
}

function flushMarkerSet(name) {
	if (this.markerSets[name]) {
		for (var i = 0; i < this.markerSets[name].markers.length; i++) {
			this.map.removeOverlay(this.markerSets[name].markers[i].marker);
		}
		this.markerSets[name].markers = [];
	}
}

function gDirections(container,onError,onLoad,useMap){
	if (useMap)
		this.gDir = new GDirections(this.map, container);
	else
		this.gDir = new GDirections(null, container);
	
    GEvent.addListener(this.gDir, "load", onLoad);
    GEvent.addListener(this.gDir, "error", onError);
}

/////////////////////////////
//PMarkerSet class
function PMarkerSet(map,icon,max)
{
	this.map = map;
	this.icon = icon;
	this.addMarker = addMarker;
	this.hideSet = hideSet;
	this.showSet = showSet;
	this.getPMarkerById = getPMarkerById;
	this.getPMarkerByName = getPMarkerByName;
	this.exists = exists;
	this.markerCount = markerCount;
	this.fullyLoaded = fullyLoaded;
	this.markers = [];
	this.max = max;
	this.isVisible = true;
}

function addMarker(point,id,name,infoWindowInfo) {
	if (!this.exists(id))
	{
		
		if (infoWindowInfo)
			var markerOptions = {title: name, icon:this.icon, clickable:true}
		else
			var markerOptions = {title: name, icon:this.icon, clickable:false}
		
		var marker = new GMarker(point,markerOptions);
		var iconPath = this.icon.image;
		var hIconPath = this.icon.himage;
		var map = this.map;
		if (infoWindowInfo) {
			GEvent.addListener(marker, "click", function() {
				marker.openInfoWindowHtml(infoWindowInfo);
			});
		}
		GEvent.addListener(marker, "mouseover", function() {
			if (map.getInfoWindow().isHidden())
				if (hIconPath)
					marker.setImage(hIconPath);
		});	
		GEvent.addListener(marker, "mouseout", function() {
			if (iconPath)
				marker.setImage(iconPath);
		});
		this.markers.push(new PMarker(marker,id,name,infoWindowInfo));
		this.map.addOverlay(marker);
		if (this.markers.length > this.max)
		{
			var oldMarker = this.markers.shift();
			this.map.removeOverlay(oldMarker.marker);
		}
	}
}

function exists(id) {
	var ret = false;
	if (this.getPMarkerById(id))
		ret = true;
	return ret;
}

function hideSet() {
	for (var i = 0; i < this.markers.length; i++) {
		this.markers[i].marker.hide();
	}
	this.isVisible = false;
}

function showSet() {
	for (var i = 0; i < this.markers.length; i++) {
		this.markers[i].marker.show();
	}
	this.isVisible = true;
}

function getPMarkerById(id) {
	var ret = null;
	for (var i = 0; i < this.markers.length; i++) {
		if (this.markers[i].id == id) {
			ret = this.markers[i];
			break;
		}
	}
	return ret;
}

function getPMarkerByName(name) {
	var ret = null;
	for (var i = 0; i < this.markers.length; i++) {
		if (this.markers[i].name == name) {
			ret = this.markers[i];
			break;
		}
	}
	return ret;
}

function markerCount() {
	return this.markers.length;
}

function fullyLoaded() {	
	
	var ret = false;
	if (this.max == -1)
		ret = false;
	else {
		if (this.markers.length >= this.max)
			ret = true;
	}
	//return ret;
	return false;
}

function moveToLocation(name) {
	map = this.map;
	bounds = map.getBounds();
	this.geocode.setViewport(bounds);
	this.geocode.getLatLng(name,function(point) {
		if (!point) {
			return false;
		}
		else {
		    locationMarker = new GMarker(point,{title: name});
			this.map.addOverlay(locationMarker);
			map.setCenter(point);
			return true;
		}
	});
}

/////////////////////////////
//PMarker class
function PMarker(marker,id,name,htmlInfo)
{
	this.id = id;
	this.name = name;
	this.marker = marker;
	this.htmlInfo = htmlInfo;
}

///////////////////////////
//Icon objects
pint = new GIcon();
pint.shadow = "image/shadow-dpint4.png";
pint.iconSize = new GSize(17.0, 31.0);
pint.shadowSize = new GSize(33.0, 31.0);
pint.iconAnchor = new GPoint(8.5, 29.0);
pint.infoWindowAnchor = new GPoint(9, 4);
pint.image = "image/dpint4.png";
pint.himage = "image/dpint4_h.png";
pint.printImage = "image/dpint4.gif";
pint.mozPrintImage = "image/dpint4.gif";

greenp = new GIcon();
greenp.shadow = "image/shadow-greenp.png";
greenp.iconSize = new GSize(24.0, 34.0);
greenp.shadowSize = new GSize(42.0, 34.0);
greenp.iconAnchor = new GPoint(12, 34.0);
greenp.infoWindowAnchor = new GPoint(14, 4);
greenp.image = "image/greenp.png";

ttc = new GIcon();
ttc.shadow = "image/shadow-ttc_logo.png";
ttc.iconSize = new GSize(44.0, 25.0);
ttc.shadowSize = new GSize(57.0, 25.0);
ttc.iconAnchor = new GPoint(22, 25.0);
ttc.infoWindowAnchor = new GPoint(25, 4);
ttc.image = "image/ttc_logo.png";

