


$(function() {

    /**
     * Show/hide the descriptions for calendar events.
     */
    $('.hoverDescriptions .widget').mouseenter(function(){

       var description = $(this).find('p.description');
        if(description.css('display') == 'none') {
            var position = $(this).find('p.image').position();
            $(this).find('p.image img').fadeTo('fast', '0');
            description.fadeTo('fast', '1');
            description.css({ top: position.top, cursor: 'pointer' }).click(function(){ window.location = $(this).find('a').attr('href') });

            $(this).mouseleave(function(){
                   $(this).find('p.image img').fadeTo('fast', '1');
                   $(this).find('p.description').fadeTo('fast', '0').css('display', 'none');
                });
        }
    });

    /**
     * Upgrade iframes...
     */
    $(this).find('iframe').wrap('<div class="video-wrapper"><div class="video-container"></div></div>');


    /**
     * Start the gallery.
     */

    if($('#headerGallery').is(":visible")) {
        $('#headerGallery p:first').addClass('show').show();
        $("#headerGallery .note").text($('#headerGallery .show img').attr('alt'));
        if($('#headerGallery p').length > 1) {
            window.currentTimer = setInterval(autoRotate, 5000);
            var i = 0;
            $("#headerGallery p").each(function(event) {
                $(this).addClass("panel_item_" + i);
                if($(this).find('img').next().is('a')) {
                    $(this).find('img').appendTo($(this).find('a'));
                }
                $("#headerGallery .pagination").append("<a href='#' onclick='showheaderGalleryPanel(\""+i+"\")' class='panel_trigger_" + i + "'>"+ i +"</a>");
                i++;
            });
            $("#headerGallery .pagination a").first().addClass('current');
        }
    } else {
        $('#headerGallery').hide();
        $('.galleryMode').removeClass('galleryMode');
    }

    /**
     * Handle submits for IE and search form
     */
    $('input.q').keypress(function(e){
        if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
            $('form.search').submit();
        }
    });

    /**
     * Filter external links.
     */
    $("a").filter(function () {
        return this.hostname && this.hostname !== location.hostname;
    }).each(function () {
        $(this).attr({
            target: "_blank",
            title: "Visit " + this.href + " (click to open in a new window)"
        });
    });

    /*

    // Probably not needed anymore, left here at version 10.

    $(".tumblrImages img").fadeTo('fast', '0.5').mouseenter(function(){
        $(this).fadeTo('fast', '1');
    }).mouseleave(function(){
        $(this).fadeTo('fast', '0.5');
    });
    */
});

var showheaderGalleryPanel = function(i) {
  clearTimeout(window.currentTimer);
  $("#headerGallery .pagination a.current").removeClass('current');
  $("#headerGallery p.show").removeClass('show').fadeTo('slow', 0, function() { $(this).hide(); });
  $("#headerGallery .panel_item_" + i).fadeTo('slow', 1).addClass('show');
  $("#headerGallery .panel_trigger_" + i).addClass('current');
  $("#headerGallery .note").text($('#headerGallery .show img').attr('alt'));
  return false;
}

var autoRotate = function() {
    var i = $('#headerGallery p').index($("#headerGallery p.show"));

    if(i + 1 >= $('#headerGallery p').size()) {
        showheaderGalleryPanel(0);
    } else {
        showheaderGalleryPanel(i+1);
    }

    window.currentTimer = setInterval(autoRotate, 5000);
}


$(window).blur(function () {
            clearTimeout(window.currentTimer);
        }).focus(function () {
            if($('#headerGallery p').length > 1) {
//            window.currentTimer = setInterval(autoRotate, 5000);
            }
        });
