/**
 * Carousel for the homepage
 * @author garyrockett
 */
var carousel = {};
carousel.scrollTo = function (mode,target) {
    if (target) {
        $('li.activePanel').removeClass('activePanel');
        $('li#' + target).addClass('activePanel');
    }
    else {
        var activePanel = $('li.activePanel');
        if(mode == 'next') {
            var nextPanel = $('.activePanel').next('li');
            //if this is the last panel, go back to the start
            if ($('.activePanel').next('li').length < 1) {
                $(activePanel).removeClass('activePanel');
                $('ul.carousel li:first').addClass('activePanel');
            }
            else {
                $(activePanel).removeClass('activePanel');
                $(nextPanel).addClass('activePanel');
            }
        }
        else {
            var nextPanel = $('.activePanel').prev('li');
            if($('.activePanel').prev('li').length < 1) {
                $(activePanel).removeClass('activePanel');
                $('ul.carousel li:last').addClass('activePanel');
            }
            else {
                $(activePanel).removeClass('activePanel');
                $(nextPanel).addClass('activePanel');
            }
        }
    }
    // animate to target panel
    var nextPanelPosition = $('.activePanel').position();
    $('ul.carousel').animate({left: nextPanelPosition.left * -1}, 100,'swing').animate({left: nextPanelPosition.left * -1 + 20}, 100,'swing').animate({left: nextPanelPosition.left * -1}, 150,'swing');
    
    // Highlight the link for the current panel
    var linkTarget = $('.activePanel').attr('id');
    $('.carousel #controls a').removeClass('active');
    $('.carousel #controls a[rel='+linkTarget+']').addClass('active');
};

$(function () {
    var int = setInterval("carousel.scrollTo('next')",10000);
    $('div.prevNext a').click(function () {
        if ($('ul.carousel').is(":animated")) {
            return false;
        }
        else{ 
            window.clearInterval(int);
            if($(this).is('.carouselNext')) {
                carousel.scrollTo('next');
            }
            else {
                carousel.scrollTo('previous');
            }
            int = setInterval("carousel.scrollTo('next')",10000);
            return false;
        }
    });
    $('.carousel #controls a.carouselSwitch').hover(function () {
        $('ul.carousel').stop('true');
        var target = $(this).attr('rel');
        if (target !== '#') {
            window.clearInterval(int);
            carousel.scrollTo('next', target);
            int = setInterval("carousel.scrollTo('next')", 10000);
        }
        else {
        
        }
        return false;
    },
    function(){
        return false;
    });
    $('div.carousel').hover(function () {
        if($('.prevNext').is(":animated")) {
              return false;    
        }
        else{
          $('.prevNext').fadeIn(50);  
        }
        },
        function() {
          if ($('.prevNext').is(":animated")) {
              return false;
          }
          else{
              $('.prevNext').fadeOut(50);
          } 
    });    
});    
