var nIDAjaxDialogTimeout = null;
var sCurrentObject = null;
var ie6 = $.browser.msie && $.browser.version < 7;

function showAjaxDialog(value,type,overlay,obj)
{
	var izq = screen.availWidth / 2 - 236;
	var arr = screen.availHeight / 2 - 200;
	var oObj = obj || null;
	var nTopDialog  = (oObj==null) ? (arr - (arr/2)) : $("#" + oObj).offset().top;
	var nLeftDialog = $("#divForm").offset().left || izq;
	var bOverlay = overlay || false;
	var bShadow = true;
	
	sCurrentObject = oObj;
		
	try {
		$('#fullOverlay').remove();
		if (bOverlay) $('body').append('<div id="fullOverlay" style="width:100%; height:' + $(document).height() + 'px"></div>');
		$('#ajxDialogShadow').remove();
		if (bShadow) $('body').append('<div id="ajxDialogShadow" class="dialog_shadow" style="position: absolute; left: '+ (nLeftDialog + 10) +'px; top: '+ (nTopDialog-10) + 'px;"></div>');		
		$('#ajxDialog').remove();
		$('body').append('<div id="ajxDialog" class="dialog" style="position: absolute; left: ' + (nLeftDialog + 20) + 'px; top: '+ nTopDialog + 'px;"></div>');		
		//$('#ajxDialog').css('top',nTopDialog - ($('#ajxDialog').height()));
		loadAjaxDialog(value,type);
		
		// Si el navegador es IE6, ocultamos los desplegables (dropdowns):
		if (ie6){
			$('SELECT').each(function(){
				var nSTop = $(this).offset().top;
				var nSLeft = $(this).offset().left;
				var nSWidth = $(this).width();
			 	var nSHeight = $(this).height();
			 	if ((nSTop >= nTopDialog && nSLeft >= nLeftDialog) && (nSTop <= nTopDialog + 110) && (nSLeft <= nLeftDialog + 472)){
			 		$(this).hide();	
				}
			});
		}
		
		
		$('#ajxDialog').css('z-index','1');
		$('#ajxDialog').show();
		/*
		$('#ajxDialog').slideDown('slow',function(){
			loadAjaxDialog(value,type);
		});
		*/
	} catch(e) {
		switch (type){
			case 'url':
				window.open(value,'ajx','width=472,height=400,left=' + izq + ',top=' + arr + ',scrollbars=yes');
				break;
			case 'image':
				window.open(value,'ajx','width=472,height=400,left=' + izq + ',top=' + arr + ',scrollbars=yes');
				break;
			case 'html':
				alert(value);
				break;	
		}
			
	}
	
}

function closeAjaxDialog(){
		$('#fullOverlay').remove();
		$('#ajxDialogShadow').remove();
		/*
		$('#ajxDialog').slideUp('slow',function(){
			$('#ajxDialog').remove();
			if (sCurrentObject!=null)
				$('#' + sCurrentObject).focus();
		});
		*/
		$('#ajxDialog').remove();
		
		// Si el navegador es IE6, volvemos a mostrar los desplegables
		if (ie6) $('SELECT').show();
		
		if (sCurrentObject!=null)
				$('#' + sCurrentObject).focus();
}

function loadAjaxDialog(url,type){
	if (nIDAjaxDialogTimeout!=null) clearInterval(nIDAjaxDialogTimeout);	
	nIDAjaxDialogTimeout = setInterval("onAjaxDialogTimeout('" + url + "')",5000);	
	switch (type){
		case 'image':
			$('#ajxDialog').html('<img src="img/loader.gif" alt="Cargando..." />');
			var lauImage = new Image();
			lauImage.onload = function(){
				
				clearInterval(nIDAjaxDialogTimeout);
				
				var izq = ($(document).width() - lauImage.width) / 2;
				var arr = ($(document).height() - lauImage.height) / 2;
				$('#ajxDialog').html('<center><img src="' + url + '" alt="' + url + '" border="0" /></center>');
				$('#ajxDialog').animate({height: lauImage.height, width: lauImage.width, left: izq, top: arr - (arr/2)});
				$('#ajxDialogShadow').animate({height: lauImage.height + 20, width: lauImage.width + 20, left: izq - 10, top: (arr - (arr/2)) - 10});
				//$('#ajxDialogShadow').css('height',($('#ajxDialog').height()+20)+'px');
			}
			lauImage.src=url;
			break;
		case 'url':
			$('#ajxDialog').html('<img src="img/loader.gif" alt="Cargando..." />');
			$.get(url,function(data){
				clearInterval(nIDAjaxDialogTimeout);
				
				$('#ajxDialog').html(data);
				//var izq = ($(document).width() - $('#ajxDialog').width) / 2;
				//var arr = ($(document).height() - $('#ajxDialog').height) / 2;				
				$('#ajxDialogShadow').css('height',($('#ajxDialog').height()+20)+'px');
				//$('#ajxDialog').animate({height: $('#ajxDialog').height, width: $('#ajxDialog').width, left: izq, top: arr - (arr/2)});
				//$('#ajxDialogShadow').animate({height: $('#ajxDialog').height + 20, width: $('#ajxDialog').width + 20, left: izq - 10, top: (arr - (arr/2)) - 10});
			});	
			break;
		case 'post':
			$('#ajxDialog').html('<img src="img/loader.gif" alt="Cargando..." />');
			$.post(url,function(data){
				clearInterval(nIDAjaxDialogTimeout);
				
				$('#ajxDialog').html(data);
				var izq = ($(document).width() - $('#ajxDialog').width()) / 2;
				var arr = ($(document).height() - $('#ajxDialog').height()) / 2;				
				//$('#ajxDialogShadow').css('height',($('#ajxDialog').height()+20)+'px');
				$('#ajxDialog').animate({height: $('#ajxDialog').height(), width: $('#ajxDialog').width(), left: izq, top: arr - (arr/2)});
				$('#ajxDialogShadow').animate({height: $('#ajxDialog').height() + 20, width: $('#ajxDialog').width() + 20, left: izq - 10, top: (arr - (arr/2)) - 10});
			});	
			break;
		default:
			clearInterval(nIDAjaxDialogTimeout);
			$('#ajxDialog').html(url);
			$('#ajxDialogShadow').css('height',($('#ajxDialog').height()+20)+'px');
			break;
	}
	$('#ajxDialog').click(function(){
		closeAjaxDialog();
		return false;
	});
}

function onAjaxDialogTimeout(url){
	$('#ajxDialog').html(getDialogMessageHTML('error',url,'No se ha podido cargar la URL. Por favor, verifique la direcci&oacute;n o int&eacute;ntelo m&aacute;s tarde.'));
	clearInterval(nIDAjaxDialogTimeout);
}

function getDialogMessageHTML(sType,sTitle,sMessage){
	sDMHTML = '<table width="452" cellspacing="0" cellpadding="0" border="0">' + 
			'<tr><td colspan="2" height="10"></td></tr>' +
			'<tr>' + 
			'	<td width="6"></td>' + 
			'	<td width="50" height="70" align="center"><img src="/img/' + sType + '.gif" alt="Error" /></td>' + 
			'	<td width="6"></td>' + 
			'	<td width="384" align="left"><span class="form_message_' + sType + '">' + sTitle + '</span><br /><span class="txt_conten">' + sMessage + '</span></td>' +
			'	<td width="6"></td>' + 
			'</tr>' +
			'<tr><td colspan="2" height="10"></td></tr>' +			
			'</table>';
	return sDMHTML;
}


/* Impromptu */
var bPrompt = false;
function fAlert(sMsg){
	try {
		if (!bPrompt) {
			bPrompt = true;
			$.prompt(sMsg, { 
				buttons 	: {	Aceptar : 1 },
				focus		: 0,
				callback	: onPrompt
			});
		}
	} catch(e){
		bPrompt = false;
		alert(sMsg.replace('<' + 'b>','').replace('<' + '/b>',''));
	}
}

function fConfirm(sMsg, aButtons, nDefault, fCallback){
	if (typeof aButtons != 'object') aButtons = {Aceptar : 1, Cancelar : 2};
	if (isNaN(nDefault)) nDefault = 0;
	if (typeof fCallback != 'function') fCallback = onPromptConfirm;
	try {
		if (!bPrompt) {
			bPrompt = true;
			$.prompt(sMsg, { 
				buttons 	: aButtons,
				focus		: nDefault,
				callback	: function(v,m,f){
					bPrompt = false;
					fCallback(v,m,f);	
				}
			});
		}
	} catch(e){
		bPrompt = false;
		if (confirm(sMsg.replace('<' + 'b>','').replace('<' + '/b>',''))){
			fCallback(1);
		} else {
			fCallback(2);
		}
	}
}


function onPrompt(v,m,f){
	bPrompt = false;
	//if (oCurrentFieldCheck != '') document.getElementById(oCurrentFieldCheck).focus();
}

function onPromptConfirm(v,m,f){
	bPrompt = false;
}
