// POI-Objekt
var _poi_throbber = new Image();
_poi_throbber.src = '/map/images/throbber.gif';

function Poi(id, channelid, item, lat, lng, title) {
	this.id = id;
	this.channelid = channelid;
	this.item = item;
	this.title = title;
	this.point = new GLatLng(lat, lng);

	this.icon = new Object();
	this.icon['small'] = iconFactory(this.item, 'small');
	this.icon['big'] = iconFactory(this.item, 'big');

	this.marker = new Object();
	this.marker['small'] = null;
	this.marker['big'] = null;
	this.usedMarker = null;

	this.createMarker();

	this.titlediv = document.createElement('div');
	this.titlediv.className = 'infowindow_title';
	this.titlediv.appendChild(document.createTextNode(this.title));

	this.bodydiv = null;

	this.content = null;

	this.dummy = document.createElement('div');

	var dummybody = document.createElement('div');
	dummybody.className = 'infowindow_body';

	var img = document.createElement('img');
	img.src = _poi_throbber.src;
	img.style.width = '16px';
	img.style.height = '16px';

	dummybody.appendChild(img);

	this.dummy.appendChild(this.titlediv);
	this.dummy.appendChild(dummybody);

	this.inCluster = false;
}

Poi.prototype.setBody = function(body) {
	this.bodydiv = document.createElement('div');
	this.bodydiv.className = 'infowindow_body';
	this.bodydiv.innerHTML = body;
}

Poi.prototype.createContent = function() {
	this.content = document.createElement('div');
	this.content.appendChild(this.titlediv);
	this.content.appendChild(this.bodydiv);
}

Poi.prototype.getContent = function(callback) {
	if(this.content != null)
		return this.content;

	var self = this;

	var a = new AJAX(_poi_gateway_url, function(response, data) {
		if(response == null)
			return;

		self.setBody(response.BODY);
		self.createContent();

		data.cb(data.id, self.content);

		return;
	}, {id:this.id, cb:callback}, 'json');
	a.setParameter('method', 'getPoiDetail');
	a.setParameter('channelId', this.channelid);
	a.setParameter('poiId', this.id);
	a.setParameter('language', _map_language);
	a.setParameter('siteId', _map_siteId);
	a.send();

	return this.dummy;
}

Poi.prototype.createMarker = function() {
	this.marker['small'] = new GMarker(this.point, {icon:this.icon['small']});
	this.marker['small']._poiref = this;

	this.marker['big'] = new GMarker(this.point, {icon:this.icon['big']});
	this.marker['big']._poiref = this;

	GEvent.addListener(this.marker['small'], 'mouseover', poiMouseover);
	GEvent.addListener(this.marker['big'], 'mouseover', poiMouseover);
	GEvent.addListener(this.marker['small'], 'mouseout', poiMouseout);
	GEvent.addListener(this.marker['big'], 'mouseout', poiMouseout);
}

Poi.prototype.getMarker = function(zoom) {
	// Damit ist alles schon abstrahiert und wir koennten im Prinzip fuer
	// jeden Zoomlevel einen eigenen Marker definieren.
	if(zoom >= 13)
		return this.marker['big'];

	return this.marker['small'];
}

function ClusterPoi(poi, bounds) {
	this.id = poi.id;
	this.channelid = poi.channelid;
	this.item = poi.item;
	this.bounds = bounds;
	this.point = this.bounds.getCenter();

	this.icon = new Object();
	this.icon['small'] = iconFactory(this.item, 'small');
	this.icon['big'] = iconFactory(this.item, 'big');

	this.marker = new Object();
	this.marker['small'] = null;
	this.marker['big'] = null;
	this.usedMarker = null;

	this.createMarker();

	this.content = null;

	this.dummy = document.createElement('div');

	var dummybody = document.createElement('div');
	dummybody.className = 'infowindow_body';

	var img = document.createElement('img');
	img.src = _poi_throbber.src;
	img.style.width = '16px';
	img.style.height = '16px';

	dummybody.appendChild(img);

	this.dummy.appendChild(dummybody);

	this.poiList = new Array();
	this.poiListMap = new Object();
}

ClusterPoi.prototype.push = function(poi) {
	if(this.item != poi.item)
		return;

	poi.inCluster = true;
	this.poiList.push(poi);

	this.poiListMap['p' + poi.id] = this.poiList.length - 1;
}

ClusterPoi.prototype.pop = function() {
	var poi = this.poiList.pop();

	if(poi == null)
		return null;

	poi.inCluster = false;
	delete this.poiListMap['p' + poi.id];

	this.createContent();

	return poi;
}

ClusterPoi.prototype.removePoi = function(poi) {
	if(this.item != poi.item)
		return;

	var i = this.poiList.indexOf(poi);

	if(i == -1)
		return;

	this.poiList[i].inCluster = false;

	this.poiList.splice(i, 1);

	delete this.poiListMap['p' + poi.id];

	this.createContent();
}

ClusterPoi.prototype.clear = function() {
	for(var i = 0; i < this.poiList.length; i++)
		this.poiList[i].inCluster = false;

	this.poiList = new Array();
	this.poiListMap = new Object();

	this.content = null;
}

ClusterPoi.prototype.setBody = function(poiid, body) {
	//GLog.write('poiid: ' + poiid + ', body: ' + body);
	try {
		var i = this.poiListMap['p' + poiid];
		this.poiList[i].setBody(body);
		this.poiList[i].createContent();
	} catch(e) {};
}

ClusterPoi.prototype.createContent = function() {
	var content = document.createElement('div');

	var poiids = new Array();

	for(var i = 0; i < this.poiList.length; i++) {
		if(this.poiList[i].content != null)
			content.appendChild(this.poiList[i].content);
		else
			poiids.push(this.poiList[i].id);
	}

	if(poiids.length == 0)
		this.content = content;

	return poiids;
}

ClusterPoi.prototype.getContent = function(callback) {
	if(this.content != null)
		return this.content;

	if(this.poiList.length == 0)
		return this.dummy;

	//GLog.write('poiList.length = ' + this.poiList.length);

	var poiids = this.createContent();

	if(poiids.length == 0)
		return this.content;

	var self = this;

	var a = new AJAX(_poi_gateway_url, function(response, data) {
		if(response == null)
			return;

		for(var i = 0; i < response.length; i++)
			self.setBody(response[i].POIID, response[i].BODY);

		self.createContent();

		data.cb(data.id, self.content);

		return;
	}, {id:this.id, cb:callback}, 'json');
	a.setParameter('method', 'getPoiDetails');
	a.setParameter('channelId', this.channelid);
	a.setParameter('poiId', poiids.join(','));
	a.setParameter('language', _map_language);
	a.setParameter('siteId', _map_siteId);
	a.send();

	return this.dummy;
}

ClusterPoi.prototype.createMarker = function() {
	this.marker['small'] = new GMarker(this.point, {icon:this.icon['small']});
	this.marker['small']._poiref = this;

	this.marker['big'] = new GMarker(this.point, {icon:this.icon['big']});
	this.marker['big']._poiref = this;

	GEvent.addListener(this.marker['small'], 'mouseover', poiMouseover);
	GEvent.addListener(this.marker['big'], 'mouseover', poiMouseover);
	GEvent.addListener(this.marker['small'], 'mouseout', poiMouseout);
	GEvent.addListener(this.marker['big'], 'mouseout', poiMouseout);
}

ClusterPoi.prototype.getMarker = function(zoom) {
	// Damit ist alles schon abstrahiert und wir koennten im Prinzip fuer
	// jeden Zoomlevel einen eigenen Marker definieren.
	if(zoom >= 13)
		return this.marker['big'];

	return this.marker['small'];
}

function poiMouseover() {
	setInfowindowPoi(this._poiref);

	_map.addOverlay(getInfowindow());
}

function poiMouseout() {
	_map.removeOverlay(getInfowindow());
}

