
jQuery('document').ready(function() {
    fleuranceTopsellingInit();
});

var topselling_currentProduct = 0;
var topselling_isAnimating = false;
var topselling_nextAnnimation = getCurrentTimeMillis()+3000;

function fleuranceTopsellingInit() {
    if (jQuery('#productoftheweek').size()) {
	    //sets the onclick for arrows
	    jQuery('#topselling_nav_prev').click(function(){         
	        topselling_nextAnnimation = getCurrentTimeMillis()+10000;
	        topselling_nav(-1); 
	        return false;
	    });
	    jQuery('#topselling_nav_next').click(function(){ 
	        topselling_nextAnnimation = getCurrentTimeMillis()+10000;
	        topselling_nav(1); 
	        return false;
	    });
	    
	    jQuery('#topselling_navigator li').click(function(){
	        if(!topselling_isAnimating)
	        {
	            topselling_nextAnnimation = getCurrentTimeMillis()+10000;
	            topselling_currentProduct = parseInt(this.id.replace("topselling_button_",""));
	            topselling_animate();
	        }
	    });
	    
	    //adds the first element at the end of the list:
	    var first = $('topselling_product_0');
	    var firstClone = document.createElement('div');
	    firstClone.id = 'topselling_product_last';
	    firstClone.className = first.className;
	    firstClone.innerHTML = first.innerHTML;
	    $(first.parentNode).appendChild(firstClone);
	    
	    setInterval(topselling_autoRotate,500);
	    
	    topselling_animate();
    }
}

function topselling_autoRotate()
{
    if (!topselling_isAnimating && getCurrentTimeMillis() >= topselling_nextAnnimation)
    {
        topselling_nextAnnimation = getCurrentTimeMillis()+3000;
        topselling_nav(+1);
    }
}

function getCurrentTimeMillis()
{
    return new Date().getTime();
}

function topselling_nav(delta)
{
    if (!topselling_isAnimating)
    {
        topselling_currentProduct += delta;
        
        //checks for "bounds cross"
        if (topselling_currentProduct < 0) {
            jQuery('#topselling_container').scrollLeft(topselling_nbElements*263); //magic numbers to remove
            topselling_currentProduct = topselling_nbElements-1;
        }
        else if (topselling_currentProduct >= topselling_nbElements) {
            //do nothing... the code is in the annimate callback
        }
        
        topselling_animate();
    }
}

function topselling_animate()
{
    if (!topselling_isAnimating)
    {
        if( jQuery('#topselling_container .current').find('.picto_remise, .cadeau_promo_diff').size() ) {
            // Discount hide
            jQuery('#topselling_container .current').find('.picto_remise, .cadeau_promo_diff').fadeOut(100);
            jQuery('#topselling_container .current').removeClass('current');
            topselling_animate();

        } else {

            jQuery('#topselling_container .current').removeClass('current');

            var scrollOffset = topselling_currentProduct*263;

            topselling_isAnimating = true;

            jQuery('#topselling_container').animate({scrollLeft: scrollOffset+'px'}, 400, function(){
                //sets navigator buttons color
                jQuery('#topselling_navigator li').removeClass('active');
                var buttonIndex = topselling_currentProduct;
                if (topselling_currentProduct >= topselling_nbElements) { //selects the fist button if we cross the borders
                    buttonIndex = 0;
                    jQuery('#topselling_container').scrollLeft(0); //reset the scrollLeft
                    topselling_currentProduct = 0;
                }
                jQuery('#topselling_button_'+buttonIndex).addClass('active');
                // Discount display
                jQuery('#topselling_product_' + topselling_currentProduct).addClass('current');
                jQuery('#topselling_container .current').find('.picto_remise, .cadeau_promo_diff').fadeIn();
                topselling_isAnimating = false;
            });

        }

    }
}
