// JavaScript Document

var menuEditPageRegion;
var menuEditPageModule;

function moduleMenuAjax(action, posts) {
	return $.ajax({
				type: "POST",
				url: "http://"+window.location.hostname+"/ajax/"+action+"/module:admin/",
				dataType: "html",
				data: posts,
				processData: false,
				async: false
			}).responseText;	
}

function addRegionMenu() {
	if( (result = moduleMenuAjax("addRegionMenu", "pr="+menuEditPageModule+"&m="+$('#i').val())) && result == "success" ) {
		window.location.reload();	
	} else {
		alert(result);
	}
}

function deleteModuleMenu(pageRegion) {
	if( (result = moduleMenuAjax("deleteRegionMenu", "pr="+pageRegion)) && result == "success" ) {
		window.location.reload();	
	} else {
		reportError(result);	
	}
}

function reportPageError() {	
	$('#error').html("We are currently experiencing issues with parts of this page. Please check back later while we correct the problem. We apologize for any inconvenience.");
	$('#errorCloseBtn').hide();
	$('#error').slideDown(250);
}

function reportError(error) {
	$('#error').html(error);
	$('#errorCloseBtn').show();
	$('#error').slideDown(250);
}

function getModuleMenu() {	
	var tmp = $.ajax({
			type: "POST",
			url: "http://"+window.location.hostname+"/ajax/region_menu_add/module:admin/",
			dataType: "html",
			data: "p=0",
			processData: false,
			async: false
		}).responseText;
	
	return tmp;
}

function showMenuEditor(pageRegion, pageModule) {
	menuEditPageRegion = pageRegion;
	menuEditPageModule = pageModule;
	
	var width = $(document).width();
	$('#mainMask').css('width', width);

	var height = $(document).height();
	$('#mainMask').css('height', height);
	
	$('#mainMask').click(function() {
		window.location.reload();
	});
	
	$('#mainAdminControl').html(getModuleMenu());
	
	$('#mainMask').fadeTo(500,0.6);
	$('#mainMask').fadeIn(500, function() {
		if($(window).height() > $('#mainContentEditor').height()) {
			$('#mainContentEditor').css('top', ( $(window).height() - $('#mainContentEditor').height() ) / 2 + $(window).scrollTop() + 'px');
		} else {
			$('#mainContentEditor').css('top', '50px');	
		}
		$('#mainContentEditor').css('left', ( $(window).width() - $('#mainContentEditor').width() ) / 2 + $(window).scrollLeft() + 'px');
		$('#mainContentEditor').show();
		
	});	
}