/**
 *	Die Klasse Place und ihre Methoden
 *
 */


/**
 * Konstruktor
 */

Place = function(id ,lat, lng, name, country, street, city, region, phoneNumbers)
{
	this.id = id; // stelle im array
	this.lat = lat;
	this.lng = lng;
	this.name = name;
	this.country = country;
	this.street = street;
	this.city = city;
	this.region = region;
	this.phoneNumbers = phoneNumbers;
}
/*
Place = function(object)
{
	this.lat = object.lat;
	this.lng = object.lng;
	this.name = object.name;
	this.country = object.country;
	this.street = object.street;
	this.city = object.city;
	this.region = object.region;
	this.phoneNumbers = object.phoneNumbers;
}
*/

/**
 * Erzeugt den HTML-Code für den Place-Marker auf der Karte
 */
Place.prototype.asMarker = function (markerId) 
{
	var id = 1;
	var tableBegin = "<table border=\"0\">";
	var titleHTML = "<p><b>" + this.name + "</b><br>";
	
	if (global.userGroup == 1){
		titleHTML += "<br> ADMIN INFO  (lat/lng " + this.lat + "/" + this.lng + "</p>";
		}
		
	var streetHTML = "<p>" + this.street + "</p>";
	var cityHTML = "<p>" + this.city + "</p></br>";

	var fcodeHTML = "";
/*	if (fcode != "") {
		fcodeHTML = "<p>" + this.country + " (" + fcode + ")</p>";
	}
*/
	var numbersHTML = "";
	if (this.phoneNumbers) {
		for (var i=0; i < this.phoneNumbers.length; i++) {
			numbersHTML += "<p>" + this.phoneNumbers[i].type + " " + this.phoneNumbers[i].number + "</p>";
		}
	}
	
	if (user != 'Guest') 
	{
		var name = this.name.replace(/'/g,"\\'");
		var linkToAdd = "<tr>"
    	+ '<td align="left" valign="middle"><a href="#" onclick=\'showAddWizard("' + this.lat + '","' + this.lng + '","' + name + '","' + this.street + '","' + this.city + '","' + this.country + '");\'>'
			+ '<img src=' + addToEF + ' /></a></td>'
			+ '<td align="left" valign="middle"><a href="#" onclick=\'showAddWizard("' + this.lat + '","' + this.lng + '","' + encodeForRequest(name) + '","' + encodeForRequest(this.street) + '","' + encodeForRequest(this.city) + '","' + encodeForRequest(this.country) + '");\'>'
			+ 'add as earthfave</a></td>'
			+ "</tr>";
	}
	else {
		var linkToAdd = "<tr>"
			+	"<td align=\"center\" valign=\"middle\"><img src=" + addToEF + " /></td>"
			+	"<td align=\"left\" valign=\"middle\"><a href=\"javascript:showLogin();\">" + global.resultlistLoginToAddAsEf + "</a></td>"
			+	"</tr>";
	}

	var zoomToEF = "<tr>"
		+	"<td align=\"center\" valign=\"middle\"><a href=\"javascript:Map.centerMarker(" + markerId + "); closeFilter();\"><img src=" + placeZoomCenterImage + " /></a></td>"
		+	"<td align=\"left\" valign=\"middle\"><a href=\"javascript:Map.centerMarker(" + markerId + "); closeFilter();\">Zoom & Center</a></td>"
		+	"</tr>";

	//var name_city = deleteHTMLTags(title) + " " + city;
	var name_city = this.name + " " + this.city;
	var quickLinks = "";

	var quickLinks = "<tr>" 
		+	"<td colspan=\"2\"><br>"
		+	"<img src=\"" + global.IMG_URL + "icons/earthfaves_icon.jpg\">";
		
	cse = global.URL + 'page.php?page_id=cse&cref=http%3A%2F%2Fwww.earthfaves.com%2Fcse%2Fcontextefs6.xml&cof=FORID%3A' + global.fordid + '&q=' + name_city;

	quickLinks += '<a id="link1" name="link1" href="javascript:openSearchWindow(\'' + cse + '\');">'
		+	"&nbsp;Find more information"
		+	"</a><br>"
		+	"<img src=\"" + global.IMG_URL + "icons/google_icon.jpg\">"
		+	"<a id=\"link2\" name=\"link2\" href=\"javascript:openSearchWindow(\'http://images.google.com/images?hl=en&q=" + name_city + "\');\">" 
		+	"&nbsp;Find images"
		+	"</a><br>"
		+	"</td>"
		+	"</tr>";

	var tableEnd = "</table>";

	return GOOGLE_STYLE + titleHTML + streetHTML + cityHTML + numbersHTML + fcodeHTML + tableBegin + linkToAdd + zoomToEF + quickLinks + tableEnd + GOOGLE_STYLE;
}


/**
 * Erzeugt einen Place-Eintrag fuer die Liste der gefundenen Places
 */
Place.prototype.asResultListEntry = function (markerId) 
{

	//info("Erzeuge Listen-Eintrag für Place mit ID " + markerId);
	
	// OnClick-Methode + Beschreibung, wenn der Benutzer auf das Bookmark-Icon klickt
//	global.action = "showAddWizard('" + lat + "', '" + lng + "', '" + encodeForRequest(description) + "', '" 

	/*var action = "showAddWizard('" + this.lat + "', '" + this.lng + "', '" + encodeForRequest(this.description) + "', '" 
			+ encodeForRequest(this.street) + "', '" + encodeForRequest(this.city) + "', '" + encodeForRequest(this.country) + "');";*/
	var action = "showAddEarthFaveOverlay('" + this.id + "', '" + this.lat + "', '" + this.lng +"'); jumpAnchor() ";
	if (user != "Guest" && user != "guest") 
	{
//		var addEFOnClick = global.action;
		var addEFOnClick = action;
		var addEFTitle = 'Turn this place into an earthfave';
		global.action = "";// Diese globale Aktion wird mit dem Onclick oben ausgefuehrt, brauchen wir also nicht nach dem Login auszufuehren
	}
	else if (user == "Guest" || user == "guest")
	{
		var addEFOnClick = 'showLogin();';
		var addEFTitle = 'Login to add turn this place into an earthfave';
	}

  // Die Beschreibung Land-Ort-Straße setzen
	var countryCityStreet = "";
	if (this.country != "") {
		countryCityStreet += this.country;
	}
	if (this.city != "") {
		if (countryCityStreet.length > 0) {
			countryCityStreet += ' > ';
		}
		countryCityStreet += this.city;
	}
	if (this.street != "") {
		if (countryCityStreet.length > 0) {
			countryCityStreet += ' > ';
		}
		countryCityStreet += this.street;
	}
//	if (fcode != "") {
//		countryCityStreet += " (" + fcode + ")";
//	}

	var	r =	'<tr class="result-list-entry" id="trPRLE_' + this.id + '">'
		+	'	<td class="image-place">'
		+	' 		<img alt="image" src="' + global.IMG_URL_TEXTLANGUAGE  + 'icons/add-as-ef-gross.gif" title="'+  addEFTitle + '" onclick="' + addEFOnClick + '" />'
		+	'	</td>'
	 	+	'	<td class="category-icon">'
		//+ ' 		<img src="img/icons/restaurants-gray-large.gif"/>'
		+ 	' 	</td>'
		+ 	'	<td class="description">'
		+	'		<h1>' + this.name + '</h1>'
		+	'		<p class="path">' + countryCityStreet  + '</p>'
		+	'	</td>'
		+ 	'	<td class="actions">'
		+ 	'		<ul>'
		+	'			<li onmouseover="myTip(\'' + addEFTitle + '\');" onmouseout="UnTip();" class="action-label-add-ef" title="'+  addEFTitle + '" onclick="' + addEFOnClick + '"></li>'
		+ 	'			<li onmouseover="myTip(\'Center this place on the map\');" onmouseout="UnTip();" class="action-label-place_show-map" title="Center this place on the map" onclick="Map.show(); Map.centerMarker(' + this.lat+',' + this.lng + '); markDiv(' + markerId + '); closeFilter();"></li>';
		+	'		</ul>'
		+	'	</td>'
		+	'</tr>' 
		+	'<tr>'
		+	'	<td colspan="5">'
		+	'		<div class="result-status" id="divPRS_' + this.id + '">&nbsp</div>'
		+ 	'	</td>'
		+   '</tr>';
		
  return r;
}


/**
 * Erzeugt einen Place-Eintrag fuer die Liste der gefundenen Places im AddWizard
 */
Place.prototype.asAddWizardResultListEntry = function (index) 
{
	//alert("hallo");
	if(this.name == global.YOUR_MARKER){ // marker hat keine adresse, hier werden die trennpfeile zwischen country, city und street entfernt
		var result = '<tr style="height:50px; background-color: white;">';
		result += '<td style="width:100px;" align="center"> <img src="' + global.IMG_URL_NONTEXT + 'icons/placeToDisEf.jpg" alt="Place" /></td>';
		result += '<td style="width:50px;" align="center"> &nbsp; </td>';
		result += '<td style="width:450px;"> <h3 style="color:#6DC067;">' + this.name + '</h1>';
		result += '<td style="width:150px;" align="center"> <img src="' + global.IMG_URL_TEXTLANGUAGE + '/buttons/beTheDis.jpg" alt="Be the Discoverer"';
		result += ' onmouseover="this.style.cursor=\'pointer\'; myTip(\'Be the Discoverer\');" onmouseout="this.style.cursor=\'auto\'; UnTip();"';
		result += ' onClick="changePageinOverlay(1, 3,\'Index\', ' + index + ', \'no\',\'no\');" /> </td>';
		result += '</tr><tr style="display:block; height:2px;"> </tr>';	
		return result;
	}
	else{
		var result = '<tr style="height:50px; background-color: white;">';
		result += '<td style="width:100px;" align="center"> <img src="' + global.IMG_URL_NONTEXT + 'icons/placeToDisEf.jpg" alt="Place" /></td>';
		result += '<td style="width:50px;" align="center"> &nbsp; </td>';
		result += '<td style="width:450px;"> <h3 style="color:#6DC067;">' + this.name + '</h1>';
		result += '<span style="font-size:10px; color:#B0A9A7"> ' + this.country + ' > ';
		result += this.city + ' > ' + this.street + ' &nbsp;</span> </td>';
		result += '<td style="width:150px;" align="center"> <img src="' + global.IMG_URL_TEXTLANGUAGE + '/buttons/beTheDis.jpg" alt="Be the Discoverer"';
		result += ' onmouseover="this.style.cursor=\'pointer\'; myTip(\'Be the Discoverer\');" onmouseout="this.style.cursor=\'auto\'; UnTip();"';
		result += ' onClick="changePageinOverlay(1, 3,\'Index\', ' + index + ', \'no\',\'no\');" /> </td>';
		result += '</tr><tr style="display:block; height:2px;"> </tr>';	
		return result;
	}
}




/**
 * Gibt den Typ zurück, wird z.B. für Namenskonvention bei den Icon auf der Karte verwendet
 */
Place.prototype.getType = function ()
{
	return "place";
}


/**
 * Vergleiche zwei Entities (Places oder earthfaves)
 */
Place.prototype.equal = function(entity)
{
//	info(this.name + " =?= " + entity.name);
//	if (this.lat - entity.lat < 0.01 && this.lat - entity.lat < 0.01) {
//		info ("===");
//		return true;
//	}
	if (this.name == entity.name) {
//		info ("===");
		return true;
	}

//	info ("!==");
		return false;
}


//Arno Place Overlay
Place.prototype.asPlaceEntry = function (markerId)
{
	var def = "";
	var undef = "";
	var z; // Fuer die Rueckgabe des HTML-Codes
	var classname = "";
	var hasBookedDisc = 0;
	
	var li_bookmark = "";
	var bookmarkOnClick = "";
	var bookmarkTitle = "";
	var prozent =  0;
	var geobookmarker = "";
	var stati;
	var target = "_self";
	var catLink = "";
	var placeLocation = "";
	var media = "";
	var categoryImage = "";


	for( prop in this ){
		if( prop == "asMarker" ){ break; } 
		if( this[prop] == undefined ){ undef += prop + ' value :' + this[prop]+ ' '; }
		else{ def += prop + ' value :' + this[prop]+ ' ';}
	}

	/*var arrayKat =  new Array()
	arrayKat["0"] = "";// gibt es nicht !!!
	arrayKat["1"] = "hotels-gray-large.gif";
	arrayKat["17"] = "restaurants-gray-large.gif";
	arrayKat["96"] = "nature-gray-large.gif";
	arrayKat["136"] = "culture-gray-large.gif";
	arrayKat["185"] = "shopping-gray-large.gif";
	arrayKat["257"] = "nightlife-gray-large.gif";
	arrayKat["207"] = "sports-gray-large.gif";

	var arrayKatName =  new Array();
	arrayKatName["0"] = "no_category";
	arrayKatName["1"] = "hotel";
	arrayKatName["17"] = "restaurant";
	arrayKatName["96"] = "nature";
	arrayKatName["136"] = "culture";
	arrayKatName["185"] = "shopping";
	arrayKatName["257"] = "nightlife";
	arrayKatName["207"] = "sports";*/

	if (get.page_id == "access_extern"){
	} 
	else {

		placeLocation = this.country ;
		if ( this.city != "" ) { placeLocation += ' > ' + this.city; }
		if ( this.street != "" ) { placeLocation += ' > ' + this.street; }
	}

	switch (this.relationship)
	{
		case 0: //user is not loged in
			if (get.page_id != "access_extern"){
				bookmarkOnClick = 'showLogin();';
				bookmarkTitle = 'Login to add to your list of earthfaves';
				li_bookmark = '<li id="li-labAdd2" style="height:24px; width:24px; padding-top:5px; magin:0px;"onmouseover="myTip(\'' + bookmarkTitle + '\');" onmouseout="UnTip();" title="' + bookmarkTitle + '" onclick= "setPlaceOverlayVisible(false); setPlaceOverlayInvisibleTimer(); ' + bookmarkOnClick + '"></li>';
			}
			break;
		case 1: //user is loged in
			if (get.page_id != "access_extern"){
				bookmarkOnClick = 'showAddEarthFaveOverlay(' + this.id + ', \'no\', \'no\')';
				bookmarkTitle = 'Add to your list of earthfaves';
				li_bookmark = '<li id="li-labAdd2" style="height:24px; width:24px; padding-top:5px; margin:0px;"onmouseover="myTip(\'' + bookmarkTitle + '\');" onmouseout="UnTip();"  title="' + bookmarkTitle + '" onclick= "setPlaceOverlayVisible(false); setPlaceOverlayInvisibleTimer(); ' + bookmarkOnClick + '"></li>';
				global.action = "";
			}
			break;
	/*	case 2:
			classname = "bookmarked";
			break; //user us booker
		case 3:
			classname = "discovered";
			break; // user ist nur DISC
		case 4:
			classname = "owner";
			break; // nur owner
		case 5:
			classname = "discovered";
			break; // user ist DISC and Owner
		case 7:
			classname = "discovered";
			break; // nur DISC und es gibt booker
		case 8:
			classname = "discovered";
			break;  // Owner und DISC und es gibt booker*/
	
	}
	/*
	var statiText = 'Discovered by';
	if( this.status == "undiscovered" ) { var statiText = 'Undiscovered by'; }
	stati = '(' + this.status + ')';
	stati = '';

	// Show a Cat-Icon if the categorie is defined
	if (this.catId0) {categoryImage =  '<img style="padding-top:10px; height:25px; width:25px;" onmouseover="myTip(\'' + arrayKatName[this.catId0] + '\');" onmouseout="UnTip();" style="width:20px; height:20px;" src="' + global.IMG_URL_NONTEXT + 'icons/' + arrayKat[this.catId0] + '"/>';	}
	if (this.catId1) {categoryImage += '<img style="padding-top:10px; height:25px; width:25px;" onmouseover="myTip(\'' + arrayKatName[this.catId1] + '\');" onmouseout="UnTip();" style="width:20px; height:20px;" src="' + global.IMG_URL_NONTEXT + 'icons/' + arrayKat[this.catId1] + '"/>';	}
	if (this.catId2) {categoryImage += '<img style="padding-top:10px; height:25px; width:25px;" onmouseover="myTip(\'' + arrayKatName[this.catId2] + '\');" onmouseout="UnTip();" style="width:20px; height:20px;" src="' + global.IMG_URL_NONTEXT + 'icons/' + arrayKat[this.catId2] + '"/>';	}
	if (this.catId3) {categoryImage += '<img style="padding-top:10px; height:25px; width:25px;" onmouseover="myTip(\'' + arrayKatName[this.catId3] + '\');" onmouseout="UnTip();" style="width:20px; height:20px;" src="' + global.IMG_URL_NONTEXT + 'icons/' + arrayKat[this.catId3] + '"/>';	}

*/
	var liShowEf = '<li id="li-addPlace"; style="height:20px; width:20px; padding-top:5px;" onmouseover="myTip(\'Turn this place into an earthfave\');" onmouseout="UnTip();" title="Turn this place into an earthfave"';
	//Arno: keine ahnung, wie es in der if-schleife heissen muss
	if (get.page_id == "access_extern") {
		liShowEf += ' onclick="onclick="setPlaceOverlayVisible(false); setPlaceOverlayInvisibleTimer(); showAddEarthFaveOverlay(\'' + this.id + '\',\'' + this.lat + '\',\'' + this.lng +'\'); jumpAnchor()"">';
	}
	else {
		if(global.user_id == "1") {
		liShowEf += 'onclick="showLogin(); setPlaceOverlayVisible(false); setPlaceOverlayInvisibleTimer()"/>';
		}
		else {
		liShowEf += ' onclick="setPlaceOverlayVisible(false); setPlaceOverlayInvisibleTimer(); showAddEarthFaveOverlay(\'' + this.id + '\',\'' + this.lat + '\',\'' + this.lng +'\'); jumpAnchor()"/>';
		}
	}
	var liCenterMap = '<li id="li-centerPlace"style="height:20px; width:20px; magin:0px; padding-top:5px;" onmouseover="myTip(\'Show this place centered on the map\');"'+markerId+' '+this.id+');" onmouseout="UnTip();"  title="Show this place centered on the map"';
		  liCenterMap +=' onclick="setPlaceOverlayVisible(false); setPlaceOverlayInvisibleTimer(); Map.show(); Map.centerMarker(' + this.lat + ', ' + this.lng+'); markDiv(' + markerId + '); closeFilter(); " />';
		  
	var trStart =	'<tr id="trRLE_' + this.id + '">';
	
	var efName = this.name;
	if(efName.length > 42){
		efName = efName.substring(0,42) + "...";
	}
	
	var myTip = this.name;
	var tdImage = '<td style="height:80px; overflow:hidden; width:80px; padding:0px; magin:0px;">'
		+		'<a onmouseover="myTip(\'' + myTip + '\');" onmouseout="UnTip();" href="' + global.URL + this.city + '/' +  this.id + '/' + this.name +'\'" target="' + target + '">'
		+		'<img style="height:80px; width:80px; padding-top:0px; margin:0px;" name="efBild_' + this.id + '" alt="'+ this.name + '" src="' + this.media + '"/>'
		+		'</a>'
		+		'</td>';
	//var tdDescText= '<td class="description-text"></td>';
	var tdCatIcon = '<td rowspan="2" style="height:100px; width:20px; magin:0px;">&nbsp;' + categoryImage	+ '</td>';	
  var liDeleteOrDiscover = "";
		if(get.page_id != "access_extern"){
			if(this.relationship == 2 ){//der bookmarker kann den Bookmark loeschen
				liDeleteOrDiscover = '<li id="li-delBookmark"; style="height:20px; width:20px; magin:0px; padding-top:5px;" onmouseover="myTip(\'Remove from your list of earthfaves?\');"';
				liDeleteOrDiscover +='onmouseout="UnTip();" title="Remove from your list of earthfaves?" onclick="showDeleteEfForm(' + global.user_id + ' , ' + this.id + ',\'book\');"></li>';
				if(this.status == "undiscovered"){
					liDeleteOrDiscover += '<li id="li-labAdd"; style="height:20px; width:20px; magin:0px; padding-top:5px;" onmouseover="myTip(\'I want become discoverer\');" onmouseout="UnTip();"';
					liDeleteOrDiscover +='  title="I want become discoverer" onclick="discoverUndiscoveredEf(' + global.user_id + ' , ' + this.id + ');"></li>';
				}
			}
			else if(this.relationship == 3 || this.relationship == 5  || this.relationship == 7 || this.relationship == 8)
			{	
			//	info("earthfaves.js Earthfave.prototype.asResultListEntry -- marker 5");		
				var 	recommender = "";
				if ( this.status != "undiscovered" ){
					if( this.numberOfBooker > 0 ){ recommender =' (' + this.numberOfBooker + ' recommender)'; }
					liDeleteOrDiscover += '<li id="li-delBookmark2"; style="height:20px; width:20px; magin:0px; padding-top:5px;" onmouseover="myTip(\'Remove from your list of earthfaves?\');" onmouseout="UnTip();"';
					liDeleteOrDiscover += ' title="Remove from your list of earthfaves?' + recommender + '" onclick="showDeleteEfForm(' + global.user_id + ' , ' + this.id + ',\'disc\');"></li>';
				}
				else {
					liDeleteOrDiscover += '<li id="li-labAdd2"; style="height:20px; width:20px; magin:0px; padding-top:5px;" onmouseover="myTip(\'Become discoverer again\');" onmouseout="UnTip();"';
					liDeleteOrDiscover += 'title="Become discoverer again" onclick="discoverUndiscoveredEf(' + global.user_id + ' , ' + this.id + ');"></li>';
				}
			}
		}
	var tdAction = '<td style="width: 0px; height:140px;">'
  	+ '<ul>'
  	+	liShowEf 			
		+	liCenterMap
		+ liDeleteOrDiscover
		+	li_bookmark
 		+ '</ul>'
		+	'</td>';


			if( this.relationship != 0 ){
			
				var allLove = parseInt(this.numberBookmarkingFriends) + parseInt(this.numberBookmarkingFF) + parseInt(this.numberBookmarkingOthers);
				if(allLove > 0 || this.relationship == 3 || this.relationship == 2){
				}
		}


	r =	"<tr>"
	+		'<td rowspan=4 style="height:130px; width:80px; vertical-align:top;" >'
	+		'<a onmouseover="myTip(\'' + myTip + '\');" onmouseout="UnTip();" >'
	if(global.user_id=="1") {
	r +=		'<img onclick="showLogin(); setPlaceOverlayVisible(false); setPlaceOverlayInvisibleTimer();" style="height:80px; width:80px;" name="efBild_' + this.id + '" alt="'+ this.name + '" src="http://www.earthfaves.com/dev12/img_sort/img-text/en/icons/add-as-ef-gross.gif"/>'
	}
	else {
	r +=		'<img onclick="setPlaceOverlayVisible(false); setPlaceOverlayInvisibleTimer(); showAddEarthFaveOverlay(\'' + this.id + '\',\'' + this.lat + '\',\'' + this.lng +'\'); jumpAnchor()" style="height:80px; width:80px;" name="efBild_' + this.id + '" alt="'+ this.name + '" src="http://www.earthfaves.com/dev12/img_sort/img-text/en/icons/add-as-ef-gross.gif"/>'
	}
	r+=		'</a>'
	+ categoryImage
	+	'</td>'
	+	"</tr>"
	+	"<tr>"
	+		'<td colspan=3 style="height:30px; width:250px; vertical-align:top;"'
	+		'<h1 style="margin:0px; position:top; overflow:hidden; height:18px; padding-top:2px; color: #3DBB4E;text-decoration: none; text-underline: none; font-size: 14px; width:205px;">'
    + this.name
	+ 		'</h1>'
	+ '<p class="path" style="font-size:10px;">' + placeLocation  + '</p>'
	+	"</td>"
	+		'<td rowspan=3 style="height:130px; width:20px; vertical-align:top;">'
	  	+ '<ul>'
  	+	liShowEf 			
		+	liCenterMap
		+ liDeleteOrDiscover
		+	li_bookmark
 		+ '</ul>'
	+'</td>'
	+	"</tr>"
	+	"<tr>"
	+		'<td style="height:70px; overflow:hidden;">'
	+'</td>'
	+	"</tr>"
	return r;
}