﻿jQuery(document).ready(function ($) {

    $('#menu .dropdown, #utilities .dropdown').hoverIntent({ interval: 0, sensitivity: 10, over: AddMega, timeout: 250, out: RemoveMega });

    $('#browsable').scrollable({ circular: true }).navigator().autoscroll(10000);

    $('#version3 *').removeAttr('class').removeAttr('style').removeAttr('align');
    $('#version3 br, #version3 hr').remove();

    if (location.search.indexOf('version3') == -1)
        BuildMyAccountMySitesMenus();

    BuildTableOfContents();

    $('#ribbon').resize(function () {
        $('#logo').css('marginTop', $('#head').height() + 'px');
        $('#MSOTlPn_MainTD').css({ 'paddingTop': $('#head').height() + 'px' });
    }).resize();
});

jQuery.fn.outerHtml = function() {
    return $('<div>').append(this.eq(0).clone()).html();
};

AddMega = function() {
    $(this).addClass('hovering').find('.drop').mouseover(function() { $(this).prev().addClass('hover') });
};

RemoveMega = function() {
    $(this).removeClass('hovering').find('.drop').prev().removeClass('hover');
};

BuildMyAccountMySitesMenus = function () {
    var context = $('#version3 div[id^=WebPart]');

    // "Your Account" menu
    var html = '<div class="drop"><div class="down half"><dl>';
    $('div span', context).has('i').each(function () {
        html += '<dt>' + $(this).outerHtml() + '</dt>';
        $(this).remove();
    });
    context.children('a').each(function () {
        html += '<dd>' + $(this).outerHtml() + '</dd>';
        $(this).remove();
    });
    html += '</dl></div></div>';
    $('#utilities .u-account').append(html);

    // "Your Sites" menu
    html = '<div class="drop" style="width: 510px"><div class="down" style="width: 492px">';
    $('span', context).each(function () {
        html += '<dl><dt>' + $(this).outerHtml();
        $(this).next('div').find('a').each(function () {
            html += '<dd>' + $(this).outerHtml() + '</dd>';
            $(this).remove();
        });
        html += '</dt></dl>';
        $(this).remove();
    });
    html += '</div></div>';
    $('#utilities .u-access').append(html);

    $('#version3').css({ 'display': 'none' });

    context.remove();
};

BuildTableOfContents = function() {
    var options = { startLevel: 4, depth: 2, topLinks: 'Top of page' };

    if ($('#page h4').length <= 1 && $('#page h5').length > 1) {
        options.startLevel = 5;
        options.depth = 1;
        options.topLinks = false;
    }

    if ($('#page h4, #page h5').length > 1) {
        $('#toc ul').tableOfContents($('#page'), options);
        $('#toc').prepend('<h5>On this page</h5>');
    }
    else {
        $('#toc').remove();
    }
};

InitializeScrollable = function (id) {
    $(id).scrollable({ vertical: true }).autoscroll({ interval: 5000, autoplay: false, autopause: false });
    id = $(id).data('scrollable');
    id.onSeek(function () {
        var items = id.getItems();
        id.addItem(items.eq(id.getIndex() - 1).clone());
    });
    id.play();
};
