
function scrollUp(selector) {
    jQuery(selector).scrollTo({top:"-=200px"},800);   
}

function scrollDown(selector) {
    jQuery(selector).scrollTo({top:"+=200px"},800);                        
}                               
 
 
 function find_template_editor_panel(e) {
 	while($(e).attr("class")!="template_editor_panel") {
 		e=$(e).parent();
 		if (e==null) {
 			alert("internal error: editor panel not found");
 			return null;
 		}
 	}
 	return e;
 }
 
 function toggle_edit_template(e) {
 	find_template_editor_panel(e).children().each(function() {
 		$(this).toggle();
 	});
 }
 
 
 function store_content_script(e) { 	 	
 	
 	if (confirm("¿Está seguro de querer guardar los cambios?")) {
 		toggle_edit_template(e);

	 	var panel=find_template_editor_panel(e); 
	 	
	 	var template_content=$(panel).find("[class='editable_content_script']").attr("value");
	 	
	 	var template_name=$(panel).parent().find("[class='template_name']").text();	 	

 		$.ajax({
 			type:"POST",
 			url:"template_content_store.php",
 			data:"template_name="+template_name+"&template_content="+template_content,
 			success:function(response) {
 				alert("Contenido guardado.");
 				history.go(0);
 			},
 			error:function() {
 				alert('fallo');
 			}
 		});
 	}
 }
 
 function cancel_content_script(e) { 	
 	if (confirm("¿Está seguro de querer descartar los cambios?")) {
 		toggle_edit_template(e);
 		var panel=find_template_editor_panel(e);
 	
 		//reseteamos el contenido. 	
 		$(panel).find("[class='editable_content_script']").attr("value",$(panel).parent().find("[class='content_script']").text());
 	}
 }
 
 
 function edit_template_content(e) {
 	var content_script=$(e).find("[class='content_script']").text();
 	
 	var help_html=$("[class='editor_help']").parent().html();
 	
 	return '<div class="template_editor_panel">'+
 				'<div class="template_editor_choice"><span class="action" onclick="toggle_edit_template(this);">editar</span></div>'+
 				'<div class="template_editor">'+help_html+
 				'<textarea class="editable_content_script">'+content_script+'</textarea><br>'+
 				'<span class="action" onclick="store_content_script(this);">guardar</span>&nbsp;<span class="action" onclick="cancel_content_script(this);">cancelar</span><br>'+
 			 	'</div>'+
 			 '</div>';
 }
 
 function activate_edit_template() {
 	$("[class='edit_template']").each(function() {
 		$(this).append(edit_template_content(this));
 		//$(this).bind("click",toggle_edit_template);
 	});
 	
 }
 
 
 $(document).ready(function() {
 	activate_edit_template();
 });
