/**
* hoverIntent r5 // 2007.03.27 // jQuery 1.1.2+
*/
(function($){$.fn.hoverIntent=function(f,g){var cfg={sensitivity:7,interval:100,timeout:0};cfg=$.extend(cfg,g?{over:f,out:g}:f);var cX,cY,pX,pY;var track=function(ev){cX=ev.pageX;cY=ev.pageY;};var compare=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);if((Math.abs(pX-cX)+Math.abs(pY-cY))<cfg.sensitivity){$(ob).unbind("mousemove",track);ob.hoverIntent_s=1;return cfg.over.apply(ob,[ev]);}else{pX=cX;pY=cY;ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}};var delay=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);ob.hoverIntent_s=0;return cfg.out.apply(ob,[ev]);};var handleHover=function(e){var p=(e.type=="mouseover"?e.fromElement:e.toElement)||e.relatedTarget;while(p&&p!=this){try{p=p.parentNode;}catch(e){p=this;}}if(p==this){return false;}var ev=jQuery.extend({},e);var ob=this;if(ob.hoverIntent_t){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);}if(e.type=="mouseover"){pX=ev.pageX;pY=ev.pageY;$(ob).bind("mousemove",track);if(ob.hoverIntent_s!=1){ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}}else{$(ob).unbind("mousemove",track);if(ob.hoverIntent_s==1){ob.hoverIntent_t=setTimeout(function(){delay(ev,ob);},cfg.timeout);}}};return this.mouseover(handleHover).mouseout(handleHover);};})(jQuery);

/**
* Technorati Click Tracking v1 // 2008.10.31
* @author    Duane Barlow
*   To any item you want to track clicks on, add: onclick="$(this).track('click-path');" to the element
*   to any parent nodes you want to appear in the click path hierarch, add the same thing to its onclick
*       - only anchor tags will fire a pixel, the others will be used to assemble the click path
*       - if you want the click path to terminate anywhere, begin the clickpath with a '/' and that will be the first to come after the '/clicks/' node
*/
$.fn.extend({
    track: function(clickPath) {
        return this.each(function() { 
            if(this.tagName == 'A') {
                var paths = new Array();
                paths.push(clickPath + '/');
                $(this).parents('[onclick]').each(function() {
                    if (matches = this.onclick.toString().match(/\.track\(['"](.+)['"]\)/)) {
                        clickPath = matches[1]; // this will be any text between .track(' and ') which is the clickPath (according to our convention)
                        if (paths[paths.length - 1].charAt(0) != '/') { // if any clickPath starts with '/' then that should terminate the clickpath
                            paths.push(clickPath);
                        }
                    }
                });
                var pathString = paths.reverse().join('/');
                pathString = (pathString.charAt(0) == '/') ? '/clicks' + pathString : '/clicks/' + pathString;
                pageTracker._trackPageview(pathString);
            }
        });
    }
});

/* Initialization of the redesigned menu.  This is called inline immediately after the navigation html */

function initNavigation () {
    $('#search').addClass('JavascriptEnabled');
    var searchLabel = $('#search label').text();
    $('#search #q')
        .val(searchLabel)
        .focus(function() {
            if (this.value == searchLabel) {
                this.value = '';
            }
        })
        .blur(function() {
            if (!this.value) this.value = searchLabel;
        });
    $('#search').submit(function() { if ($('#search #q').val() == searchLabel) $('#search #q').val(''); });
        
    $('#nav ul.dropdown').each(function() { // for every nav item that has a dropdown
        $(this).parent().hoverIntent({ // get that dropdown's previous sibling (which will be the anchor tag for that menu)
            sensitiviy: 10,
            timeout: 100,
            over: function(){ // on enter of the menu
                var hovered_menu = this;                                                /* for IE6: add a class for this  */
                $('li.featured + li', $(this).parent()).each(function(){                /* element if it's adjacent to a  */
                    if (this == hovered_menu) $(this).addClass('adjacent-featured');    /* 'featured' element             */
                });
                $(this).siblings().find('.menu-in, .dropdown-in').hide(); // quickly hide any other dropdown that might be showing
                $(this).addClass('hovered');
                $(this).siblings('.hovered').removeClass('hovered');
                $(this).find('.dropdown').addClass('menu-in').show(); // show the menu's next sibbling (which is the dropdown), indicate the cursor is in the menu
            },
            out: function(){ // on exit of the menu
                $(this).find('.dropdown').removeClass('menu-in'); // indicate that the cursor is no longer in the menu
                if (!$(this).find('.dropdown').hasClass('dropdown-in')) { // if the cursor isn't in the dropdown
                    $(this).find('.dropdown').hide(); // fade the dropdown
                    $(this).removeClass('hovered adjacent-featured');
                }
            }
        });
        $(this).hoverIntent({
            sensitivity: 300,
            timeout: 700,
            over: function(){ // on enter of the dropdown
                $(this.parentNode).addClass('hovered');
                $(this).addClass('dropdown-in').show(); // show the dropdown, indicate that the cursor is in the dropdown
            },
            out: function(){ 
                $(this).removeClass('dropdown-in'); // indicate that the cursor has left the dropdown
                if (!$(this).hasClass('menu-in')) { // if the cursor isn't in the menu
                    $(this).hide(); // fade the dropdown
                    $(this.parentNode).removeClass('hovered');
                }
            }
        });
    });
    // IE6 compatability; otherwise this problem is solved with a sibling selector
    // $('#nav li.featured + li')
    //     .mouseover( function(){ $(this).addClass('adjacent-featured');    })
    //     .mouseout(  function(){ $(this).removeClass('adjacent-featured'); });
    
    var menuBubbled = false;
    $('#nav').click(function() { menuBubbled = false; });
    $('#nav #channels-menu').click(function() { if (!menuBubbled) { return false; } });
    $('#nav #channels-menu ul a').click(function() { menuBubbled = true; })
}

function initTagChart() {
    var label = $("#tag-chart form label").remove().text();
    $("#tag-chart #compare-tag").val(label);
    $("#tag-chart #compare-tag").focus(function(){
        if ($(this).val() == label) {
            $(this).val("");
        }
    }).blur(function(){
        if ($(this).val() == "") {
            $(this).val(label);
        }
    });
    $("#tag-chart form").submit(function(){
        var tag = $("#tag-chart form .tag").text();
        var compare = $("#tag-chart #compare-tag").val();
        var chartUrl = "/chart.php?tag=" + encodeURIComponent(tag) + "&compare=" + encodeURIComponent(compare);
        $("#tag-chart input").attr("disabled", "disabled").blur();
        $("#tag-chart img").attr("src", chartUrl).load(function(){
            $("#tag-chart input").attr("disabled", "");
        })
        return false;
    });
}

function initTagFilter() {
    $('#settings ul').addClass('JavascriptEnabled');
    var label = $("#settings label").remove().text();
    $('#settings').submit(function() { if ($('#settings #filter').val() == label) $('#settings #filter').val(''); })
    if ($('#settings #filter').val() == '') $("#settings #filter").val(label);
    $("#settings #filter")
        .focus(
            function() {
                if ($(this).val() == label) { $(this).val(""); }
            })
        .blur(function(){
            if ($(this).val() == "") {
                $(this).val(label);
            }
        }
    );
}

function initTagPage() { // Miscellaneous tag page initialization here
    $('.green-module textarea').focus(function() { this.select(); });
}

function initGlobalWidgets() {
    $('div.help h3 a').click(function() { /* For each "?" icon inside a help module */
        var jParent = $(this).parents().find('.help .inner'); /* gets the inner container of the help module */
        if (jParent.hasClass('collapsed')) {  /* if that container has the class collpased */
            jParent.removeClass('collapsed'); /* remove it */
        } else {                              /* otherwise */
            jParent.addClass('collapsed');    /* add it */
        }
        return false;                         /* cancel the click on the anchor tag */
    });
}

$(initGlobalWidgets);

