// EntityRecord is a structure for entities

function EntityRecord( key, name, latlng ) {
	this.key = key;
	this.name = name;
	this.point = latlng;
	this.marker = null;
}

// EntityControl manages the entities on the map and gives two button

EntityControl.prototype = new GControl();

function EntityControl( list, params, minZoom ) {
	this.entityList = list;
	this.parameters = params;
	this.minZoom = minZoom;
	
	this.defaultIcon = new GIcon();
	this.defaultIcon.image = "http://labs.google.com/ridefinder/images/mm_20_yellow.png";
	this.defaultIcon.iconSize = new GSize(12, 20);
	this.defaultIcon.shadowSize = new GSize(22, 20);
	this.defaultIcon.iconAnchor = new GPoint(6, 20);
	this.defaultIcon.infoWindowAnchor = new GPoint(5, 1);
}

EntityControl.prototype.entityInfoWindow = function( entityRecord, parameters ) {
	try { 
		url = "/digitalcity/map/infoWindow.jsp?";
		if ( parameters != undefined )
			for ( parameter in parameters )
				url += parameter + "=" + eval("parameters." + parameter ) + "&";
		GDownloadUrl( url + "&egd=" + entityRecord.key,
			function( text, code ) {
				if ( code == 200 )
					entityRecord.marker.openInfoWindowHtml(text);
				else
					entityRecord.marker.openInfoWindowHtml(entityRecord.name);	
			});
	} catch ( e ) {
		entityRecord.marker.openInfoWindowHtml(entityRecord.name);
	}
}

EntityControl.prototype.createEntityMarker = function( entity, parameters ) {
    var marker = new GMarker( entity.point, { icon: (( entity.icon == undefined ) ? new GIcon( this.defaultIcon ) : entity.icon), clickable: true, title: entity.name } );
	//var marker = new GMarker( entity.point, ( entity.icon == undefined ) ? new GIcon( this.defaultIcon ) : entity.icon );
	var control = this;
	GEvent.addListener(marker, 'click', function() {
			control.entityInfoWindow( entity, parameters );
		});
	return entity.marker = marker;
}
   
EntityControl.prototype.refresh = function() {
	var minX, maxX, minY, maxY;
	minX = maxX = minY = maxY = undefined;
	
	for ( i = 0; i < this.entityList.length; i++ )
		if ( this.entityList[i] != null && this.entityList[i].marker != null ) 
			if ( minX != undefined ) {
				if ( minX > this.entityList[i].point.x ) minX = this.entityList[i].point.x;
				if ( minY > this.entityList[i].point.y ) minY = this.entityList[i].point.y;
				if ( maxX < this.entityList[i].point.x ) maxX = this.entityList[i].point.x;
				if ( maxY < this.entityList[i].point.y ) maxY = this.entityList[i].point.y;
			} else {
				minX = maxX = this.entityList[i].point.x;
				minY = maxY = this.entityList[i].point.y;
			}

	if ( minX != undefined ) {
		var center, zoom;
			deltaX = ( maxX - minX ) * 0.25; 
			deltaY = ( maxY - minY ) * 0.25; 
			eBounds = new GLatLngBounds( new GLatLng( minY - deltaY, minX - deltaX ), new GLatLng( maxY + deltaY, maxX + deltaX ) );
			center = new GLatLng( (minY + maxY) / 2.0, (minX + maxX) / 2.0 );
			zoomLevel = this.entityMap.getBoundsZoomLevel( eBounds );
			zoom = ( zoomLevel > this.minZoom ) ? this.minZoom : zoomLevel;
		if ( zoom == this.entityMap.getZoom() )
			this.entityMap.panTo( center );
		else    
			this.entityMap.setCenter( center, zoom );
	}        	
};

EntityControl.prototype.isVisible = function( index ) {
	if ( typeof index != "number" ) return false;
	if ( this.entityList[index] == undefined ) return false;
	if ( this.entityList[index].marker == null ) return false;
	return true;
}
	
EntityControl.prototype.showEntity = function( index ) {
	if ( typeof index != "number" ) return;
	if ( this.entityList[index] == undefined ) return;
	if ( this.entityList[index].marker != null ) return;
	this.createEntityMarker( this.entityList[index], this.parameters );
	this.entityMap.addOverlay( this.entityList[index].marker );
	this.refresh();
}
	
EntityControl.prototype.hideEntity = function( index ) {
	if ( typeof index != "number" ) return;
	if ( this.entityList[index] == undefined ) return;
	if ( this.entityList[index].marker == null ) return;
	this.entityMap.removeOverlay( this.entityList[index].marker );
	this.entityList[index].marker = null;
	this.entityMap.getInfoWindow().hide();
	this.refresh();
}
	
EntityControl.prototype.popup = function( index ) {
	if ( typeof index != "number" ) return;
	if ( this.entityList[index] == undefined ) return;
	if ( this.entityList[index].marker == null ) {
		this.createEntityMarker( this.entityList[index], this.parameters );
		this.entityMap.addOverlay( this.entityList[index].marker );
		//this.refresh();
	}
	this.entityInfoWindow( this.entityList[index], this.parameters );
}
	
EntityControl.prototype.showAll = function() {
	this.entityMap.getInfoWindow().hide();
	for ( i = 0; i < this.entityList.length; i++ )
		if ( this.entityList[i] != null && this.entityList[i].marker == null ) {
			this.createEntityMarker( this.entityList[i], this.parameters );
			this.entityMap.addOverlay( this.entityList[i].marker );			
		}
	this.refresh();
}
	
EntityControl.prototype.hideAll = function() {
	this.entityMap.getInfoWindow().hide();
	for ( i = 0; i < this.entityList.length; i++ )
		if ( this.entityList[i] != null && this.entityList[i].marker != null ) {
			this.entityMap.removeOverlay( this.entityList[i].marker );
			this.entityList[i].marker = null;			
		}
}
	
EntityControl.prototype.initialize = function(map) {
	this.entityMap = map;
	var container = document.createElement("div");
	
	var entityDiv = document.createElement("div");
	this.setButtonStyle_(entityDiv);
	container.appendChild(entityDiv);
	
	this.showImg = document.createElement("img");
	this.showImg.src = "/kepek/terkep/map_show_all.gif";
	entityDiv.appendChild( this.showImg );
	
	this.hideImg = document.createElement("img");
	this.hideImg.src = "/kepek/terkep/map_hide_all.gif";
	entityDiv.appendChild( this.hideImg );
	
	map.getContainer().appendChild(container);
	return container;
}

EntityControl.prototype.getDefaultPosition = function() {
	return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(7, 7));
}

EntityControl.prototype.setButtonStyle_ = function(button) {
	button.style.textDecoration = "underline";
	button.style.color = "#0000cc";
	button.style.backgroundColor = "white";
	button.style.font = "small Arial";
	button.style.border = "1px solid black";
	button.style.padding = "2px";
	button.style.marginBottom = "3px";
	button.style.textAlign = "center";
	button.style.width = "51px";
	button.style.cursor = "pointer";
}

EntityControl.prototype.setShowClick = function(showclick) {
	if ( typeof showclick == "function" )
	GEvent.addDomListener(this.showImg, "click", showclick);
}

EntityControl.prototype.setHideClick = function(hideclick) {
	if ( typeof hideclick == "function" )
	GEvent.addDomListener(this.hideImg, "click", hideclick);
}

