
window.addEvent('domready', function() {
	
	// Allenfalls vorhandene Meldungen anzeigen
	displayFeedback();
	
	// Flash
	//swfobject.embedSWF(baseUrl + "/public/flash/version.swf","flash","100%","100%","9.0.0","",{},{wmode:"transparent"});
	
	// Ajax
	$$('form.ajaxForm').addEvent('submit', function(e) {
		new Event(e).stop();
		if(typeof(this.callback) != 'undefined') {
			var callbackFunction = this.callback.value;
			if(typeof(eval( callbackFunction )) == 'function') {
				// Callback-Funktion korrekt definiert
				callbackFunction += '();';
			} else {
				// Definierte Callback-Funktion existiert nicht
				dialogBox('critical', 'Callback', 'Die Callback-Funktion "' + callbackFunction + '()" existiert nicht.');
				callbackFunction = '';
			}
		} else {
			// Kein callback definiert
			dialogBox('critical', 'Callback', 'Es ist keine Callback-Funktion definiert.');
			var callbackFunction = '';
		}
		this.set('send', {
			onSuccess: function(result, xml) {
				//dialogBox('info', 'Ajax Success','Antwort vom Server:<p><textarea>' + result + '</textarea></p>');
				eval(result);
				eval(callbackFunction);
				displayFeedback();
			},
			onFailure: function(xhr) {
				dialogBox('critical', 'Ajax Fehler','Details:<p><textarea>' + xhr.responseText + '</textarea></p>');
			},
			onException: function(headerName, value) {
				dialogBox('critical', 'Ajax Exception','Details:<p><textarea>' + headerName + " | " + value + '</textarea></p>');
			}
		}).send();
	});
	
	// Elemente an Fensterhöhe anpassen
	var sidebarHeight = $(document).getScrollSize().y - $('header').getSize().y - $('navigation').getSize().y + 4;
	if($('main_fullWidth') != null) {
		$('main_fullWidth').set('styles', { 'height' : sidebarHeight + 'px' } );
	} else {
		$('sidebar').set('styles', { 'height' : sidebarHeight + 'px' } );
		$('main').set('styles', { 'height' : sidebarHeight + 'px' } );
	}
	
});

// Ajax-Request
function jsonRequest(controllerName, actionName, jsonData) {
	var r = new Request({
		method: 'post',
		url: baseUrl + '/' + controllerName + '/' + actionName,
		data: { 'jsonData' : jsonData },
		evalResponse: true,
		onSuccess: function(result, xml) { debug('ajaxFunction Erfolg:' + result); },
		onFailure: function(xhr) { debug('ajaxFunction Fail.'); }
	}).send();
}

// Feedback
function displayFeedback() {
	if(typeof(jsonObj['feedback']) != 'undefined') {
		while(jsonObj['feedback'].length > 0) {
			var feedback = jsonObj['feedback'].shift();
			dialogBox(feedback['type'], feedback['title'], feedback['text']);
		}
	}
}

// Dialogbox
function dialogBox(type, title, contents) {
	
	var now = new Date();
	var dialogId = 'dialogBox' + now.getTime();
	var dialogTitle = '<h1 id="dialogTitlebar' + dialogId + '"><span>' + title + '</span></h1>';
	var dialogContents = '<div class="contents" id="c">' + contents + '</div>';
	var dialogButtons = '<div class="buttons">';
	dialogButtons += '<button name="feedbackDialog" onclick="$(\'' + dialogId + '\').hide()">OK</button>';
	dialogButtons += '</div>';
	
	var d = new Element('div',{'id': dialogId, 'class' : 'dialogBox ' + type});
	d.set('html', dialogTitle + dialogContents + dialogButtons);
	d.inject('contents', 'bottom');
	d.set('styles', {
		'left' : $(document).getScroll().x + $(document).getSize().x / 2 - d.getSize().x / 2,
		'top' : $(document).getScroll().y + $(document).getSize().y / 2 - d.getSize().y / 2
	});
	d.show = function() {
		if(!Browser.Engine.trident) {
			d.fade('hide').fade('in');
		} else {
			d.fade('show');
		}
	}
	d.hide = function() {
		if(!Browser.Engine.trident) {
			this.fade('out');
		} else {
			this.fade('hide');
		}
	}
	d.show();
	
	var drag = new Drag(d,{handle:$('dialogTitlebar' + dialogId)});
}

// Debug
function debug() {
	dialogBox('info', 'debug', typeof(arguments[0]) != 'undefined' ? ('<textarea>' + arguments[0] + '</textarea>') : 'Test');
}
