//___________________ CLASS: BUILDING

function Building(b, mode) {
	this.id = b.id;
	
	//Filter attributes
	this.street = b.street;
	this.postcode = b.postcode;
	this.city = b.city;
	this.nutzungsart = b.nutzungsart;
	this.totalArea = b.totalArea;	
	this.freeArea = b.freeArea;			//Nur dieses hier verwenden. Auch in der Ausgabe nur diese Fläche anzeigen.
	this.minFreeArea = b.smallestFreeArea;
	this.lage = b.lage;
	this.verkehrsanbindung = b.verkehrsanbindung;
	
	this.lat = b.lat;
	this.lon = b.lon;

	//Additional attributes
	this.header = b.header;
	this.description = b.description;
	this.thumbUri = b.thumbUri;
	this.linkUri = b.linkUri;
	this.address = this.street + ", " + this.postcode + " " + this.city;
	this.visible;
	this.aufAnfrage;
	
	//Formatted
	this.minFreeAreaFormatted = formatNumber(this.minFreeArea);
	this.totalAreaFormatted = formatNumber(this.totalArea);
	this.freeAreaFormatted = formatNumber(this.freeArea);
	this.setFreeArea();

	//GoogleMaps relevant
	this.position = new google.maps.LatLng(this.lat, this.lon);
	this.distanceToStartPos = null;
	this.infoWindow = null;
	this.marker = new google.maps.Marker({map: map, position: this.position, icon: "fileadmin/tmpl/kgal_big_active.png"});
	this.setAufAnfrage(mode == "overview" ? false : this.freeArea > 0);
	
	//Nicht mehr nötig, seitdem Lat/Lng per JSON übergeben wird: this.updatePosition();
	
	//alert(mode == "overview" ? false : this.freeArea > 0);
	
	//InfoWindow-Bubble
	this.mode = mode;
	
	this.setInfoWindow();
	//this.addTableEntry("block");
		
	//Area stuff
	/*this.nowFreeArea = b.nowFreeArea;	//Momentan nicht verwendet.
	this.notYetFreeArea = b.notYetFreeArea;
	this.notYetDate = new Date(b.notYetDate);*/
}

Building.prototype.setVisible = function(b) {
	if (b == this.visible)
		return;
 
	if (b) {
		$("#results > tbody > tr#buildingID" + this.id + " > td > div").slideDown(400);
		this.marker.setMap(map);
								
		//MouseOver for table-rows.
		$("#results > tbody > tr#buildingID" + this.id).hover(
			function() { onMouseOverDetail(this); },
			function() { onMouseOutDetail(this); }
		);
	}
	else {
		$("#results > tbody > tr#buildingID" + this.id + " > td > div").slideUp(400);
		this.marker.setMap(null);
						
		//MouseOver for table-rows.
		$("#results > tbody > tr#buildingID" + this.id).unbind('mouseenter mouseleave');
	}

	this.visible = b;
}

Building.prototype.addTableEntry = function(display) {
	/*var notYetFreeArea = this.notYetFreeArea != 0 ? "<br />+ " + this.notYetFreeArea + "<br /><span style='font-weight: normal'>ab&nbsp;" + 
				   		 this.notYetDate.getDay() + "." +  
				   		 this.notYetDate.getMonth() + "." + 
				   		 this.notYetDate.getFullYear() + "</span>" : "";*/	
						 
	/*alert(
		this.street+"\n"+
		this.postcode+"\n"+
		this.city+"\n"+
		this.nutzungsart+"\n"+
		this.totalArea+"\n"+
		this.freeArea+"\n"+
		this.minFreeArea+"\n"+
		this.lage+"\n"+
		this.verkehrsanbindung+"\n"
		);	*/				 

	$("#results tbody").append(
	'<tr id="buildingID' + this.id + '">' + 
		'<td class="colimg"><div style="display: ' + display + '"><img src="' + this.thumbUri + '" alt="' + this.street + '" /></div></td>' + 
		'<td class="coladdr"><div style="display: ' + display + '">' + 
			'<strong>' + this.header + '</strong><br />' + 
			this.street + '<br />' + 
			this.postcode + ' ' + this.city + 
			'<div class="details">' + 
				'<p>' + this.description + '</p>' + 
				'<a href="' + this.linkUri + '">&raquo; Detailinfos</a>' + 
			'</div>' + 
		'</div></td>' + 
		'<td class="coltype"><div style="display: ' + display + '">' + this.nutzungsart + '</div></td>' + 
		'<td class="colfullarea"><div style="display: ' + display + '">' + this.totalAreaFormatted + '</div></td>' + 
		'<td class="colfreearea"><div style="display: ' + display + '"><span>' + this.freeAreaInfo + '</span>' + aufAnfrageLink + '</div></td>' + 
	'</tr>');
}

/* Nicht mehr nötig, seitdem Lat/Lng per JSON übergeben wird.

Building.prototype.updatePosition = function() {
	var me = this; //Backup "this" for callback-function.
	if (geocoder) {	
		geocoder.geocode( { 'address': this.address, 'country': geocoderCountry}, function(results, status) {
			if (status == google.maps.GeocoderStatus.OK) {
				me.position = results[0].geometry.location;
				me.marker.setPosition(me.position);
				
				buildingsInitialized++;
				if (buildingsInitialized == buildingsToInit)
					finalize();
			}
		});
    }
}*/

Building.prototype.updateDistanceToStartPos = function() {
	this.distanceToStartPos = distHaversine(this.position, startPos);
}

Building.prototype.setInfoWindow = function() {
	var imgHeight = (this.mode == "home") ? "60px" : "auto";
	var infoContent = 
		'<div style="width: 280px;"><img src="' + this.thumbUri + '" alt="this.street" style="float: left; margin-right: 10px; height: ' + imgHeight + '" />' + 
		'<strong>' + this.header + '</strong><br />' + 
		this.street + '<br />' + 
		this.postcode + ' ' + this.city + 
		'<br /><br /><a style="text-decoration: underline" href="' + this.linkUri + '">&raquo; Detailinfos</a></div>';

	switch (this.mode) {
		case "home":
		case "overview":		
			this.infoWindow = new google.maps.InfoWindow({content: infoContent});
			
			var infoWindow = this.infoWindow;
			var marker = this.marker;
			google.maps.event.addListener(this.marker, 'click', function() {
				closeAllInfoWindows();
				infoWindow.open(map, marker);
			});		
			break;
			
		case "search":
			var id = this.id;
			var me = this;
			google.maps.event.addListener(this.marker, 'mouseover', function() {
				onMouseOverDetail($("#results > tbody > tr#buildingID" + id));
			});
			google.maps.event.addListener(this.marker, 'mouseout', function() {
				onMouseOutDetail($("#results > tbody > tr#buildingID" + id));
			});
			google.maps.event.addListener(this.marker, 'click', function() {
				window.location = "/"+me.linkUri;
			});		
			break;
	}
}

Building.prototype.setFreeArea = function() {
	if (this.freeArea == 0)
		this.freeAreaInfo = aufAnfrageLink;
	else if (this.freeArea == this.minFreeArea)
		this.freeAreaInfo = this.freeAreaFormatted;
	else
		this.freeAreaInfo = this.minFreeAreaFormatted + " – " + this.freeAreaFormatted;
}

Building.prototype.setAufAnfrage = function(b) {
	if (b == this.aufAnfrage)
		return;
		
	if (b) {
		this.marker.setIcon("fileadmin/tmpl/kgal_big_inactive.png");
		this.marker.setZIndex(0);
	}
	else {
		this.marker.setIcon("fileadmin/tmpl/kgal_big_active.png");
		this.marker.setZIndex(1000);
	}
		
	this.aufAnfrage = b;
}
