
function TMapRecord( key, type, name, lat, lng ) {
	this.key = key;
	this.type = type;
	this.name = name;
	this.lat = lat;
	this.lng = lng;
}

function TLabel( position, text ) {
	if ( position instanceof GPoint )
		this.position = position;
	else throw "Unsupported location format!";
	this.text = text;
}

TLabel.prototype = new GControl();

TLabel.prototype.getDefaultPosition = function() {
	return new GControlPosition( G_ANCHOR_TOP_LEFT, new GSize( this.position.x + 13, this.position.y ) );
}

TLabel.prototype.initialize = function( map ) {
	var container = document.createElement("div");
	
	container.style.backgroundColor = "rgb(255,255,225)";
	container.style.width = "50px";
	container.style.height = "17px";
	container.style.border = "1px solid black";
	container.style.font = "small Arial";
	container.style.width = ( this.text.length ) + "ex";
	
	container.appendChild( document.createTextNode( this.text ) );
	
	return map.getContainer().appendChild( container );
} 


function TMapObjects( map ) {
	if ( ! ( map instanceof GMap2 ) )
		throw "Invalid map argument!";
	this.map = map;
	this.objects = {};
	this.parameters = {};
	this.projection = new GMercatorProjection( 19 );
}

TMapObjects.prototype.onclick = null;

TMapObjects.prototype.onmouseover = null;

TMapObjects.prototype.onmouseout = null;

TMapObjects.prototype.url = "/digitalcity/map/infoWindow.jsp";

TMapObjects.prototype.openInfoWindow = function( record ) {
	if ( ! ( record instanceof TMapRecord ) )
		throw "Incompatibile parameter type!";
	if ( ! ( record.marker instanceof GMarker ) )
		throw "No marker found!";
	var page = this.url + "?";
	try { 
		if ( this.parameters != undefined )
			for ( parameter in this.parameters )
				page += parameter + "=" + this.parameters[parameter] + "&";
		page +=  record.type + "=" + record.key;
		GDownloadUrl( page , function( text, code ) {
				if ( code == 200 )
					record.marker.openInfoWindowHtml(text);
				else
					record.marker.openInfoWindowHtml(record.name);	
			});
	} catch ( e ) {
		record.marker.openInfoWindowHtml(record.name );
	}
}

TMapObjects.prototype.addRecord = function( record, gicon ) {
	if ( ! ( record instanceof TMapRecord ) )
		throw "Incompatibile parameter type!";
	if ( ! ( gicon instanceof GIcon ) )
		throw "Incompatibile parameter type!";
	if ( this.objects[record.key] != undefined  ) 
		return false;
	var container = this;
  	var marker = new GMarker( new GLatLng( record.lat, record.lng ), { icon: gicon, clickable: true, title: record.name } );
  	record.marker = marker;
	GEvent.addListener(marker, 'click', function() {
    		if ( typeof container.onclick == "function" ) {
				container.onclick( record );
			} else { 
				if ( container.label ) container.map.removeControl( container.label );
	    		container.openInfoWindow( record );
	    	}	
		});
	GEvent.addListener(marker, 'mouseover', function() {
    		if ( typeof container.onmouseover == "function" )
				container.onmouseover( record );
		});	
	GEvent.addListener(marker, 'mouseout', function() {
    		if ( typeof container.onmouseout == "function" ) 
				container.onmouseout( record );
		});	
  	this.map.addOverlay( marker );
  	this.objects[record.key] = record;
  	return true;
}

TMapObjects.prototype.delRecord = function( key ) {
	if ( typeof key != "string" )
		throw "Incompatibile parameter type!";
  
	if ( this.objects[key] == undefined )
		return false; 

	this.map.removeOverlay( this.objects[key].marker );
	this.objects[key] = undefined;
	
	return true;
}

TMapObjects.prototype.showRecord = function( key ) {
	if ( typeof key != "string" )
		throw "Incompatibile parameter type!";
  
  	var object;
	if ( ( object = this.objects[key] ) == undefined )
		return false; 

	this.openInfoWindow( object );
	return true;
}

TMapObjects.prototype.getBounds = function() {
	var maxLat, minLat, maxLng, minLng, lat, lng, record;
	for ( key in this.objects ) {
		record = this.objects[key];
		if ( record == undefined ) continue;
		if ( maxLat == undefined ) {
			maxLat = minLat = record.lat;
			maxLng = minLng = record.lng;
		} else {
			maxLat = ( maxLat < record.lat ) ? record.lat : maxLat;
			minLat = ( minLat > record.lat ) ? record.lat : minLat;
			maxLng = ( maxLng < record.lng ) ? record.lng : maxLng;
			minLng = ( minLng > record.lng ) ? record.lng : minLng;
		}	
	}
	if ( maxLat != undefined )
		return new GLatLngBounds( new GLatLng( minLat, minLng ), new GLatLng( maxLat, maxLng ) );
	else
		return null;	
}

/*TMapObjects.prototype.getPixel = function( coords ) {
	if ( ! ( coords instanceof GLatLng ) )
		return null;
	var bounds = this.map.getBounds();
	var zoom = this.map.getZoom();
	var nw = new GLatLng( bounds.getNorthEast().lat(), bounds.getSouthWest().lng() );
	var point1 = this.projection.fromLatLngToPixel( coords, zoom );
	var point2 = this.projection.fromLatLngToPixel( nw, zoom );
	window.status = point2.x + ":" + point2.y;
	while ( point2.x > point1.x )
		point2.x -= this.projection.getWrapWidth( zoom );
	return new GPoint( point1.x - point2.x, point1.y - point2.y );
}*/



TFleckControl.prototype = new GControl();

function TFleckControl( index ) {
	this.selectedFelck = null;
	this.defaultSelected = index;
	this.onFleckClick = null;
}

TFleckControl.prototype.getDefaultPosition = function() {
	return new GControlPosition( G_ANCHOR_BOTTOM_RIGHT, new GSize( 22, 0 ) );
}

TFleckControl.prototype.getFleckIndex = function() {
	return this.selectedFleck.index;
}

TFleckControl.prototype.fleckClick = function( div ) {
	this.selectedFleck.style.border = "none";
	this.selectedFleck.style.padding = "4px";
	this.selectedFleck = div;
	this.selectedFleck.style.border = "2px groove white";
	this.selectedFleck.style.padding = "2px";
	if ( typeof this.onFleckClick == "function" ) this.onFleckClick( div.index );
}

TFleckControl.prototype.initialize = function( map ) {
	var main  = document.createElement("div");
	main.className = "panel_body";
				
	for ( i = 0; i <= 7; i++ ) {
		if ( i == 4 ) main.appendChild( document.createElement("br") );
		var div = document.createElement("div");
		div.className = "fleck" + i;
		if ( i == this.defaultSelected ) {
			div.style.border = "2px groove white";
			div.style.padding = "2px";
			this.selectedFleck = div;
		} else {
			div.style.border = "none";
			div.style.padding = "4px";
		}	
		div.index = i;
		container = this;
		div.onclick = function() { container.fleckClick( this ) };
		div.appendChild( document.createTextNode(" ") );
		main.appendChild( div );
	}

	var left = document.createElement("div");
	left.className = "panel_left";
	left.appendChild( document.createTextNode(" ") );			
				
	var right = document.createElement("div");
	right.className = "panel_right";
	right.appendChild( document.createTextNode(" ") );
	
	var div = document.createElement("div");
	div.style.width  = "125px";
	div.style.height = "70px";

	div.appendChild( left );
	div.appendChild( main );
	div.appendChild( right );

	map.getContainer().appendChild( div );
	
	return div;
}



