/*
 * Para incluir, criar botoes que chamam:
 *	- corrigirPosto(postoId)
 *	- confirmarPreco(precoId)
 * 
 * e registrar callbacks:
 *  - refreshPosto(postoId)
 * 
 * alem disso, setar variavel pdc.userAutenticado = true, conforme o caso
 */
document.observe("dom:loaded", function() {
	document.body.appendChild(new Element('div',{'id':'modalBackgroundDiv','class':'modalPopupTransparent','style':'display: none;'}));
	
	var div = new Element('div',{'id':'modalWindow','class':'modalPopupWindow','style':'display: none;'});
	document.body.appendChild(div);
});

var pdc = new Object();
pdc.postoId = null;
pdc.userAutenticado = false;

function corrigirPosto(postoId) {
	pdc.postoId = postoId;
	openModalWindow();
	if (pdc.userAutenticado) {
		handleOpenIDResponse();
	} else {
		new Ajax.Updater('modalWindow', '/autenticacao/popUpLogin');
	}
}

function openPopupWindow(openid) {
	$('modalWindow').update('Autenticando...');
	window.open('/autenticacao/processarLogin?openid='+encodeURIComponent(openid), 'openid_popup', 'width=810,height=580');
	return false;
}

function handleOpenIDResponse() {
	pdc.userAutenticado = true;
	$('modalWindow').update('Obtendo posto...');
	new Ajax.Updater('modalWindow', '/postos/editarPosto', {
		parameters: { 'postoId': pdc.postoId},
		onComplete: function(){ $('input-preco-gasolina').focus();}
	});
	return false;
}

function enviarPosto() {
	$('postoForm').request({
		onComplete: function(transport){ 
			if (transport.responseText.match('ERROR')) {
				alert('Valores inválidos');
			} else {
				closeModalWindow(); 
				refreshPosto(pdc.postoId);
			}
		}
	});
}

function refreshPosto(postoId) {
	alert("criar calback refreshPosto(postoId)");
}

function confirmarPreco(precoId) {
	new Ajax.Updater('div-preco-'+precoId, '/postos/confirmarPreco', {parameters: { 'id': precoId}});
	$('table-preco-'+precoId).update(new Element('span',{'class':'data'}).update('atualizando...'));
}

function openModalWindow() {
    var div = $('modalWindow');
    var bgDiv = $('modalBackgroundDiv');

    var docDim = Element.getDimensions(document.body);

    //get the size of the window and calculate where the box should be placed
    var wDim = getBrowserWindowSize();
    var dDim = Element.getDimensions(div);

    div.style.top = '110px';
    div.style.left = ((wDim.width - dDim.width) / 2) + 'px';
    
    if (docDim.height > wDim.height) {
        wDim.height = docDim.height;
    }

    bgDiv.style.width = wDim.width + 'px';
    bgDiv.style.height = wDim.height + 'px';

    Element.show(div);
    Element.show(bgDiv);
}

function closeModalWindow() {
Element.hide('modalWindow');
Element.hide('modalBackgroundDiv');
}

function getBrowserWindowSize() {
var winW = 630, winH = 460;

if (parseInt(navigator.appVersion)>3) {
    if (navigator.appName=="Netscape") {
        winW = window.innerWidth;
        winH = window.innerHeight;
    }
    if (navigator.appName.indexOf("Microsoft")!=-1) {
        winW = document.body.offsetWidth;
        winH = document.body.offsetHeight;
    }
}

var rval = {
    width: winW,
    height: winH
};

return rval;
}

