/*
* Yo!
*/
var errFunc = function(xhr, desc, exceptionobj) {
    if (confirm('AJAX Error. Do you want see returned value?')) {
        alert(xhr.responseText);
    }
    popupHide();
}

$(document).keydown(function(e) {
    if (e.which == 27) {
        popupHide();
    }
});
$(function(){
    jQuery.preloadImages = function() {
        for (var i = 0; i<arguments.length; i++) {
            jQuery('<img>').attr('src', arguments[i]);
        }
    }
    
    $.preloadImages('images/close.png', 'images/loading2.gif');
});

var popupWidth    = 200;
var popupHeight   = 170;
var popupTitle    = 'Loading...';
var popupContent  = '<img src="images/loading2.gif" width="31" height="31" alt="" /><br /><br />Please wait...';
var popupFormdata = '';
var isLoadingNow  = false;
function popupShow(uri) {
    $('#popup,#popup-overlay').remove();
    $('body').append('<div id="popup-overlay"></div><div id="popup"><div id="popup-header"><div id="popup-title"></div><div id="popup-close"><img src="images/close.png" alt="X" title="Close" /></div></div><div id="popup-content"></div></div>');
    $('#popup,#popup-overlay').hide();
    
    $('#popup-overlay,#popup-close').unbind().click(function(){ popupHide(); });
    
    $('#popup-overlay').css(getOverlayCss()).fadeIn(500, function(){
        $('#popup').css(getPopupCss());
        $('#popup-title').html(popupTitle);
        $('#popup-content').html('<div align="center" style="padding-top:'+(popupHeight / 2 - 30)+'px;">'+popupContent+'</div>');
        
        $('#popup').fadeIn(300, function(){
            popupLoad(uri, '');
        });
        
        isLoadingNow = true;
    });
}
function popupLoad(uri, formName) {
    var winData = {};
    popupFormdata = '';
    
    if (!formName) {
        formName = '';
    }
    if (formName != '') {
        popupFormdata = $('#'+formName).serialize();
    }
    
    if (!isLoadingNow) {
        $('#popup-content').fadeOut(300, function(){
            $('#popup-title').html(popupTitle);
            $('#popup-content').html('<div align="center" style="padding-top:'+(popupHeight / 2 - 30)+'px;">'+popupContent+'</div>');
            $('#popup-content').fadeIn(100, function(){
                sendRequest(uri, formName);
            });
        });
    }
    else {
        sendRequest(uri, formName);
    }
}
function popupHide() {
    $('#popup').hide();
    $('#popup-overlay').fadeOut();
}
function getPopupCss() {
    var popupCss = {
        'width': popupWidth + 'px',
        'height': (popupHeight + 30) + 'px',
        'left': (($(window).width() - popupWidth) / 2) + 'px',
        'top': (($(window).height() - popupHeight - 30) / 2) + 'px'
    };
    
    return popupCss;
}
function getOverlayCss() {
    var overlayCss = {
        'width': $(document).width()+'px',
        'height': $(document).height()+'px',
        'opacity': .9
    };
    
    return overlayCss;
}
function sendRequest(uri, formName) {
    $.ajax({
        url: uri,
        type: 'POST',
        data: popupFormdata,
        error: errFunc,
        dataType: "json",
        success: function(retData){
            winData = retData;
            popupWidth  = winData.width;
            popupHeight = winData.height;
            isLoadingNow = false;
        },
        complete: function() {
            $('#popup-title').fadeOut(300);
            $('#popup-content').fadeOut(function(){
                $('#popup-title').html(winData.title);
                $('#popup-content').html(winData.content);
                $('#popup').animate(getPopupCss(), 400, function(){
                    $('#popup-title,#popup-content').fadeIn(300);
                });
            });
        }
    });
}

function winShow(url)
{
    window.open (url, "pt","location=1,status=1,scrollbars=1, width=800,height=600");
}