if (! window.loading) var loading = new Object();
loading.Methods = {
	isInitialized: false,
	init: function() {
		this.loadingElement = document.createElement('div');
		Element.extend(this.loadingElement);
		this.loadingElement.setAttribute("id", "loading");
		document.body.insertBefore(this.loadingElement, document.body.childNodes[0]);
		this.isInitialized = true;
	},
	show: function() {
		if(! this.isInitialized) this.init();
		this.loadingElement.style.top = (window.pageYOffset || document.body.scrollTop || document.documentElement.scrollTop) + "px";
		this.loadingElement.show();
	},
	hide: function() {
		if(! this.isInitialized) this.init();
		this.loadingElement.hide();
	}
}
Object.extend(loading, loading.Methods);

dwrDisableLoadingMessage = function() {
	dwr.engine.setPreHook(function() { });
	dwr.engine.setPostHook(function() { });
}
dwrEnableLoadingMessage = function() {
	dwr.engine.setPreHook(function() {
		loading.show();
	});
	dwr.engine.setPostHook(function() {
		loading.hide();
	});
}

dwrEnableLoadingMessage();

Ajax.Responders.register( {
        onCreate: function() {
            if(Ajax.activeRequestCount > 0) {
				loading.show();
            }
        },
        onComplete: function() {
            if(Ajax.activeRequestCount == 0) {
				loading.hide();
            }
        }
    }
);

ajaxError = function() {
	alert("Oooops, erro do servidor! Tente novamente em alguns instantes.");
}

ajaxUpdate = function(url, parms, divId, method) {
	myRand = parseInt(Math.random() * 99999999);
	var myAjax = new Ajax.Updater(
		{ success: divId },
		url,
		{
			method: method,
			parameters: parms + "&r=" + myRand,
			onFailure: ajaxError
		});
}

ajaxGet = function(url, parms, divId) {
	ajaxUpdate(url, parms, divId, "get");
}

ajaxPost = function(url, parms, divId) {
	ajaxUpdate(url, parms, divId, "post");
}

ajaxFormGet = function(url, form, divId) {
	var parms = $(form).serialize();
	ajaxGet(url, parms, divId);
}

ajaxFormPost = function(url, form, divId) {
	var parms = $(form).serialize();
	ajaxPost(url, parms, divId);
}

popup1 = function(caption, url) {
	if (url.indexOf('?') == -1) {
		url = url + "?decorator=empty";
	} else {
		url = url + "&decorator=empty";
	}
    return GB_showPage(caption, url);
}

popup2 = function(caption, url, width, height) {
	if (url.indexOf('?') == -1) {
		url = url + "?decorator=empty";
	} else {
		url = url + "&decorator=empty";
	}
	return GB_showCenter(caption, url, height, width);
}

vote = function(id) {
	text.vote(id, {
		callback: function(data) {
			if (data == -1)
				alert("Não foi possível contabilizar o voto.");
			else {
				var oldRating = $("rating-" + id).innerHTML;
				updateRatingValue(data, "rating-" + id);
				if (oldRating < data) {
					$("news-vote-" + id).className = "news-vote-checked";
				} else {
					$("news-vote-" + id).className = "news-vote-unchecked";
				}
			}
		}
	});
}
updateRatingValue = function(rating, elementId) {
	$(elementId).update(rating);
}

ajaxifyLink = function(id, target) {
	$(id).onclick = function() { ajaxGet($(id).href, '', target);return false; }
}
clickAjaxLink = function(id, target) {
	ajaxGet($(id).href, '', target);return false;
}
redirect = function(dest) {
	window.location.assign(dest);
}
suggestUsername = function(name) {
	
}
