var fx_fadeMxcVideoAccueil = null;
var playerTimer = null;
var playerPosition = null;

var tabIndexMenu = {};
    tabIndexMenu = {'APPRENDRE' : 0, 'COMPRENDRE' : 1, 'REVISER' : 2, 'PROGRESSER' : 3};

var tabCouleurMenu = [];
    tabCouleurMenu[0] = {'libelle' : 'apprendre',   'couleur' : '#0b61b8'};
    tabCouleurMenu[1] = {'libelle' : 'comprendre',  'couleur' : '#f5a400'};
    tabCouleurMenu[2] = {'libelle' : 'reviser',     'couleur' : '#f43500'};
    tabCouleurMenu[3] = {'libelle' : 'progresser',  'couleur' : '#538c09'};

var nb_photoAleatoire = Math.round(Math.random() * (tabCouleurMenu.length-1));

function chargeImagesPlayer() {
    for (i=0 ; i < tabCouleurMenu.length; i++) {
        new Image().src = '/i/site/accueil/v2/animation/' + tabCouleurMenu[i].libelle + '.jpg';
    }
}

function fadeMxcVideoAccueil(indexPhoto) {
   changeColorMenu(indexPhoto, tabCouleurMenu[indexPhoto].couleur);
   fx_fadeMxcVideoAccueil.stop();

   $('mxcFadeVideo').setStyles({'background-image': $('mxcFadeVideo-under').getStyle('background-image'), 'opacity' : 1});
   $('mxcFadeVideo-under').setStyle('background-image', 'url(/i/site/accueil/v2/animation/' + tabCouleurMenu[indexPhoto].libelle + '.jpg)');

   fx_fadeMxcVideoAccueil.start({
       'opacity' : [1,0]
   });
}

function playerStart(position) {
    playerStop();
    if(position !== undefined) {
        playerSetPosition(position);
    }
    playerTimer = setInterval("playerNext()", 4500);
}

function playerStop() {
    if(playerTimer != null) clearInterval(playerTimer);
    playerTimer = null;
}

function playerSetPosition(position) {
    var min = 0;
    var max = tabCouleurMenu.length-1;

    if(min <= position && position <= max) {
        playerPosition = position;
    } else if(max < position) {
        playerPosition = min;
    } else if(position < min) {
        playerPosition = max;
    }
    fadeMxcVideoAccueil(playerPosition);
}

function playerNext() {
    playerSetPosition(playerPosition + 1);
}

function playerPrev() {
    playerSetPosition(playerPosition - 1);
}

function changeColorMenu(indexPhoto, color) {
    $ES('#menuFadeHome div').setStyle('color', '#1c3193');
    $ES('#menuFadeHome .bt-' + tabCouleurMenu[indexPhoto].libelle).setStyle('color', color);
}

function afficherVideoMaxicours() {
	var pop_mod = null;
	if ((pop_mod = $('pop-modal')) != null) {
        if (window.Audience && Audience.clic) {            
            ["optimizerHome20100503", "optimizerHome20100512"].each(function(cookieName) {
                var cookiePresent = Cookie.get(cookieName) || '';
                if (cookiePresent != '') {
                    //Audience.clic(null, null, cookieName+'Videomark');
                }
            });
        }
        pop_modal('bleu-800x565',{modal: true, fondModalClose : true});
		$('titre-modal').setHTML($('suite-titre').innerHTML);
        $('fermer-modal-bt').setHTML('&nbsp;');

		$('contenu-modal').addClass('iframeVideoMarkLoading');

		var iframeVideoMark = new Element('iframe',  {'id' : 'iframeVideoMark'}).injectInside($('contenu-modal'));
        iframeVideoMark.src='/demo/videomark/video1.html';

        iframeVideoMark.setStyles({
			'width': 796,
			'height': 541,
			'overflow' : 'hidden',
			'border' : 0
		});
		iframeVideoMark.addEvent('load', function() {
		    $('contenu-modal').removeClass('iframeVideoMarkLoading');
        });
		$('contenu-modal').setStyle('overflow' , 'hidden');
	}
}

function initMxcVideoAccueil() {
   fx_fadeMxcVideoAccueil = new Fx.Styles($('mxcFadeVideo'), {duration: 500, wait : false});
   $('mxcFadeVideo-under').setStyle('background-image', 'url(/i/site/accueil/v2/animation/' + tabCouleurMenu[nb_photoAleatoire].libelle + '.jpg)');
   changeColorMenu(nb_photoAleatoire, tabCouleurMenu[nb_photoAleatoire].couleur);

    var list = $$('#menuFadeHome .menuFadeHomeBt');
    list.each(function(element) {
        var elTitle = element.title;
    	element.addEvent('mouseenter', function(){
            playerStop();
            fx_fadeMxcVideoAccueil.stop();
            playerSetPosition(tabIndexMenu[elTitle]);
    	});

    	element.addEvent('mouseleave', function(){
            playerStart();
    	});
    });

    $('mxcFadeVideoContainer').addEvent('mouseenter', function() {
        playerStop();
    });

    $('mxcFadeVideoContainer').addEvent('mouseleave', function() {
        playerStart();
    });
    
    $('mxcFadeVideo-bt').addEvent("click", function() {
       playerStop();
       afficherVideoMaxicours();
    });

    playerStart(nb_photoAleatoire);
}
