$(document).ready(function(){

    $('#carousel').cycle();

    $('#carousel-inner').cycle({
        fx: 'fade',
        speed: 3500,
        timeout: 8000,
        cleartype: 1,
        fit: 1,
        activePagerClass: 'active',
        pager: '#carousel-nav',
        pagerAnchorBuilder: function(idx, slide) {
            return '#carousel-nav a:eq(' + idx + ')';
        },
        fastOnEvent: 300,
        manualTrump: true,
        after: function(currSlideElement, nextSlideElement, options, forwardFlag){
            var carouselNav = $('#carousel-nav img');

            /*naviItem.each(function(){

                parent = $(this).parent('a');

                if(!(parent.hasClass('active')))   {
                    grayscale($(this));
                } else  {
                    grayscale.reset($(this));
                }
            });*/

            renderGrayscale(carouselNav);
        },
        pause: 0
    });



    $('ul.pagination ul').centerAlign();

    //grayscale rendering
    var carouselNav = $('#carousel-nav img');

    if(carouselNav.length){
        renderGrayscale(carouselNav);
    }


    //equalHeights

    //footer columns
    var footeCols = $('.footer ul > li').not('.footer ul li ul li');
    footeCols.equalise();

    //content panels
    $('.content_panels div').equalise();

    //homepage second level panels
    $('.home_r1').equalise();

    //homepage second level panels
    $('.home_r2').equalise();

    $('.vcard div').equalise();

});

//render the grayscale images
function renderGrayscale(carouselNav)    {
    carouselNav.each(function(){
        $(this).data('trigger', false);

        $(this).load(function() {

            if($(this).data('trigger') == false)  {
                $(this).data('trigger', true);

                parent = $(this).parent('a');

                if(!(parent.hasClass('active')))   {
                    grayscale($(this));
                } else  {
                    grayscale.reset($(this));
                }
            };
        });
    })
}

//Equal Heights
(function($){
    $.fn.equalise = function()   {
        var tallest = 0;

        //loop over each element and workout which is the tallest
        $(this).each(function(){
            if ($(this).height() > tallest) {
                tallest = $(this).height();
            }
        })

        //if using IE6 then do this
        if ($.browser.msie && $.browser.version == 6.0) {
            $(this).height(tallest);
        //otherwise for everyone else
        } else  {
            $(this).css('min-height', tallest + 'px');
        }
    return this;
    }
}) (jQuery);

//Center align function
(function($) {
    $.fn.centerAlign = function()   {

        if($(this).length)    {
            //set this inline before we can get the actual width
            $(this).css('display', 'inline');

            w = $(this).width();

            $(this).width(w).css('display', 'block');
        }

    return this;
    }
}) (jQuery);
