
// create collapsed Lists
/////////////////////////
function collapseLists() {
	$.each($('ul.list li:has(ul)'), function(key,obj) {
		$(obj).children('ul').hide();
		$(obj).attr('id','key'+key);
		// cookie abrufen
		arr = new Array(); if ($.cookie($('body').attr('class')+'openLists')) arr = $.cookie($('body').attr('class')+'openLists').split('#');
		if (jQuery.inArray($(obj).attr('id'),arr)!=-1) {
			$(obj).prepend('<a href="#" class="collapse"><span class="minus"></span></a>');
			$(obj).find('ul:first').toggle();
		} else
			$(obj).prepend('<a href="#" class="collapse"><span class="plus"></span></a>');
		// click function
		$(obj).children('a.collapse').click( function() {
			$(this).parent().find('ul:first').toggle();
			// open
			if ($(this).html()=='<span class="plus"></span>') {
				$(this).html('<span class="minus"></span>');
				// write cookie
				arr = new Array(); if ($.cookie($('body').attr('class')+'openLists')) arr = $.cookie($('body').attr('class')+'openLists').split('#');
				arr.push($(this).parent().attr('id'));
				jQuery.unique(arr);
				$.cookie($('body').attr('class')+'openLists',arr.join('#'));
			// close
			} else {
				$(this).html('<span class="plus"></span>');
				// write cookie
				arr = new Array(); if ($.cookie($('body').attr('class')+'openLists')) arr = $.cookie($('body').attr('class')+'openLists').split('#');
				// delete from array
				index = jQuery.inArray($(this).parent().attr('id'),arr);
				arr.splice(index,1);
				jQuery.unique(arr);
				$.cookie($('body').attr('class')+'openLists',arr.join('#'));
			}
			return false;
		});
	});
}

// create Tabs from divs
////////////////////////
function createTabs() {
	$('#tabs').prepend('<ul id="tabmenu"></ul>');
	$.each($('#tabs .tab'), function(key,obj) {
		$('#tabmenu').append('<li><a href="#" id="tablink-'+$(obj).attr('id')+'">'+$(obj).attr('title')+'</a></li>');
		$(obj).removeAttr('title');
		if (key==0) {
			$(obj).show();
			$('#tablink-'+$(obj).attr('id')).addClass('active');
		} else
			$(obj).hide();
		$('#tablink-'+$(obj).attr('id')).click( function() {
			$('#tabs .tab').hide();
			$('#tabmenu li a').removeClass('active');
			$('#'+$(this).attr('id').replace(/tablink-/g,'')).show();
			$(this).addClass('active');
			return false;
		});
	});
}

// mark all Checkboxes
/////////////////////////
function markCheckboxes(name) {
	$.each($('input[name="'+name+'"][type=checkbox]'), function(key,obj) {
		if ($(obj).attr('checked')) 	$(obj).removeAttr('checked');
		else							$(obj).attr('checked','checked');
	});
}

// show Folder-Select-Element for data-Types image and file
// show Default-Input-Element for dataTypes with can_have_default = 1
/////////////////////////////////////////////////////////////////////
function showDefaultOrFolderElement() {
	// add event to select 'dataTypes'
	$('select.dataTypes').change( function() {
		$('.hide').hide();
		parentFieldset = $(this).parent().parent();
		if ($(this).children(':selected').attr('class') == 'default')
			$(parentFieldset).children('div.default').show();
		if ($(this).children(':selected').attr('class') == 'folder') {
			$(this).parent().parent().children('div.folder').show();
			folderSelect 	= $(parentFieldset).children('div.folder').children('select.folderSelect');
			imageFolders 	= $(parentFieldset).children('div.folder').children('select.imageFolders');
			fileFolders 	= $(parentFieldset).children('div.folder').children('select.fileFolders');
			if ($(this).children(':selected').text()=='image')
				$(folderSelect).html($(imageFolders).html());
			else
				$(folderSelect).html($(fileFolders).html());
		}
	});
}

// duplicate Fields
// adds button '+' on any element with class 'duplicatable'
// options: increment ids, fors for form-fields
/////////// id, for => 'xyz-0' || name => 'xyz[0]'
//////////////////////////////////////////////////
function duplicateableElements() {
	$('.duplicateable').append('<a href="#" class="buttonPlus">+</a>');
	$('.buttonPlus').click( function() {
		// clone Node
		clone = $(this).parent().clone(true);
		// remove text input values
		$(clone).find('input[type=text]').attr('value','');
		// remove textarea content
		$(clone).find('textarea').html('');
		// set first select option as selected
		$(clone).find('select:first-child').attr('selected','selected');
		$(this).parent().after(clone);
		// add minus if not present
		if ($(clone).children('a.buttonMinus').length==0)
			$(clone).append('<a href="#" class="buttonMinus">-</a>');
			$('.buttonMinus').click( function() {
				$(this).parent().remove();
				return false;
			});
		// hide
		$(clone).find('.hide').hide();
		// class increment
		if ($(this).parent().hasClass('increment')) {
			$.each($('.increment'), function(key,obj) {
				// replace id, for
				$.each($(obj).find('[id],[for],[name]'), function(key1,obj1) {
					if ($(obj1).attr('id'))		$(obj1).attr('id', $(obj1).attr('id').replace(/-[0-9]+$/g,'-'+key));
					if ($(obj1).attr('for'))	$(obj1).attr('for', $(obj1).attr('for').replace(/-[0-9]+$/g,'-'+key));
					if ($(obj1).attr('name'))	$(obj1).attr('name', $(obj1).attr('name').replace(/\[[0-9]+\]/g,'['+key+']'));
				});
			});
		}
		return false;
	});
}

// prompt for new Name, progress
////////////////////////////////
function renameAndRelocate(href,oldName) {
	newName = prompt('Geben Sie den neuen Namen ein:',oldName);
	if (!newName || newName == '') return false;
	newNameValid = newName.replace(/[^a-zA-Z0-9-_.]/g,'');
	if (newNameValid == '') {
		alert('Der Name ist ungü�ltig!');
		return false;
	}
	if (newNameValid != newName) {
		io = confirm('Der angegebene Name enthielt unerlaubte Zeichen. Der Name wurde in "'+newNameValid+'" ge�ändert');
		if (!io) return false;
	}
	window.location.href = href+newNameValid;
}

function setPageTitle() {
	set = $('select#parent option:selected').val();
	if ($('#new-level-true').attr('checked')==true) {
		$.each($('select#parentTitles option'), function(key,obj) {
			if ($(obj).val()==set) {
				$('#title').val($(obj).text()+': ');
				return;
			}
		});
	}
}

// show Field
/////////////
function showField(id) {
	$('.hide').hide();
	$('#field-'+id).show();
}

// open File or Image Browser
/////////////////////////////
function openBrowser(varName,folder,type,value) {
	if (type=='image') {
		myWindow = window.open('/admin/popup/images/dir/'+folder+'?selected='+value,'images','width=620,height=600,scrollbars=yes');
		myWindow.location.href = '/admin/popup/images/dir/'+folder+'?selected='+value;
	}
	if (type=='file')  {
		myWindow = window.open('/admin/popup/files/dir/'+folder+'?selected='+value,'files','width=620,height=600,scrollbars=yes');
		myWindow.location.href = '/admin/popup/files/dir/'+folder+'?selected='+value;
	}
	if (type=='link')  {
		myWindow = window.open('/admin/popup/links/?selected='+value,'links','width=620,height=600,scrollbars=yes');
		myWindow.location.href = '/admin/popup/links/?selected='+value;
	}
	if (myWindow.opener == null)
		myWindow.opener = self;
	myWindow.opener.feld = document.getElementById(varName);
}
function chooseFile(file) {
	opener.feld.value = file;
	window.close();
}

// add Option
/////////////
function addOption() {
	set = false;
	$.each( $('select.addOption'), function(key,obj) {
		set = true;
		$(obj).parent().append('<a href="#" class="addOption">Trifft kein Element der Liste zu? Dann klicken Sie hier.</a>');
	});
	$.each( $('a.addOption'), function(key,obj) {
		$(obj).toggle(
			function() {
				elem = $(obj).parent().find('select');
				$(obj).parent().find('select').hide();
				$(obj).before('<input type="text" name="'+$(elem).attr('name')+'">');
				$(obj).html('Auswahlliste zeigen');
				return false;
			},
			function () {
				$(obj).parent().find('input').remove();
				$(obj).parent().find('select').show();
				$(obj).html('Trifft kein Element der Liste zu? Dann klicken Sie hier.');
				return false;
			});
	});
	if (set) initEditors();
}

// add Files
////////////
function addFiles() {
	$('a.do-export').click(function() {
		files = prompt('Weitere Dateien, durch Komma getrennt: (Kompletter Pfad, z.B.: /scripts/script.js)');
		window.location.href = $(this).attr('href')+'?addFiles='+files;
		return false;
	});
}

// call functions onLoad
////////////////////////
$(document).ready(function() {
	collapseLists();
	createTabs();
	addOption();
	addFiles();
	$('.hide').hide();
	showDefaultOrFolderElement();
	duplicateableElements();
	$('select#parent').change( function() { setPageTitle(); } );
	// sortables
	if ($('#elements').html()!=null)
		$('#elements').sortable({ stop: function() { resortElements(); } });
});

