var dsc_GoogleMaps = new Class({
	
	Implements: [Options, Events],
	options: {
		numberOfSurroundMarkers: 4
	},
	
	map: $empty,
	geocoder: $empty,
	baseIcon: $empty,
	markerOptions: $empty,
	markers: $empty,
	
	initialize: function(containerId, mapType, options) {
		if(GBrowserIsCompatible()) {
			this.map = new GMap2(document.getElementById(containerId));
			if(mapType == 1) {
				this.map.setUIToDefault();
			}
			this.map.setCenter(new GLatLng(51.5, 10.6), 6);
			this.geocoder = new GClientGeocoder();
			this.setOptions(options);
			this.markers = [];
			this.baseIcon = new GIcon(G_DEFAULT_ICON);
			this.baseIcon.iconSize = new GSize(18, 28);
			this.baseIcon.shadowSize = new GSize(35, 28);
			this.baseIcon.iconAnchor = new GPoint(7, 28);
			this.baseIcon.infoWindowAnchor = new GPoint(7, 2);
			this.initMarkerOptions();
		}
	},
	
	setCenter: function(lat, lng) {
		this.map.setCenter(new GLatLng(lat, lng), 6);
	},
	
	setCenterZoom: function(lat, lng) {
		this.map.setCenter(new GLatLng(lat, lng), 13);
	},
	
	initMarkerOptions: function(point, index) {
		var markerIconOther = new GIcon(this.baseIcon);
		var markerIconHit = new GIcon(this.baseIcon);
		var markerIconSurround = new Array();
                var i;
		for(i=0; i<this.options.numberOfSurroundMarkers; i++) {
			markerIconSurround[i] = new GIcon(this.baseIcon);
			//markerIconSurround[i].image = "/typo3conf/ext/dsc_locationfinder/res/ico-marker-surround-" + (i+1) + ".png";
			markerIconSurround[i].image = "/typo3conf/ext/dsc_locationfinder/res/ico-marker-surround.png";
		}
		markerIconOther.image = "/typo3conf/ext/dsc_locationfinder/res/ico-marker-other.png";
		markerIconHit.image = "/typo3conf/ext/dsc_locationfinder/res/ico-marker-hit.png";
		this.markerOptions = [];
		this.markerOptions[0] = {icon:markerIconOther};
		this.markerOptions[1] = [];
		for(i=0; i<this.options.numberOfSurroundMarkers; i++) {
			this.markerOptions[1][i] = {icon:markerIconSurround[i]};
		}
		this.markerOptions[2] = {icon:markerIconHit};
	},
	
	addMarkerByCoordinates: function(uid, lat, lng, data, markerType, section, orderNr) {
                var elem = new dsc_GoogleMapsMarker(this.map, uid, lat, lng, data, markerType, this.markerOptions, orderNr, section);
                this.markers.push(elem);
                elem.show();
	},
	
	highlightMarker: function(lat, lng, orderNr) {
		var marker = this.getMarkerByCoordinates(lat, lng);
		if(marker) {marker.highlight(orderNr);}
	},
	
	setMarkerType: function(lat, lng, markerType, orderNr) {
                this.markers.each(function(el,index) {
                    if(el.point != null) {
			if(el.point.lat() == lat && el.point.lng() == lng) {
				el.setMarkerType(markerType, orderNr);
			}
                    }
		});
	},
	
	getMarkerByCoordinates: function(lat, lng) {
		var foundMarker = null;
		this.markers.each(function(el) {
			if(el.point.lat() == lat && el.point.lng() == lng) {
				foundMarker = el;
			}
		});
		return foundMarker;
	},
	
	resetMarkersByType: function(markerType) {
		this.markers.each(function(el, index) {
			if(el.markerType == markerType) {
				el.reset();
			}
		});
	},
	
	setMarkersToDefaultByType: function(markerType) {
		this.markers.each(function(el, index) {
			if(el.markerType == markerType) {
				el.setMarkerType(0);
			}
		});
	},
	
	removeMarker: function(uid) {
                this.markers.each(function(marker){
                    if(marker.uid == uid) {
                        this.map.removeOverlay(marker);        
                    }
                }) ;
	},
	
	getMarkers: function(markerType) {
		var foundMarkers = new Array();
		var counter = 0;
		this.markers.each(function(el) {
			if(el.markerType == markerType || el.origMarkerType == markerType) {
				foundMarkers[foundMarkers.length] = el;
			}
			counter++;
		});	

		// Bubble Sort
		var x, y, temp;
		for(x = 0; x < foundMarkers.length; x++) {
			for(y = 0; y < (foundMarkers.length-1); y++) {
			    if(foundMarkers[y].order[markerType] > foundMarkers[y+1].order[markerType]) {
			    	temp = foundMarkers[y+1];
			        foundMarkers[y+1] = foundMarkers[y];
			        foundMarkers[y] = temp;
			    }
			}
		}
		
		return foundMarkers;
	}
});



/**
 * 
 */
var dsc_GoogleMapsMarker = new Class({
	
	Implements: [Options, Events],
	options: {},
	
	markerOptions: $empty,
	marker: $empty,
	point: $empty,
	data: $empty,
	content: $empty,
	type: $empty,
	uid: $empty,
	map: $empty,
	origMarkerType: $empty,
	markerType: $empty,
	order: $empty,
	section: $empty,
	
	initialize: function(map, uid, lat, lng, data, origMarkerType, markerOptions, orderNr, section) {
		var self = this;
		this.map = map;
		this.uid = uid;
		this.data = data.split("|");
		this.content = this.getContent();
		this.origMarkerType = origMarkerType;
		this.markerType = origMarkerType;
		this.markerOptions = markerOptions;
		this.point = new GLatLng(lat, lng);
		this.section = section;
                this.order = [];
		this.order[origMarkerType] = orderNr;
		if(origMarkerType == 1) {
			this.marker = new GMarker(this.point, markerOptions[origMarkerType][orderNr]);
                        var icon = this.marker.getIcon();
                        
		}
		else {
			this.marker = new GMarker(this.point, markerOptions[origMarkerType]);
		}
//                GEvent.addListener(this.marker, "infowindowopen", function() {
//                    var divs = $$('img');
//                    divs.each(function(el) {
//                        if(el.src=="http://maps.gstatic.com/intl/en_ALL/mapfiles/iw_close.gif") {
//                            el.src="";
//                        }
//                    });
//                });
		if(data != '') {
			GEvent.addListener(this.marker, "click", function() {
				self.marker.openInfoWindowHtml(self.content);
			});
		}
	},
	
	getContent: function() {
		var markerContent = '';
		var linksLine = this.data[9];
		var links = new Array();
		if(linksLine) {
			links = linksLine.split("!");
		}
//                if(links.length < 5) {
//                    alert(links[0] + " / " + links[1] + " / " + links[2] + " / " + links[3] + " / " + links[4]);
//                }
		markerContent += '<div style="width:200px;">';
		markerContent += '<div style="margin:5px 0 7px 0;">';
		markerContent += '<span style="font-size:12px;font-weight:bold;">' + this.data[0] + '</span>';
		markerContent += '</div>';
		if(this.data[8]) {
			markerContent += '<div>';
			//markerContent += '<img src="' + this.data[8] + '" alt="Hintergrundbild" title="Hintergrundbild"/>';
			markerContent += this.data[8];
			markerContent += '</div>';
		}
		markerContent += '<div style="margin:5px 0 7px 0">';
		markerContent += this.data[1] + '<br/>';
		markerContent += this.data[2] + ' ' + this.data[3] + '<br/>';
		markerContent += 'Tel.: ' + this.data[4];
		markerContent += '</div>';
		markerContent += '<div style="margin:5px 0">';
		markerContent += '<a style="font-weight:bold;text-decoration:underline;" href="' + this.data[7] + '" target="_blank">Webseite</a><br/>';
		if(links[0]) {   
			markerContent += '<a style="font-weight:bold;text-decoration:underline;display:block;" href="' + links[0] + '" target="_blank">' + linkTitles[0] + '</a>';
		}
                if(links[1] != '') {
			markerContent += '<a style="font-weight:bold;text-decoration:underline;display:block;" href="' + links[1] + '" target="_blank">' + linkTitles[1] + '</a>';
		}
		if(links[2]) {
			markerContent += '<a style="font-weight:bold;text-decoration:underline;display:block;" href="' + links[2] + '" target="_blank">' + linkTitles[2] + '</a>';
		}
		if(links[3]) {
			markerContent += '<a style="font-weight:bold;text-decoration:underline;display:block;" href="' + links[3] + '" target="_blank">' + linkTitles[3] + '</a>';
		}
		if(links[4]) {
			markerContent += '<a style="font-weight:bold;text-decoration:underline;display:block;" href="' + links[4] + '" target="_blank">' + linkTitles[4] + '</a>';
		}
		markerContent += '</div>';
		markerContent += '</div>';
		return markerContent;
	},
	
	show: function() {
		this.map.addOverlay(this.marker);
	},
	
	highlight: function(orderNr) {
		var self = this;
		this.map.removeOverlay(this.marker);
		if(orderNr == 0) {
			this.marker = new GMarker(this.point, this.markerOptions[2]);
		}
		else {
			this.marker = new GMarker(this.point, this.markerOptions[1][1]);
		}
		if(this.data != '') {
			GEvent.addListener(this.marker, "click", function() {
				self.marker.openInfoWindowHtml(self.content);
			});
		}
		this.map.addOverlay(this.marker);
		this.markerType = 2;
		this.order[this.markerType] = orderNr;
	},
	
	setMarkerType: function(markerType, orderNr) {
		var self = this;
		this.map.removeOverlay(this.marker);
		this.order[markerType] = orderNr;
		if(markerType == 1) {
			this.marker = new GMarker(this.point, this.markerOptions[markerType][orderNr]);
		}
		else {
			this.marker = new GMarker(this.point, this.markerOptions[markerType]);
		}
		if(this.data != '') {
			GEvent.addListener(this.marker, "click", function() {
				self.marker.openInfoWindowHtml(self.content);
			});
		}
		this.map.addOverlay(this.marker);
		this.markerType = markerType;
		this.origMarkerType = markerType;
		this.order[this.origMarkerType] = orderNr;
	},
	
	reset: function() {
		var self = this;
		this.map.removeOverlay(this.marker);
		if(this.origMarkerType == 1) {
			this.marker = new GMarker(this.point, this.markerOptions[this.origMarkerType][this.order[this.origMarkerType]]);
		}
		else {
			this.marker = new GMarker(this.point, this.markerOptions[this.origMarkerType]);
		}
		if(this.data != '') {
			GEvent.addListener(this.marker, "click", function() {
				self.marker.openInfoWindowHtml(self.content);
			});
		}
		this.map.addOverlay(this.marker);
		this.markerType = this.origMarkerType;
	}
});
