function InitTopicTabHovers() {
    $("table.topic").each(function(idx) {

        var info_id = "info_" + idx;
        $(this).attr("info_id", info_id);
        var info = $(this).find("div.info");
        info.attr('id', info_id);
        info.prependTo($(document.body));

    });
    $("table.topic").mouseover(function() {

        $(this).css("cursor", "pointer");

        var info = $('#' + $(this).attr('info_id'));
        
        var info_width = info.width();
        var info_height = info.height();

        var window_width = $(window).width();
        var window_height = $(window).height();

        var props = {iw: info_width, ih: info_height, ww: window_width, wh: window_height};

        info.attr(props);

        info.show();

    }).mousemove(function(event) {

        var left = event.pageX + 10;
        var top = event.pageY + -25;
        var info = $('#' + $(this).attr('info_id'));

        var info_width = parseInt(info.attr('iw'));
        var info_height = parseInt(info.attr('ih'));
        var window_width = parseInt(info.attr('ww'));
        var window_height = parseInt(info.attr('wh'));

        if (left + info_width > window_width - 30) left = left - info_width - 40;
        if (top + info_height > window_height - 30) top = top - info_height;

        info.css("top", top + "px").css("left", left + "px");

    }).mouseout(function() {

        $(this).css("cursor", "default");
        $('#' + $(this).attr('info_id')).hide();

    }).click(function() {

        location.href = $(this).find("td.tab > a").attr('href');

    });
}
function InitSearchTools() {
    $('input[name=btnTheme]').click(function() {
        var val = $('select[name=theme]').val();
        if (val) location.href = val;
        return false;
    });
    $('input[name=btnTopic]').click(function() {
        var val = $('select[name=topic]').val();
        if (val) location.href = val;
        return false;
    });
    $('input[name=btnSearch]').click(function() {
        var val = $.trim($('input[name=q]').val().toLowerCase());
        return val && val != "search" && val != "search...";
    });
    $('input[name=q]').focus(function() {
        var val = $.trim($(this).val().toLowerCase());
        if (val == 'search' || val == 'search...') $(this).val('');
    }).blur(function() {
        var val = $.trim($(this).val().toLowerCase());
        if (!val || val == 'search' || val == 'search...') $(this).val('Search...');
    });
    /*$('form#search-tools').validate({
        submitHandler: function(form) {
            if (!$.trim($('form#search-tools input[name=q]').val()) && !$.trim($('form#search-tools select[name=theme]').val())) {
                return false;
            }
            form.submit();
        }
    });*/
}
function InitHoverButtons() {
    $("img.j-hover, input[type=image].j-hover").hover(
        function() {
            $(this).attr('src', $(this).attr('src').replace('.', '-hover.'));
        },
        function() {
            $(this).attr('src', $(this).attr('src').replace('-hover.', '.'));
        }   
    );
}
function InitAnalyticsFileTracking() {
    var regex = /\.(pdf|mov|mp3)$/i;
    
    $("#main a").each(function(idx) {
        var href = $(this).attr('href');

        if (regex.test(href) && (href.indexOf('http://' + location.hostname) == 0 || href.indexOf('http://') == -1)) {
            $(this).click(function() {
                var href = ConvertURL($(this).attr('href'), location.pathname);
                //alert('Track PDF -- ' + href);
                pageTracker._trackPageview(href);
            });
            $(this).rightClick(function() {
                var href = ConvertURL($(this).attr('href'), location.pathname);
                //alert('Track PDF -- ' + href);
                pageTracker._trackPageview(href);
            });
        };
    });   
}
function ConvertURL(rel_href, cur_pathname) {
    var href = rel_href;
    var pathbase = cur_pathname;
    var backupcount = 0;
    if (href.indexOf('/') == 0) return href                          //already completely qualified href
    if (href.indexOf('../') == 0) {                                     //need to back up one or more parent levels
        var nodes= href.split("../");
        backupcount = nodes.length-1;
        href  = rel_href;
        while (href.indexOf('../') != -1) href = href.replace("../", "");
    }
    //the href is relative to the cur_pathname; need to complete the qualification
    var nodes = pathbase.split("/");
    var nodecount = nodes.length;
    if ( $("base").length == 0 ) {     //No base tag exists, so cur_pathname includes the pagename as the last node
        nodecount=nodecount-1; 
        //alert ('decremented nodecount, no base tag');
    }  //No base tag exists, so we're executing from a page (as opposed to a default
                                                                             
    href = nodes.slice(0, nodecount-backupcount).join("/")  + "/" + href;
    return href;
}

$(document).ready(function() {
    InitAnalyticsFileTracking();
    InitSearchTools();
    InitHoverButtons();
    InitTopicTabHovers();
});