﻿/// <reference path="./jquery-1.4.4-vsdoc.js">

function initializeRequest(sender, args) 
{ 
    if (prm.get_isInAsyncPostBack()) 
        args.set_cancel(true); 

    postBackElement = args.get_postBackElement();
    setUpdatePanelsByPostBack(postBackElement);

    if (updatePanelElement != null && updateProgressElement != null) {
        updatePanelElement.style.display = 'none'; 
        updateProgressElement.style.display = 'block'; 
    }
} 

function endRequest(sender, args) 
{ 
    setUpdatePanelsByPostBack(postBackElement);
    if (updatePanelElement != null && updateProgressElement != null) {
        updatePanelElement.style.display = 'block'; 
        updateProgressElement.style.display = 'none'; 
    }
}

function setUpdatePanelsByPostBack(postBackElement)
{
    if (postBackElement.id.indexOf('_noticias1_') != -1) {
        updatePanelElement    = $get('ctl00_ContentPlaceHolderBody_noticias1_UpdatePanelNoticias');
        updateProgressElement = $get('ctl00_ContentPlaceHolderBody_noticias1_UpdateProgressNoticias');
        return;          
    }
        
    if (postBackElement.id.indexOf('_cotizaciones1_') != -1) {
        updatePanelElement = $get('ctl00_ContentPlaceHolderBody_cotizaciones1_UpdatePanelCuadro');
        updateProgressElement = $get('ctl00_ContentPlaceHolderBody_cotizaciones1_UpdateProgressCuadro');
        return;          
    }

    updatePanelElement = null;
    updateProgressElement = null;
}

function actualizarPagina()
{
    ticker1.changeStatusText("Actualizando...");
    
    var pedido = new SitioNosisWeb.Servicios.DtoPedidoActualizacionPagina();
    pedido.IdTicker = ticker1.idTicker;
    pedido.IdCuadro = cotizaciones1.idCuadro;
    
    SitioNosisWeb.Servicios.ServiciosNym.ActualizarPagina(
        pedido,
        onServiceSucceeded,
        onServiceFailed);
}

function onServiceSucceeded(result, eventArgs)
{
    // Actualizar ticker            
    var dtoTicker = result.DtoTicker;
    if (dtoTicker != null) 
    {
        for (var i = 0; i < dtoTicker.Cotizaciones.length; i++)
        {
            var cot = dtoTicker.Cotizaciones[i];
            ticker1.changeItemData(cot.IdEspecie, cot.Valores.ULT, cot.Valores.PVAR);
        }
    }

    // Actualizar cuadro
    var dtoCuadro = result.DtoCuadro;
    if (dtoCuadro != null) 
    {
        // Solo actualiza si el id no fue cambiado por un click en otra solapa
        if (dtoCuadro.IdCuadro == cotizaciones1.idCuadro)
        {
            for (var i = 0; i < dtoCuadro.Cotizaciones.length; i++)
            {
                var cot = dtoCuadro.Cotizaciones[i];
                for (var campo in cot.Valores)
                {
                    cotizaciones1.changeItemData(cot.IdEspecie, campo, cot.Valores[campo]);
                }
            }
        }
    }
    
    // Actualizar status en el ticker
    ticker1.changeStatusText(result.TextoUltimaActualizacion);
    
    // Restablecer timer
    setTimeout('actualizarPagina()', delayActualizarPagina);
}

function onServiceFailed(error)
{
    // Restablecer timer sileciosamente
    setTimeout('actualizarPagina()', delayActualizarPagina);
}

function OpenInformesComercioInternacional(ev) {

    ev.stopPropagation();

    jQuery(this).find(".ici-button:eq(0)").focus();

    // Abrir popup Innformes Comercio Internacional
    var url = "Controles/InformesComercioInternacional.aspx",
        popup = jQuery("<div></div>").appendTo("body");

    popup.dialogPlugin({
        width: 761,
        height: 490,
        resizable: false,
        intTieneIframe: true,
        intIframeScrolling: "no",
        intSrcIframe: url,
        close: function (event, ui) {
            OpenInformesComercioInternacionalCerrar(popup);
        },
        modal: false
    });
    popup.dialogPlugin("fn", "open");
    window["PopUpContainerDiv"] = popup;

    return false;
}

window["OpenInformesComercioInternacionalTitulo"] = function (popup, titulo) {
    popup.dialogPlugin("option", "title", titulo);
}

window["OpenInformesComercioInternacionalCerrar"] = function (popup) {
    popup.find("iframe").remove();
    popup.dialogPlugin("fn", "destroy").remove();
}

jQuery(function () {
    // Envia informacion del cliente
//    var info = jQuery.browserInfo(), data = new SitioNosisWeb.Servicios.DtoBrowserInfo();
//    if (!info.error) {
//        data.browserType = info.browserType;
//        data.browserVersion = info.browserVersion;
//        data.screenWidth = info.screenWidth;
//        data.screenHeight = info.screenHeight;
//        SitioNosisWeb.Servicios.ServiciosJavascript.LogBrowserInfo(data, function () { }, function () { });
//    }

    jQuery('#informe-comercion-internacional-ref')
        .bind('click', OpenInformesComercioInternacional)
        .bind('keypress', OpenInformesComercioInternacional);
});
