var vpWidth;
var vpHeight;
var xPos;
var yPos;
var pageWidth;
var pageHeight;
var msgBoxHtml;
var fctObj;

var tag = '<div id="msgbox-container">'
	+ '<table cellpadding="0" cellspacing="0">'
	+ '<tr><td align="center"><div id="msgbox-border"><div id="msgbox">'
	+ '<div id="msgbox-close"><a href="javascript:void(0)"></a></div>'
	+ '<div id="msgbox-content"></div></div></div></td></tr>'
	+ '</table></div>';

document.write(tag);

// ==============================================================================================================================
// =  Calculer la zone d'écran																									=
// ==============================================================================================================================
function getViewport()
{
	if (self.pageXOffset)
	{
		xPos = self.pageXOffset;
		yPos = self.pageYOffset;
	}
	else if (document.documentElement && document.documentElement.scrollTop)
	{
		xPos = document.documentElement.scrollLeft;
		yPos = document.documentElement.scrollTop; 
	}
	else if (document.body)
	{
		xPos = document.body.scrollLeft;
		yPos = document.body.scrollTop;
	}
  
	if (window.innerHeight && window.scrollMaxY)
	{
		pageWidth = window.innerWidth + window.scrollMaxX;
		pageHeight = window.innerHeight + window.scrollMaxY;
	}
	else if(document.body.scrollHeight > document.body.offsetHeight)
	{
		pageWidth = document.body.scrollWidth;
		pageHeight = document.body.scrollHeight;
	}
	else if(document.documentElement.scrollHeight > document.documentElement.clientHeight)
	{
		pageWidth = document.documentElement.scrollWidth;	
    	pageHeight  = document.documentElement.scrollHeight;
  	}
	else
	{
		pageWidth = document.body.offsetWidth + document.body.offsetLeft;
		pageHeight = document.body.offsetHeight + document.body.offsetTop;
	}

	if (window.innerWidth)
	{
		if (navigator.appName.indexOf('Netscape') != -1)
		{
			vpWidth = document.documentElement.clientWidth;
			vpHeight = document.documentElement.clientHeight;
		}
		else
		{
			vpWidth = window.innerWidth;
			vpHeight = window.innerHeight;
		}
	}
	else if (document.documentElement && document.documentElement.clientWidth && document.documentElement.clientWidth != 0)
	{
	   vpWidth = document.documentElement.clientWidth;
	   vpHeight = document.documentElement.clientHeight;
	}
	else
	{
	   vpWidth = document.getElementsByTagName('body')[0].clientWidth;
	   vpHeight = document.getElementsByTagName('body')[0].clientHeight;
	}
};

// ==============================================================================================================================
// =  Document prêt																												=
// ==============================================================================================================================
$(document).ready(function()
{
	// Event sur redimensionnent
	$(window).bind('resize', function()
	{
		if ($('#msgbox-container').is(':visible'))
			centerMsgBox();
	});
	
	// Fermer la boîte de dialogue
	$('#msgbox-close').click(function()
	{
		hideMsgBox();
	});
});

// ==============================================================================================================================
// =  Centrer la boîte de dialogue																								=
// ==============================================================================================================================
function centerMsgBox()
{
	$('#msgbox-container').hide();

	getViewport();
	
	$('#msgbox-container').width(vpWidth + 'px');
	$('#msgbox-container').height(vpHeight + 'px');
	$('#msgbox-container').show();
};

// ==============================================================================================================================
// =  Afficher la boîte de dialogue																								=
// ==============================================================================================================================
function showMsgBox(closing, url, data, fct)
{
	if (!$('#msgbox-container').is(':visible'))
	{	
		hideMsgBox();
		$('#msgbox-content').html('');

		$('#msgbox-close').hide();
		$('#msgbox-border').addClass('loader');		
		$('#msgbox').addClass('loader');

		fctObj = fct;
		getViewport();
			
		$('#msgbox-container').width(vpWidth + 'px');
		$('#msgbox-container').height(vpHeight + 'px');		

		$('#msgbox-container').show();
		$('#msgbox-border').show();	
	}

	if (url == null)
		return;

	if (data)
	{
		$.ajax(
		{
			type: 'POST',	
			data: data,
			url: url,
			cache: false,			
			success: function(html)
			{
				msgBoxHtml = html;
				setTimeout('loadContent(' + closing + ')', 500);
			},
			error: function(html)
			{
				alert('Impossible d\'acc\351der \340 la page demand\351e');
				hideMsgBox(null, true);
			}	
		});
	}
	else
	{
		$.ajax(
		{
			url: url,
			cache: false,			
			success: function(html)
			{
				msgBoxHtml = html;
				setTimeout('loadContent(' + closing + ')', 500);
			},
			error: function(html)
			{
				alert('Impossible d\'acc\351der \340 la page demand\351e');
				hideMsgBox(null, true);
			}
		});
	}
};

function loadContent(closing)
{
	$('#msgbox-border').hide();
	$('#msgbox-border').toggleClass('loader');	
	$('#msgbox').toggleClass('loader');
		
	if (closing)
		$('#msgbox-close').show();
	else
		$('#msgbox-close').hide();

	$('#msgbox-content').html(msgBoxHtml);	
//  setFormFocus();
	
//	$("#msgbox-content img").tooltip({ track: true, showURL: false });
//	$("#msgbox-content a").tooltip({ track: true, showURL: false });	

	if ($.browser.msie)
		setTimeout("$('#msgbox-border').show();$('#msgbox-content').show()", 250);
	else
		setTimeout("$('#msgbox-border').fadeIn('fast');$('#msgbox-content').fadeIn('fast')", 250);	
};

// ==============================================================================================================================
// =  Soumettre la boîte de dialogue																							=
// ==============================================================================================================================
function submitMsgBox(param)
{
	hideMsgBox();

	if (fctObj)
		fctObj(param);
};

// ==============================================================================================================================
// =  Cacher la boîte de dialogue																								=
// ==============================================================================================================================
function hideMsgBox(action, noAnim)
{
	if ($('#msgbox-container').is(':visible'))
	{	
		if ($.browser.msie || noAnim)
		{
			$('#msgbox-container').hide();
			$('#msgbox-border').hide();			
			$('#msgbox-content').hide();
		}
		else
		{
			$('#msgbox-border').fadeOut('fast');
			$('#msgbox-content').fadeOut('fast', function() { $('#msgbox-container').hide(); } );
		}
			
		if (action)
			eval(action);
	}
};