﻿var originaltitle = "";

function setFilter(filter) {
    $("#checkboxes input").each(function() {
        this.checked = (this.value == filter);
    });

    updateFilter();
}

function updateFilterInternal(selector) {
    var filters = [];
    var keyword = $("#search").val().toLowerCase();

    $("#checkboxes input").each(function() {
        if (this.checked) {
            filters.push(this.value);
        }
    });

    $(selector).each(function() {
        var item = dataitems[getItemId(this.id)];

        var itemTheme = item["Theme"];
        var itemTitle = getItemFilterText(item);

        var visible = false;

        // check for filter match
        for (var i = 0; i < filters.length; i++) {
            if (itemTheme == filters[i]) {
                visible = true;
                break;
            }
        }

        // additional keyword filtering.
        if (initSearch == false && keyword.length > 0) {
            visible = visible && (itemTitle.indexOf(keyword) >= 0);
        }

        if (!visible) {
            $(this).hide();
        }
        else {
            $(this).show();
        }
    });
}

function startsWith(string, substring) {
    var ln = substring.length;

    return string.substring(0, ln) == substring;
}

function checkAllThemes() {
    $("#checkboxes input").attr('checked', true);
}

function showVideo(flv) {
    $("#videoplayer").show();

    if (flv.indexOf(".flv") != -1) {
        initializePlayer(flv);
    }
}

function stopVideo() {
    $("#videoplayer").replaceWith("<div id=\"videoplayer\" style=\"display: block;\"></div>");
}

function showHighlightBox(width, height) {
    $("#highlight").css("left", ($(window).width() / 2) - (width / 2));
    $("#highlight").css("top", ($(window).height() / 2) - (height / 2));
    $("#highlight").css("width", width);
    $("#highlight").css("height", height);
    $("#highlight").css("z-index", 5);
    if (typeof document.body.style.maxHeight === "undefined") {
        $("html").css("overflow", "hidden");
    }
    $("#highlight").show();

    $("#overlay").css("opacity", 0.75);
    $("#overlay").show();
}

function showGlobalHighlight(item) {
    $("#close, #overlay").unbind("click");

    window.location.hash = "#" + item["Id"];

    $("#close").click(function() {
        hideHighlight(item);
    });

    $("#overlay").click(function() {
        hideHighlight(item);
    }); 
}

function hideHighlightBox() {
    $("#highlight").css("z-index", 3);
    $("#highlight").hide();
    $("#overlay").hide();
    if (typeof document.body.style.maxHeight === "undefined") {
        $("html").css("overflow", "");
    }

    window.location.hash = "#default";
    document.title = originaltitle;
}

function initWindow() {
    originaltitle = document.title;

    $("#checkboxes input").each(function() {
        $(this).click(function() { setTimeout(updateFilter, 50); });
    });

    // custom checkboxes
    $("#checkboxes input[type=checkbox]").checkbox({ empty: '/images/empty.png' });

    // searchbox
    $('#search').focus(function() {
        if (initSearch == true) {
            $('#search').css({ color: "#343434" });
            $('#search').val("");
            updateFilter();
            initSearch = false;
        }
    });

    $("#search").keydown(function() { setTimeout(updateFilter, 100); checkAllThemes(); });

    var hash = window.location.hash;

    if (!startsWith(hash, "#default")) {
        if (startsWith(hash, "#filter-")) {
            setFilter(hash.replace("#filter-", ""));
        }
        else if (startsWith(hash, "#")) {
            window.setTimeout(function() { showHighlight(hash.substring(1)); }, 1000);
        }
    }    
}
