/**
 * Custom JS objects and functions for use with Google Maps
 * Author: William Bagby (wbagby@tripconnect.com)
 * $Id: googlemaps.js,v 1.7 2007/04/10 17:47:29 isacolic Exp $ $State: Exp $
 */
 
function gMapMarker( id, name, desc, lat, lng, zoom, users, cats, reviews, groups ) {
	var id=id, name=name, desc=desc, lat=lat, lng=lng, zoom=zoom;
	var users	= new Array();
	var cats	= new Array();
	var reviews	= new Array();
	var image = '';
	this.getId	 = function() { return id; };
	this.getName = function() { return name; };
	this.getDesc = function() { return desc; };
	this.getLat  = function() { return lat; };
	this.getLng  = function() { return lng; };
	this.getZoom = function() { return zoom; };
	this.getUser	= function(index)	{ return users[i]; };
	this.getCategory= function(index)	{ return cats[i]; };
	this.getReview	= function(index)	{ return reviews[i]; };
	this.addUser	= function(user)	{ return users[users.length] = user; };
	this.addCategory= function(cat)		{ return cats[cats.length] = cat; };
	this.addReview	= function(review)	{ return reviews[reviews.length] = review; };
	this.getUsers		= function() { return users; };
	this.getCategories	= function() { return cats; };
	this.getReviews		= function() { return reviews; };
	this.getImage		= function() { return this.image; };
	this.setImage		= function(x) { this.image = x; };
	this.toString = function() { return "Name: "+name+"\nDescription: "+desc+"\nLatitude: "+lat+"\nLongitude: "+lng+"\n"; };
}

function gMapEntity( name, id ) {
	var name=name, id=id;
	this.getName= function() { return name; };
	this.getId	= function() { return id; };
}

function popMap(mapid,containerid) {
	mapid = mapid || "gmap";
	containerid = containerid || "gmap-container";
	showElement(containerid);
	createMap(mapid);
}

function unpopMap(mapid,containerid) {
	mapid = mapid || "gmap";
	containerid = containerid || "gmap-container";
	hideElement(containerid);
	destroyMap(mapid);
}

function createMap(mapid) {
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById(mapid));
		map.addControl(new GSmallMapControl());
		//map.addControl(new GMapTypeControl());
		map.setCenter( new GLatLng(sMarker.getLat(),sMarker.getLng()), sMarker.getZoom());
		setMarkers();
	}
}

function destroyMap(mapid) {
	map = null;
	document.getElementById(mapid).innerHTML = '';
}

function setMarkers() {
	if ( mapType == "single" ) {
		var point = new GLatLng(sMarker.getLat(),sMarker.getLng());
		map.setCenter(point, 13);
		var marker = new GMarker(point);
		map.addOverlay(marker);
		marker.openInfoWindowHtml("<b>"+sMarker.getName()+"</b><br/>"+sMarker.getDesc());
	} else if ( mapType == "multi" ) {
		bounds = map.getBounds();
		for ( var i=0, len=markers.length; i<len; i++ ) {
			var tMarker = markers[i];
			if ( tMarker == null ) continue;
			var point = new GLatLng(tMarker.getLat(),tMarker.getLng());
			map.addOverlay(createMarker(point,i,tMarker.getImage()));
			if ( !bounds.contains(point) ) {
				bounds.extend(point);
			}
		}
		map.setCenter(bounds.getCenter(),map.getBoundsZoomLevel(bounds));
	}
}

// Create a base icon for all of our markers that specifies the
// shadow, icon dimensions, etc.
var baseIcon = new GIcon();
baseIcon.shadow = "/tripconnect/images/gmap/shadow_20x20.png";
baseIcon.iconSize = new GSize(16, 16);
baseIcon.shadowSize = new GSize(20, 20);
baseIcon.iconAnchor = new GPoint(9, 34);
baseIcon.infoWindowAnchor = new GPoint(9, 2);
baseIcon.infoShadowAnchor = new GPoint(18, 25);

function setBaseIcon(iconSize, shadowSize, iconAnchor, infoWindowAnchor, infoShadowAnchor) {
	baseIcon.iconSize = iconSize;
	if(shadowSize) { baseIcon.shadowSize = shadowSize; }
	if(iconAnchor) { baseIcon.iconAnchor = iconAnchor; }
	if(infoWindowAnchor) { baseIcon.infoWindowAnchor = infoWindowAnchor; }
	if(infoShadowAnchor) { baseIcon.infoShadowAnchor = infoShadowAnchor; }	
} 

function createMarker(point, index, image) {
	// Create a numbered icon for this point using our icon class
	var icon = new GIcon(baseIcon);
	if(image && image != '') {
		icon.image = IMAGEPATH + image;
	} else { 
		icon.image = IMAGEPATH + "gmap/icon_orange_" + (index+1) + ".png";
	}
	var cmarker = new GMarker(point, icon);

	GEvent.addListener(cmarker, "click", function() {
		document.location.href='HotelReview.do?hotelID='+markers[index].getId();
	});
	GEvent.addListener(cmarker, "mouseover", function() {
		cmarker.openInfoWindowHtml("<b>"+markers[index].getName()+"</b><br/>"+markers[index].getDesc());
	});
	return cmarker;
}
