function matrix_show_label() {

    if(!this.matrix_label_text) {
        this.matrix_label_text = $(this).attr("title");
        if(!this.matrix_label_text) return;
        $(this).attr("title",""); // remove title to prevent tooltip display
        $(this).find("img").attr("alt",""); // remove img alt text too
    }

    if(this.className.indexOf("filtered_out") >= 0) {
        return;
    }

    $("#matrix_popup").remove();
    var popup = $("<div id='matrix_popup'>").text(this.matrix_label_text);
    var offset = $(this).find("img").offset();

    $(document.body).append(popup);
    $(popup).hide().fadeIn("fast");

    var left_pos = offset.left + ($.browser.mozilla? 1 : 0) ;// + $(this).find("img").width()/2 - $(popup).width()/2 - 10;

    popup.css("left", left_pos+"px").css("top", (offset.top-$(popup).height()-4)+"px");

}

function matrix_hide_label() {
    $("#matrix_popup").fadeOut("fast");
}

var current_category = null;

function matrix_filter(link) {
    var category = link.href.toString().split("#")[1];
    return matrix_filter_category(category);
}

function matrix_filter_category(category) {
    var cat_class = "c_" + category;

    $("#snav li a").removeClass("active");

    var visitor = function(i) {
        if(this.className.indexOf(cat_class) >= 0) {
            $(this).removeClass("filtered_out").find("img").fadeTo("slow", 1.0);
        } else {
            $(this).addClass("filtered_out").find("img").fadeTo("slow", 0.25);
        }
    };

    if(category=="ALL" || category==current_category) { // switch back to all cats
        category = null;
        
        visitor = function(i) {
            $(this).removeClass("filtered_out").find("img").fadeTo("slow", 1.0);
        };
    }
    current_category = category;

    $("#matrix a").each(visitor);
    
    if(category) {
        $("#snav_"+category+" a").addClass("active");
    } else {
        document.location = "#";
        return false;
    }

    return true;
}

function matrix_maybe_link() {
    if(this.className.indexOf("filtered_out") < 0) {
        return true;
    } else {
        matrix_filter_category("ALL");
        $(this).trigger("mouseover");
        return false;
    }
}

$(document).ready(function() {
        $("#matrix div a").hover(matrix_show_label, matrix_hide_label).click(matrix_maybe_link);

        // are we loading a pre-selected category pseudopage?
        try {
            var category = document.location.href.toString().split("#")[1];
            
            if(category) {
                $("#snav_"+category+" a").click();
            }
        } catch(e) { }
 
    });
