

var gardaMaps = Class.create();
gardaMaps.prototype = {

	initialize : function (options)
	{
		this.options = options;
		this.map = this.options.map;
		
		// get all handles
		var handles = this.options.handleContainer.getElementsBySelector("input");
		
		for(var i=0; i<handles.length;i++)
		{
			handles[i].onclick = this.clickHandler;
			handles[i].myObject = this;
		}
		
		this.activeMarkers = new Array();
		this.marker = new Array();
		
		// listener auf die Google Karte
	},
	
	clickHandler : function ()
	{
		if(this.checked == true)
		{
			this.myObject.toogle(this.value,true);
		}
		else
		{
			this.myObject.toogle(this.value,false);
		}
	},
	
	toogle : function (method,display)
	{
		var tmpActiveMarkers = new Array();
		
		if(display)
		{
			this.activeMarkers.push(method);
			this.displayMarkers(method);
		}
		else
		{
			for(var i=0; i<this.activeMarkers.length; i++)
			{
				if(this.activeMarkers[i] != method)
				{
					tmpActiveMarkers.push(this.activeMarkers[i]);
				}
			}
			this.activeMarkers = tmpActiveMarkers;
			this.removeMarkers(method);
		}		
	},
	
	removeMarkers : function (method)
	{
		delete this.marker[method];
		this.drawMarkers();
	},
	
	displayMarkers : function (method)
	{
		var parameters = {
			SWLat: this.map.getBounds().getSouthWest().lat(),
			SWLng: this.map.getBounds().getSouthWest().lng(),
			NELat: this.map.getBounds().getNorthEast().lat(),
			NELng: this.map.getBounds().getNorthEast().lng(),
			categories: this.activeMarkers.join(","),
			lang: this.options.lang
		};
		new Ajax.Request('/tools/gardamaps/getByBounds.php',{method: 'post', onComplete: this.addMarkersToRepository.bind(this), parameters: parameters});
	},
	
	addMarkersToRepository : function (transport)
	{
		this.drawMarkers();
		
		var data = transport.responseText.evalJSON();
				
		for(var i=0; i<data.length; i++)
		{
			this.drawCategoryMarkers(data[i].category,data[i].data);
		}
	},
	
	drawMarkers : function ()
	{
		this.map.clearOverlays();
		
		if(this.markerCluster)
		{
			GEvent.removeListener(this.markerCluster.zoomendListener);
			GEvent.removeListener(this.markerCluster.moveendListener);
			GEvent.removeListener(this.markerCluster.infowindowcloseListener);
			
			delete this.markerCluster;
		}
		
		this.markerCluster = new Clusterer(this.map);
		this.markerCluster.SetMaxVisibleMarkers(30);
		this.markerCluster.SetMinMarkersPerCluster(1);
				
		for(var i=0; i<this.activeMarkers.length; i++)
		{
			if(typeof this.marker[this.activeMarkers[i]] != "undefined")
			{
				for(var u=0; u<this.marker[this.activeMarkers[i]].length; u++)
				{
					this.markerCluster.AddMarker(this.marker[this.activeMarkers[i]][u]);
				}
			}
		}
	},
	
	drawCategoryMarkers : function (category, data)
	{
		this.marker[category] = new Array();
		
		for(var i=0; i<data.length; i++)
		{
			var markerIndex = this.marker[category].length;
			
			if(category == "weather")
			{
				var icon = new GIcon();
				icon.image = "/static/img/garda_weather/" + data[i].data.icon1 + "_" + data[i].data.icon2 + ".png";
				icon.iconSize = new GSize(38, 31);
				icon.iconAnchor = new GPoint(19, 15);
			}
			else
			{
				var icon = mapsIconsCategory[category];
			}
			
			this.marker[category].push(new GMarker(new GLatLng(data[i].lat, data[i].lng),icon));
			this.marker[category][markerIndex].myData = data[i];
			this.marker[category][markerIndex].myCategory = category;
			
			GEvent.addListener(this.marker[category][markerIndex],"click",function () { 
				this.openInfoWindowHtml(infoWindowTemplates[this.myCategory].process(this.myData));
			});			
			this.markerCluster.AddMarker(this.marker[category][markerIndex]);
		}
	}
}



/* Markers */


var markerCategories = ["hotel5","hotel4","hotel3","hotel2","hotel1","appartments","privaterooms","camping","farm","schoolandrent","beaches","restaurants","nightlife","shopping","webcams","panoramio"];

var mapsIconsCategory = new Array();


mapsIconsCategory['standard'] = new GIcon();
mapsIconsCategory['standard'].iconSize = new GSize(31, 30);
mapsIconsCategory['standard'].iconAnchor = new GPoint(15, 15);
mapsIconsCategory['standard'].infoWindowAnchor = new GPoint(15, 0);

/* init small mapicons */

for(var u=0; u<markerCategories.length; u++)
{
	mapsIconsCategory[markerCategories[u]] = new GIcon(mapsIconsCategory['standard']);
	mapsIconsCategory[markerCategories[u]].image = "/static/img/markers/" + markerCategories[u] + ".png";
}


var infoWindowTemplates = new Array();


infoWindowTemplates['hotels'] = ('' +
	'<div>' +
		'<img src="${image}" />' +
		'<b>${name}</b><br />' +
		'${contact.street} <br />' +
		'${contact.zip} / ${contact.city} <br />' +
		'<a href="mailto:${contact.email}?subject=Anfrage%20%FCber%20www.garda-see.com">${contact.email}</a><br />' +
		'{if contact.homepage.length > 1}<a href="${contact.homepage}" target="_blank" onlick="clickTracker(\'/external/${contact.homepage}\');">${contact.homepage}</a><br />{/if}' +
		'${contact.phone}<br />' +
		'${contact.fax}<br />' +
	'</div>' +
'');

infoWindowTemplates['hotel1'] = infoWindowTemplates['hotels'];
infoWindowTemplates['hotel2'] = infoWindowTemplates['hotels'];
infoWindowTemplates['hotel3'] = infoWindowTemplates['hotels'];
infoWindowTemplates['hotel4'] = infoWindowTemplates['hotels'];
infoWindowTemplates['hotel5'] = infoWindowTemplates['hotels'];
infoWindowTemplates['appartments'] = infoWindowTemplates['hotels'];
infoWindowTemplates['privaterooms'] = infoWindowTemplates['hotels'];
infoWindowTemplates['camping'] = infoWindowTemplates['hotels'];
infoWindowTemplates['farm'] = infoWindowTemplates['hotels'];


infoWindowTemplates['az'] = ('' +
	'<div>' + 
		'<b>${name}</b> <br />' +
		'${street} <br />' +
		'${zip} / ${city}<br />' +
		'<a href="mailto:${email}?subject=Anfrage%20%FCber%20www.garda-see.com">${email}</a><br />' +
		'<a href="${www}" onlick="clickTracker(\'/external/${www}\');">${www}</a><br />' +
		'${phone}<br />' +
	'</div>' +
'');

infoWindowTemplates['schoolandrent'] = infoWindowTemplates['az'];
infoWindowTemplates['beaches'] = infoWindowTemplates['az'];
infoWindowTemplates['restaurants'] = infoWindowTemplates['az'];
infoWindowTemplates['shopping'] = infoWindowTemplates['az'];
infoWindowTemplates['nightlife'] = infoWindowTemplates['az'];


infoWindowTemplates['webcams'] = ('' + 
	'<div>' +
		'<b>${name}</b> <br />' +
		'<img src="/static/cache/webcams/${key}_128x96.jpg" /><br />' +
		'&copy; ${copyright}' +
	'</div>' +
'');

infoWindowTemplates['panoramio'] = ('' +
	'<div>' +
		'<b>${name}</b> <br />' +
		'<a href="${detailURL}" target="_blank"><img src="http://www.panoramio.com/photos/small/${id}.jpg" /></a><br />' +
		'${owner.name}' +
	'</div>' +
'');


