var review_Scrolling = false;


function review_scrollToNext()
{
    if (!review_Scrolling && review_canScrollDown())
    {
        //we can scroll
        review_currentReview++;
        review_scrollToCurrentReview();
    }
}

function review_scrollToPrevious()
{
    if (!review_Scrolling && review_canScrollUp())
    {
        //we are not a the top of the list
        review_currentReview--;
        review_scrollToCurrentReview();
    }
}

function review_scrollToCurrentReview()
{    
    var id = review_currentReview;
    // to get the offset inside the scrollable div, we caclculate 
    // the difference between the first and the current review
    var scrollOffset = review_getReviewOffsetTop(id) - review_getReviewOffsetTop(0);
    review_Scrolling = true;
    jQuery('#list_avis_container').animate({scrollTop: scrollOffset+'px'}, 400, function(){
        //sets the buttons visibility
        var arrowUp = jQuery('#fleche_avis_haut_home')[0];
        var arrowDown = jQuery('#fleche_avis_bas_home')[0];
        if (review_canScrollUp()) {
            arrowUp.src = removeDisabledFromImageName(arrowUp.src);
        } else {
            arrowUp.src = addDisabledToImageName(arrowUp.src);
        }
        
        if (review_canScrollDown()) {
            arrowDown.src = removeDisabledFromImageName(arrowDown.src);
        } else {
            arrowDown.src = addDisabledToImageName(arrowDown.src);
        }
        
        review_Scrolling = false;
    });
}

function addDisabledToImageName(img)
{
    if (typeof(img) != 'undefined' && img.indexOf('_disabled.png') == -1)
    {
        return img.replace(".png","_disabled.png");
    }
    return img;
}

function removeDisabledFromImageName(img)
{
    if (typeof(img) != 'undefined' && img.indexOf('_disabled') != -1)
    {
        return img.replace("_disabled","");
    }
    return img;
}

function review_getReviewOffsetTop(id)
{
	var result = 0;
	
	if (jQuery('#review-'+id).offset()) {
		result = jQuery('#review-'+id).offset().top;
	}
	
    return result;
}

function review_canScrollDown()
{
    var tmpReviewId = review_currentReview + 1;
    //to be able to scroll down, we have to be able to scroll...
    // witch means that the last element needs to be under the 
    // visible part of the container
    if (jQuery('#review-'+tmpReviewId).lenght != 0)
    {
        //the element exists
        var last = jQuery('#list_avis_container .list_avis.last')
        var container = jQuery('#list_avis_container');
        var lastBottom = last.outerHeight();
        
        if (last.size()) {
        	lastBottom = lastBottom + last.offset().top;
        }
        
        var containerBottom = container.offset().top + container.outerHeight();
        if (lastBottom > containerBottom)
        {
            return true;
        }
    }
    return false;
}

function review_canScrollUp()
{
    return review_currentReview > 0;
}
