/**
 * Core literal
 */
var core = {
	/**
	 * Questen with confirm
	 */
	askQuestion: function(question, url) {
		check = confirm(question);
		if (check != false) {
			if (typeof url == 'undefined') {
				return true;
			} else {
				document.location.href = url;
			}
		}
		return false;
	},
	jQuestion: function(question, callback) {
		$("#question").remove();
		$('<div id="question">'+question+'</div>').dialog({
			title: 'Sicherheitsfrage',
			resizable: false,
			buttons: {
				"Ok": function() {
					$(this).dialog("close");
					callback.call();
				},
				"Abbrechen": function() {
					$(this).dialog("close");
				}
			}
		});
		return false;
	},
	/**
	 * 
	 */
	createSitetree: function() {
		$('#sitetree').tree({
			cookies: true,
			ui: {
				theme_path: '/css/backend/tree/',
				theme_name: 'apple',
				context: [
					{
						id: 'edit_meta',
						label: 'Link bearbeiten',
						icon: '/libraries/media/icons/16x16/internet.png',
						visible: function (NODE, TREE_OBJ) {
							var ok = true;
							$.each(NODE, function() {
								if (NODE.length != 1 || $(this).attr("rel") == 'root') ok = false;
								return false;
							});
							return ok;
						},
						action:  function (NODE, TREE_OBJ) {
							var sitetree = $(NODE).attr('id').split('_');
							$.ajax({
								type: 'GET',
								url: '/ecco/sitetree/edit',
								data: 'sitetree_id='+sitetree[1],
								success: function(html){
									$('#col3_content').html(html);
								}
							});
						}
					},
					{
						id: 'edit',
						label: 'Inhalt bearbeiten',
						icon: '/libraries/media/icons/16x16/edit.png',
						visible: function(NODE, TREE_OBJ) {
							var ok = true;
							$.each(NODE, function() {
								if (NODE.length != 1 || $(this).attr("rel") == 'root') ok = false;
								return false;
							});
							return ok;
						},
						action: function(NODE, TREE_OBJ) {
							var sitetree = $(NODE).attr('id').split('_');
							$.ajax({
								type: 'GET',
								url: '/cms/page/edit',
								data: 'sitetree_id='+sitetree[1],
								success: function(html){
									$('#col3_content').html(html);
								}
							});
						}
					},
					{
						id: 'create',
						label: 'Erstellen',
						icon: '/libraries/media/icons/16x16/edit_add.png',
						visible: function(NODE, TREE_OBJ) {
							if (NODE.length != 1) return false;
							return TREE_OBJ.check('creatable', NODE);
						},
						action: function(NODE, TREE_OBJ) {
							var parent = $(NODE).attr('id').split('_');
							$.ajax({
								type: 'GET',
								url: '/ecco/sitetree/edit',
								data: 'parent_sitetree_id='+parent[1],
								success: function(html){
									$('#col3_content').html(html);
								}
							});
						}
					},
					{
						id: 'delete',
						label: 'Löschen',
						icon: '/libraries/media/icons/16x16/edit_remove.png',
						visible: function(NODE, TREE_OBJ) {
							var ok = true;
							$.each(NODE, function() {
								if (TREE_OBJ.check('deletable', this) == false || $(this).attr("rel") == 'root') ok = false;
								return false;
							});
							return ok;
						},
						action: function(NODE, TREE_OBJ) {
							object = this;
							core.jQuestion("Wollen Sie diesen Datensatz wirklich löschen?", function(object) {
								var url = '';
								$.each(NODE, function() {
									var sitetree = $(this).attr('id').split('_');
									url = url + 'id[]='+sitetree[1]+'&';
									TREE_OBJ.remove(object);
								});
								$.ajax({
									type: 'POST',
									url: '/ecco/sitetree/delete',
									data: url,
									success: function(html){
										$('#col3_content').html(html);
									}
								});
							});
						}
					}
				]
			},
			rules: {
				draggable: 'all',
				dragrules: ['* * folder']
			},
			lang: {
				new_node: 'Neuer Ordner',
				loading:  'Laden...'
			},
			callback: {
				oncreate: function(NODE, REF_NODE, TYPE, TREE_OBJ) {
				},
				ondblclk: function(NODE, TREE_OBJ) {
					var sitetree = $(NODE).attr('id').split('_');
					$.ajax({
						type: 'GET',
						url: '/cms/page/edit',
						data: 'sitetree_id='+sitetree[1],
						success: function(html){
							$('#col3_content').html(html);
						}
					});
				},
				onmove: function(NODE, REF_NODE, TYPE, TREE_OBJ) {
					var url = '';
					$.each($('#'+$(TREE_OBJ.container).attr('id') + ' li'), function(id, item) {
						var sitetree = $(this).attr('id').split('_');
						var parents  = $(this).parents();
						var parent   = $(parents[1]).attr('id').split('_');
						if (typeof parent[1] == 'undefined') {
							parent[1] = 0;
						}
						url = url + 'id[]='+sitetree[1]+'&pid[]='+parent[1]+'&';
					});
					$.ajax({
						type: 'POST',
						url: '/ecco/sitetree/saveorder',
						data: url,
						success: function(html){
							$('#col3_content').html(html);
						}
					});
				}
			}
		});
	},
	/**
	 * 
	 */
	treeRename: function(object) {
		$.ajax({
			type: 'POST',
			url: $(object).attr("action"),
			data: $(object).serialize(),
			success: function(html){
				var id = $('#sitetree_id').val();
				var name = $('#sitetree_name').val();
				$('#n_'+id+' > a').html(name);
				$('#col3_content').html(html);
			}
		});
		return false;
	},
	/**
	 * Update all Editors befor saving
	 */
	updateAllEditors: function() {
		for ( i = 0; i < parent.frames.length; ++i ) {
			if ( parent.frames[i].FCK ) {
				parent.frames[i].FCK.UpdateLinkedField();
			}
		}
	},
	/**
	 * 
	 */
	ajaxFormSave: function(object) {
		this.updateAllEditors();
		$.ajax({
			type: 'POST',
			url: $(object).attr("action"),
			data: $(object).serialize(),
			success: function(html){
				$('#col3_content').html(html);
			}
		});
		return false;
	},
	/**
	 * 
	 */
	ajaxLink: function(object, div, callback) {
		$.ajax({
			type: 'GET',
			url: $(object).attr("href"),
			data: "",
			success: function(html){
				$('#'+div).html(html);
				callback.call();
			}
		});
		return false;
	}
};

/**
* Hide all elements with the class hide_me_on_load
*/
$(document).ready(function() {
	$.each($(".hide_me_onload"), function(id, item) {
		$(item).hide();
	});
});



