var dpbImages = 'http://www.docteurduparebrise.com/skin/frontend/nvi/dpb/images/';
var dpbBulle = dpbImages + 'dpb-bulle.png';
var dpbIcon = dpbImages + 'dpb-icon.png';
var dpbIconRed = dpbImages + 'dpb-icon-red.png';
var dpbShadow = dpbImages + 'dpb-shadow.png';
var dpbShadow2 = dpbImages +  'dpb-shadow2.png';
var dpbHome = dpbImages + 'dpb-maison.png';

var Jlib = {
	Name: "Google Maps Interface",
	Version: "1.1.0",
	Author: "Joel Perras",
	Notes: "Unaffiliated with Google Inc. and any subsidiaries",
	printInfo: function() {
		document.write(Jlib.Name+": By "+Jlib.Author+", Version: "+Jlib.Version+" ("+Jlib.Notes+")");
	}
}

/* Class JSON for consistent object creation and initialization */
var Class = { /* Thank you Prototype */
  create: function() {
    return function() {
      this.initialize.apply(this, arguments);
    }
  }
}

var Map = Class.create();
Map.prototype = {
	/* Initialization routines */
	initialize: function(mapID, version) {
		// Variables
	    this.VERSION = '0.1.a';
	    this.initialized = false;
	   	this.markers = new Array();
	    this.baseIcon = new GIcon();
	    this.baseIcon.image = dpbIcon;
	    this.baseIcon.shadow = dpbShadow;
	    this.baseIcon.iconSize = new GSize(21, 40);
	    this.baseIcon.iconAnchor = new GPoint(9, 34);
	    this.baseIcon.shadowSize = new GSize(52, 45);
	    this.baseIcon.infoWindowAnchor = new GPoint(9, 2);
	    this.baseIcon.infoShadowAnchor = new GPoint(18, 25);
	    
	    this.homeIcon = new GIcon(this.baseIcon);
		this.homeIcon.image = dpbHome;
		this.homeIcon.shadow = dpbShadow2;
		this.homeIcon.iconSize = new GSize(41, 42);
		this.homeIcon.shadowSize = new GSize(60, 34);
		this.homeIcon.iconAnchor = new GPoint(9, 34);
		
		this.alertIcon = new GIcon(this.baseIcon);
		this.alertIcon.image = dpbIconRed;
		this.alertIcon.shadow = dpbShadow;
		

	    this.map = null;
	    this.geocoder = null;

	},
	
	/* Will plot the map */
	plot: function(mapID) {
		if (!this.initialized) {
			var mapElement = document.getElementById(mapID);
			
			/* Google Map initialization routines */
			var map = new GMap2(mapElement); 
			this.map = map;
			this.addControl(new GLargeMapControl());
			this.map.setCenter(new GLatLng(54.878506,-70.280949), 4); 
			this.addControl(new GMapTypeControl());
			this.geoCoder = new GClientGeocoder();
			this.initialized = true;
			
			return this.map;
		}
	},
		
	/* Add google API specific controls  to the current map */
	addControl: function(control) {
		this.map.addControl(control);
		return this.map;
	},
	
	/* Creates a standard marker on the map */
	createMarker: function(title, street_address, postal_code, link, latitude, longitude, icon) {
		var point = new GLatLng(latitude, longitude);
	    var marker = new GMarker(point, icon);
	    var infoTab = [
	    		      	new GInfoWindowTab(latitude+" "+longitude, "<table width='320' border='0' cellspacing='0' cellpadding='0'><tr><td width='50'><img src='" + dpbBulle + "'></td><td width='20'></td><td width='250'><div style='font-family:Arial, Helvetica, sans-serif; font-size:11px'><b>Lieu:</b> " + title + "<br>" + "<b>Addresse:</b> " + street_address + "<br>" + "<b>Code postale:</b> " + postal_code + "<br>" + link + "</div></td></tr></table>")
					  ];
	    
	    this.markers[this.markers.length] = marker;
		this.map.addOverlay(marker);    

		GEvent.addListener(marker, "click", function() {
	       	 marker.openInfoWindowTabsHtml(infoTab);
	    });
	   	return this.map;
	},
	
	setHomeMarker: function(postal_code) {
		this.geoCoder.getLatLng(postal_code, this._homeMarker);
	},
	
	_homeMarker: function(response) {
			place = response;
			point = new GLatLng(place.Point.coordinates[1],place.Point.coordinates[0]);
		    var infoTab =	[
		      				new GInfoWindowTab(point.lng()+" "+point.lat(), "<table width='320' border='0' cellspacing='0' cellpadding='0'><tr><td width='50'><img src='" + dpbBulle + "'></td><td width='20'></td><td width='250'><div style='font-family:Arial, Helvetica, sans-serif; font-size:11px'><b>Lieu:</b> " + postal_code + "</div></td></tr></table>")
							];
			var marker = new GMarker(point, this.homeIcon);
			this.map.addOverlay(marker);
			
			GEvent.addListener(marker, "click", function() {
	       		marker.openInfoWindowTabsHtml(infoTab);
	    	});
			return this.map;
	},
	
	/* buggy, since getLatLng makes an asynchronous call... DO NOT USE */
	getGeoLocation: function(postal_code) {
		if (this.geoCoder) {
			this.geoCoder.getLatLng(postal_code, function(point) {
				var geoPoints = new Array(2);
				geoPoints[0] = point.lat();
				geoPoints[1] = point.lng();
			  return geoPoints;
			});
		} else {
			document.write('Geo-coding unavailable at this time.');
		}
	}
}


