var mainManchette = function() {
    var timeToFadeInAndOutInMiliSeconds = 300;
	var timeToNextBlockinMiliSeconds = 7000;
    var intervalId = null;
    var playStatus = true;
    var blocks = $j('.machetteJsBlock');
    var currentBlock = 0;
    var previousBlock = 0;
    var blocksLength = blocks.length;
	var playImage = 'manchetteControlBarPlay.jpg';
	var pauseImage = 'manchetteControlBarPauze.jpg';

    var updateDisplayedCounter = function() {
        $j('#manchetteCounter').text(currentBlock+1 + ' / ' + blocksLength);
    };

    var hideBlocks = function() {
        for(i = 1; i < blocksLength; i++) {
            $j(blocks[i]).css('display', 'none');
        }
    };

    var addMachetteBlockIdentifierForGoogleAnalytics = function() {
        for(i = 0; i < blocksLength; i++) {
            var html = $j(blocks[i]).html();
            html = html.replace(/content-manchettebox/g, 'content-manchettebox'+(i+1));
            $j(blocks[i]).html(html);
        }
    };

    var extractImagePath = function() {
        var imagePath = $j('#manchettePlayPause').attr('src');
        var returnPath = imagePath.replace(pauseImage, '');
        returnPath = returnPath.replace(playImage, '');
        return returnPath;
    }

    $j('#manchetteForward').click(function(){
        displayNextBlock();
        skynet_event('control-manchettebox', 'click', 'forward','');
    });

    var displayNextBlock = function() {
        previousBlock = currentBlock;
        currentBlock++;
        if(currentBlock > blocksLength-1) {
            currentBlock = 0;
            displayPlayAndSetPause();
        }
        fadeInBlock();
    };

    $j('#manchetteRewind').click(function(){
        displayPreviousBlock();
        skynet_event('control-manchettebox', 'click', 'rewind','');
    });

    var displayPreviousBlock = function() {
        previousBlock = currentBlock;
        currentBlock--;
        if(currentBlock < 0) {
            currentBlock = blocksLength-1;
        }
        fadeInBlock();
    };

    var fadeInBlock = function(){
        $j(blocks[previousBlock]).fadeOut(timeToFadeInAndOutInMiliSeconds);
        $j(blocks[currentBlock]).fadeIn(timeToFadeInAndOutInMiliSeconds);
        updateDisplayedCounter();
    };

    var setBlockInterval = function() {
        intervalId = setInterval(displayNextBlock,timeToNextBlockinMiliSeconds);
    };

    $j('#manchette,#manchetteControlBar').mouseover(function(){
        clearInterval(intervalId);
    });

    $j('#manchette,#manchetteControlBar').mouseout(function(){
        if(playStatus) {
            setBlockInterval();
        }
    });

    $j('#manchettePlayPause').click(function(){
        if(playStatus) {
            displayPlayAndSetPause();
            skynet_event('control-manchettebox', 'click', 'pause','');
        } else {
            displayPauseAndSetPlay();
            skynet_event('control-manchettebox', 'click', 'play','');
        }
    });

    var displayPlayAndSetPause = function() {
        playStatus = false;
        clearInterval(intervalId);
        $j('#manchettePlayPause').attr('src', extractImagePath()+playImage);
    };

    var displayPauseAndSetPlay = function() {
        playStatus = true;
        setBlockInterval();
        $j('#manchettePlayPause').attr('src', extractImagePath()+pauseImage);
    };

    updateDisplayedCounter();
    hideBlocks();
    addMachetteBlockIdentifierForGoogleAnalytics();
    setBlockInterval();
}();