/**
 * /common.js
 *
 * Dragonea (http://www.dragonea.com)
 *
 * @author Jejem <jejem@phyrexia.org>
 * @copyright Jérémy 'Jejem' Desvages
**/

var my_width;
var my_height;

var pos_x;
var pos_y;

function position(e) {
	pos_x = Event.pointerX(e);
	pos_y = Event.pointerY(e);
}

Event.observe(window, 'load', function() {
	if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
		my_width  = document.body.clientWidth;
		my_height = document.body.clientHeight;
	} else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
		my_width  = document.documentElement.clientWidth;
		my_height = document.documentElement.clientHeight;
	}

	//Event.observe(document, 'mousemove', position, false);

	$('lockWrap').hide();
	$('lockWrap').style.width = my_width+'px';
	$('lockWrap').style.height = my_height+'px';

	$('errorWrap').hide();
	$('okWrap').hide();
	$('infoWrap').hide();

	Event.observe($('lockWrap'), 'click', function() {
		if ($('errorWrap').visible())
			hideError();

		if ($('okWrap').visible())
			hideOk();

		if ($('infoWrap').visible())
			hideInfo();
	});
});

function showLock() {
	if (! $('lockWrap').visible()) {
		$('lockWrap').style.width = my_width+'px';
		$('lockWrap').style.height = my_height+'px';

		$('lockWrap').show();
	}
}

function hideLock() {
	if ($('lockWrap').visible() && (! $('errorWrap').visible() && ! $('okWrap').visible() && ! $('infoWrap').visible()))
		$('lockWrap').hide();
}

function showError(errormsg_) {
	hideOk();
	hideInfo();

	showLock();

	$('errorWrap').innerHTML = '<div style="width: 16px; height: 16px; float: right;"><a href="javascript:;" onclick="hideError(); return false;"><img src="media/image/cross.png" width="16" height="16" alt="fermer" title="Fermer" /></a></div>'+errormsg_;

	Effect.Center($('errorWrap'));

	Effect.Appear($('errorWrap'));
}

function hideError() {
	$('errorWrap').hide();

	hideLock();

	$('errorWrap').innerHTML = '';
	$('errorWrap').style.width = '';
	$('errorWrap').style.height = '';
}

function showOk(okmsg_) {
	hideError();
	hideInfo();

	showLock();

	$('okWrap').innerHTML = '<div style="width: 16px; height: 16px; float: right;"><a href="javascript:;" onclick="hideOk(); return false;"><img src="media/image/cross.png" width="16" height="16" alt="fermer" title="Fermer" /></a></div>'+okmsg_;

	Effect.Center($('okWrap'));

	Effect.Appear($('okWrap'));

	setTimeout(function() {
		hideOk();
	}, 5000);
}

function hideOk() {
	$('okWrap').hide();

	hideLock();

	$('okWrap').innerHTML = '';
	$('okWrap').style.width = '';
	$('okWrap').style.height = '';
}

function showInfo(infomsg_) {
	hideError();
	hideOk();

	showLock();

	$('infoWrap').innerHTML = '<div style="width: 16px; height: 16px; float: right;"><a href="javascript:;" onclick="hideInfo(); return false;"><img src="media/image/cross.png" width="16" height="16" alt="fermer" title="Fermer" /></a></div>'+infomsg_;

	Effect.Center($('infoWrap'));

	Effect.Appear($('infoWrap'));
}

function hideInfo() {
	$('infoWrap').hide();

	hideLock();

	$('infoWrap').innerHTML = '';
	$('infoWrap').style.width = '';
	$('infoWrap').style.height = '';
}

function markAllRows(container_id) {
	var rows = document.getElementById(container_id).getElementsByTagName('tr');
	var checkbox;

	for (var i = 0; i < rows.length; i++) {
		checkbox = rows[i].getElementsByTagName('input')[0];

		if (checkbox && checkbox.type == 'checkbox' && checkbox.disabled == false)
			checkbox.checked = true;
	}

	return true;
}

function unmarkAllRows(container_id) {
	var rows = document.getElementById(container_id).getElementsByTagName('tr');
	var checkbox;

	for (var i = 0; i < rows.length; i++) {
		checkbox = rows[i].getElementsByTagName('input')[0];

		if (checkbox && checkbox.type == 'checkbox' && checkbox.disabled == false)
			checkbox.checked = false;
	}

	return true;
}
