function addLoader($t){
    var $bt = $('<a href="#" class="reload">More</a>').click(function(){
        $t.addClass('loading');
        $t.load("/testimonials/next/", function(){ 
            $t.removeClass('loading');
            addLoader($t);
        });
        return false;
    });
    $('.meta', $t).append($bt);
}

function openInNew(url){
    var loc = window.location;
    var abs = loc.protocol + '//' + loc.host;
    if (loc.port) {
        abs = abs + ':' + loc.port;
    }
    url = abs + url;
    window.open(url, "_blank", "width=500,height=400,location=no,menubar=no,status=no,toolbar=no,scrollbars=yes");
}

$.fn.ifixpng = function(){};

$(document).ready(function(){
    // Prevent double submissions
    $('#con form').submit(function(){
        $('input[type="submit"]', this)
            .attr('disabled', 'disabled')
            .addClass('disabled');
    });
    // Navigation menu mouse over
    $('.link a').focus(function(){
            $('.actor', this).mouseover();
    }).blur(function(){
            $('.actor', this).mouseout();
    });

    $('a.new').bind('click', function(){
        var $this = $(this);
        openInNew($this.attr('href'));
        return false;
    });

    $('body#philosophies #con .bd').sectionate({ wholeClass: 'minimize'});

    // UI
    $.fn.minimize.defaults = {
        headSelector: '.hd', 
        bodySelector: '.bd', 
        defaultState: 'closed', 
        buttonPosition: 'end'
    };
    $('.minimize').minimize();
    $('.accordion').accordionize();

    // AJAX
    $('.testimonial').each(function(){
            addLoader($(this));
    });

    // Forms
    $('#findaclass .overlabel').overlabel();
    $('#login .overlabel').overlabel();

    $.extend($.datepicker._defaults, {
        changeMonth: true,
        changeYear: true,
        dateFormat: 'yy-mm-dd',
        maxDate: new Date()
    });

    $('.form-date input').datepicker().each(function(){
        var $this = $(this);
        var $btn = $('<img src="/media/images/icons/calendar.png" height="18" width="18" alt="X" style="line-height: 1em; vertical-align: top; " />');
        var f = function(){
            $this.focus();
        };
        $btn.click(f).focus(f);
        $this.after($btn);
    });

    $('.highlight-rows tr').hover(
        function(){
            $(this).addClass('highlight-row');
        }
    ,   function(){
            $(this).removeClass('highlight-row');
        }
    );

    $($('#philosophies .minimize .hd').get(0)).trigger('click');
    $('#termdates #region1.minimize .hd').trigger('click');

    if ( window.location.hash ) {
        $('.clickable', window.location.hash).click();
    }
});
