﻿/*CENTRA UN OGGETTO NELLA PAGINA*/
$.fn.center = function() {
    this.css("position", "absolute");
    this.css("top", ($(window).height() - this.height()) / 2 + $(window).scrollTop() + "px");
    this.css("left", ($(window).width() - this.width()) / 2 + $(window).scrollLeft() + "px");
    return this;
}

/*VISUALIZZA DIALOG*/
function showPopWin(url, width, height, returnFunc, title, showCloseBox) {
    var pdf = $('.ifPDF');
    if (pdf.length > 0)
        pdf.hide();

    gReturnFunc = returnFunc;
    var html = '<div id="divLoading" style="text-align: center;display:block;"><font color="gray" face="tahoma, verdana, arial" size="2"></font><br />Caricamento in corso...<br /><img alt="Caricamento in corso" src="'+parent.currentLocation+'StatPortal_RSC/images/loading.gif" /></div>';
    html += '<div id="divContent" style="display:none"><iframe id="modalIframeId" width="100%" height="' + height + 'px" marginWidth="0" marginHeight="0" frameBorder="0" scrolling="auto" /></div>';
    $("#divId").html(html).dialog('open');
    $("#divId").dialog("option", 'title', title);
    $("#divId").dialog("option", 'width', width + 50);
    $("#divId").dialog("option", 'position', [($(window).width() - width+50) / 2 + $(window).scrollLeft(), ($(window).height() - height) / 2 + $(window).scrollTop()]);    
    
    

    //imposto l'url al frame
    $("#modalIframeId").attr("src", currentLocation + 'WindowPage.aspx?Control=' + url);

    //imposto le dimensioni al contenuto dell'iframe
    $('#modalIframeId').load(function() {
        $('#divLoading').hide();
        $('#divContent').show();
        $(this.contentDocument).find('#window_table').width(width-10);
        $(this.contentDocument).find('#window_table').height(height-10);
    });
}

function ahowAlert(alertText) {
    $('.divAlert').html("<p class='ui-state-error-text'>" + alertText + "</p>");
    $('.divAlert').dialog({ dialogClass: 'ui-state-error' });
}

/*NASCONDE DIALOG*/
function hidePopWin(callReturnFunc, returnVal) {

    //se c'è un iframe con un pdf nascosto lo devo visualizzare!
    var pdf = $('.ifPDF');
    if (pdf.length > 0)
        pdf.show();

    if (callReturnFunc == true && gReturnFunc != null) {

        // Set the return code to run in a timeout.
        // Was having issues using with an Ajax.Request();
        //gReturnVal = window.frames["popupFrame"].returnVal;		
        gReturnVal = returnVal;
        window.setTimeout('gReturnFunc(gReturnVal);', 1);
    }

    $('#divId').dialog('close');
}

/*IMPOSTA TITOLO DI UNA DIALOG*/
function setPopTitle(title) {
    $("#divId").dialog("option", 'title', title);
}

//QUESTO SERVE PER GESTIRE DA JQUERY I PARAMETRI PASSATI DA URL
$.extend({
    getUrlVars: function() {
        var vars = [], hash;
        var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
        for (var i = 0; i < hashes.length; i++) {
            hash = hashes[i].split('=');
            vars.push(hash[0]);
            vars[hash[0]] = hash[1];
        }
        return vars;
    },
    getUrlVar: function(name) {
        return $.getUrlVars()[name];
    }
});


