﻿
$(document).ready(function () {

    // Set up for displaying modal dialogs
    $('a[href=#modalwindow]').click(function (e) {
        // Prevent the default link behavior of navigation so we can use the link to show the Window
        e.preventDefault();

        // Determine which href was clicked if it was in fact an href(though this demo as only one.) 
        var id = $(this).attr('href');
        var codProce = $(this).attr('id');
       $("input[id$=hfCodProcesso]").val(codProce);
      
        // Determine browser windows dimensions. 
        var maskHeight = $(document).height();
        var maskWidth = $(window).width();

        // Set dimensions for the mask to opaque the screen when the modal window is displayed.
        $('#mask').css({ 'width': maskWidth, 'height': maskHeight });

        // Make the Window Opaque		
        $('#mask').fadeIn("fast");
        $('#mask').fadeTo("slow", 0.8);

        //Get the window height and width
        var winH = $(window).height();
        var winW = $(window).width();

        // Set the Modal Window's dimensions to center in the browser window.
        $(id).css('top', winH / 2 - $(id).height() / 2);
        $(id).css('left', winW / 2 - $(id).width() / 2);

        // Show the Modal Window
        $(id).fadeIn("fast");

    });

    // Handle Close Button Click Event
    $('.window .close').click(function (e) {
        // Cancel the link behavior
        e.preventDefault();

        $('#mask').hide();
        $('.window').hide();
    });

    // The user clicks OUTSIDE the Modal Window and the window will be closed without save.
    $('#mask').click(function () {
        $(this).hide();
        $('.window').hide();
    });

});

function openProcesso(codPro, titProc) {
    var imgIndicadorProcesso = document.getElementById("imgIndExeProcAGV");
    imgIndicadorProcesso.style.display = "";
    var idCanal="";
    var hfCodGrupoCanal = $("input[id$=hfCodCanal]");
    if (hfCodGrupoCanal[0] != null)
        idCanal = hfCodGrupoCanal[0].value;
    else
        idCanal = "5";
    openProcessoNew(codPro, titProc,idCanal);
}

function desableIndicadorProcesso() {
    var imgIndicadorProcesso = document.getElementById("imgIndExeProcAGV");
    imgIndicadorProcesso.style.display = "none";

}

function openProcessoNew(codPro, titProc, idCanal) {
    $.ajax({
        type: "POST",
        url: "/_vti_bin/ValidaServicoAGV.asmx/validaProcesso",
        data: "{idProc:'" + codPro + "',titProc:'" + titProc + "',idCanal:'" + idCanal + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (result) {

            desableIndicadorProcesso();
            if (result.d.urlPopUP == "X") {
                confirmReloadPage('Favor efetuar o login novamente.');
            }
            else {
                if (result.d.urlPopUP == "TE") {
                    confirmReloadPage('Tempo de acesso excedido, favor efetuar o login novamente.');
                }
                else {
                    if (result.d.urlPopUP == "Erro") {
                        exibeAlerta('Erro ao executar o serviço ' + titProc + " Por favor tente mais tarde.");
                    }
                    else {
                        openNewWindow(result.d.urlPopUP, result.d.nomePopUp, result.d.atributosPopUp, result.d.titProc);
                    }
                }
            }

            $('#mask').hide();
            $('.window').hide();
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            desableIndicadorProcesso();
            alert(XMLHttpRequest.responseText);
            $('#mask').hide();
            $('.window').hide();
        }

    });
};

function openNewWindow(url, nomePopUp, atributosWindows,titProc) {
    var popUp=window.open(url, nomePopUp, atributosWindows);
    if (popUp == null || typeof (popUp) == 'undefined') {
         exibeAlerta('Por favor desabilite seu bloqueador de  pop-up e clique novamente no serviço ' + titProc+".");
    }
    else {
        popUp.focus();
    }
}

function exibeAlerta(msg) {
    jAlert(msg, 'Alerta');
}
//$(function () {
//    $("ul#ticker01").liScroll();
//});

$(document).ready(function () {
    $('#ticker01').innerfade({
        animationtype: 'slide',
        speed: 'slow',
        timeout: 10000,
        type: 'sequence',
        containerheight: '30px',
        runningclass: 'newsticker'
    });
});


function confirmReloadPage(msg) {
    jAlert(msg, 'Alerta', function (r) {
        if (r) {
            var sURL = unescape(window.location.pathname);
            window.location.replace(sURL);
        }
    });
}

function AGVCemigTabs(targetCell,codGrupo,titCanal) {

    if (targetCell.className == "on")
        return;
   
    $("input[id$=hfCodProcesso]").val("");

    var lblTitModal = $("span[id$=lblModalTitCanal]");
    var lblTitLogin = $("span[id$=lblTitCanalOn]");
    var hfCodGrupoCanal = $("input[id$=hfCodCanal]");
    if (hfCodGrupoCanal[0] != null)
        hfCodGrupoCanal[0].value=codGrupo;
    if (lblTitModal[0] != null) {
        lblTitModal[0].innerHTML = titCanal;
    }
    if (lblTitLogin[0] != null) {
        lblTitLogin[0].innerHTML = titCanal;
    }
    var tabsRow = document.getElementById("TabsRow");

    var lblResultLogin = $("span[id$=lblResultLogin]");
    if (lblResultLogin[0] != null) {
        lblResultLogin[0].innerHTML ="";
    }
   lblResultLogin= $("span[id$=lblStatusLogin]");
   if (lblResultLogin[0] != null) {
       lblResultLogin[0].innerHTML = "";
   }
    for (var i = 0; i < tabsRow.cells.length; i++) {
        var cell = tabsRow.cells[i];
        if (cell.className == "on") {
            cell.className = "off";
        }
    }
    targetCell.className = "on";

    var contentRow = $("div[id$=ContentRow]");
    cleanWhitespaceCemigTabs(contentRow[0]);
    if (contentRow != null) {
        for (x = 0; x < contentRow[0].childNodes.length; x++) {
            var cell = contentRow[0].childNodes[x];
            cell.style.display = "none";
        }

        var idConteudoAba = new String();
        idConteudoAba = targetCell.id;

        if (idConteudoAba.indexOf("OffAgv") != -1)
            idConteudoAba = "OffAgv";

        var divAbas = $("div[id$=aba" + idConteudoAba + "]");
        var divAba = divAbas[0];
        if (!(divAba == null))
            divAba.style.display = "";
    }
}

var notWhitespace = /\S/;

function cleanWhitespaceCemigTabs(node) {
    if (node!= null) {
        for (var x = 0; x < node.childNodes.length; x++) {
            var childNode = node.childNodes[x];
            if ((childNode.nodeType == 3) && (!notWhitespace.test(childNode.nodeValue))) {
                // that is, if it's a whitespace text node
                node.removeChild(node.childNodes[x]);
                x--;
            }
            if (childNode.nodeType == 1) {
                // elements can have text child nodes of their own
                cleanWhitespaceCemigTabs(childNode);
            }
        }
    }
}




