
S1Local.Map = {

	plot_data: null,
	browser_check: false,
	map: null,
	curr_cid: null,
	entity_list: null,
	plot_longlat: null,
	plot_ename: null,
	limit_count: 100,
	limit_radius: 20,
	map_zoom: 13,
	map_longlat: [56.0177,-3.60734],
	map_pcode: "EH510QT",
	cids: null,

	init: function ()
	{
		if (!$('map')) return;
		if (GBrowserIsCompatible())         // Do Map if Compatible Browser only
		{
			this.browser_check = true;
			this.map = new GMap2(document.getElementById("map"));
			var latlng = new GLatLng(this.map_longlat[0],this.map_longlat[1]) ;
			this.map.setCenter(latlng,this.map_zoom);
		    this.map.addControl(new GSmallMapControl());
			this.refrMap();
		}
		else
		{
			document.getElementById("map").innerHTML = "<h1>Browser not compatible with Google Maps. Sorry...</h1>" ;
		}
	},

	drawMap: function ()
	{
		if (!this.browser_check || !this.plot_data) return;
		for (var i = 0; i < this.plot_data.length; i++)
		{
			var point = new GLatLng(this.plot_data[i].lat, this.plot_data[i].lo);
			var icon = new GIcon();
			icon.shadow = "";
			icon.iconSize = new GSize(10, 10);
			icon.iconAnchor = new GPoint(0, 0);
			icon.infoWindowAnchor = new GPoint(0, 0);
			icon.infoShadowAnchor = new GPoint(0, 0);
			icon.image = S1Cfg.SITE_URL+"images/map/"+this.plot_data[i].img;
			var click = new GMarker(point,icon);
			if (this.plot_data[i].link_url)
			{
				GEvent.addListener(click, 'click', this.actIconLink.bind(this, this.plot_data[i].cid, this.plot_data[i].etype, this.plot_data[i].link_url));
			}
			this.map.addOverlay( click ) ;
		}
	},

	refrMap: function ()
	{
		if (this.plot_longlat && this.plot_ename)
		{
			this.plot_data = [
				{ lo:this.plot_longlat[1], lat:this.plot_longlat[0], img:this.plot_ename+"1.png" }
			 ];
			this.drawMap();
			return;
		}
		var params = {data:'map_conts'};
		if (this.curr_cid)
		{
			params.cid = this.curr_cid;
		}
		if (this.entity_list)
		{
			params.en = this.entity_list;
		}
		new Ajax.Request(S1Cfg.SITE_URL+"content.cgi", {
			method: 'GET',
			parameters: params,
			onSuccess: this.loadedData.bindAsEventListener(this)
			});
	},

	loadedData: function (r)
	{
		this.plot_data = null;
		var resp = r.responseText;
		var data = eval(resp);
		if (data) this.plot_data = data[0];
		this.drawMap();
	},

	actIconLink: function (cid, etype, link_url)
	{
		if (!etype || !cid) return;
		var url = S1Cfg.SITE_URL+link_url;
		document.location.href = url;
	}

};

// Register loaded
S1Local.setLoaded('Map');

window.onunload = function ()
{
	GUnload();
}