/**
* We use the initCallback callback
* to assign functionality to the controls
*/
function mycarousel_initCallback(carousel) {
    jQuery('.mycarousel .jcarousel-control a').bind('click', function () {
        carousel.scroll(jQuery.jcarousel.intval(jQuery(this).text()));
        return false;
    });

    jQuery('.mycarousel-next').bind('click', function () {
        carousel.next();
        return false;
    });

    jQuery('.mycarousel-prev').bind('click', function () {
        carousel.prev();
        return false;
    });

    //Injecting pause button
    $('.homepage .jcarousel-prev-horizontal').after('<div class="jcarousel-pause-button" style="display:block;"><a href="#">pause</a></div>');
    $('.theStadium .jcarousel-prev-horizontal').after('<div class="jcarousel-pause-button" style="display:block;"><a href="#">pause</a></div>');

    $(".jcarousel-pause-button").toggle(
      function () {
          carousel.stopAuto();
          $(this).addClass('jcarousel-pause-button-paused');
      },

      function () {
          //alert("carousel start pressed")
          carousel.startAuto();
          $(this).removeClass('jcarousel-pause-button-paused');
      }
    );


};


function newsScroller_initCallback(carousel) {
    jQuery('.newsScroller .jcarousel-control a').bind('click', function () {
        carousel.scroll(jQuery.jcarousel.intval(jQuery(this).text()));
        return false;
    });

    jQuery('.newsScroller-next').bind('click', function () {
        carousel.next();
        return false;
    });

    jQuery('.newsScroller-prev').bind('click', function () {
        carousel.prev();
        return false;
    });

    //Injecting pause button
    $('.subNav div.jcarousel-prev-horizontal').after('<div class="jcarousel-pause-button" style="display:block;"><a href="#">pause</a></div>');

    $(".subNav .jcarousel-pause-button").toggle(
      function () {
          carousel.stopAuto();
          $(this).addClass('jcarousel-pause-button-paused');
      },

      function () {
          //alert("carousel start pressed")
          carousel.startAuto();
          $(this).removeClass('jcarousel-pause-button-paused');
      }
    );


};





function eventsCarousel_initCallback(carousel) {
    jQuery('.eventsCarouselContainer .jcarousel-control a').bind('click', function () {
        carousel.scroll(jQuery.jcarousel.intval(jQuery(this).text()));
        return false;
    });

    jQuery('.eventsCarouse-next').bind('click', function () {
        carousel.next();
        return false;
    });

    jQuery('.eventsCarouse-prev').bind('click', function () {
        carousel.prev();
        return false;
    });

};

// it will append class="selected" into <a>
// <a href="#">1</a> to <a href="#" class="selected">1</a>
function highlight(carousel, obejctli, liindex, listate) {
    jQuery('.jcarousel-control a:nth-child(' + liindex + ')').attr("class", "selected");
};
// it will remove last selected slide from <a>
// <a href="#" class="selected">1</a> to <a href="#">1</a>
function removehighlight(carousel, obejctli, liindex, listate) {
    jQuery('.jcarousel-control a:nth-child(' + liindex + ')').removeAttr("class", "selected");
};

// Ride the carousel...
jQuery(document).ready(function () {
    jQuery(".mycarousel").jcarousel({
        scroll: 1,                 // autoscrolling enabled
        auto: 8,                   //8 seconds
        wrap: 'last',
        initCallback: mycarousel_initCallback,
        // This tells jCarousel NOT to autobuild prev/next buttons
        itemVisibleInCallback: highlight,
        itemVisibleOutCallback: removehighlight,
        buttonNextHTML: '<div></div>',
        buttonPrevHTML: '<div></div>'
    });
});



// Ride the carousel...
jQuery(document).ready(function () {
    jQuery(".newsScroller").jcarousel({
        scroll: 1,                 // autoscrolling enabled
        auto: 5,                   //8 seconds
        wrap: 'last',
        initCallback: newsScroller_initCallback,
        // This tells jCarousel NOT to autobuild prev/next buttons
        //itemVisibleInCallback: highlight,
        //itemVisibleOutCallback: removehighlight,
        buttonNextHTML: '<div></div>',
        buttonPrevHTML: '<div></div>'
    });
});

//EVENTS CAROUSEL
function SetClickedEvent(index) {
    $.cookie('WSEventCalendarItem', index, { path: '/', expires: 7 });
};

function bindEvents() {
    $(".eventCategory li").click(function () {
        $.cookie('WSEventCalendarFilterEventType', $(this).attr('id'), { path: '/', expires: 7 });
        $.cookie('WSEventCalendarFilterTimespan', null, { path: '/', expires: 0 });
        $.cookie('WSEventCalendarFilterYear', null, { path: '/', expires: 0 });
        $.cookie('WSEventCalendarItem', 0, { path: '/', expires: 0 });
    });

    $(".eventsTab a").click(function () {
        $.cookie('WSEventCalendarFilterTimespan', $(this).attr('id'), { path: '/', expires: 7 });
        $.cookie('WSEventCalendarFilterEventType', null, { path: '/', expires: 0 });
        $.cookie('WSEventCalendarFilterYear', null, { path: '/', expires: 0 });
        $.cookie('WSEventCalendarItem', 0, { path: '/', expires: 0 });
    });

    $(".dates a").click(function () {
        $.cookie('WSEventCalendarFilterYear', $(this).attr('id'), { path: '/', expires: 7 });
        $.cookie('WSEventCalendarFilterTimespan', null, { path: '/', expires: 0 });
        $.cookie('WSEventCalendarFilterEventType', null, { path: '/', expires: 0 });
        $.cookie('WSEventCalendarItem', 0, { path: '/', expires: 0 });
    });
};

function bindCarousel() {

    if ($.cookie('WSEventCalendarFilterEventType') != null) {
        var cookie = $.cookie('WSEventCalendarFilterEventType');
        $('#' + cookie).addClass('selected');

        // set the year 
        if ($.cookie('WSEventCalendarFilterYear') != null) {
            var cookieYear = $.cookie('WSEventCalendarFilterYear');
            $('#' + cookieYear).addClass('selected');
        }

        // set to upcoming
        $('.upcomingEvents a').addClass('selected');
    }
    else if ($.cookie('WSEventCalendarFilterTimespan') != null) {
        var cookie = $.cookie('WSEventCalendarFilterTimespan');
        $('#' + cookie).addClass('selected');

        // set the year 
        if ($.cookie('WSEventCalendarFilterYear') != null) {
            var cookieYear = $.cookie('WSEventCalendarFilterYear');
            $('#' + cookieYear).addClass('selected');
        }

        // set to upcoming
        $('#EventCalendar_liAll').addClass('selected');
    }
    else if ($.cookie('WSEventCalendarFilterYear') != null) {
        $('#EventCalendar_liAll').addClass('selected');
        // set the year 
        if ($.cookie('WSEventCalendarFilterYear') != null) {
            var cookieYear = $.cookie('WSEventCalendarFilterYear');
            $('#' + cookieYear).addClass('selected');
        }
    }
    else {
        $('#EventCalendar_liAll').addClass('selected');
        $('.upcomingEvents a').addClass('selected');
        // set the year 
        if ($.cookie('WSEventCalendarFilterYear') != null) {
            var cookieYear = $.cookie('WSEventCalendarFilterYear');
            $('#' + cookieYear).addClass('selected');
        }
    }

    var index = 0;

    //check if we clicked a calendar item and therefore need to select it
    if ($.cookie('WSEventCalendarItem') != null) {
        // yes so get cookie and index of the clicked item
        index = $.cookie('WSEventCalendarItem');
        // now set back to zero
        $.cookie('WSEventCalendarItem', 0, { path: '/', expires: 7 });
    }
      

    // offset the index
    index = Number(index) + Number(1);
    //index = 1;
    
    jQuery(".eventsCarousel").jcarousel({
        scroll: 1,
        auto: 0,
        start: index,
        wrap: null,
        initCallback: eventsCarousel_initCallback,
        // This tells jCarousel NOT to autobuild prev/next buttons
        //itemVisibleInCallback: highlight,
        //itemVisibleOutCallback: removehighlight,
        buttonNextHTML: '<input type="button"></input>', // Added for accessibility, so that it could be accessed with keyboard when tabbing
        buttonPrevHTML: '<input type="button"></input>'
    });
};
//QUOTE SCROLLER

jQuery(document).ready(function () {
    jQuery(".newsBox").jcarousel({
        scroll: 1,                 // autoscrolling enabled
        auto: 0,                   //8 seconds
        wrap: 'last',
        initCallback: eventsCarousel_initCallback, //  init as events carousel
        // This tells jCarousel NOT to autobuild prev/next buttons
        //itemVisibleInCallback: highlight,
        //itemVisibleOutCallback: removehighlight,
        buttonNextHTML: '<div class="next">NEXT</div>',
        buttonPrevHTML: '<div></div>'
    });
});

//QUOTE SCROLLER END

function ajaxRebind() {
    bindEvents();    
    bindCarousel();
    hoverCarousel();
};
