$(document).ready(function() {
	
	// Danger widget setup
	$(".danger-widget .carousel-next").css({ "display": "block" });
	$(".danger-widget .carousel-back").css({ "display": "block" });

	// Know widget setup
	$(".widget-view-all").css({ "display": "none" });
	$(".carousel-controls").css({ "display": "block" });
  
	//carousel functionality setup

    	//Start or stop button is triggered
   	$(".carousel-motion").click(function() {
    
        	if ($(this).html() == "<span></span>Stop" || $(this).html() == "<SPAN></SPAN>Stop") {
            		carouselStop();
            		$(this).html("<span></span>Play");
            		$(this).addClass("carousel-play");
            		$(this).removeClass("carousel-pause");
            		$(this).attr("title", "Start the Carousel");
        	} else {
            		carouselPlay();
            		$(this).html("<span></span>Stop");
            		$(this).removeClass("carousel-play");
            		$(this).addClass("carousel-pause");
            		$(this).attr("title", "Stop the Carousel");
        	}
        	return false;
    	});

    	//Forward button is triggered
    	$(".carousel-next").click(function() {
	
		if( $(this).parent().parent().get(0).className != 'danger-widget' ) {
        		//Change the text of the motion button
        		if ($(".carousel-motion").html() == "<span></span>Stop" || $(".carousel-motion").html() == "<SPAN></SPAN>Stop") {  
            			$(".carousel-motion").html("<span></span>Play");
            			$(".carousel-motion").addClass("carousel-play");
            			$(".carousel-motion").removeClass("carousel-pause");
            			$(".carousel-motion").attr("title", "Start the Carousel");
        		}
        		carouselStop();
			carouselForward();
		} else
			carouselForward2();
        	return false;
    	});

    	//Back button is triggered
    	$(".carousel-back").click(function() {

		if( $(this).parent().parent().get(0).className != 'danger-widget' ) {
        		//Change the text of the motion button
        		if ( $(".carousel-motion").html() == "<span></span>Stop" || $(".carousel-motion").html() == "<SPAN></SPAN>Stop" ) {  
            			$(".carousel-motion").html("<span></span>Play");
            			$(".carousel-motion").addClass("carousel-play");
            			$(".carousel-motion").removeClass("carousel-pause");
            			$(".carousel-motion").attr("title", "Start the Carousel");
        		}
        		carouselStop();
        		carouselBack();
		} else 
			carouselBack2();
        	return false;
    	});

    	//Start the carousel playing when the document is loaded
    	carouselPlay();

});

//Carousel variables
var animationLength = 1000; //time in milliseconds
var animationIterval = 10000; //time in milliseconds
var interval;



//Carousel fuctionality

//Start the carousel playing
function carouselPlay() {
    interval = setInterval(carouselForward, animationIterval); 
}

//Move the animation forward one step
function carouselForward() {
    //Move the carousel forward
    $(".carousel-holder ul").animate({ 
        left: "-167px"
    }, {  duration:animationLength, easing:"bounceEaseOut", complete:appendItem });
}

function appendItem() {
    //Move the first item to the back of the list
    $(".carousel-holder ul li:first").insertAfter(".carousel-holder ul li:last");
    $(".carousel-holder ul").css("left", "0px");
}

//Move the animation back one step
function carouselBack() {
    //Move the last item to the front of the list
    $(".carousel-holder ul li:last").insertBefore(".carousel-holder ul li:first");
    $(".carousel-holder ul").css("left", "-167px");
    //Move the carousel forward
    $(".carousel-holder ul").animate({
        left: "1px"
    }, { duration: animationLength, easing: "bounceEaseOut" });
}

//Stop the carousel playing
function carouselStop() {
    clearInterval(interval);
}

//Danger widget functions

//Move the animation forward one step
function carouselForward2() {
    //Move the carousel forward
    $(".carousel-holder2 ul").animate({ 
        left: "-167px"
    }, {  duration:animationLength, easing:"bounceEaseOut", complete:appendItem2 });
}

function appendItem2() {
    //Move the first item to the back of the list
    $(".carousel-holder2 ul li:first").insertAfter(".carousel-holder2 ul li:last");
    $(".carousel-holder2 ul").css("left", "0px");
}

//Move the animation back one step
function carouselBack2() {
    //Move the last item to the front of the list
    $(".carousel-holder2 ul li:last").insertBefore(".carousel-holder2 ul li:first");
    $(".carousel-holder2 ul").css("left", "-167px");
    //Move the carousel forward
    $(".carousel-holder2 ul").animate({
        left: "1px"
    }, { duration: animationLength, easing: "bounceEaseOut" });
}