// Triggers scrolling from right to left on carousel (used on timer to trigger auto scroll)
function scrollCarousel(){
	$('.jcarousel-next').trigger('click');
}

jQuery(function($) {

    // Home page carousel
    var carousel_arr = [];

    // Temporary containers for form data in carousel
    var form_data = [];
    var new_item;

    var msie = $.browser.msie;

    // Initialization 
    function carouselInitCallback(carousel) {
        $.each($('#carousel ul').children(), function() {
            carousel_arr.push($(this).html());
        });
        $('.jcarousel-next,.jcarousel-prev').insertAfter('#carousel-wrap');
        var auto_carousel = setInterval("scrollCarousel()", 10000);
        $('#carousel, .jcarousel-next, .jcarousel-prev').mouseover(function(e) {
            e.stopPropagation();

            carousel.options.animation = 1000;
            carousel.options.easing = 'easeOutCubic';
            clearInterval(auto_carousel);
            carousel.startAuto(0);

            // Adds mouseover function for body, to resume auto carousel motion
            // Added 2009-03-25
            $('body').mouseover(function() {
                carousel.options.animation = 1500;
                carousel.options.easing = 'easeInOutCubic';
                //$('.jcarousel-next').trigger('click');
                auto_carousel = setInterval("scrollCarousel()", 10000);
                $(this).unbind('mouseover');
            });

        });
    }

    // Fired before carousel moved, appends new item to carousel to give illusion of infininate list
    function carouselInCallback(carousel, item, i, state, evt) {
        var idx = carousel.index(i, carousel_arr.length);
        new_item = carousel.add(i, carousel_arr[idx - 1]);
        // Apply PNG Fix for IE6
        $('#carousel .desc-wrap, #carousel .lbl').ifixpng();
    }

    // Fired after carousel move, removes item from end of carousel (same one which was appended beforehand)
    function carouselOutCallback(carousel, item, i, state, evt) {
        carousel.remove(i);
    }

    // Invoke home page carousel
    $('#carousel ul').jcarousel({
        scroll: 1,
        auto: 5,
        wrap: 'circular',
        initCallback: carouselInitCallback,
        itemVisibleInCallback: { onBeforeAnimation: carouselInCallback },
        itemVisibleOutCallback: { onAfterAnimation: carouselOutCallback },
        easing: 'easeInOutCubic',
        animation: 1500
    });

    // If good ole IE, apply some PNG fixes and allow for hover state on previous and next buttons
    if (msie) {
        $('#carousel .desc-wrap, #carousel .lbl, .jcarousel-prev, .jcarousel-next').ifixpng();
        $('.jcarousel-prev,.jcarousel-next').hover(function() {
            $(this).iunfixpng().css('background-position', '-50px 0');
        }, function() {
            $(this).css('background-position', '0 0').ifixpng();
        });
    }

    // Basic hide/display functionality for 'Attend Event' panel - invoked by clicking the Attend an Event button
    $('#carousel .attend-event').live("click", function(e) {
        e.preventDefault();
        var item = $(this).parents('.item');
        var event_pnl = $(this).parents('li').find('.pnl-event');
        if (!msie) {
            item.fadeOut("medium", function() {
                event_pnl.fadeIn("medium");
            });
        } else {
            item.hide();
            event_pnl.show();
        }
    });

    // Close the 'Attend an Event' panel and show the default state
    $('#carousel .close').live("click", function(e) {
        e.preventDefault();
        var item = $(this).parents('li').find('.item');
        var event_pnl = $(this).parents('.pnl-event');
        if (!msie) {
            event_pnl.fadeOut("medium", function() {
                item.fadeIn("medium");
            });
        } else {
            event_pnl.hide();
            item.show();
        }
    });

});