/**
 * @author alex
 */
var tgMapWidgetManager = Class.create({
	
	className:'tgMapManager',
	anbCheck:null,
	privacyCheck:null,
	frmCode: null,
	outputDummy: null,
	output: null,

	initialize: function()
	{
		$('htmlCode').observe('click', function(e) {
			Event.element(e).select();
		});

		$('htmlCode').disabled = true;
		
		this.frmCode      = $('submitBuild');
		this.outputDummy  = this.frmCode.down('textarea.anb');
		this.output       = this.frmCode.down('textarea.code');		
		this.anbCheck     = $$('form#widgetAnb input')[0];
		this.privacyCheck = $$('form#widgetAnb input')[1];
		
		this.anbCheck.observe('click', this.changeAnbCheck.bindAsEventListener(this));
		this.privacyCheck.observe('click', this.changeAnbCheck.bindAsEventListener(this));
	},
	
	init: function()
	{
		$('preForm').observe('submit', this.checkStart.bindAsEventListener(this));
	},
	
	checkStart: function(e)
	{
		Event.stop(e);
		var address = $F('address');
		if (address != '') {
			this.locationSearch(address);
		}
	},
	
	locationSearch: function(where)
	{
		$("htmlCode").setValue('');
		$('address_ajaxIndicator').show();
		var aj = new Ajax.Request("/ajax/mapwidget/locationSearch", {
			parameters: {
				location: where,
				map: $("preForm").map.value
			},
			method: 'get',
			onComplete:function()
			{
				$('address_ajaxIndicator').hide();
				$('htmlCode').disabled = false;
			},
			onSuccess: function(originalRequest)
			{
				var json = originalRequest.responseJSON;
				if (json.error == null) {
					$('address').setValue(where);
					this.buildHTML(where);
				} else {
					if(json.error == "Multiple Locations")
					{
						tgmMain.log("too many");
						$("toCopyCode").hide();
						$('htmlCode').disabled = false;
						$("mapPreview").addClassName('errorMessageMW');
						$("mapPreview").update(json.html);
						var locationLinks = $$(".locations a");
						locationLinks.each(function(link) {
							Event.observe(link, "click", function(e){
								this.locationSearch(link.rel);
								$("toCopyCode").show();
							}.bindAsEventListener(this));
						}.bind(this));
					} else {
						tgmMain.log("no Location");
						alert('Es wurde leider kein Ort in Deutschland gefunden, der Ihrer Anfrage entspricht.');
					}
				}
			}.bind(this)
		});
	},

	buildHTML: function(where){
	tgmMain.log('buildHTML '+where);
		var aj = new Ajax.Request("/ajax/mapwidget/buildHTML", {
			parameters: {
				location: where,
				map: $("preForm").map.value
			},
			method: 'get',
			onSuccess: function(originalRequest) {
				var json = originalRequest.responseJSON;
				if (json.error == null) {
					$("mapPreview").removeClassName('errorMessageMW');
					$("mapPreview").update(json.html);
					$('htmlCode').disabled = false;
					$("htmlCode").setValue(json.html);
					$("toCopyCode").show();
					$("htmlCode").select();
				}
			}.bind(this)
		});
	},
	
	changeAnbCheck:function(e) {
		if (this.anbCheck.checked && this.privacyCheck.checked) {
			this.outputDummy.style.display = "none";
			this.output.style.display      = "block";
			var address                    = $F('address');
			if (address != '') {
				this.locationSearch(address);
			}
		} else {
			this.output.style.display      = "none";
			this.outputDummy.style.display = "block";
		}
	}
});
