/* tcms Lib */

// Hilfsfunktion
function newTcmsSite_filename(){
	document.form1.filename.value=document.form1.name.value;
	document.form1.keywords.value=document.form1.name.value;
}

// moveTcmsSite.html - Ruft die Seite editTcmsSite.html auf, und veranlasst das editieren einer Seite 'filename'
// @author Christian Asche
// @since 11.08.2005 13:58:40 
function editTcmsFile(filename){
	document.form1.action='editTcmsSite.html';
	document.getElementById('node').value=filename;
	document.form1.submit();
	return true;
}

// moveTcmsSite.html - Ruft die Seite editTcmsSite.html auf, und veranlasst das löschen einer Seite 'filename'
// @author Christian Asche
// @since 11.08.2005 13:58:40 
function deleteTcmsFile(filename){
	if(confirm('Möchten Sie diese Datei wirklich löschen?\nDies kann NICHT rückgängig gemacht werden!')){
		document.form1.action='deleteTcmsSite.html';
		document.getElementById('filename').value=filename;
		document.form1.submit();
		return true;
	}else{
		return false;
	}
}

$(document).ready(function(){
	if($('#tcmsLocationsSearchForm').children('form:first').length) {
		$('#tcmsLocationsSearchForm').children('form:first').ajaxForm({  
			        dataType: 'xml',
			        url: '/tcmsLocationsNew.html?ajax',
			        beforeSubmit: showAnimator,
			        success: show_tcms_yahoo,
			        error: showError,
			        ajaxError: showError
		});
	}

	if(typeof edit === 'undefined') {
		$('div.tcmsLocationsSubmitButton').hide(0);
	}
});


if(typeof onMarkerDragStop === 'undefined') {
	function onMarkerDragStop(event) {
		$('#longitude').val(event.latlon.x);
		$('#latitude').val(event.latlon.y);
	}
}

// tcmsSiteAdmin
$(document).ready(function(){
	oldSiteName = $('#filename').val();
	$('#weiterleitung').parent().hide(0);
});

function showWeiterleitungsFormular(ele) {
	var l_div = $(ele).parent().parent().parent();
	$.blockUI(		'<div width="80%" />'
					+'<h3>Der Dateiname wurde geändert</h3><p>Evtl. vorhandene Links verweisen daher auf das alte Ziel.</p>'
					+'<ul class="form"><li>'
					+'<label for="weiterleitung_new">Soll eine Weiterleitung eingerichtet werden?</label>'
					+'<input type="checkbox" name="weiterleitung_new" id="weiterleitung_new" />'
					+'<input type="hidden" name="weiterleitung_old" id="weiterleitung_old" value="'+oldSiteName+'" />'
					+'</li></ul></div>'); 
}

// tcmsLocationsNew
 function ask_tcms_yahoo(ele) {
		$(ele).ajaxForm({  
	        dataType: 'xml',
	        url: '/tcmsLocationsNew.html?ajax',
	        beforeSubmit: showAnimator,
	        success: show_tcms_yahoo,
	        error: showError,
	        ajaxError: showError
	    });
	    
	    return false;
}
function showError(request) {
	$("#ajax_error").append('<p>'+request.responseText+'</p>');
}

function showAnimator() {
	$.blockUI('<h1 style="padding: 5px;"><img src="/images/indicator.gif" style="vertical-align:middle"/> Bitte warten Sie einen Moment...</h1> ',  { width:'300px' });
	$('div.tcmsLocationsSubmitButton').hide();
}

function hideAnimator() {
	$.unblockUI();
}

function show_tcms_yahoo(responseXML) {
	result = {};
	try {	
		var childNodes = responseXML.documentElement.childNodes;
		for (var i = 0, child; child = childNodes[i]; i++) {
			if (child.nodeType != 1) {
				continue;
			}
			if(child.firstChild != null) {
				result[child.tagName] = child.firstChild.nodeValue;
			}
		}
	} catch(error) {
		alert('Sie müssen genauere Daten eingeben!');
		hideAnimator();
		return;
	}

	var longitude = $('#longitude');
	var latitude = $('#latitude');
	if((latitude.val().length > 0 && latitude.val() != result.latitude) || (longitude.val().length > 0 && longitude.val() != result.longitude)) {
		var check_longlat = confirm(	
								'Die neu ermittelten Werte für Longitude/Latitude '+ 
								'stimmen nicht mit den von Ihnen eingegebenen Werten überein.'+
								'\n\n Klicken Sie auf [OK] umd die neuen Werte zu übernehmen'+ 
								'\n Klicken sie auf [Abbrechen] um die alten Werte zu behalten');
										
		if(check_longlat) {
			longitude.val(result.longitude);
			latitude.val(result.latitude);
		} else {
			result.latitude = latitude.val();
			result.longitude = longitude.val();
		}
	}
	
	if(latitude.val().length == 0 && longitude.val().length == 0) {
		longitude.val(result.longitude);
		latitude.val(result.latitude);	
	}
	
	$('#precision').val(result.precision);
	
	var latlon = new LatLon(result.latitude, result.longitude);
	var mymap = new Map("mapContainer", yahoo_application_id, latlon, 3, MapViews.MAP, Languages.de_DE);
	mymap.addTool( new PanTool(), true );
	navWidget = new NavigatorWidget(); 
	mymap.addWidget(navWidget); 
	marker1 = new CustomPOIMarker('A','Informationen', result.infos, '0x0012f0', '0x88CCFF');
	marker1.setDraggable(true);
	
	mymap.addEventListener(Map.EVENT_MARKER_DRAG_STOP, onMarkerDragStop);
	mymap.addMarkerByLatLon(marker1, latlon);
	
	$('div.tcmsLocationsSubmitButton').show();
	hideAnimator();
}

function insert_tcms_yahoo() {
	showAnimator();
	var result = $('#tcmsLocationsSearchForm').find(':input').serializeArray();
	if(typeof edit != 'undefined') {
		result.push({name:"edit", value:edit});
	}
	result.push({name:"submit", value:'1'});
	
	$.post('/tcmsLocationsNew.html?submit', result, insert_tcmy_yahoo_done);

	return false;
}

function insert_tcmy_yahoo_done(data, textStatus) {
	hideAnimator();

	$('#tcmsLocationsAjaxSubmit').hide(0);
	$('#tcmsLocationsAjaxSubmit').show(2000);
	$('#tcmsLocationsAjaxSubmit').html(data);

}

$().ajaxStop($.unblockUI); 