﻿var targetWindow;

function newToolboxWindow(url, targetName, resWidth, resHeight, appId, appName) {
    var offsetWidth = screen.width - screen.availWidth;
    var offsetHeight = screen.height - screen.availHeight;
    var adjResWidth;
    var adjResHeight;
    var resize = true;

    // Assume a maximum amount of space around the browser content and allow the content size accordingly
    if ((resWidth + 32) <= screen.availWidth && (resHeight + 90) <= screen.availHeight) {
        resize = false;
        adjResWidth = resWidth;
        adjResHeight = resHeight;
    }

    if (resize) {
        if (resWidth >= screen.width) {
            resWidth = screen.width;
            adjResWidth = screen.availWidth;
        }
        else if (resWidth <= screen.availWidth) {
            adjResWidth = resWidth;
        }
        else {
            adjResWidth = resWidth - offsetWidth >= screen.availWidth ? screen.availWidth : resWidth - offsetWidth;
        }

        if (resHeight >= screen.height) {
            resHeight = screen.height;
            adjResHeight = screen.availHeight;
        }
        else if (resHeight <= screen.availHeight) {
            adjResHeight = resHeight;
        }
        else {
            adjResHeight = resHeight - offsetHeight >= screen.availHeight ? screen.availHeight : resHeight - offsetHeight;
        }
    }

    var leftAdjust = screen.availWidth > adjResWidth ? ((screen.availWidth - adjResWidth) / 2).toString() : "0";
    var topAdjust = screen.availHeight > adjResHeight ? ((screen.availHeight - adjResHeight) / 2).toString() : "0";

    //if (document.all) {
    //adjResWidth -= (leftAdjust == "0" && resWidth == screen.width) ? 5 : 10;
    //adjResHeight -= 10;
    //topAdjust = (resHeight == screen.height) ? "5" : topAdjust;
    //}

    var width = adjResWidth.toString();
    var height = adjResHeight.toString();

    targetWindow = window.open("", targetName, "width=" + width + ",height=" + height + "outerWidth=" + width + ",outerHeight=" + height + ",top=" + topAdjust + ",left=" + leftAdjust + ",menubar=0,location=0,scrollbars=1,resizable=1,toolbar=0,status=0,titlebar=0");

    try {
        if (targetWindow.closed || (!targetWindow.document.URL) || (targetWindow.document.URL.indexOf("about") == 0)) {
            targetWindow = window.open(url, targetName, "width=" + width + ",height=" + height + "outerWidth=" + width + ",outerHeight=" + height + ",top=" + topAdjust + ",left=" + leftAdjust + ",menubar=0,location=0,scrollbars=1,resizable=1,toolbar=0,status=0,titlebar=0");
            if (resize) {

                if (document.all) {
                    targetWindow.resizeTo(adjResWidth, adjResHeight);
                }
                else {
                    targetWindow.outerWidth = adjResWidth;
                    targetWindow.outerHeight = adjResHeight;
                }
            }
        }
    }
    catch (e) { }

    if (window.focus) { targetWindow.focus() }

    if (appId && appName && appId.length > 0 && appName.length > 0) {
        $.ajax({
            type: "POST",
            url: rootpath + "/GeneralHandler.ashx",
            global: true,
            data: { type: 'accessApplication', appid: appId, appname: appName },
            contentType: "application/x-www-form-urlencoded; charset=utf-8",
            beforeSend: function (xhr) {
                xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=utf-8");
            },
            dataType: "text",
            success: function (msg) {
                var strippedMsg = msg.replace(/(<([^>]+)>)/ig, "");
                var responseData = eval("(" + strippedMsg + ")");
                if (responseData.message == "success") {
                }
                else {
                }
            },
            error: function (xhr, msg, e) {
            }
        });
    }
}

$(document).ready(function () {

    var $toolboxItems = $(".toolbox li[userdata]");
    if ($toolboxItems.length > 0) {
        var $trParent = $($toolboxItems[0]).closest('tr');
        var $tableParent = $trParent.closest('table');
        var $tableHead = $tableParent.find('thead');
        var parentHeight = $trParent.height();
        var parentOffset = $trParent.offset();
        var headHeight = $tableHead.height();
        var headOffset = ($tableHead.offset().top - $tableParent.offset().top) + 3;
        var ltIE8 = ($.browser.msie && $.browser.version < 8);

        $toolboxItems.each(function (i, el) {
            var $this = $(this);
            var tipId = '#' + $(this).attr('userdata');
            var $tip = $(tipId);

            var thisOffset = $this.offset();
            var toolHeight = $tip.height();
            var $contentBody = $tip.find("div.contentbody");
            var contentHeight = $tip.find(".content").height();
            var contentHeightAdjust = $contentBody.outerHeight() - $contentBody.height();
            var contentHeadHeight = $tip.find("div.contenthead").outerHeight();
            var contentFootHeight = $tip.find("div.contentfoot").outerHeight();
            $contentBody.height(contentHeight - contentHeadHeight - contentFootHeight - contentHeightAdjust);

            // Now figure out how much to adjust the top offset
            var topOffset = thisOffset.top;
            var newOffset = $tip.height() - headHeight - headOffset; // +10;
            var leftOffset = ltIE8 ? 0 : 0;

            $this.tooltip({
                tip: tipId,
                position: 'top right',
                offset: [newOffset, leftOffset],
                predelay: 0.5,
                delay: 0.5,
                events: {
                    def: "mouseenter,mouseleave", //  for an element  
                    input: "focus,blur", // for all input elements  
                    widget: "focus mouseover,blur mouseout", // select, checkbox, radio, button  
                    tooltip: "mouseenter,mouseleave" // the tooltip element  
                },
                effect: 'toggle',
                onBeforeShow: function () { $this.addClass("tooltip_visible"); },
                onBeforeHide: function () {
                    // Bug in firefox (or plug-in) causes screen to flash if the content
                    // body has been scrolled down.
                    if ($.browser.mozilla) {
                        $contentBody.scrollTop(0);
                    }
                },
                onHide: function () { $this.removeClass("tooltip_visible"); }
            });

            $tip.css({ display: 'none' });
        });

        $("div.tooltips").css({ left: '0px', top: '0px', position: 'static' });
    }

    $(document).bind('cbox_closed', function () {
        $('#sendrequest').hide();
        $('#requestsuccess').hide();
        $('#requesterror').hide();
    });
}); 
