//var bgInterval;
//var scrollers;
//var api;

$(function() {
   if($.browser.msie && $.browser.version < 8) {
       alert("This website is not optimized for Internet Explorer 7 and older, please upgrade to the latest version of Internet explorer for optimal viewing.")
   }
});

$(function() {
    $('.content-module .content, .content-module .mini-content, .tweets-wrapper').jScrollPane();

    if ( ! $.browser.msie ) {
        scroller = $('#media_view_scroller').jScrollPane({autoReinitialise: true});
        if(scroller)
            api = scroller.data('jsp');
    }

    $('#ad_box').cycle({
        fx: 'fade'
    });
});

$(function() {
    $('.content-module-wrapper').hide();
    $('.autohide, .hidden').hide();

    $('.list-widget-link, .category-widget-link').click(function(e) {
        e.preventDefault();

        $('.list-widget-item, .category-widget-article').hide();
        $('#' + $(this).parent().attr('rel')).show();

        $('#' + $(this).attr('rel')).fadeIn(2000);

        //setTimeout("api.reinitialise", 300);
        //api.reinitialise();
    });


    //$('.list-widget-link, .category-widget-link').first().click();

    $('.media-toggle a').click(function(e) {
        e.preventDefault();

        $('.media-list').hide();
        $('.' + $(this).attr('rel')).fadeIn(2000);

        //if($('.media_view_container').is(':visible')) {
            $('.' + $(this).attr('rel')).find('.media-link').first().click();
        //}

        //api.reinitialise();
    });

    $('.media-toggle').first().find('a').first().click();

    $('.media-link').click(function() {
        $('#media_view').load($(this).attr('rel').replace(/_/g, '/'));
        
        $('.autoload').fadeIn(2000);

        if( ! $('#media_view_container').is(':visible')) {
            $('#media_view_container').show();
        }
    });

    if ( ! $('#media_view_container').hasClass('autohide') ) {
        $('.media-list-container').first().find('.media-link').first().click();
    }

    $('#page_backgrounds .bg-link').first().addClass('active');

    // Background switcher
    swapBg();
});

//Background switcher
function swapBg() {
    var active = $('#page_backgrounds').find('.active').first();
    var next = (active.next().length > 0) ? active.next() : false;

    if(!active.length > 0) {
        $('.content-module-wrapper').fadeIn(2000, function() {
            //api.reinitialise();
            //setTimeout(function(api_obj) { alert('deh yah...'); api_obj.reinitialise(); }, 300, api);
        });  // This is here in case no backgrounds have been set.
        return;
    }

    $('#page_bg').fadeOut(1000, function(){
        $(this).attr('src', active.attr('href')).fadeIn(function() {

            active.removeClass('active');
            if(next !== false) {
                next.addClass('active');
            }
            
            setTimeout(swapBg, 2000);
        })
    });
}

