/*
 Contains javascript functions for displaying and hiding video player and image player.
 26/05/06
 Julien Roubieu
 Rodrigo Budzinsky
*/

var BIG_WIDTH = '1000px';
var BIG_HEIGHT = '562.5px';
var BIG_HEIGHT_WITH_CONTROL = '640px';
var NORMAL_WIDTH = '512px';
var NORMAL_HEIGHT = '288px';
var NORMAL_HEIGHT_WITH_CONTROL = '318px';

var currentImageRatio;

/*
    Shows image player with given image
    @param imageSource source uri of the image
    @param imageRatio ratio of the image (width / height)
*/
function viewImage(imageSource, imageRatio)
{
    showPlayerArea();

    currentImageRatio = imageRatio;

    var imagePlayer = $('imagePlayer');

    var availableWidth = NORMAL_WIDTH;
    var availableHeight = NORMAL_HEIGHT;

    if (isMaximized())
    {
        availableWidth = BIG_WIDTH;
        availableHeight = BIG_HEIGHT;
    }

    stopVideo();

    hideVideoPlayer();

    if (!isImageVisible()) show(imagePlayer);

    var dim = getImageDimensionsToFitIn(availableWidth, availableHeight);
    changeImage(imageSource, dim[0], dim[1]);
}

function changeImage(source, w, h)
{
    var image = $('viewedImage');
    Element.hide(image);
    image.width = w;
    image.height = h;
    if (isMaximized()) {
        image.style.marginTop = ((BIG_HEIGHT.replace('px', '') - h) / 2) + 'px';
    }
    else {
        image.style.marginTop = ((NORMAL_HEIGHT.replace('px', '') - h) / 2) + 'px';
    }
    var newImage = new Image();
    newImage.onload = function() {
        $('viewedImage').src = source;
        MyEffect.Appear('viewedImage');
        newImage.onload = function() {};
    };
    newImage.src = source;
}

function isMaximized()
{
    return $("playerArea").style.height == BIG_HEIGHT_WITH_CONTROL;
}

function isImageVisible()
{
    return $('imagePlayer').style.display != 'none';
}

function isVideoVisible()
{
    return $('flashPlayer').style.display != 'none';
}


/* Calculate image dimensions that should be used so as the image has good ratio
   and fits in available space, for the current image being displayed.
   @param availableWitdh + "px"
   @param availableHeight + "px"
   @return an array [width, height].
*/
function getImageDimensionsToFitIn(availableWidth, availableHeight)
{
    availableHeight = availableHeight.replace("px", "");
    availableWidth = availableWidth.replace("px", "");
    var resizedHeight = availableHeight;
    var resizedWidth = availableWidth;

    var playerRatio = availableWidth / availableHeight;
    var imageRatio = currentImageRatio;
    if (playerRatio < imageRatio) {
      resizedHeight = availableWidth / imageRatio;
    } else {
      resizedWidth = availableHeight * imageRatio;
    }

    return new Array(resizedWidth, resizedHeight);
}


/*
    Hides image player
*/
function hideImage()
{
	var imagePlayer = $('imagePlayer');
	var image =  $('viewedImage');

    hide(image);
    image.height = '0';
    image.width = '0';

    hide(imagePlayer);
}


/*
    Hides the flash player object
*/
function hideVideoPlayer()
{
    if (!isVideoVisible()) return;

    var player = $("flashPlayer");
    var controls = $("controls");

        hide(player); // not sufficient in Safari and IE
        player.style.visibility = "hidden";
        player.style.position = "absolute";

        hide(controls); // not sufficient in Safari and IE
        controls.style.visibility = "hidden";
        controls.style.position = "absolute";
    }

/* Shows the player area if it is hidden. Since 1.6 */
function showPlayerArea()
{
    if ($('playerArea').style.display == 'none') {
        MyEffect.BlindDown('playerArea');
    }
}

/*
    Show the video player object
*/
function showVideoPlayer()
{
    showPlayerArea();

    var player = $("flashPlayer");
    var controls = $("controls");

	hideImage();
    show(player);
    player.style.visibility = "visible";
    player.style.position = "relative";
    show(controls);
    controls.style.visibility = "visible";
    controls.style.position = "relative";
}

function maximize(color)
{
	var info = $("infoArea");
	var player = $("flashPlayer");
	var embedded = $("flashEmbed");
	var imagePlayer = $('imagePlayer');
    var image = $('viewedImage');
	var playerDiv = $("playerDiv");
	var playerArea = $("playerArea");
    var playerControlImage = $('maxMinPlayerImage');

	playerArea.style.height = BIG_HEIGHT_WITH_CONTROL;
	playerDiv.style.height = BIG_HEIGHT;
	playerDiv.style.width = BIG_WIDTH;

    hide(info);

    player.width = BIG_WIDTH.replace("px", "");
    player.height = BIG_HEIGHT.replace("px", "");
    if (embedded) {
        embedded.setAttribute('width', BIG_WIDTH);
        embedded.setAttribute('height', BIG_HEIGHT);
    }

    imagePlayer.style.width = BIG_WIDTH;
    imagePlayer.style.height = BIG_HEIGHT;

    var dim = getImageDimensionsToFitIn(BIG_WIDTH, BIG_HEIGHT);
    image.width = dim[0];
    image.height = dim[1];
    image.style.marginTop = ((BIG_HEIGHT.replace('px', '') - image.height) / 2) + 'px';

    playerControlImage.src = "images/backgrounds/player_control_grande_"+color+".jpg";
    playerControlImage.setAttribute("useMap","#minimizarMap");
}

function minimize(color)
{
	var info = $("infoArea");
	var player = $("flashPlayer");
	var embedded = $("flashEmbed");
	var imagePlayer = $('imagePlayer');
    var image = $('viewedImage');
	var playerDiv = $("playerDiv");
	var playerArea = $("playerArea");
    var playerControlImage =  $('maxMinPlayerImage') ;

	playerArea.style.height = NORMAL_HEIGHT_WITH_CONTROL;
	playerDiv.style.height = NORMAL_HEIGHT;
	playerDiv.style.width = NORMAL_WIDTH;

    player.width = NORMAL_WIDTH.replace("px", "");
    player.height = NORMAL_HEIGHT.replace("px", "");
    if (embedded) {
        embedded.setAttribute('width', NORMAL_WIDTH);
        embedded.setAttribute('height', NORMAL_HEIGHT);
    }

    imagePlayer.style.width = NORMAL_WIDTH;
	imagePlayer.style.height = NORMAL_HEIGHT;

    var dim = getImageDimensionsToFitIn(NORMAL_WIDTH, NORMAL_HEIGHT);
    image.width = dim[0];
    image.height = dim[1];
    image.style.marginTop = ((NORMAL_HEIGHT.replace('px', '') - image.height) / 2) + 'px';

    show(info);

    playerControlImage.src = "images/backgrounds/player_control_peq_"+color+".jpg";
    playerControlImage.setAttribute("useMap","#maximizarMap");

}

/**
    Callback function. Called by the flash player whenever a new media is being played.
    It is called for each part of a playlist.
*/
function onPlayingMedia(mediaId)
{
    $A(document.getElementsByClassName("playingPart", $("playlistParts"))).each(function(element) {
       element.className = "notPlayingPart";
    });

    var partIndexSpan = $('partIndex'+mediaId);
    var partTitleSpan = $('partTitle'+mediaId);

    if (partIndexSpan != null) partIndexSpan.className = 'playingPart';
    if (partTitleSpan != null) partTitleSpan.className = 'playingPart';
}


/*
    Maximizes and mimizes player area
*/
function resize(color)
{
	if (isMaximized()) {
	   minimize(color);
	}
	else {
	   maximize(color);
	}
}

/* Hides an element */
function hide(element)
{
    Element.hide(element);
}

/* Show an element */
function show(element)
{
    Element.show(element);
}


/*
    Passes parameters to flash player to play a media
*/
function play(media, systemUrl, canEmail)
{
    showVideoPlayer();

    if (window.document.all || window.document.flashPlayer.GotoFrame) {
        // IE
        var player = window.document.flashPlayer;
        if (player && player.flashPlay) {
            playIE(media, systemUrl, canEmail);
        }
        else {
            setTimeout("playIE('"+media+"','"+systemUrl+"','"+canEmail+"');", 2500);
        }
    }
	else {
        // Firefox
        var player = $('flashEmbed');
        if (player.flashPlay) {
            playFirefox(media, systemUrl, canEmail);
        }
        else {
            setTimeout("playFirefox('"+media+"','"+systemUrl+"','"+canEmail+"');", 2500);
        }
	}
}


function playIE(media, systemUrl, canEmail)
{
    window.document.controls.GotoFrame(0);
    window.document.controls.Play();
    window.document.flashPlayer.flashPlay(media, canEmail, systemUrl);
}

function playFirefox(media, systemUrl, canEmail)
{
    var player = $("flashEmbed");
    var controls = $("flashControlEmbed");

    if (controls.GotoFrame) controls.GotoFrame(0);
    if (controls.Play) controls.Play();
    if (player.flashPlay) player.flashPlay(media, canEmail, systemUrl);
}

function stopVideo()
{
        if (window.document.flashPlayer.flashStop) {
        // IE and Safari
               window.document.flashPlayer.flashStop();
	}
	else if($('flashEmbed')){
        // Firefox
               var player = $('flashEmbed');
               if (player.flashStop) player.flashStop();
	}
}

function viewNews(title, content){
    var newsTitle = $('newsTitle');
    newsTitle.innerHTML = title;

    var newsContent = $('newsContent');
    newsContent.innerHTML = content;
}
