$(document).ready(function(){
	$('input:checkbox').each(function(index) {
		$(this).click(function() {
			checkboxDependencies(this);
		});
		$(this).change(function() {
			colorCheckbox(this);
		});
		colorCheckbox(this);
	});
	
	$('input[name=admin]').click(function() {
		markAllCheckboxes(true);
	});
	
	$('input[name=guest]').click(function() {
		markAllCheckboxes(false);
		checkboxDependencies($('input[name=event_gallery-view]').attr('checked', true).change());
		checkboxDependencies($('input[name=event_report-view]').attr('checked', true).change());
	});
	
	$('input[name=default]').click(function() {
		$('input[name=guest]').click();
		checkboxDependencies($('input[name=own_profile-edit]').attr('checked', true).change());
		checkboxDependencies($('input[name=event_participation-view]').attr('checked', true).change());
		checkboxDependencies($('input[name=own_event_participation-add]').attr('checked', true).change());
	});
});

function markAllCheckboxes(checked) {
	$('input:checkbox').each(function(index) {
		$(this).attr('checked', Boolean(checked)).change();
	});
}

function checkboxDependencies(checkbox) {
	var name = $(checkbox).attr('name');
	var checked = $(checkbox).attr('checked');
	var client = null;
	var parent = null;
	var own;
	
	// no view no add
	if (name.substr(-4) == 'view') {
		client = $('input[name='+name.substr(0,name.length - 4)+'add]:checked');
		if (client.length == 0) client = $('input[name='+name.substr(0,name.length - 4)+'edit]:checked');
	}
		
	// add implies view, no add no edit
	else if (name.substr(-3) == 'add') {
		parent = $('input[name='+name.substr(0,name.length - 3)+'view]:not(:checked)');
		client = $('input[name='+name.substr(0,name.length - 3)+'edit]:checked');
		if (client.length == 0) client = $('input[name='+name.substr(0,name.length - 3)+'delete]:checked');
	}
	
	// edit implies add, no edit no delete
	else if (name.substr(-4) == 'edit') {
		parent = $('input[name='+name.substr(0,name.length - 4)+'add]:not(:checked)');
		client = $('input[name='+name.substr(0,name.length - 4)+'delete]:checked');
	}
	
	// delete implies edit
	else if (name.substr(-6) == 'delete') {
		parent = $('input[name='+name.substr(0,name.length - 6)+'edit]:not(:checked)');
		if (parent.length == 0) parent = $('input[name='+name.substr(0,name.length - 6)+'add]:not(:checked)');
	}
	
	if (checked && parent != null && parent.length > 0)
		checkboxDependencies($(parent[0]).attr('checked', $(checkbox).attr('checked')).change());
	else if (!checked && client != null && client.length > 0)
		checkboxDependencies($(client[0]).attr('checked', $(checkbox).attr('checked')).change());
	
	// general implies own
	if (checked && name.substr(0,3) != 'own') {
		own = $('input[name=own_'+name+']');
		if (own.length > 0) checkboxDependencies($(own[0]).attr('checked', $(checkbox).attr('checked')).change());
	} else if (!checked && name.substr(0,3) == 'own') {
		own = $('input[name='+name.substr(4)+']:checked');
		if (own.length > 0) checkboxDependencies($(own[0]).attr('checked', $(checkbox).attr('checked')).change());
	}
}

function colorCheckbox(checkbox) {
	if ($(checkbox).attr('checked'))
		$(checkbox).parent().css('backgroundColor', '#E0FFE0');
	else $(checkbox).parent().css('backgroundColor', '#FFE0E0');
}

function toggleDisplay(elementId) {
	var element = document.getElementById(elementId);
	
	if (element.style.display == "none") element.style.display = "block";
	else element.style.display = "none";
}


function profilePopup (id) {
	profilePopup = window.open("/profile/"+id, "_blank", "width=400,height=500,status=no,scrollbars=no,resizable=no");
	profilePopup.focus();
}

function galleryPicturePopup (id,width,height) {
	picturePopup = window.open("/gallery/picture/"+id, "_blank", "width="+(parseInt(width) + 50)+",height="+(parseInt(height) + 190)+",status=no,scrollbars=no,resizable=no");
	picturePopup.focus();
}

tinyMCE_GZ.init({
	plugins : 'style,layer,table,save,advhr,advimage,advlink,emotions,iespell,insertdatetime,preview,media,'+ 
        'searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras',
	themes : 'simple,advanced',
	languages : 'de',
	disk_cache : true,
	debug : false
});

tinyMCE.init({
		language : "de",
		mode : "textareas",
		theme : "advanced",
		editor_selector: "mceAdvanced",
		plugins : "safari,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,wordcount",

		forced_root_block : '',
		force_br_newlines : true,
		
		theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,styleselect,formatselect,fontselect,fontsizeselect,|,fullscreen",
		theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,forecolor,backcolor",
		theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,print,|,ltr,rtl",
		theme_advanced_toolbar_location : "external",
		theme_advanced_toolbar_align : "left",
		theme_advanced_statusbar_location : "bottom"
});

tinyMCE.init({
	language : "de",
	mode : "textareas",
	theme : "advanced",
	editor_selector : "mceSimpleSmall",
	plugins : "safari,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,wordcount",
	
	setup : function(ed) {
        ed.onInit.add(function() {
            var w = ed.getWin();
            var h = 50; //new height of edit area
            ed.theme.resizeTo(w.clientWidth, h);
        });
    },
    theme_advanced_resizing_min_height : 50,
	
	forced_root_block : '',
	force_br_newlines : true,
	entity_encoding : "raw",
	
	theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,fontselect,fontsizeselect,|,forecolor,backcolor,|,cut,copy,paste,pastetext,pasteword,|,bullist,numlist,|,undo,redo,cleanup",
	theme_advanced_buttons2 : "",
	theme_advanced_buttons3 : "",
	theme_advanced_toolbar_location : "external",
	theme_advanced_toolbar_align : "center",
	theme_advanced_statusbar_location : "none"
});

tinyMCE.init({
	language : "de",
	mode : "textareas",
	theme : "advanced",
	editor_selector : "mceSimple",
	plugins : "safari,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,wordcount",
	
	forced_root_block : '',
	force_br_newlines : true,
	entity_encoding : "raw",
	
	theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,fontselect,fontsizeselect,|,forecolor,backcolor,|,cut,copy,paste,pastetext,pasteword,|,bullist,numlist,|,undo,redo,cleanup",
	theme_advanced_buttons2 : "",
	theme_advanced_buttons3 : "",
	theme_advanced_toolbar_location : "external",
	theme_advanced_toolbar_align : "center",
	theme_advanced_statusbar_location : "none"
});

