var __initial = true;
var __carousel_position = 3;
var __carousel_speed    = 250; // milliseconds

var is_animating = false;

var __leftmost, __innerleft, __selected, __innerright, __rightmost;

// CSS attributes for each image container
var css_leftmost   = {
    "height" : "130",
    "left" : "0",
    "top" : "13",
    "width" : "157"
};
var css_rightmost  = {
    "height" : "130",
    "left" : "743",
    "top" : "13",
    "width" : "157"
};
var css_innerleft  = {
    "height" : "149",
    "left" : "145",
    "top" : "5",
    "width" : "199"
};
var css_innerright = {
    "height" : "149",
    "left" : "556",
    "top" : "5",
    "width" : "199"
};
var css_selected   = {
    "height" : "178",
    "left" : "332",
    "top" : "-5",
    "width" : "241"
};

var init_carousel = function()
{

    __leftmost    = $("#carousel_image_" + (__carousel_position - 2));
    __rightmost   = $("#carousel_image_" + (__carousel_position + 2));

    __innerleft   = $("#carousel_image_" + (__carousel_position - 1));
    __innerright  = $("#carousel_image_" + (__carousel_position + 1))

    __selected    = $("#carousel_image_" + __carousel_position);

    if ( __initial === true ) {

        // $("#carousel_arrow_left").hide();
	
        __leftmost.addClass("leftmost").fadeIn("fast");
        __rightmost.addClass("rightmost").fadeIn("fast");

        __innerleft.addClass("innerleft").fadeIn("fast");
        __innerright.addClass("innerright").fadeIn("fast");

        __selected.addClass("carousel_selected").fadeIn("fast");

        __initial = false;

    }
    /*if ( __carousel_position == 7 ) {
        $("#carousel_arrow_right img").attr('src','/templates/whatsfordinner/images/carousel/arrow_right_inactive.png');
    }
    if ( __carousel_position > 3 && __carousel_position != 7  ) {
        $("#carousel_arrow_left img").attr('src','/templates/whatsfordinner/images/carousel/arrow_left.png');
        $("#carousel_arrow_right img").attr('src','/templates/whatsfordinner/images/carousel/arrow_right.png');
    }
    if ( __carousel_position == 3 ) {
        $("#carousel_arrow_left img").attr('src','/templates/whatsfordinner/images/carousel/arrow_left_inactive.png');
    }*/
   

};
var counter = 1;

var move_carousel_right = function() {
   
    if ( __carousel_position == 7 ) {
        return false;
    }

    // Fade in the left arrow
    $("#carousel_arrow_left").fadeIn();
    
    if (!is_animating) {
        is_animating = true;

        // Get the next image positioned to make the animation smoother
        var next_image = "#carousel_image_" + (__carousel_position + 3);
        $(next_image).attr({
            "style": "height: 130px; top: 13px; left: 900px; width: 157px"
        });
        
        $(next_image + " img").attr({
            "style": "height: 85px; width: 147px;"
        });
		
        __leftmost.animate({
            left: "-157"
        }, __carousel_speed, function(){
            $(this).attr({
                "style": "left: -1000px"
            });
            $(this).removeClass("leftmost");
            $(this).addClass("carousel_images_hidden");
			
			
        });
		
        __innerleft.animate(css_leftmost, __carousel_speed, function(){
            $(this).addClass("leftmost");
            $(this).removeClass("innerleft");
        });
        $(".innerleft img").animate({
            "height": "85",
            "width": "147"
        }, __carousel_speed);
		
        __selected.animate(css_innerleft, __carousel_speed, function(){
            $(this).addClass("innerleft");
            $(this).removeClass("carousel_selected");
        });
        $(".carousel_selected img").animate({
            "height": "113",
            "width": "190"
        }, __carousel_speed);
		
        __innerright.animate(css_selected, __carousel_speed, function(){
            $(this).addClass("carousel_selected");
            $(this).removeClass("innerright");
        });
        $(".innerright img").animate({
            "height": "139",
            "width": "230"
        }, __carousel_speed);
		
        __rightmost.animate(css_innerright, __carousel_speed, function(){
            $(this).addClass("innerright");
            $(this).removeClass("rightmost");
        });
        $(".rightmost img").animate({
            "height": "113",
            "width": "190"
        }, __carousel_speed);
		
        $(next_image).animate(css_rightmost, __carousel_speed, function(){
            $(this).addClass("rightmost");
            __carousel_position++;
            init_carousel();
            is_animating = false;
        });
    }
    return true;
 

}

var move_carousel_left = function()
{
    if ( __carousel_position == 3 ) {
        return false;
    }

    // Fade in the right arrow
    $("#carousel_arrow_right").fadeIn();
    if (!is_animating) {
        is_animating = true;
        // Get the next image positioned to make the animation smoother
        var next_image = "#carousel_image_" + (__carousel_position - 3);
        $(next_image).attr({
            "style": "height: 130px; left: -157px; top: 13px; width: 157px"
        });
        $(next_image + " img").attr({
            "style": "height: 85px; width: 147px;"
        });
		
        __rightmost.animate({
            left: "900"
        }, __carousel_speed, function(){
            $(this).attr({
                "style": "left: -1000px"
            });
            $(this).removeClass("rightmost");
            $(this).addClass("carousel_images_hidden");
        });
		
        __innerright.animate(css_rightmost, __carousel_speed, function(){
            $(this).addClass("rightmost");
            $(this).removeClass("innerright");
        });
        $(".innerright img").animate({
            "height": "85",
            "width": "147"
        }, __carousel_speed);
		
        __selected.animate(css_innerright, __carousel_speed, function(){
            $(this).addClass("innerright");
            $(this).removeClass("carousel_selected");
        });
        $(".carousel_selected img").animate({
            "height": "113",
            "width": "190"
        }, __carousel_speed);
		
        __innerleft.animate(css_selected, __carousel_speed, function(){
            $(this).addClass("carousel_selected");
            $(this).removeClass("innerleft");
        });
        $(".innerleft img").animate({
            "height": "139",
            "width": "230"
        }, __carousel_speed);
		
        __leftmost.animate(css_innerleft, __carousel_speed, function(){
            $(this).addClass("innerleft");
            $(this).removeClass("leftmost");
        });
        $(".leftmost img").animate({
            "height": "113",
            "width": "190"
        }, __carousel_speed);
		
        $(next_image).animate({
            "left": "0"
        }, __carousel_speed, function(){
            $(this).addClass("leftmost");
            __carousel_position--;
            init_carousel();
            is_animating = false;
        });
    }
    return true;

};

var show_recipe_name = function( id )
{
    $(".carousel_recipe_name_" + id).fadeIn('fast');
};

var hide_recipe_name = function( id )
{
    $("#" + id + " .carousel_recipe_name").fadeOut('fast');
};

