﻿//--- Functions for handling multiple players at the same page.

var playerInitDelay = 1; //000;
var playerHiddenLeft = -435; // -500;
var playerHiddenTop = 0;
var playerHomeTop;
var playerHomeLeft;
var activePlayer;

//--- Init player system
function InitMultiPlayer()
{
    //--- Get player home position
    layer = GetObject("MultiPlayerHome");
    //Dump(layer);
	if (!layer)
	    alert("Layer \"" + layerName + "\" not found.");
    else
    {
        playerHomeTop = layer.offsetTop;
        playerHomeLeft = layer.offsetLeft;
        //alert(playerHomeLeft + ":" + playerHomeTop);
    }
    
    //--- Get flash media player, set playlist flag
    movie = thisMovie("DigitaliaPlayer");
    if (movie)
        movie.PlayListOpen = false;
}



//--- Activate the given player layer
function ActivatePlayer(layerName)
{
    switch (layerName)
    {
        case "SingleImageLayer":
            ShowPlayer("SingleImageLayer");
            HidePlayer("FlashImagePlayerLayer");
            StopFlashMedia();
            CloseFlashMediaPlaylist();
            HidePlayer("FlashMediaPlayerLayer");
            //HidePlayer("divDigitaliaPlayer");       //--- The second div to support opening/closing playlist.
            StopWindowsMedia();
            HidePlayer("MediaPlayerLayer");
            break;

        case "FlashImagePlayerLayer":
            HidePlayer("SingleImageLayer");
            ShowPlayer("FlashImagePlayerLayer");
            StopFlashMedia();
            CloseFlashMediaPlaylist();
            HidePlayer("FlashMediaPlayerLayer");
            //HidePlayer("divDigitaliaPlayer");
            StopWindowsMedia();
            HidePlayer("MediaPlayerLayer");
            break;
            
        case "FlashMediaPlayerLayer":
            HidePlayer("SingleImageLayer");
            HidePlayer("FlashImagePlayerLayer");
            ShowPlayer("FlashMediaPlayerLayer");
            //ShowPlayer("divDigitaliaPlayer");
            StopWindowsMedia();
            HidePlayer("MediaPlayerLayer");
            break;
            
        case "MediaPlayerLayer":
            HidePlayer("SingleImageLayer");
            HidePlayer("FlashImagePlayerLayer");
            StopFlashMedia();
            CloseFlashMediaPlaylist();
            HidePlayer("FlashMediaPlayerLayer");
            //HidePlayer("divDigitaliaPlayer");
            ShowPlayer("MediaPlayerLayer");
            break;
        }
        
        activePlayer = layerName;
}

//--- Show a player layer
function ShowPlayer(layerName)
{
		layer = GetObject(layerName); 
		if (!layer)
		    alert("Layer \"" + layerName + "\" not found.");
	    else
	    {
	        layer.style.left = playerHomeLeft + "px";
	        layer.style.top = playerHomeTop + "px";
		}
}

//--- Hide a player layer
function HidePlayer(layerName)
{
 		layer = GetObject(layerName); 
		if (!layer)
		    alert("Layer \"" + layerName + "\" not found.");
		else
		{
		    //--- "Hide"
		    layer.style.left = playerHiddenLeft + "px";
	        layer.style.top = playerHiddenTop + "px";
		}  
}

/*

        Image player

*/
//--- Select an image by id
function SelectImage(productId, productFileId)
{
    //--- If there is only one image, we do nothing
    if (ImageCount == 1)
        return;
    
    //--- Activate the image player
    ActivatePlayer("FlashImagePlayerLayer");
    
    movie = thisMovie("DigitaliaImagePlayerNormal");
    if (movie)
	    movie.selectImageByID(productFileId);
	else
	    alert("Flash Image Player not found.");
}

/*

        Flash media player

*/
//--- Select a flash media by id
function SelectFlashMedia(productId, productFileId)
{
    if (activePlayer == "FlashMediaPlayerLayer")
        StartFlashMedia(productId, productFileId);
    else
    {
        //--- Activate the flash media player
        ActivatePlayer("FlashMediaPlayerLayer");
        
        //--- Some delay to init the flash player
        setTimeout("StartFlashMedia(" + productId + ", " + productFileId + ")", playerInitDelay);
    }
}

//--- Start the flash media player
function StartFlashMedia(productId, productFileId)
{
    movie = thisMovie("DigitaliaPlayer");
    if (movie)
    {
	    movie.selectItemByID(productFileId);
	    
	    //--- Open playlist
	    if (!movie.PlayListOpen)
	    {
	        movie.forceOpen();
	        movie.PlayListOpen = true;
	    }
	}
	else
	    alert("Flash Media Player not found.");
}

//--- Stop the flash media player
function StopFlashMedia()
{
    movie = thisMovie("DigitaliaPlayer");
    if (movie && movie.stopPlaying)
    {
        //alert("stop");
	    movie.stopPlaying();
	}
}

//--- Close the flash media player playlist
function CloseFlashMediaPlaylist()
{
    movie = thisMovie("DigitaliaPlayer");
    if (movie && movie.closeImmediately)
    {
        //alert("close");
	    movie.closeImmediately();
	    
	    //--- Close the div - the event is not fired in closeImmediately
        setPlayerWidth("DigitaliaPlayer", closedWidth);
        setPlayerWidth("DigitaliaPlayer", closedWidth);
        setStyleWidth(videoPlayerDivName, closedWidth);
	    
	    movie.PlayListOpen = false;
	}
}

/*

        Windows media player

*/
//--- Select a windows video
function SelectWindowsVideo(productId, productFileId)
{
    //--- Activate the windows media player and seek to the given id
    ActivatePlayer("MediaPlayerLayer");

    //--- Seek to the given id
    StartWindowsMedia(productId, productFileId);
}

//--- Select a windows audio
function SelectWindowsAudio(productId, productFileId)
{
    //--- Activate the image player or the single image
    if (ImageCount == 1)
        ActivatePlayer("SingleImageLayer");
    else
        ActivatePlayer("FlashImagePlayerLayer");
    
    //---  Seek to the given id
    StartWindowsMedia(productId, productFileId);
}

function StartWindowsMedia(productId, productFileId)
{
    player = GetObject("MediaPlayerLayer");
    if (player)
    {
        player.innerHTML = 
        '<object id="MediaPlayer" class="MediaPlayer" classid="CLSID:22D6F312-B0F6-11D0-94AB-0080C74C7E95" codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=6,4,5,715" standby="Loading Microsoft® Windows® Media Player components..." type="application/x-oleobject">' +
        '      <param name="AutoStart" value="True" />' +
        '      <param name="ShowControls" value="True" />' +
        '      <param name="ShowStatusBar" value="True" />' +
        '      <param name="FileName" value="' + MediaPlayerPlaylistBase + "&ProductId=" + productId + "&ProductFileId=" + productFileId + '" />' +
              
        '      <embed type="application/x-mplayer2" name="MediaPlayer" class="MediaPlayer" pluginspage="http://www.microsoft.com/Windows/MediaPlayer/"' +
        '       autostart="1"' +
        '       showcontrols="1"' +
        '       showstatusbar="1"' +
        '       src="' + MediaPlayerPlaylistBase + "&ProductId=" + productId + "&ProductFileId=" + productFileId + '" >' +
        '      </embed>' +
        '    </object>';
        
	    //player.URL = MediaPlayerPlaylistBase + "&ProductId=" + productId + "&ProductFileId=" + productFileId; //.controls.play(); 
	    //alert(player.URL);
	    //player.controls.play();
	}
	else
	    alert("Windows Media Player layer not found.");
}

//--- Stop the windows media player
function StopWindowsMedia()
{
    player = GetObject("MediaPlayerLayer");
    if (player)
	    player.innerHTML = '';
}



