﻿//
// This page is included on Orion's Master Pages just before the closing body tag.
//


// Eval Banner Handling.
$('#evalMsg span').click(function() {
    var html = '<div id="evalDetails">' +
               '  <div style="padding: 3px 10px;">' +
               '    <div>' +
               '       <p>You have the following SolarWinds Orion free trails installed:' +
               '       <ul id="evalDetailsList"/>' +
               '    </div>' +
               '    <div style="position:absolute;bottom:10px;right:10px;padding-right:5px;cursor:pointer;" >' +
               '       <img src="/Orion/images/Button.Close.gif" alt="Close" id="evalDetailsClose" />' +
               '    </div>' +
               '  </div>' +
               '</div>';
    var dlg = $(html);

    $('#evalDetailsClose', dlg).click(function() {
        dlg.dialog('close');
    });

    var items = '';
    $.each(EvalBannerDetails, function() {
        var day = this.DaysRemaining == 1 ? " day" : " days";
        var countTxt = this.IsExpired ? "Expired" : this.DaysRemaining + day + " left";
        items += '<li><b>' + this.DisplayName + '</b> - ' + countTxt + '</li>';
    });
    $('#evalDetailsList', dlg).empty().append(items);

    dlg.dialog({
        title: 'Evaluation Details',
        width: 500
    });

});

// SWTabs
$(function() {
    var url = window.location + "";
    if (url.toLowerCase().search("login.aspx") > 0) {
        return true;
    }

    var tabCount = $('div.tabs ul.tabNavigation a').length;

    if (tabCount == 0) {
        return true;
    }

    var $currentTab = '';
    var $selectedTab = '';
    var overTimeout = 0;
    var outTimeout = 0;
    var sensitiv = 1;

    sensitiv = OrionTabSettings.TabsSensitivity;
    overTimeout = OrionTabSettings.TabsMouseOverTimeout;
    outTimeout = OrionTabSettings.TabsMouseOutTimeout;

    var tabContainers = $('div.tabs > div');
    var redirect = false;
    var mouseOver = false;
    var mouseOut = false;
    var hash;
    var allowSetActiveTabIdCallback = true;

    var isAdmin = window.location.pathname.match(new RegExp('Orion[/a-zA-Z]+Admin')) != null;  //temporary approach - will be reworked for all pages
    var tabsConfig = {
        sensitivity: sensitiv,
        interval: 0,
        over: function(e) { },
        timeout: outTimeout,
        out: function(e) {
            // if e doesn't exist, get it.
            if (!e) var e = window.event;
            // this is the element we've moved to
            var reltg = (e.relatedTarget) ? e.relatedTarget : e.toElement;
            // if reltg is null - cursor is out of browser window
            if (reltg != null && reltg != undefined) {
                while (reltg.id != 'tabs' && reltg.tagName != 'BODY' && reltg.tagName != 'HTML')
                    reltg = reltg.parentNode;
                if (reltg.id == 'tabs') {
                    // we found tg as a parent. So this is not really leaving the element
                    return;
                }
            }
            // return to current tab
            mouseOut = true;
            if (!isAdmin) {
                $("div[id*='tabs-empty']").css('display', 'none');
                $("a[href='" + $currentTab + "']").click();
            }
            else {
                $("div[id*='tabs-']").css('display', 'none');
                $("div[id*='tabs-empty']").css('display', 'block');

                $('div.tabs ul.tabNavigation a').each(function(idx) {
                    $(this).css('backgroundImage', 'url(/Orion/images/NavigationItems/tab_' + this.id + '_off.gif)');
                });
            }
        }
    };

    var tabConfig = {
        sensitivity: sensitiv,
        interval: overTimeout,
        over: function(e) {
            mouseOver = true;
            mouseOut = false;
            $(this).click();
        },
        timeout: 0,
        out: function(e) { }
    };

    $("div[id='tabs']").hoverIntent(tabsConfig);
    $('div.tabs ul.tabNavigation a').hoverIntent(tabConfig);

    $('div.tabs ul.tabNavigation a').click(function() {
        tabContainers.hide().filter(this.hash).show();
        $('div.tabs ul.tabNavigation a').removeClass('selected');
        $(this).addClass('selected');

        $('div.tabs ul.tabNavigation a').each(function(idx) {
            $(this).css('backgroundImage', 'url(/Orion/images/NavigationItems/tab_' + this.id + '_off.gif)');
        });

        $(this).css('backgroundImage', 'url(/Orion/images/NavigationItems/tab_' + this.id + '_on.gif)');
        hash = this.hash;

        $selectedTab = $(this).href;
        if (checkCookie()) {
            setCookie('SelectedTab', this.hash.replace('#tabs-', ''), 'hours', 2);
        }

        if (allowSetActiveTabIdCallback) {
            $.ajax({
                type: 'Post',
                url: '/Orion/Services/TabsManager.asmx/SetActiveTabId',
                data: "{'tabId': '" + this.hash.replace('#tabs-', '') + "'}",
                contentType: 'application/json; charset=utf-8',
                dataType: 'json',
                success: function() {
                },
                error: function() {
                    redirect = true;
                    mouseOver = false;
                    mouseOut = false;
                }
            });
        }

        if (redirect && !mouseOver && !mouseOut) {
            $currentTab = $selectedTab;
            isAdmin = false;
            window.location.href = tabContainers.filter(hash).find('.navLink').filter(':first')[0].href;
        }
        redirect = true;
        mouseOver = false;
        mouseOut = false;

        return false;
    });

    $('UL.tabNavigation LI A').hover(function() {
        $(this).css('backgroundImage', 'url(/Orion/images/NavigationItems/tab_' + this.id + '_mo.gif)');
    },
        function() {
            if ($(this).is('.selected')) {
                $(this).css('backgroundImage', 'url(/Orion/images/NavigationItems/tab_' + this.id + '_on.gif)');
            }
            else {
                $(this).css('backgroundImage', 'url(/Orion/images/NavigationItems/tab_' + this.id + '_off.gif)');
            }
        });

    $('a.navLink').click(function() {
        if (this.target != '_blank') {
            $currentTab = $selectedTab;
            isAdmin = false;
            window.location.href = this.href;
            return false;
        }
        else {
            window.open(this.href);
            return false;
        }
    })

    // Set the current tab...
    if (!isAdmin) {
        allowSetActiveTabIdCallback = false;
        $("a[href='#tabs-" + OrionTabSettings.CurrentTabId + "']").click();
        allowSetActiveTabIdCallback = true;
    }

    $currentTab = '#tabs-' + OrionTabSettings.CurrentTabId;
    $selectedTab = '#tabs-' + OrionTabSettings.CurrentTabId;
});


