$(document).ready(function() {

    /*
    * Accessible carousel function
    */
    var carouselItems = $("#carouselWrapper li").length,
		currentSet = 1;

    if (carouselItems < 4) {
        $("#carouselNav").remove();
    }
    else {


        // Get previous carousel item set
        $("#carouselNav .prev a").click(function() {
            if (currentSet == 1) {
                currentSet = Math.floor(carouselItems / 3), lastSetPos = Math.floor(carouselItems / 3) * 510;

                if (carouselItems != 6 && carouselItems != 9 && carouselItems != 12 && carouselItems != 15)

                    currentSet++;
                else
                    lastSetPos = lastSetPos - 510;

                $("#carouselWrapper ul").stop(true, true).animate({
                    opacity: 0
                }, 500, function() {
                    $(this).animate({
                        left: '-=' + lastSetPos
                    }, 0, function() {
                        $(this).animate({
                            opacity: 1
                        }, 500);
                    });
                });
            }
            else {
                currentSet = currentSet - 1;
                $("#carouselWrapper ul").stop(true, true).animate({
                    opacity: 0
                }, 500, function() {
                    $(this).animate({
                        left: '+=510'
                    }, 0, function() {
                        $(this).animate({
                            opacity: 1
                        }, 500);
                    });
                });
            }
            return false;
        });

        // Get next carousel item set
        $("#carouselNav .next a").click(function() {
            if (currentSet < (carouselItems / 3)) {
                currentSet = currentSet + 1;
                $("#carouselWrapper ul").stop(true, true).animate({
                    opacity: 0
                }, 500, function() {
                    $(this).animate({
                        left: '-=510'
                    }, 0, function() {
                        $(this).animate({
                            opacity: 1
                        }, 500);
                    });
                });
            }
            else {
                currentSet = 1;
                $("#carouselWrapper ul").stop(true, true).animate({
                    opacity: 0
                }, 500, function() {
                    $(this).animate({
                        left: '0'
                    }, 0, function() {
                        $(this).animate({
                            opacity: 1
                        }, 500);
                    });
                });
            }
            return false;
        });
    }

    // Initialise more info boxes
    $("#carouselWrapper li:nth-child(3n+3) .moreInfo").addClass("third");

    /*
    * Carousel elements hover, focus and blur states
    */
    $(".moreInfo").addClass("inactive")
		.mouseleave(function() {
		    $(this).removeClass("active")
			.addClass("inactive");
		    $(this).parent().css("z-index", "1");
		    $(".trigger").find("span").show();
		});
    $("#carousel a.trigger").bind("mouseenter focus", function() {
        $(this).parent().siblings().find(".moreInfo").removeClass("active").addClass("inactive");
        $(this).find("span").hide();
        $(this).next(".moreInfo")
			.removeClass("inactive")
			.addClass("active");
        $(this).parent().siblings().css("z-index", "1");
        $(this).parent().css("z-index", "1000");
    }).blur(function() {
        $(this).find("span").show();
        $(this).next(".moreInfo")
			.removeClass("active")
			.addClass("inactive");
        $(this).parent().css("z-index", "1");
    });
    $("#carousel").mouseleave(function() {
        $(".moreInfo").removeClass("active").addClass("inactive");
        $(".trigger").find("span").show();
    })

    /*
    * Lower content adustments
    */
    if ($("#leftCol li").length) {
        $("#midCol").addClass("hasLeftCol");
    }
    else {
        $("#midCol").css({
            "border-left": "none",
            "padding-left": "0"
        });
    }

    var lowerHeight = $("#midCol").height();
    if ($("#leftCol").height() > lowerHeight) {
        $("#midCol").css("height", $("#leftCol").height());
    }
    if ($("#lowerRight").height() > lowerHeight) {
        $("#midCol").css("height", $("#lowerRight").height());
    }

    var rapportLink = '<span class="rapportLink">PDF</span>';
    $("#midCol li a.pdfLink").append(rapportLink);

});

