var $j = jQuery.noConflict();

$j(document).ready(function() {
    //check to see if its the homepage
    if ($j('#homeBanner').length > 0) {
        homeBanner();
    }
    //hide the last border on the footer links
    $j('#footer .links li:last-child').css('border', 'none');

    if ($j('#navLeftCntnr').length > 0) { hideLeftNav();}
});

function hideLeftNav() {
    var leftNav = $j('#navLeftCntnr');
    if ($j(leftNav).children().size() ==0) {
        $j(leftNav).parent().css('visibility', 'hidden');
    }
}
function homeBanner() {
    //general variables
    var fadeSpeed = 500;
    var timerActive = true;
    var count = 2;
    var totalBanners = $j('#homeBanner .contentArea').children().length;
    var bannerDiv = $j('#homeBanner div');
    var buttons = $j('#buttonArea a');

    //initialise the page
    //hide the href attributes (they will be left in their to link to relevant pages if javascript is turned off
    $j(buttons).removeAttr('href');
    var selectedBtn = 1;
    $j('#homeBanner .contentArea').children('div').hide();
    $j('#banner1').fadeIn(fadeSpeed);

    //events	
    $j(buttons).click(function() {
        var clicked = parseInt($j(this).attr('class'));
        timerActive = false;
        changeBanner(clicked);
    });

    $j.timer(5000, function(timer) {
        if (timerActive) {
            changeBanner(count);
            count++;
            if (count > totalBanners) {
                count = 1;
            }
        } else {
            timer.stop();
        }
    });

    //main function to change the banner
    function changeBanner(c) {
        //fade out the current one
        $j('#buttonArea .' + selectedBtn + '').parent().removeClass();
        $j(bannerDiv).eq(selectedBtn).fadeOut(fadeSpeed);

        //select the new one and fade it in
        selectedBtn = c;
        $j('.' + selectedBtn + '').parent().addClass('selected');
        $j(bannerDiv).eq(selectedBtn).fadeIn('slow');

    }

}