function slideSwitch(selector, index) {
    var $active = $(selector + '.active');
    if ( $active.length == 0 ) $active = $(selector + ':last');
    if (index == -1) {
        var $next =  $active.next().length ? $active.next()
            : $(selector + ':first');
    } else {
        var $next = $($(selector).get(index));
    }
    $active.addClass('last-active');
    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
        });
}

$(document).ready(function(){
    //LOGO HOVER FADE
    /*
    $("#Logo").hover(function() {
        $(this).fadeTo(200, 0.0);
    },
    function() {
        $(this).fadeTo(300, 1.0);
    });
    */

    //HOME CAROUSEL
    function carouselFade(target, callback){
        if (target == 'next') {
            target = $('.carousel.active').next('.carousel');
            if (target.length == 0) {
                target = '#Carousel_1';
                targetLink = '#Carousel_1_Link';
            } else {
                targetLink = '#' + target.attr('id') + '_Link';
            }
        } else {
            targetLink = target + '_Link';
        }
        $('.carousel.active').fadeOut(250, function() {
            $('.carousel.active').removeClass('active');
            $('.carouselLink.active').removeClass('active');
            $(target).fadeIn(150);
            $(target).addClass('active');
            $(targetLink).addClass('active');
        });
        if (callback) {
            callback();
        }
    }
    $(".carouselLink").click(function() {
        clearInterval(carousel);
        target = '#' + $(this).attr('rel');
        carouselFade(target, function () {
            carousel = setInterval(function(){carouselFade('next')}, 10000 );
        });
        return false;
    });

    var carousel = setInterval(function(){carouselFade('next')}, 10000 );
   
    //BOX HOVER FADE
    $(".box img").hover(function() {
        $(this).fadeTo(100, 0.1);
        //$(this).css({ opacity: 0.1 });
    },
    function() {
        $(this).fadeTo(200, 1.0);
    });

    //ARTICLE SCROLL
    var sidebar = $("#ArticleSidebar");
    $(window).scroll(function(){
        if ($(window).scrollTop() > 332) {
            $(sidebar).stop().animate({"top": ($(window).scrollTop()) + "px"}, "slow" );
        } else {
            $(sidebar).stop().animate({"top": "332px"}, "slow" );
        }
    });
    //NEWS MENU
    $('.newsMenuLink').click(function() {
        target = "#" + $(this).attr("rel");
        $('.newsMenuLink').removeClass('active');
        $(this).addClass('active');
        $('.newsSection').hide();
        $(target).show();
    });

    // gallery
    if ($.fancybox) {
        $(".gallery A.fancybox").fancybox();
    }

    //slideshow
    setInterval("slideSwitch('#SuggestSlide .slideshow DIV', -1)", 5000);
    setInterval("slideSwitch('#EventSlide .slideshow DIV', -1)", 6000);
});

