
//+---------------------------------------------------------------------+
// | ADEQUAE - www.atoutlibre.fr                                        |
// +---------------------------------------------------------------------+
// | Author : Alaric Mestre de Laroque <alaric.delaroque@atoutlibre.fr>   |
// | 10 août 2009  -  14:58:19
// +---------------------------------------------------------------------+

	/** PARAMETER */
	var wideIdActionPlayer = "myFlash_wild";
	var tinyIdActionPlayer = "myFlash";
	
	/*var wideWidth = 320;
	var wideHeight = 240;*/
	var wideWidth = 450;
	var wideHeight = 338;
	
	
	var tinyWidth = 150;
	var tinyHeight = 120;
	
	var playerPATH = "/flv_player/"
    
	var startVideoPosition = 1000;
		
	/** VARS */
	var currentIdActionPlayer = "myFlash";
    var currentIdUrlVideo = "";
    var currentIdUrlImage = "";
    
    var videoPosition = 0;
    
    var wideDisplay = 0;
    
    // PERMET D'EFFECTUER UNE SYNCHRO DES ACTIONS
    var lockActions = 1;
    
    var myListener = new Object();
    
    /**
     * Initialisation
     */
    myListener.onInit = function()
    {
        
    };
    /**
     * onClick event on the video
     */
    myListener.onClick = function()
    {
    	
    	if (lockActions!=1){
    		//lockActions==1 on attend d'avoir lancer la video une premiere fois pour initialiser le player
    		videoPosition = this.position;
	    	
	    	if (wideDisplay==0) {
	    		pause();
	    		displayWide();
	    		
	    	}else{
	    		setVolume(40);
	    		displayTiny();
	    	}
    	}
    };
    
    /**
     * onKeyUp event on the video
     */
    myListener.onKeyUp = function(pKey)
    {	
    	setVolume(100);
        
    };
    /**
     * onComplete event
     */
    myListener.onFinished = function()
    {
    	if (wideDisplay==1)
    	displayTiny();
    	stop();
    	setPosition(startVideoPosition);
    	loadImage(playerPATH+'actionPlay.png');
    	
    };
    
    /**
     * Update
     */
    myListener.onUpdate = function()
    {
        document.getElementById("info_playing").innerHTML = this.isPlaying;
        document.getElementById("info_url").innerHTML = this.url;
        document.getElementById("info_volume").innerHTML = this.volume;
        document.getElementById("info_position").innerHTML = this.position;
        document.getElementById("info_duration").innerHTML = this.duration;
        document.getElementById("info_buffer").innerHTML = this.bufferLength + "/" + this.bufferTime;
        document.getElementById("info_bytes").innerHTML = this.bytesLoaded + "/" + this.bytesTotal + " (" + this.bytesPercent + "%)";
        
        
        var isPlaying = (this.isPlaying == "true");
        /*document.getElementById("playerplay").style.display = (isPlaying)?"none":"block";
        document.getElementById("playerpause").style.display = (isPlaying)?"block":"none";
        */
        var timelineWidth = 160;
        var sliderWidth = 40;
        var sliderPositionMin = startVideoPosition;
        var sliderPositionMax = sliderPositionMin + timelineWidth - sliderWidth;
        var sliderPosition = sliderPositionMin + Math.round((timelineWidth - sliderWidth) * this.position / this.duration);
        
        if (sliderPosition < sliderPositionMin) {
            sliderPosition = sliderPositionMin;
        }
        if (sliderPosition > sliderPositionMax) {
            sliderPosition = sliderPositionMax;
        }
        
        //document.getElementById("playerslider").style.left = sliderPosition+"px";
    };
    
    function getFlashObject()
    {
        return document.getElementById(currentIdActionPlayer);
    }
    function play(url)
    {
    	_url = "";
    	if (url){
    		_url = url;
    		currentIdUrlVideo = url;
    		tUrl = _url.split("/");
    		sUrl = tUrl[tUrl.length-1];
    		(document.getElementById('FLV_VIDEO_TITLE')).innerHTML = "&nbsp;&nbsp;"+(sUrl.split("."))[0].toUpperCase().replace("_"," ");
    	}else
    		_url = currentIdUrlVideo;
    	
    	if (myListener.position == 0) {
            //getFlashObject().SetVariable("method:setUrl", "http://flv-player.net/medias/KyodaiNoGilga.flv");
    		getFlashObject().SetVariable("method:setUrl", _url);
    		
        }
    	
        getFlashObject().SetVariable("method:play", "");
        if (wideDisplay)
        	loadImage(playerPATH+'actionStop.png');
        else
        	loadImage(playerPATH+'actionGrandEcran.png');
        lockActions = 0;
    }
    function pause()
    {
        getFlashObject().SetVariable("method:pause", "");
        loadImage(playerPATH+'actionPlay.png');
    }
    function stop()
    {
        getFlashObject().SetVariable("method:stop", "");
        loadImage(playerPATH+'actionPlay.png');
    }
    function setWidth(iWidth)
    {
        getFlashObject().width = iWidth+"px";
    }
    function setHeight(iHeight)
    {
    	getFlashObject().height = iHeight+"px";
    }
    function setPosition(iPosition)
    {
        getFlashObject().SetVariable("method:setPosition", iPosition);
    }
    function setVolume(iVolume)
    {
        getFlashObject().SetVariable("method:setVolume", iVolume);
    }
    function loadImage(url)
    {
        //var url = document.getElementById("inputImage").value;
        /*var depth = document.getElementById("inputImageDepth").value;
        var verticalAlign = document.getElementById("inputImageVertical").value;
        var horizontalAlign = document.getElementById("inputImageHorizontal").value;
        */
    	_url = "";
    	if (url){
    		_url = url;
    		currentIdUrlImage = url;
    	}else
    		_url = currentIdUrlImage;
    	
    	
    	var depth = 1;
        var verticalAlign = 10;
        var horizontalAlign = 10;
        getFlashObject().SetVariable("method:loadMovieOnTop", _url+"|"+depth+"|"+verticalAlign+"|"+horizontalAlign);
    }
    function unloadImage()
    {
        var depth = document.getElementById("inputUnloadDepth").value;
        getFlashObject().SetVariable("method:unloadMovieOnTop", depth);
    }

    /**
     * Pilote le player qui sera l'objet de l'action
     */
    
    function defineCurrentActionPlayer(id_currentActionPlayer){
    	currentIdActionPlayer = id_currentActionPlayer;
    }
    
    /** AFFICHAGE DES VIDEO EN GRAND */
    
    var _oActionControlVideoPanel;
    
    function displayWide(){
    	
    	var madiv = document.createElement("div");
     	//madiv.style.='position:absolute;width:100%;height:500px;background-color:black;z-index:255;filter:alpha(opacity=10);-moz-opacity:10;opacity:0.1;';
     	madiv.style.position='absolute';
     	madiv.style.width = '100%';
     	madiv.style.height = '150%';
     	madiv.style.backgroundColor='black';
     	madiv.style.top='0';
     	madiv.style.left='0';
     	madiv.style.zIndex='253';	
     	if(document.all){
     		madiv.style.filter='progid:DXImageTransform.Microsoft.Alpha(Opacity=30)';
    	}else{
    		madiv.style.setProperty("-moz-opacity", 0.3, "");
    		madiv.style.setProperty("opacity", 0.3, "");
     	}
     	_oActionControlVideoPanel = madiv;
     	document.body.appendChild(madiv);
    	
    	pause();
    	(document.getElementById("HTML_ID_TINY_PLAYER_CONTENER")).style.display="none";
    	(document.getElementById("HTML_ID_WIDE_PLAYER_CONTENER")).style.display="block";
    	
    	// CALLE LA FONCTION AU MILIEU DE L'ECRAN
    	if (document.body)
    	{
	    	var larg = (document.body.clientWidth);
	    	var haut = (document.body.clientHeight);
	    }else{
	    	var larg = (window.innerWidth);
	    	var haut = (window.innerHeight);	
	    }
    	
    	larg = (larg/2) - (wideWidth/2);
    	haut = (haut/2) - (wideHeight/2);
    	
		(document.getElementById("HTML_ID_WIDE_PLAYER_CONTENER")).style.left = larg+"px";
    	(document.getElementById("HTML_ID_WIDE_PLAYER_CONTENER")).style.top = haut+"px";
		
    	url_video = (getFlashObject()).url;
    	position = (getFlashObject()).position;
    	defineCurrentActionPlayer(wideIdActionPlayer);
    	(getFlashObject()).url = url_video;
    	(getFlashObject()).position = position;
    	setWidth(wideWidth);
    	setHeight(wideHeight);
    	setTimeout(
    			function(){
    				
    				play();
	    			setTimeout(function (){
	    				setVolume(100);
	    				setPosition(videoPosition)},100);
    			}	
    		,2000);	
    	
    	wideDisplay = 1;
    }
    
    function displayTiny(){
    	document.body.removeChild(_oActionControlVideoPanel);
    	pause();
    	(document.getElementById("HTML_ID_TINY_PLAYER_CONTENER")).style.display="block";
    	(document.getElementById("HTML_ID_WIDE_PLAYER_CONTENER")).style.display="none";
    	url_video = (getFlashObject()).url;
    	position = (getFlashObject()).position;
    	defineCurrentActionPlayer(tinyIdActionPlayer);
    	(getFlashObject()).url = url_video;
    	(getFlashObject()).position = position;
    	setWidth(tinyWidth);
    	setHeight(tinyHeight);
    	
    	setTimeout(function() {
    			setPosition(videoPosition);
    			setVolume(40);
    	    	play();
    			setTimeout(pause,50);
    	}
    		,2000);
    	
    	wideDisplay = 0;
    	if (toolbarDefine){
    		refreshAction();
    	}
  
    }
    
    
    
    