// JavaScript Document
function errorClose() {
	jQuery("#error").css('display', 'none');
}

function ajaxCall(post, refreshOnSuccess) {
	if (post.indexOf('token=') == -1)
		post += '&token='+jQuery('#token').val();

	var tmp = jQuery.ajax({
			type: "POST", 
			url: window.location.protocol+window.location.hostname+"/ajax.html",
			data: post,
			processData: false,
			async: false
		}).responseText;
	
	if (refreshOnSuccess) {
		if (tmp == 1) {
			window.location.reload();
		} else {
			reportError(tmp);
		}
	} else {
		return tmp;
	}
}

function adminRelay(token) {
	jQuery('#mainAdminRelay').html(ajaxCall(token));
	jQuery('#mainAdminControl').slideUp();
	jQuery('#mainAdminRelay').slideDown();
}

function cancelRelay() {
	jQuery('#mainAdminControl').slideDown();
	jQuery('#mainAdminRelay').slideUp();
	jQuery('#mainAdminRelay').html('');
}

function loadModuleAdmin(posts) {
	if(posts == undefined) {  posts = "";	}
	
	var result = ajaxCall(posts);
	if(result != 'fail')
		jQuery('#mainAdminControl').html(result);
}

function initializeAdmin(posts)
{	
	jQuery('#mainMask').click(function() { window.location.reload(); });

	loadModule(posts);
	
	jQuery('#mainMask').fadeTo(500,0.7);
	jQuery('#mainMask').fadeIn(500, function() {		
		jQuery('#mainContentEditor').fadeIn(300);
	});
}

function windowCancel(Reload)
{
	if (!Reload) {
		jQuery('#mainContentEditor').fadeOut(300);
		jQuery('#mainMask').fadeOut(500);
	} else {
		window.location.reload();
	}
}

function loadModule(posts) {
	if(posts == undefined) {  posts = "";	}
	jQuery('#mainAdminControl').html(ajaxCall(posts));
}

function reportError(error) {
	jQuery('#error').html(error);
	jQuery('#error').slideDown(250);	
}

function toggleToolbar(obj) {
	jQuery(obj).children('.regionMenu').toggleClass('toolbar');
}

jQuery(document).ready( function() {
	jQuery('a[rel*="external"]').click(function(){return !window.open(this.href);});
});
