﻿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() {

    $(".toolbox li[userdata]").each(function(i, el) {
        var $this = $(this);
        var tipId = '#' + $(this).attr('userdata');
        var $tip = $(tipId);

        // Get the parent element ul element's height - the commented out section would align the top and height of the box with the overall toolbox including base/topper
        // var parentHeight = $(this).parent().parent().parent().parent().height() + $(this).parent().parent().parent().parent().prev().height() + $(this).parent().parent().parent().parent().next().height();
        // var parentOffset = $(this).parent().parent().parent().parent().prev().offset();
        var parentHeight = $this.parent().parent().parent().parent().height();
        var parentOffset = $this.parent().parent().parent().parent().offset();

        var thisOffset = $this.offset();
        $tip.find("div.contentbody").height(0);
        var toolHeight = $tip.height();
        if (toolHeight < parentHeight) {
            $tip.find("div.contentbody").height(parentHeight - toolHeight);
        }

        // Now figure out how much to adjust the top offset
        var topOffset = thisOffset.top;
        var newOffset = $tip.height() - (thisOffset.top - parentOffset.top); // +10;

        $this.tooltip({
            tip: tipId,
            position: 'top right',
            offset: [newOffset, -5],
            delay: 20,
            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"); },
            onHide: function() { $this.removeClass("tooltip_visible"); }
        });
    });

    $(document).bind('cbox_closed', function() {
        $('#sendrequest').hide();
        $('#requestsuccess').hide();
        $('#requesterror').hide();
    });

    $(".tooltips .requestaccess").each(function(i, el) {
        var appId = $(this).attr('appid');
        var appName = $(this).attr('appname');
        $(this).click(function() {
            var $anchor = $(this);
            $('#appname').html('<h2>' + appName + '</h2>');
            $.fn.colorbox({ href: '#sendrequest', open: true, inline: true, width: '300px', height: '300px' });
            $('#sendrequest').show();

            $.ajax({
                type: "POST",
                url: rootpath + "/GeneralHandler.ashx",
                global: true,
                data: { type: 'requestAccess', 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 + ")");
                    $('#sendrequest').hide();
                    if (responseData.message == "success") {
                        $.fn.colorbox({ href: '#requestsuccess', open: true, inline: true, width: '300px', height: '300px' });
                        $('#requestsuccess').show();
                        var div = $("<div class='coloredtext' style='width:100%;text-align:center'>" + responseData.displayText + "<i>" + responseData.displayName + "</i></div><br/>");
                        $(div).insertAfter($anchor);
                        $anchor.remove();
                    }
                    else {
                        $.fn.colorbox({ href: '#requesterror', open: true, inline: true, width: '300px', height: '300px' });
                        $('#requesterror').show();
                    }
                },
                error: function(xhr, msg, e) {
                    $('#sendrequest').hide();
                    $.fn.colorbox({ href: '#requesterror', open: true, inline: true, width: '300px', height: '300px' });
                    $('#requesterror').show();
                }
            });
            return false;
        });
    });
}); 
