var TO = false;
$(window).resize(function(){
    if(TO !== false) {
        clearTimeout(TO);
    }
    TO = setTimeout(function() {
        resizeNavigation();
        normalizeTagsListDimensions();

        clearTimeout(TO);
    }, 200); //200 is time in miliseconds
});

$(window).load(function() {
    resizeNavigation();
    normalizeTagsListDimensions();
});

$(window).scroll(function(event) {
    normalizeTagsListDimensions();
});

function normalizeTagsListDimensions() {
    var navigation_tags = $("div#navigation_tags_root");

    var h = $(window).height() - $('.navbar-fixed-top').height();
    var w = navigation_tags.closest(".span3").width();

    if ($(window).width() > 768) {
        // Make the left navigation tags completely match with the width of the sidebar
        navigation_tags.height(h);
        navigation_tags.width(w);

        navigation_tags.css({
            top: $('.navbar-fixed-top').height()
        });
    } else {
        navigation_tags.css({
            height: "auto",
            width: "auto",
            top: "auto"
        });
    }

    // Fix float the tags list
    if ($(window).width() <= 940 && $(window).width() >= 768) {
        var y = $(this).scrollTop();

        var header_height = 49;

        var top = y >= header_height ? 0 : header_height - y;
        var height = y >= header_height ? $(window).height() : $(window).height() -  header_height + y;

        $('div#navigation_tags_root').css({
            top: top,
            height: height
        });
    }
}

function resizeNavigation()
{
    var left_sidebar_exists, right_sidebar_exists;
    var main_content_class_open, main_content_class_closed;

    left_sidebar_exists = $('#left_sidebar').length > 0
    right_sidebar_exists = $('#rightNavigation').length > 0 && $(window).width() >= 940;

    // Make the layout responsive
    if (left_sidebar_exists) {
        main_content_class_closed = "span6";
        main_content_class_open = "span9";
    } else {
        main_content_class_closed = "span9";
        main_content_class_open = "span12";
    }

    if ($(window).width() >= 940) {
        $("#rightNavigation").addClass("span3");
        $("#mainContent").addClass(main_content_class_closed);
        $("#mainContent").removeClass(main_content_class_open);
    } else {


        $("#rightNavigation").removeClass("span3");
        $("#mainContent").removeClass(main_content_class_closed);
        $("#mainContent").addClass(main_content_class_open);
    }

    // Normalize right navigation width
    $('#mainContent').removeAttr('style');
    $('#rightNavigation').removeAttr('style');

    if (right_sidebar_exists) {
        var right_navigation_normal_width = 300;
        var right_navigation_old_width = $('#rightNavigation').width();
        var main_content_old_width = $('#mainContent').width();

        var right_navigation_width_margin = right_navigation_normal_width - right_navigation_old_width;

        $('#rightNavigation').width(right_navigation_old_width + right_navigation_width_margin);
        $('#mainContent').width(main_content_old_width - right_navigation_width_margin);
    }
}

$(document).ready(function(){
    $("#navigation_tags_root").accordion();

    $('.dropdown-toggle').dropdown();

    $('#navigation_tags_root a').each(function() {
        $(this).click(function() {
            var element = $($(this).attr("href"));
            //console.log(element);
            if (element.attr("id")) {
                $.scrollTo(element, 750);
            }
        });
    });

    $('a.cheatsheet').each(function() {
        $(this).qtip({
            content: {
                text: "Loading...",
                ajax: {
                    url: "/ajax_details/" + $(this).attr("id") + "/"
                }
            },
            position: {
                at: 'right center',
                my: 'left center',
                viewport: $(window),
                effect: false
            },
            show: {
                delay: 500
            },
            hide: {
                fixed: true,
                delay: 500,
                when: {
                    event: "mouseover"
                }
            },
            style: {
                classes: "tooltip-blue ui-tooltip-tipped ui-tooltip-shadow",
                width: 450
            }
        });
    });

    $('a.cheatsheet').click(function() {
        $.post("/api/sheets/track_visit/", { id : this.id }, function(data) {
        });
        return true;
    });

    $('#top-link').topLink({
        min: 400,
        fadeSpeed: 500
    });

    $('#top-link').click(function(e) {
        e.preventDefault();
        $.scrollTo(0,300);
    });

    $("input#search").smartSuggest({
        src: "/search/json/",
        showEmptyCategories: true,
        fillBox: true,
        fillBoxWith: 'primary',
        executeCode: false,
        delay: 100
    });

    $("table.sortableSearch").each(function() {
        $(this).tablesorter({ sortList: [[0,0]] })
    });
});


