// #PLib::PLoad('PLib.Dom.Client.prototype');
// #PLib::PLoad('com.sanethis.cms.Modules.Map.Scripts.TLabel');

var GoogleMaps = Class.create();

GoogleMaps.prototype = {
	initialize: function() {
		this.tlabels = new Array();
		Event.observe(window, 'load', this.bindToDocument.bindAsEventListener(this));
	},
	
	setIconUrl: function(url) {
		this.iconUrl = url;
	},
	
	setCenter: function(lat, long, zoom) {
		this.lat = lat;
		this.long = long;
		this.zoom = zoom;
	},
	
	bindToDocument: function(event) {
	//	google.load("maps", "2.x");
		
	    if (GBrowserIsCompatible()) {
		  Event.observe(document.body, 'onunload', GUnload);
	      
	      // Display the map, with some controls and set the initial location 
	      this.map = new GMap2(document.getElementById("embedded-map"));
	      this.map.addControl(new GLargeMapControl());
		  this.map.addControl(new GOverviewMapControl());
	      this.map.addControl(new GMapTypeControl());
	      this.map.setCenter(new GLatLng(this.lat, this.long),this.zoom); 
	      	
	      this.icon = '<img src="' + this.iconUrl + '" width=20 height=34 />'
	      // Is this IE, if so we need to use AlphaImageLoader
	      var agent = navigator.userAgent.toLowerCase();
	        if ((agent.indexOf("msie") > -1) && (agent.indexOf("opera") < 1)){
	        var loader = "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + this.iconUrl + "', sizingMethod='crop');";
	        this.icon = '<div  style="height:34px; width:20px; ' +loader+ '" ></div>'
	      }  
	
	      this.n=0;
	

			var myThis = this;
			this.tlabels.each(function(item) {
				myThis.createTLabel(item.get('point'), item.get('html'));
			});

	    /*
	      createTLabel(new GLatLng(48.21180522147898,16.364006996154785),'<div style="width:240px"><a href="#">1010 Wien</a><br>Freyung 5<br>Tel  01/533 73 04<br>1010@bstaendig.at<br><a href="#">Details</a></div>');
	      createTLabel(new GLatLng(48.21160503594102,16.363534927368164),'<div style="width:240px"><a href="#">1010 Wien</a><br><font color="#55ccbb"><strong>Sportshop</strong></font><br>Teinfaltstrasse 4 5<br>Tel  01/533 73 04<br>1010@bstaendig.at<br><a href="#">Details</a></div>');
	      createTLabel(new GLatLng(47.846051, 16.52171),'<div style="width:240px"><a href="#">7000 Eisenstadt</a><br />Hauptstra�e 11<br />Tel: 02682/61 060<br /><LINK 7000@bstaendig.at>7000@bstaendig.at</LINK></div>');
		createTLabel(new GLatLng(48.226622, 15.32807),'<div style="width:240px"><a href="#">3390 Melk</a><br />Linzerstra�e 10<br />Tel: 02682/61 060<br /><LINK 7000@bstaendig.at>7000@bstaendig.at</LINK></div>');
	*/
	
	    }
	    
	    // display a warning if the browser was not compatible
	    else {
	      alert("Sorry, the Google Maps API is not compatible with this browser");
	    }
			
	},
	
	addTLabel: function(point, html) {
		var tmp = new Hash();
		tmp.set('point', point);
		tmp.set('html', html);
		this.tlabels.push(tmp);
	},
	
	createTLabel: function(point,html) {
		var label = new TLabel();
		label.id = 'Label '+this.n;
		label.anchorLatLng = point;
		label.anchorPoint = 'bottomCenter';
		label.content = this.icon;
		label.markerOffset = new GSize(-1,-5);
		this.map.addTLabel(label);
		
		var myThis = this;
		GEvent.addDomListener(document.getElementById(label.id), "click", function() {
		  myThis.map.openInfoWindowHtml(point, html, {pixelOffset: new GSize(0,-34)} );
		});         
		
		this.n++;
   }
}

var googleMaps = new GoogleMaps();

