/** 
 * @author Andreas Bethge
 * @copyright 2009, Andreas Bethge | it consulting
 */

jQuery(document).ready(
	function(){
		jQuery("#dlgPhoto").hide();
		jQuery("#dlgPhoto").css("background-color", "#000000");
		// Eingabefocus setzen
		jQuery(".Focus").focus();
	}
);

var com = new AjaxJqueryCom(1);


function showImage(id){
	imgHtml  = '<div class="DiaFrame">';
	imgHtml += '<div class="DiaPane">';
	imgHtml += '<div class="DiaCtrl">';
	imgHtml += '<img onclick="showNeighborImage(0, ' + id + ');" src="/projects/' + window.location.hostname + '/img/arrowLeft.png" alt="ZurÃ¼ck" title="ZurÃ¼ck" />';
	imgHtml += '<img onclick="hideImage();" src="/projects/' + window.location.hostname + '/img/close.png" alt="Beenden" title="Beenden" />';
	imgHtml += '<img onclick="showNeighborImage(1, ' + id + ');" src="/projects/' + window.location.hostname + '/img/arrowRight.png" alt="VorwÃ¤rts" title="VorwÃ¤rts" />';
	imgHtml += '</div>';
	imgHtml += '<div class="Dia"><img src="/global/show.image.php?id=' + id + '&amp;x=1000&amp;y=600" alt="" /></div>';
	imgHtml += "</div>";
	imgHtml += "</div>";
	
	jQuery("#dlgPhoto").css("height", jQuery("html").scrollTop() + 2000);
	jQuery("#dlgPhotoFrame").css("margin-top", jQuery("html").scrollTop());
	jQuery("#dlgPhotoFrame").html(imgHtml);
	/*jQuery("#dlgPhotoFrame").css("width", "900px");*/
	jQuery("#dlgPhoto").show();
}

var requestKey = 1;

function showNeighborImage(prevNext, id){
	com.setUrl("/projects/" + window.location.hostname + "/getAjaxResponse.php");
	com.sendAjaxRequest("getNeighborImageId", {"prevNext": prevNext, "id": id}, "post", getNeighborImageIdCallback);
}

function getNeighborImageIdCallback(data, textStatus){
	//alert(data + " --- " + data.data.id);
	showImage(data.data.id);
}

function hideImage(){
	jQuery("#dlgPhoto").hide();
}

// Karte vergrÃ¶ÃŸern/verkleinern
function blowUpMap(){
	if(jQuery("#rp1").attr("class") == "MapSmall"){
		jQuery("#rp1").attr("class", "MapBig");
	}else{
		jQuery("#rp1").attr("class", "MapSmall");
	}
}

// Favoriten hinzufÃ¼gen
function addToFavorites(id){
	com.setUrl("/projects/" + window.location.hostname + "/getAjaxResponse.php");
	com.sendAjaxRequest("addToFavorites", {"id": id}, "post", addToFavoritesCallback);
}
function addToFavoritesCallback(data, textStatus){
	jQuery("#favorites").html(data.data);
}

// Favorite lÃ¶schen
function removeFromFavorites(id, name){
	if(confirm("'" + name + "' aus Ihren Favoriten entfernen?")){
		com.setUrl("/projects/" + window.location.hostname + "/getAjaxResponse.php");
		com.sendAjaxRequest("removeFromFavorites", {"id": id}, "post", removeFromFavoritesCallback);
	}
}
function removeFromFavoritesCallback(data, textStatus){
	jQuery("#favorites").html(data.data);
}

// zur Registrierungs-Seite wechseln?

function showRegistrationPage(){
	if(confirm("Diese Funktion steht nur angemeldeten Benutzern zur VerfÃ¼gung.\nMÃ¶chten Sie sich jetzt bei xtreks anmelden?")){
		window.location.href = "/projects/" + window.location.hostname + "/dialogs/showRouteEditor.php";
	}
}


// Weitere Reise-Infos hinzufÃ¼gen
function addTravellingInfosDlg(routeId){
	var url = prompt("Geben Sie hier den Link der Website ein.", "");
	if(url.length > 0){
		com.setUrl("/projects/" + window.location.hostname + "/getAjaxResponse.php");
		com.sendAjaxRequest("addTravellingInfos", {"url": url, "routeId": routeId}, "post", updateTravellingInfosCallback);
	}
}
function removeFromTravellingInfos(id, name, routeId){
	if(confirm("'" + name + "' aus Ihren Reise-Infos entfernen?")){
		com.setUrl("/projects/" + window.location.hostname + "/getAjaxResponse.php");
		com.sendAjaxRequest("removeFromTravellingInfos", {"id": id, "routeId": routeId}, "post", updateTravellingInfosCallback);
	}	
}
function updateTravellingInfosCallback(data, textStatus){
	jQuery("#myTravellingInfoList").html(data.data);
}


// Nachricht an Autor
function showMsgToAuthorDlg(ctrlId, authorId, routeId){
	var html = "";
	hideDlg("commentDlg_1");
	hideDlg("commentDlg_2");

	html  = "<p>Mit dem Absenden Ihrer Nachricht wird dem EmfÃ¤nger Ihre E-Mail-Adresse bekanntgegeben!</p>";
	html += "<p>Ihre Nachricht:</p>";
	html += '<textarea name="msg_' + routeId + '" id="msg_' + routeId + '" type="text"></textarea>';
	html += "<br class='c' />";
	html += "<div class='InputButtonRow'>";
	html += "<input class='Button' type='button' id='commentOk' value='Senden' onclick='sendMsgToAuthor(\"" + ctrlId + "\", " + authorId + ", " + routeId + ");'/>";
	html += "<input class='Button' type='button' id='commentCancel' value='Abbrechen' onclick='hideDlg(\"" + ctrlId + "\");'/>";
	html += "</div>";
	html += "<br class='c' />";
	jQuery("#" + ctrlId).html(html);
}

function sendMsgToAuthor(ctrlId, authorId, routeId){
	//alert("sendMsgToAuthor(" + autorId + ", " + routeId +")");
	com.setUrl("/projects/" + window.location.hostname + "/getAjaxResponse.php");
	com.sendAjaxRequest("sendMsgToAuthor", {"authorId": authorId, "routeId": routeId, "msg": jQuery("#msg_" + routeId).attr("value")}, "post", sendMsgToAuthorCallback);
	jQuery("#" + ctrlId).html('');
}

function sendMsgToAuthorCallback(data, textStatus){
	//alert("Message vom Server verarbeitet!\n" + data.errors);
}

// Kommentar hinzufÃ¼gen

function showCommentDlg(ctrlId, userId, routeId){
	var html = "";
	hideDlg("commentDlg_1");
	hideDlg("commentDlg_2");
	/*
	html += "Kommentar:";
	html += "<textarea id='commentText' type='text'></textarea>";
	*/
	html += "<p>Wie gefiel Ihnen diesen Beitrag? Vergeben Sie 1-5 Sterne.</p>";
	html += "<input class='Rating' type='radio' name='rating' value='1' /><span class='Rating'>1 Stern</span>";
	html += "<input class='Rating' type='radio' name='rating' value='2' /><span class='Rating'>2 Sterne</span>";
	html += "<input class='Rating' type='radio' name='rating' value='3' /><span class='Rating'>3 Sterne</span>";
	html += "<input class='Rating' type='radio' name='rating' value='4' /><span class='Rating'>4 Sterne</span>";
	html += "<input class='Rating' type='radio' name='rating' value='5' /><span class='Rating'>5 Sterne</span>";
	
	html += "<br class='c' />";
	html += "<div class='InputButtonRow'>";
	html += "<input class='Button' type='button' id='commentOk' value='Senden' onclick='addComment(\"" + ctrlId + "\", " + userId + ", " + routeId + ", " + "jQuery(\".Rating:checked\").val()" + ");'/>";
	html += "<input class='Button' type='button' id='commentCancel' value='Abbrechen' onclick='hideDlg(\"" + ctrlId + "\");'/>";
	html += "</div>";
	html += "<br class='c' />";
	jQuery("#" + ctrlId).html(html);
}
function addComment(ctrlId, userId, routeId, rating){
	if(!rating){
		rating = -1;
	}
	com.setUrl("/projects/" + window.location.hostname + "/getAjaxResponse.php");
	com.sendAjaxRequest("addComment", {"userId": userId, "routeId": routeId, "comment": jQuery("#commentText").attr("value"), "rating": rating}, "post", addCommentCallback);
	jQuery("#" + ctrlId).html('');
}

function addCommentCallback(){
}

function hideDlg(ctrlId){
	jQuery("#" + ctrlId).html('');
}



// Weitere Routenpunkt-Infos anzeigen/hinzufÃ¼gen
function showMoreRoutepointInfos(id){
	com.setUrl("/projects/" + window.location.hostname + "/getAjaxResponse.php");
	com.sendAjaxRequest("showMoreRoutepointInfos", {"routepointId": id}, "post", showMoreRoutepointInfosCallback);	
}
function showMoreRoutepointInfosCallback(data, textStatus){
	var html = jQuery("#moreRpInfos_" + data.data.routepointId).html();
	if(data.data.html.length > 0){
		if(html.length > 0){
			jQuery("#moreRpInfos_" + data.data.routepointId).html("");
		}else{
			jQuery("#moreRpInfos_" + data.data.routepointId).html(data.data.html);
		}
	}
}

function showMoreRoutepointInfosDlg(userId, routeId, routepointId){
	
	showMoreRoutepointInfos(routepointId);
	
	var html = "";

	html += '<div class="InputElementSite">';
	html += '	<div>Titel:</div>';
	html += '	<input name="rpTitle_' + routepointId + '" id="rpTitle_' + routepointId + '" type="text" />';
	html += '	<br class="c" />';	
	html += '</div>';

	html += '<div class="InputElementSite">';
	html += '	<div>Beschreibung:</div>';
	html += '	<textarea name="rpDescription_' + routepointId + '" id="rpDescription_' + routepointId + '" type="text"></textarea>';
	html += '	<br class="c" />';	
	html += '</div>';
	/*
	 * Ein Bild-Upload ist an dieser Stelle nicht sinnvoll, da
	 * dazu ein Routenpunkt in der DB gespeichert sein muss. Die
	 * Speicherung erfolgt aber erst, wenn der Kommentator den 
	 * Absenden-Button drÃ¼ckt. Vorher temporÃ¤r einen Routenpunkt
	 * zum Zweck der Kommentierung in der DB zu speichern ist auch
	 * nicht sinnvoll. Dieser mÃ¼sste gelÃ¶scht werden, wenn der
	 * Kommentator auf "Abbrechen" klickt. Allerdings kann nicht
	 * gewÃ¤hrleistet werden, das der Kommentator das tut, falls 
	 * er doch keinen Kommentar verfassen will! 
	 * Der Kommentator muss daher die MÃ¶glichkeit haben, seine 
	 * Kommentare im regulÃ¤ren Editor nachzubearbeiten.
	 */
	html += '<div class="InputButtonRow" style="margin-bottom: 10px;">';
	html += '<input class="Button" type="button" id="commentOk" value="Senden" onclick="addMoreRoutepointInfos(' + userId + ', ' + routepointId + ');"/>';
	html += '<input class="Button" type="button" id="commentCancel" value="Abbrechen" onclick="hideMoreRoutepointsInfosDlg(' + routepointId + ');"/>';
	html += '</div>';	
	html += '<br class="c" />';
	
	jQuery("#moreRoutepointInfosDlg_" + routepointId).html("<div>" + html + "</div>");
}
function hideMoreRoutepointsInfosDlg(routepointId){
	jQuery("#moreRoutepointInfosDlg_" + routepointId).html("");
}
function addMoreRoutepointInfos(userId, routepointId){
	var rp = new Routepoint();
	rp.id = -1;
	rp.title = jQuery("#rpTitle_" + routepointId).val();
	rp.description = jQuery("#rpDescription_" + routepointId).val();
	hideMoreRoutepointsInfosDlg(routepointId);
	com.setUrl("/projects/" + window.location.hostname + "/getAjaxResponse.php");
	com.sendAjaxRequest("addMoreRoutepointInfos", {"userId": userId, "routepointId": routepointId, "newRp": rp}, "post", addMoreRoutepointInfosCallback);
}
function addMoreRoutepointInfosCallback(data, textStatus){
	//alert(data.data.routepointId);
	showMoreRoutepointInfos(data.data.routepointId);
}

// Registrierung und Passwort-Vergessen-Dialog

function showRegistrationDlg(){
	alert("Nicht verfügbar!");
	//window.open("/projects/" + window.location.hostname + "/dialogs/registration/showRegistrationDlg.php", "registrationDlg", "width=600,height=600,left=100,top=100");
}

function showNewPasswdDlg(){
	window.open("/projects/" + window.location.hostname + "/dialogs/registration/showNewPasswdDlg.php", "newPasswdDlg", "width=600,height=300,left=100,top=100");
}

function showPersonalDataDlg(){
	// ExtraDialog ??? window.open("/projects/" + window.location.hostname + "/dialogs/showPersonalDataDlg.php", "personalDataDlg", "width=600,height=600,left=100,top=100");
	window.open("/projects/" + window.location.hostname + "/dialogs/showPersonalDataDlg.php", "personalDataDlg", "width=600,height=550,left=100,top=100");
}
