var is_editable = false;
var temp_content;
var right_content;
var left_content;

var closeFromButton = false;

function makeEditable(name, nameLeft, nameRight) {
	$("#saveBtn").toggle();
	
	var right_div = $("#"+name).find("div."+nameRight);
	var left_div = $("#"+name).find("div."+nameLeft);
	
	if (!is_editable) {
		temp_content = $("#"+name).html();
		right_content = right_div.html();
		left_content = left_div.html() ;
		
		$("#addNewLink").toggle();
		$(".delete").toggle();
		
		right_div.html('<textarea name="content" id="content">' + right_content + '</textarea>');
		
		tinyMCE.init({
			theme : "advanced",
			mode: "exact",
			elements : "content",
			auto_focus : "content",
			plugins : "safari,spellchecker,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras",
			theme_advanced_buttons1 : "save,newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,styleselect,formatselect,fontselect,fontsizeselect",
			theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor",
			theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,print,|,ltr,rtl,|,fullscreen",
			theme_advanced_buttons4 : "insertlayer,moveforward,movebackward,absolute,|,styleprops,spellchecker,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,blockquote,pagebreak,|,insertfile,insertimage",
			theme_advanced_toolbar_location : "top",
			theme_advanced_toolbar_align : "left",
			theme_advanced_statusbar_location : "bottom",
			theme_advanced_resizing : true,
			height:"500px",
			width:"750px"
		});
		is_editable = true;
		$("#editBtn").find("span").html("Close");
		tinyMCE.execCommand('mceFocus',false,'content');
		
		
	}
	else {
		$("#addNewLink").toggle();
		$(".delete").toggle();
		
		right_div.html(right_content);
		is_editable = false;
		$("#editBtn").find("span").html("Edit");
	}
	
	return false;
}

// Deprecated, no longer used
function saveMe(name, nameLeft, nameRight) {
	var right_div = $("#"+name).find("div."+nameRight);
	var left_div = $("#"+name).find("div."+nameLeft);
	
	var new_content = tinyMCE.activeEditor.getContent();
	right_div.html(escape(new_content));
	is_editable = false;
	
	$.ajax({
		type: "POST",
		url: 'index.php',
		data: 'page=ajax&action=savePage&p='+page_name+'&content='+new_content,
		success: function(data) {
			
		}
	});
	$("#addNewLink").toggle();
	$("#editBtn").find("span").html("Edit");
	$("#saveBtn").toggle();
	$(".delete").toggle();
}

function addNew() {
	$("#addNewDialog").modal({
		overlayClose:true,
		onClose: function (dialog) {
			if (!closeFromButton) {
				var newName = $("#addNewName").val();
				if (newName) {
					if (confirm("Are you sure you want to cancel?"))
						$.modal.close();
				} else
					$.modal.close()
			} else
				$.modal.close();
		}

	});
}

function savePage(o) {
	var newName = $("#addNewName").val();
	var newShort = $("#addNewShort").val();
	
	var newLink = '<p id="link_'+page_url2+'_'+newShort+'" class="links"><a href="javascript:none;" onclick="javascript:return deletePage(\''+page_url+'/'+newShort+'\')"><img src="'+root_path+'templates/images/delete.png" width="15px" class="delete" alt="delete" align="top" title="Delete '+newName+'" /></a><a href="'+root_path+page_url+'/'+newShort+'" style="margin-left:'+(parseInt(page_off)+1)*10+'px"> '+newName+'</a></p>';
	
	$.ajax({
		type: "POST",
		url: root_path + 'ajax',
		data: 'action=newPage&name='+newName+'&short='+newShort+'&parent='+page_url,
		success: function(data) {
			if (data == "-1")
				$("#addNewDialogErrors").html("The page already exists. Please chose a different short name for the page");
			else if (data == "-2")
				$("#addNewDialogErrors").html("Short name may contain only letters and numbers and underscore");
			else if (data == "-3")
				$("#addNewDialogErrors").html("Name cannot be empty");
			else if (data == "-4")
				$("#addNewDialogErrors").html("Short name cannot be empty");
			else if (data == "-5")
				$("#addNewDialogErrors").html("Short name is reserved. Please chose another one.");
			else if (data != 'done')
				$("#addNewDialogErrors").html("An error occured. Please try again.");
			else {
				$("#link_" + page_url2).after(newLink);
				closeFromButton = true;
				$.modal.close();
				closeFromButton = false;
			}
		}
	});

	return false;
}

function deletePage(name) {
	if (!confirm("Are you sure you want to delete '"+name+"'?"))
		return false;
	
	$.ajax({
		type: "POST",
		url: root_path + 'ajax/',
		data: '&action=deletePage&p='+name+'&curPage='+page_url,
		success: function(data) {
			var x = data.split("|");
			$('p[id^="link_'+x[0]+'"]').hide();
			if (x[1])
				window.location = root_path + x[1];
		}
	});
}

