jQuery(function() {

	var resizeContent = function() {
		var w = jQuery(window);
		var t = jQuery('#form-screen');
		t.height(w.height());
		t.width(w.width());
	}

	jQuery(window).resize(resizeContent);
	resizeContent();

	jQuery("input,select").change(function() {
		bindConfirmDialogToTabs();
	});
	
	
});

var confirmIsBound = false;
function bindConfirmDialogToTabs() {
	if (!confirmIsBound) {
		jQuery("#tab1,#tab2,#tab3,#tab4").click(function() {
			return confirm("Ihre Änderungen wurden noch nicht gespeichert. Wollen Sie den Reiter wechseln ohne zu speichern?");
		});
		confirmIsBound = true;
	}
}

function insertDocumentContent(uploadName) {
	if (uploadName && uploadName != '') {
		jQuery.ajax({
			url : '/r/i/f/getUploadContent',
			type: 'POST',
			data: {
				'upload-name' : uploadName
			},
			success: function(msg){
				jQuery('#editor').ckeditorGet().setData(msg);
				jQuery('#editor').ckeditorGet().focus();
			}
		});
	}
	
	jQuery("#docFilename\\:input").val(uploadName);
}


function openUpload(closeFunction) {
	openModalDialog(
		'/r/i/f/upload',
		'Dateien hochladen ...',
		{
			width	: 800, 
			height	: 400, 
			close	: function() {
				closeFunction();
			}
		}
	); 
	return false;
}

function submitForm() {
	document.forms[0].submit();
}

function TreeAction(id, act, path) {
	var ele = document.getElementById(id);
	if (!ele) {
		alert("ERROR: can not find element with id " + id);
		return;
	}
	var form = cocoon.forms.getForm(ele);
	var actInput = id + ":action";
	var pathInput = id + ":path";
	form[actInput].value = act;
	form[pathInput].value = path;
	forms_submitForm(ele, id);
	form[actInput].value = "";
	form[pathInput].value = "";
	return false;
}

function TreeToggleCollapse(id, path) {
	return TreeAction(id, "toggle-collapse", path);
}

function TreeToggleSelect(id, path) {
	return TreeAction(id, "toggle-select", path);
}

function updateMapping(elementId, nodeId, checked, id, path) {
	jQuery.ajax({
		url : '/r/i/f/updateMapping',
		type: 'POST',
		data: {
			'elementId' : elementId,
			'nodeId' : nodeId,
			'checked' : checked
		},
		success : function () {
			TreeToggleSelect(id, "toggle-select", path);
			jQuery("#submitbutton1").focus();
		}
	});
}

function createNewElementReference(type, id) {
	var url = "";
	var title = "";
	
	if (type == "NewBild") {
		url = '/r/i/f/createImage';//?page=2
		title = 'Neues Bild';
		//title = 'New Image';
	} else if (type == "NewVideo") {
		url = '/r/i/f/createVideo';
		title = 'Neues Video';
		//title = 'New Video';
	} else if (type == "NewRessource") {
		url = '/r/i/f/createRessource';
		title = 'Neues Dokument';
		//title = 'New Document';
	} else if (type == "AddElement") {
		url = "/r/i/f/overview?contextElementId=" + id;
		title = 'Objekt verknüpfen';
		//title = 'Link to File';
	}

	openModalDialog(url,title,{
			close : function(dialog) {
				var elementIds = jQuery(dialog).attr('element-ids');
				if (elementIds) {
					elementIds = elementIds + jQuery("#addedRessources\\:input").val();
					jQuery("#addedRessources\\:input").val(elementIds);
				}
				jQuery('#hidden-submit').click();
			} 
	}); 
	
	return false;
}
			
function deleteElementReference(id) {
	jQuery("#deletedRessources\\:input").val(id);
	return true;
}			
			
function editElementReference(id) {
	openModalDialog(
		'/r/i/f/editElementStep2?elementId='+id,
		'Inhalt bearbeiten...',
		{
			width:820, 
			height:570,
			close : function(dialog) {
				submitForm();
			}
		}
	);
	return false;
}
			
			
