/**
 * @author	Jesse Cooke
 * Refreshes a region of the current page with the updated content. Used from the ContentsController::update action.
 * This function also rebuilds the "edit_bar" layer, then rebinds all links on the page that are used to 
 * edit content blocks. It highlights for a visual cue that something happened.
 * @param {Int} content_id	The id of the specific block of content.
 * @param {String} region_name	The name of the region that was edited. We have to pass it along from the beginning so we can update it here.
 */
var refreshRegion = function(content_id, region_name, page_id) {
	url = '/admin/contents/' + content_id + '.js?region_name=' + region_name;
	if (page_id) {
		url += '&page_id=' + page_id
	}
	
	new Ajax.Request(url, {
		method: 'get',
		onComplete: function(transport) {
			element = $('x_'+region_name);
			if (element == null) {
				element = $('x_'+region_name+'_container');
			}
			content = transport.responseText;
			setTimeout(
				function() {
					element.update(content);
				},
				1000
			);
			setTimeout(
				function() {
					element.highlight();
				},
				1100
			);
		}
	});
};

/**
 * @author	Jesse Cooke
 * The content of this method is what's actually bound as the behavior in the bindContentEditEvents function.
 * @param {Event} event	The event that is automatically passed.
 * @param {String} href	The href of the link that was bound.
 */
var showEditModalBox = function(href) {
	Modalbox.show(href, {title: "Edit content", width : 700, height: 520});
	return false;	
};

var showEditPopupBox = function(href) {
	window.open(href, "WYSIWYG Editor", "width=700,height=520,title=0,resize=0" );
	return false;	
};

var replaceContentModalBox = function(href){
	Modalbox.show(href, {title: "Replace or Create Content", width: 700, height: 520});
};


var showEditor = function(elementToUpdate) {
	var url = '/wysiwyg.html?elementToUpdate=' + elementToUpdate;
	window.open(url, "width=400, height=300, toolbar=0, resize=0");
};

var updateContent = function(elementToUpdate,value) {
	var set = "$('" + elementToUpdate + "').value = '" + value + "'";
	var highlight = "new Effect.Highlight('" + elementToUpdate + "')";
	setTimeout(set, 500);	
	setTimeout(highlight, 500);
};

/**
 * @author Sean
 * This removes TinyMCE from the form and moves the contents in the
 * TinyMCE editor to the Textarea.  This is required for the form to post correctly.
 */
function removeTinyMCE(){
	$$('#MB_content textarea').each(function(elem) {
		tinyMCE.execCommand('mceRemoveControl',false,elem.id);
	});
}

document.observe('dom:loaded', function(){
	notice_container = $('flash_notice');
	Event.observe(document, 'click', function(event) {
		var el = event.target;
		if (Event.isLeftClick(event)) {
			if (el.hasClassName('edit-content-modalbox')) {
				showEditModalBox(el.href);
			}
			if (el.hasClassName('edit-content-popup')) {
				showEditPopupBox(el.href);
			}
			if (el.hasClassName('replace-content-modalbox')) {
				replaceContentModalBox(el.href);
			}
		}
	});
});

// -- Content Selection Section -- Start

function getContentPreview(text, li_element){
	var id = li_element.id.split('_')[1];
	new Ajax.Updater('x-content-preview', '/admin/contents/preview?id='+id, { method: 'get'	});
	$('content_id').value = id;
}

function toggleOrphans(){
	Effect.toggle('orphaned-content', 'blind');
	var text = $('orphaned-content').visible() ? 'Show' : 'Hide';
	$('orphan-toggler').update(text + ' Orphans >>');
}
// -- Content Selection Section -- End

function toggleSharedPages(event, link){
	var parent_div = link.up('div.front-end-admin-bar');
	var shared_pages = parent_div.down('ul.admin-pages-sharing-content');
	shared_pages.setStyle({display: (shared_pages.getStyle('display') == 'none' ? 'inline' : 'none' )})
}


var adminMenuItems = [
	{
		name: 'Admin home',
		callback: function(){
			window.location.assign("/admin/home");
		}
	},{
		name: 'Site Map',
		callback: function(){
			window.location.assign("/admin/main_navigation/edit");
		}
	},{
		name: 'Toggle edit links',
		callback: function(){
			$$('.front-end-admin-bar').invoke("toggle");
		}
	},{
		name: "Logout",
		callback: function(){
			window.location.assign("/admin/logout");
		}
	}			
];

if ($('popup') == null) {
	new Proto.Menu({
		selector: '.contextmenu', className: 'admin-menu', menuItems: adminMenuItems
	});
}
 
tinyMCE.init({
	mode:"textareas", editor_selector : "tiny_mce"
});
