var oFCKeditor;
var oEditor;

window.onbeforeunload = function()
{
	/*if(oEditor.Status == 2)
	{
		//alert('onbeforeunload');
		//confirm('Achtung! Bitte speichern Sie Ihre Änderungen, da sie sonst verloren gehen!');
	}*/
}

function doSave()
{
	save_content();
	return false; //this disables default action (submitting the form)
}

function FCKeditor_OnComplete(editorInstance)
{
	// API initialisieren
	oEditor = FCKeditorAPI.GetInstance('editor');
	// Funktion definieren, die bei Klick auf Save-Button ausgeloest wird
	editorInstance.LinkedField.form.onsubmit = doSave;
	// Blur-Funktion initialisieren
	//editorInstance.Events.AttachEvent('OnBlur', FCKeditor_OnBlur);
	
	
	// ActivityIndicator manuell entfernen, da er beim Laden des Editors mit den prototype-Responders nicht funktioniert
	$('system_working').remove();
}

function FCKeditor_OnBlur(editorInstance)
{
	if(oEditor.Status == 2)
	{
		var savecheck	=	confirm('Achtung! Bitte speichern Sie Ihre Änderungen, da sie sonst verloren gehen!');

		if(savecheck	==	true)
		{
			return false;
		}
		else
		{
			return true;
		}
	}
}

function load_editor(hash)
{
	var hash_ar	=	new Array();
	hash_ar	=	params_from_hash();
	main = hash_ar[0];
	sub = hash_ar[1];
	var le	=	new Ajax
				(
					'inc/load_editor.php?table=' + main + '&row=' + sub,
					{
						method: 'post',
						update: $('content_update'),
						onRequest: Nav.show_loader,
						onComplete: function()
						{
							oFCKeditor = new FCKeditor('editor');
							oFCKeditor.BasePath = "js/fckeditor/";
							oFCKeditor.ToolbarSet = 'Ilcl';
							oFCKeditor.Height = 600;
							oFCKeditor.ReplaceTextarea();
						}
					}
				).request();
}


function save_content_exit(id)
{
	var cont	=	oEditor.getHTML;
	new Ajax.Updater('right', '../../inc/save_content.php?id=1&cont=' + escape(cont), { method: 'post' });	
}

function save_content()
{
	var hash_ar	=	params_from_hash();
	var cont	=	oEditor.GetHTML();
	// Url eruieren - es gab Probleme mit relativem Pfad: waren nicht glecih bei IE und Firefox
	var url		=	window.location.pathname;
	url			=	url.split('index.php')[0];
	
	var sc	=	new Ajax
	(
		url + "inc/save_content.php?table=" + hash_ar[0] + "&row=" + hash_ar[1],
		{
			method: 'post',
			/*onRequest: Nav.show_loader,*/
			onSuccess: function()
			{
				show_feedback('<img src="images/tick_animated.gif" />', 'feedback');
				
				//print_obj(this.transport);
			},
			onFailure: function()
			{
				show_feedback('<img src="images/cross_animated.gif" />', 'feedback');
				//print_obj(this.transport);
			}
		}
	);
	
	sc.request("cont=" + escape(cont));
}

function params_from_hash()
{
	// Default hash setzen, falls hash = ""
	var hash	=	window.location.hash;

	if(hash	==	"")
	{
		hash	==	"#ilcl";
	}
	var sub		=	hash.split('_')[1];
	var main	=	hash.split('_')[0];
	main	=	main.substring(1, main.length);
	
	// Falls Hash keine Subnav enthaelt, die erste in der Link-Liste bestimmen
	if(sub == undefined)
	{
		if(main != 'kontakt' && main != 'links' && main != 'impressum')
		{
			var mainnavs = $$('dt.stretchtoggle');
			mainnavs.each(function(el)
			{
				h	=	el.getChildren()[0].href.split('#')[1];
				if(('#' + h) == hash)
				{
					//alert(el.getNext().getChildren()[0].getChildren()[0].getChildren()[0].href.split('#')[1]);
					sub = el.getNext().getChildren()[0].getChildren()[0].getChildren()[0].href.split('#')[1].split('_')[1];
					//alert(sub);
				}
			});
		}
		else
		{
			sub = main;
		}
	}
	hash_ar	= new Array(main, sub);
	return hash_ar;
}

function print_obj(obj)
{
	$('debug').innerHTML	= "";
	for(var i in obj)
	{
		$('debug').innerHTML	+= "<pre>";
		$('debug').innerHTML	+=	i + ": " + obj[i] + "<br/><br/>";
	}
}

function show_feedback(text, id)
{
	var el	=	new Element('span').setHTML('<span id="' + id + '" style="padding-top: 2px; float: right; height: 0px;">' + text + '\</span>');
	$('actind').adopt(el);
	window.setTimeout("remove_feedback('" + id + "')", 2000);
}

function remove_feedback(id)
{
	$(id).remove();
}

function edit_in_place(fieldtype, table, row, content, el)
{
	var dbfieldname = "";
	
	if(el.getChildren() == "")
	{	
		if(fieldtype == 'text')
		{
			field = new Element('input').setText(content);
			field.addClass('eip_input_txt');
			field.setProperty('size', '40');
			field.setProperty('id', 'eip_input_txt_' + row);
			dbfieldname	=	"title";
		}
		else if (fieldtype == 'textarea')
		{
			field	= new Element('textarea').setText(content);
			field.addClass('eip_textarea');
			field.setProperty('id', 'eip_textarea_' + row);
			dbfieldname	=	"text";
		}
		field.setProperty('value', content)
		
		var save	=	new Element('a').setHTML('<img src="images/save.gif" style="margin: 0px 0 0 4px;"/>');
		save.setProperty('onclick', "save_in_place('" + fieldtype + "', '" + dbfieldname + "', '" + table + "', '" + row + "');");
		save.addClass('eip_button');
		var cancel	=	new Element('a').setHTML('<img src="images/cancel.gif" style="padding: 0px 0 0 4px;"/>');
		cancel.setProperty('onclick', "cancel_in_place('" + fieldtype + "', '" + table + "', '" + row + "');");
		cancel.addClass('eip_button');

		el.innerHTML = "";
		el.adopt(field);
		el.adopt(save);
		el.adopt(cancel);
		
		// clickarea-Class aus td entfernen
		el.removeClass('clickarea');
	}
}

function save_in_place(fieldtype, dbfieldname, table, row)
{
	var id = "";
	if(fieldtype == 'text')
	{
		id = 'eip_input_txt_' + row;
	}
	else if (fieldtype == 'textarea')
	{
		id = 'eip_textarea_' + row;
	}	
	var content = $(id).value;

	// Content in DB speichern
	var gc	=	new Ajax('inc/handle_eip_content.php?action=save&table=' + table + '&dbfieldname=' + dbfieldname + '&row=' + row,
	{
		method: 'post',
		update: id,
		onRequest: Nav.show_loader,
		onSuccess: function()
		{
			Nav.remove_loader();
			show_feedback('<img src="images/tick_animated.gif" /> OK', 'feedback');
			load_content_via_hash();
		},
		onFailure: function()
		{
			Nav.remove_loader();
			show_feedback('<img src="images/cross_animated.gif" /> ERROR', 'feedback');
		}
	}).request("content=" + escape(content));
}

function cancel_in_place()
{
	load_content_via_hash();
}

function addrows_in_place(table)
{
	var gc	=	new Ajax('inc/handle_eip_content.php?action=insert&table=' + table,
	{
		method: 'post',
		onRequest: Nav.show_loader,
		onSuccess: function()
		{
			Nav.remove_loader();
			show_feedback('<img src="images/tick_animated.gif" /> OK', 'feedback');
			load_content_via_hash();
		},
		onFailure: function()
		{
			Nav.remove_loader();
			show_feedback('<img src="images/cross_animated.gif" /> ERROR', 'feedback');
		}
	}).request();
}

function deleterows_in_place(table, row)
{
	var check	=	confirm("Datensatz löschen?");
	if(check	==	true)
	{
		var gc	=	new Ajax('inc/handle_eip_content.php?action=delete&table=' + table + '&row=' + row,
		{
			method: 'post',
			onRequest: Nav.show_loader,
			onSuccess: function()
			{
				Nav.remove_loader();
				show_feedback('<img src="images/tick_animated.gif" /> OK', 'feedback');
			load_content_via_hash();
			},
			onFailure: function()
			{
				Nav.remove_loader();
				show_feedback('<img src="images/cross_animated.gif" /> ERROR', 'feedback');
			}
		}).request();
	}
}