﻿$(document).ready(function () {
ProcessImn();
    //1: Hide all top level links in SharePoint menu (our CSS adds our own images)
    $(".s4-tn").find("div").children("ul").children("li").children("a").each(function () {
        //set CSS style for top-menu hyperlink
        $(this).addClass('hiddenMenuText');
        ($(this).html(""));
    });

    //2: Wire up menu item hover CSS (defined in custom CSS above)
    var i = 1;
    $(".s4-tn").find("div").children("ul").children("li").each(function () {
        //apply top-menu class
        $(this).addClass('topNavDropDown' + i);
        i++;
    });

    var i = 1;

    //3: Hide the SharePoint-default 'Home' link
    //$(document).ready(function () {
   //     (($("a:contains('CIBMTR')")).filter("*^[class^=static selected menu-item]").hide());
   // });

    // CUSTOM SEARCH ACTIONS:
    $('#searchbutton').click(function () {
        var searchtext = $('#searchbox').val();
        var searchscope = $('#se_realm').val();
        window.location = String.format("/_layouts/OSSSearchResults.aspx?k={0}&s={1}", searchtext, searchscope);
    });

    // check to see if the developer dashboard should be shown. in order for this to work, the dashboard
    // needs to be turned on via an stsadm command or powershell script
    if (getParameterByName('ddb') == 1)
        $('#DeveloperDashboard').show();
    else
        $('#DeveloperDashboard').hide();

    // START SIDE NAV
    // ==============
    //this insures that we're only running this code when the side-nav is available:
    if ($('#side-nav').length > 0) {
        //set the 4th and 5th level selected menu items bold (for some reason this doesn't work in straight css):
        $('#side-nav ul.root > li.static > ul.static > li.static > ul.static > li.static > a.selected > span.additional-background > span.menu-item-text').css('font-weight', 'bold');
        $('#side-nav ul.root > li.static > ul.static > li.static > ul.static > li.static > ul.static > li.static > a.selected > span.additional-background > span.menu-item-text').css('font-weight', 'bold');

        //implode all items
        $('#side-nav ul.static.root ul.static').each(function () {
            $(this).hide();
        });

        //explode select items in selected menu
        $('#side-nav div.vertical li.static.selected > ul.static').show();
        $('#side-nav div.vertical li.static.selected').parents('#side-nav div.vertical ul.static').show();

        //if the selected menu item has an arrow but no sub menu items, we don't want to turn the arrow down
        if ($('#side-nav div.vertical li.static.selected > ul.static').length == 0) {
            var background_image = $('#side-nav div.vertical li.static a.selected > span.additional-background').css("background-image");
            if (background_image != null) {
                background_image = background_image.replace("menu-selected-", "menu-item-");
                $('#side-nav div.vertical li.static a.selected > span.additional-background').css('background-image', background_image);
            }
        }

        //now go up the chain and turn down any parent menu arrows
        var parentLI = $('#side-nav div.vertical li.static.selected').parent('ul.static').parent('li.static')
        if (parentLI != null) {
            var background_image = $(parentLI).children('a.static.menu-item').children('span.additional-background').css("background-image");
            if (background_image != null) {
                background_image = background_image.replace("menu-item-", "menu-selected-");
                $(parentLI).children('a.static.menu-item').children('span.additional-background').css("background-image", background_image);
            }
        }

        var parentParentLI = $('#side-nav div.vertical li.static.selected').parent('ul.static').parent('li.static').parent('ul.static').parent('li.static')
        if (parentParentLI != null) {
            var background_image = $(parentParentLI).children('a.static.menu-item').children('span.additional-background').css("background-image");
            if (background_image != null) {
                background_image = background_image.replace("menu-item-", "menu-selected-");
                $(parentParentLI).children('a.static.menu-item').children('span.additional-background').css("background-image", background_image);
            }
        }
    }
    // END SIDE NAV
    // +===========

    // this looks for a keystroke pattern of ctrl+shift+r to show the ribbon
    // we do a redirect, as opposed to just toggling on the ribbon, so sharepoint can detect there's no ribbon,
    // and adjust the heights of the content appropriately.
    // we detect the F5 key (116) so that the refresh can work.
    $(document).keydown(function (e) {
        if (e.keyCode == 17) { ks = "a"; return false; }
        if (e.keyCode == 16) { if (ks == "a") { ks = ks + "b"; return false; } }
        if (e.keyCode == 82) {
            if (ks == "ab") {
                ks = "";

                /*if (getParameterByName('ribn') == 1)
                window.location = window.location.pathname + "?ribn=0";
                else
                window.location = window.location.pathname + "?ribn=1";*/

                if ($('#s4-ribbonrow').css('display') == "none")
                    ShowRibbon();
                else
                    HideRibbon();

                return false;
            }
        }
        return true;
    });

    //Ribbon Handeling On Page Load:
    if (getParameterByName('ribn') != '')
        QueryToTurnOnRibbon();
    else
        GetRibbonCookie();

    //Handle Print View
    if (getParameterByName('IsPrint') == 1) {
        $('#PrintLaunch').hide();
        print();
    }
});

function GetRibbonCookie() {
    if ($.cookie != null) {
        var c = $.cookie("RibbonState");
        if (c == "ON")
            ShowRibbon();
        else
            if (c == null || c == "OFF")
                HideRibbon();
    }
}

function QueryToTurnOnRibbon() { if (getParameterByName('ribn') == "1") { ShowRibbon(); } else { HideRibbon(); } }

function ShowRibbon() {
    if ($.cookie != null) {
        $('#s4-ribbonrow').show();
        $('#s4-workspace').height($(document).height() - $('#s4-ribbonrow').height() * 2);
        $.cookie("RibbonState", "ON", { path: "/" });
    }
}

function HideRibbon() {
    if ($.cookie != null) {
        $('#s4-ribbonrow').hide();
        var newHeight = $(document).height();
        if ($.browser.msie) { newHeight = newHeight - 3; }
        $('#s4-workspace').height(newHeight);
        $.cookie("RibbonState", "OFF", { path: "/" });
    }
}

function getParameterByName(name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.href);
    if (results == null)
        return "";
    else
        return decodeURIComponent(results[1].replace(/\+/g, " "));
}

/*
//launch a SharePoint modal dialog
function launchModal(sUrl, iWidth, iHeight) {
var options = SP.UI.$create_DialogOptions();
options.url = sUrl;
options.width = iWidth;
options.height = iHeight;
options.dialogReturnValueCallback = Function.createDelegate(null, CloseCallback);
SP.UI.ModalDialog.showModalDialog(options);
//modalDialog = SP.UI.ModalDialog.showModalDialog(options);		
}

function closeWindow(updateMessage) {
SP.UI.ModalDialog.commonModalDialogClose(updateMessage);
}

// Dialog callback 
function CloseCallback(result, target) {
if (result != '' && result != '0') {
messageId = SP.UI.Notify.addNotification(result, false);
}
}
*/
function printerFriendly() { makeNewWindow(document.URL , "print", 700); }

function makeNewWindow(url, name, winW, winH) {
if (!(winH)) { winH = (screen.availHeight) ? screen.availHeight - 300 : 400; }
if (!(winW)) { winW = (screen.availWidth) ? screen.availWidth - 200 : 600; }
var options = "resizable,titlebar,scrollbars,location,menubar,personalbar,status,toolbar,height=" + winH + ",width=" + winW;
if (name == "Email") {
options = "resizable,titlebar,scrollbars,height=" + winH + ",width=" + winW;
}
var newWindow = window.open("", name, options);
if (newWindow.focus) { newWindow.focus(); }
newWindow.location = url;
}

if (window.name && window.name == "print") {
document.write('<link rel="stylesheet" id="print" type="text/css" href="/siteassets/cibmtr/print.css" media="all">');
}

function ProcessImn()
{
       if (typeof(ctx)=="undefined") return;
       if (ctx.CurrentUserId == -1) return;
       if (EnsureIMNControl() && IMNControlObj.PresenceEnabled)
       {
              imnElems=document.getElementsByName("imnmark");
              imnElemsCount=imnElems.length;
              ProcessImnMarkers();
       }
}



/*
//used to activate current section navigation highlighting
function switchIt(objID) {
var cname = "active";
if (objID.className) { objID.className = cname; }
else if (document.getElementById(objID)) { document.getElementById(objID).className = cname; }
}

function AlertCustom(alertText, timeout) {
var ScrollTop = $('#s4-workspace').scrollTop();
$('#CustomAlert').show();
$('#CustomAlert').html(alertText);
$('#CustomAlert').animate({ "top": ScrollTop })
if (timeout != null) {
if (timeout > 0) {
$('#CustomAlert').delay(timeout).animate({ "top": "-30" });
}
}
}

//launch a SharePoint modal dialog
function launchModal(sUrl, iWidth, iHeight) {
//var options = SP.UI.$create_DialogOptions(); 
var options = SP.UI.$create_DialogOptions;
options.url = sUrl;
options.width = iWidth;
options.height = iHeight;
options.dialogReturnValueCallback = Function.createDelegate(null, CloseCallback);
SP.UI.ModalDialog.showModalDialog(options);
}

function closeWindow(updateMessage) {
SP.UI.ModalDialog.commonModalDialogClose(updateMessage);
}

// Dialog callback 
function CloseCallback(result, target) {
if (result != '' && result != '0') {
messageId = SP.UI.Notify.addNotification(result, false);
}
}

function launchModalFrame(url, isSharePoint, modalHeight, modalWidth) {
if (isSharePoint == 'True') {
if (url == 'login.aspx')
ExecuteOrDelayUntilScriptLoaded(launchLogin, 'sp.js');
else
launchModal(url, modalWidth, modalHeight);
}
else {
//jQuery modal in web test project
//set iframe src
document.getElementById('frameModal').src = url;

//open results dialog
jQuery('#dialog').dialog('open');
}
}

function launchLogin() {
launchModal('/sitepages/login.aspx', 650, 600);
}

function launchProfile() {
launchModal('/sitepages/login.aspx?action=update', 650, 600);
}

function launchCreateAccount() {
launchModal('/sitepages/login.aspx?action=create', 650, 600);
}

function DoSearch() {
var searchText = $('#Nav_Searchbox').val();
window.location = String.format("_layouts/OSSSearchResults.aspx?k={0}" + searchText);
}*/
