﻿var divPopup = "";
var divWidth = 0;
var divHeight = 0;
var srcElement = "";
var autoClose = true;

$(document).ready(function() {
    divPopup = $('<div style=\'display:none;\' class=\'divpop\'></div>').appendTo(document.body);

    $('a.more-info, img.more-info').click(function(event) {
        var xPos = -1;
        var yPos = -1;
        var animate_roll="false";
        var url = "/_popups/";
        showAllSelects();
        srcElement = $(this);

        autoClose = ($(this).attr("autoclose").toLowerCase() == "true");
        yPos = parseInt($(this).attr("yCoordinate"));
        xPos = parseInt($(this).attr("xCoordinate"));
        animate_roll = $(this).attr("animate_roll");

        if ($(this).attr("servicePath") != null && $(this).attr("servicePath") != '') {
            url = "/" + $(this).attr("servicePath") + "/_popups/" + $(this).attr("file") + "?" + $(this).attr("qs");
        }
        else {
            url = "/_popups/" + $(this).attr("file") + "?" + $(this).attr("qs");
        }
        $.ajax({ type: "GET", url: url, data: "{}", contentType: "application/x-www-form-urlencoded;", dataType: "html",
            success: function(e) {
                return renderInlineJQ(e, event, xPos, yPos, animate_roll);
            }
        });
        return false;
    });
    // hides divs
    $(document).click(function(event) {
        if ($(divPopup) != null
        && $(divPopup).css('display') != null
        && $(divPopup).css('display') != 'none') {
            if (IE) {
                thisX = event.clientX + $(window).scrollLeft();
                thisY = event.clientY + $(window).scrollTop();
            }
            else {
                thisX = event.pageX;
                thisY = event.pageY;
            }
            var posLeft = $(divPopup).offset().left;
            var posTop = $(divPopup).offset().top;

            var posRight = $(divPopup).offset().left + $(divPopup).outerWidth();
            var posBottom = $(divPopup).offset().top + $(divPopup).outerHeight();

            if ($(event.target).parents(' .divpop').length == 0
                && autoClose
                && ((thisX < posLeft || thisX > posRight)
                    || (thisY < posTop || thisY > posBottom))) {
                hideDiv();
            }
        }
    });
    $(document).keypress(function(event) {
        escKeyJQ(event);
    });
});

function escKeyJQ(evt) {
    var evt = (evt) ? evt : ((event) ? event : null);
    if (evt.keyCode == 27) {
        hideDiv();
    }
}
$.inlineObjPostJQ = function(url, data, event) {
    $.ajax({ type: "POST", url: url, data: data, contentType: "application/x-www-form-urlencoded;", dataType: "html",
        success: function(e) {
            return renderInlineJQ(e, event);
        }
    });
}

function renderInlineJQ(response, event, xPos, yPos, animate_roll) {
    var padDistance = 15;
    var srcHeight = $(srcElement).height();
    var srcWidth = $(srcElement).width();
    var srcTop = $(srcElement).offset().top;
    var srcLeft = $(srcElement).offset().left;

    divWidth = parseInt($(srcElement).attr("popWidth"));
    divHeight = parseInt($(srcElement).attr("popHeight"));
    
    var bodyHeight = $(document).height();
    var bodyWidth = $(document).width();
    var windowTop = $(window).scrollTop();

    response = replaceChars(response, '&lt;', '<');
    response = replaceChars(response, '&gt;', '>');
    response = replaceChars(response, '&amp;', '&');

    if (response.length != 0) {
        if (divPopup != null) {
            hideDiv();
        }
        $(divPopup).html(response);
        $(divPopup).hide();

        if (yPos<0)
            yPos = (srcTop + padDistance);

        if ((yPos + divHeight > windowTop + $(window).height())
            && divHeight < $(window).height()) {
            yPos = srcTop - divHeight;
        }
        if (yPos < windowTop) {
            yPos = windowTop + padDistance;
        }
        if (yPos <= 0) {
            yPos = 0;
        }

        if (xPos < 0)
            xPos = (srcLeft + srcWidth + padDistance);
        if ((xPos + divWidth) > bodyWidth) {
            xPos = (srcLeft - divWidth - padDistance);
        }
        if (xPos <= 0) {
            xPos = 0;
        }
        if (animate_roll == "True") {
            var divCss = {
                'z-Index': '100',
                'left': xPos,
                'top': yPos,
                'position': 'absolute'
            };
            $(divPopup).css(divCss);
            hideAllSelects();

            $(divPopup).slideDown(3000);
        }
        else {
            var divCss = {
                'z-Index': '100',
                'left': xPos,
                'top': yPos,
                'position': 'absolute',
                'display': 'block'
            };
            $(divPopup).css(divCss);
            hideAllSelects();

            $(divPopup).show();
        }
        
    }
}

