var pluginRetryCount = 0;
var ret;

function fd_resizeimagebyid(id)
{
    fd_resizeimage(getElement(id), arguments[1], arguments[2], arguments[3]);
}

function fd_resizeimage(img)
{
    if (img.id != null && img.id != "" && img.width == 0 && img.getAttribute('retryCount') < 10)
    {
        var retryCount = img.getAttribute('retryCount') == null ? 1 : (img.getAttribute('retryCount') + 1);
        img.setAttribute('retryCount', retryCount);
        setTimeout('fd_resizeimagebyid(\'' + img.id + '\',' + arguments[1] + ',' + arguments[2] + ',\'' + arguments[3] + '\')', 100);
        return;
    }

    var width = (arguments[1] == null ? 320 : arguments[1]);
    var height = (arguments[2] == null ? 240 : arguments[2]);
    var color = (arguments[3] == null ? 'black' : arguments[3]);
    var verteven = (arguments[4] == null ? false : arguments[4]);

    fd_resizeimagetosize(img, width, height);

    if (img.style.borderColor == "") img.style.borderColor = color;
    img.style.borderStyle = 'solid';
    img.style.borderTopWidth = (verteven ? (Math.ceil((height - img.height) / 2) + 'px') : '0');
    img.style.borderBottomWidth = (verteven ? (Math.floor((height - img.height) / 2) + 'px') : ((height - img.height) + 'px'));
    img.style.borderLeftWidth = Math.ceil((width - img.width) / 2) + 'px';
    img.style.borderRightWidth = Math.floor((width - img.width) / 2) + 'px';
    img.style.visibility = 'visible';
}

function fd_resizeimagetosize(img, width, height)
{
    var origHeight = img.height;
    var origWidth = img.width;
    img.height = height;
    if (img.width == origWidth) img.width = img.width * height * 1.0 / origHeight;

    origHeight = img.height;
    origWidth = img.width;
    if (img.width > width)
    {
        img.width = width;
        if (img.height == origHeight) img.height = img.height * width * 1.0 / origWidth;
    }
}

function fd_resizeimagetowidth(img, width)
{
    var origWidth = img.width;
    img.height = img.height * width * 1.0 / img.width;
    if (img.width == origWidth) img.width = width;
}

function fd_resizeimagetoheight(img, height)
{
    var origHeight = img.height;
    img.width = img.width * height * 1.0 / img.height;
    if (img.height == origHeight) img.height = height;
}

function fd_resizeImagesUnder(elem)
{
    var images = elem.getElementsByTagName('img');
    for (i = 0; i < images.length; i++)
    {
        fd_resizeimage(images[i], arguments[1], arguments[2], arguments[3]);
    }
}

function fd_resizeImagesUnderToWidth(elem, width)
{
    var images = elem.getElementsByTagName('img');
    for (i = 0; i < images.length; i++)
    {
        var img = images[i];

        if (img.width > width)
        {
            fd_resizeimagetowidth(img, width);
        }
    }
}

function fd_playVideo()
{
    var video = getElement('filmVideo');
    var videoImg = getElement('filmVideoImg');

    video.style.zIndex = videoImg.style.zIndex + 5;
    video.style.visibility = 'visible';
    videoImg.style.visibility = 'hidden';

    pluginRetryCount = 0;
    fd_playWhenReady();
}

function fd_stopVideo()
{
    try
    {
        document.filmVideo.Stop();
    }
    catch (e)
    {
        // ignore since that probably means that the video isn't playing    
    }

    var controller = (arguments[0] == null) ? false : arguments[0];
    if (!controller) fd_stopButtonToPlay(getElement('playButtonLink'));
}

function fd_playWhenReady()
{
    // First disable the play button.  Then we'll change it to a stop button once we are playing the movie
    var playButtonLink = getElement('playButtonLink');
    playButtonLink.disabled = true;
    getElement('mediaContent').cursor = 'wait';

    var status;
    try
    {
        status = document.filmVideo.GetPluginStatus();
    }
    catch (e)
    {
        pluginRetryCount++;
        //getElement('mediaTest').innerHTML = 'Plugin retry #' + pluginRetryCount;

        if (pluginRetryCount == 2)
        {
            // re-enable the play button because there was a problem
            playButtonLink.disabled = false;
            getElement('mediaContent').cursor = 'auto';
            alert(getFilmDetailsErrorPlaying());
            return;
        }

        setTimeout("fd_playWhenReady()", 1000);
        return;
    }

    if (status != 'Playable' && status != 'Complete' && status.substring(0, 5) != 'Error')
    {
        /*var mt = getElement('mediaTest');
        if (mt.innerHTML.indexOf('Status') != -1) mt.innerHTML += '.'
        else mt.innerHTML = 'Status: ' + status;*/

        setTimeout("fd_playWhenReady()", 1000);
        return;
    }

    if (status == 'Playable' || status == 'Complete')
    {
        //getElement('mediaTest').innerHTML = 'Playing: ' + status;
        document.filmVideo.Play();

        // change the play button to a stop button
        fd_playButtonToStop(playButtonLink);
        playButtonLink.disabled = false;
        getElement('mediaContent').cursor = 'auto';

        // look for the video to finish so that we can change the play button back
        fd_restoreAfterVideo();
    }
    else
    {
        // re-enable the play button because there was a problem
        playButtonLink.disabled = false;
        getElement('mediaContent').cursor = 'auto';
    }
}

function fd_restoreAfterVideo()
{
    // make sure the video is playing
    var playButtonLink = getElement('playButtonLink');
    if (playButtonLink.innerHTML == getFilmDetailsStop())
    {
        // check every 2 seconds for the video to finish
        var videoObj = document.filmVideo;
        if (videoObj.GetTime() == videoObj.GetEndTime())
        {
            // change the stop button to a play button and replace the image over the video
            fd_stopButtonToPlay(playButtonLink);

            var video = getElement('filmVideo');
            var videoImg = getElement('filmVideoImg');

            video.style.zIndex = videoImg.style.zIndex - 5;
            video.style.visibility = 'hidden';
            fd_resizeimage(videoImg);
        }
        else
        {
            setTimeout("fd_restoreAfterVideo()", 2000);
        }
    }
}

function fd_playButtonToStop(buttonLink)
{
    buttonLink.href = 'javascript:fd_stopVideo();';
    buttonLink.innerHTML = getFilmDetailsStop();
}

function fd_stopButtonToPlay(buttonLink)
{
    buttonLink.href = 'javascript:fd_playVideo();';
    buttonLink.innerHTML = getFilmDetailsPlay();
}

/** Ajax function to send emails with status notification on return **/
function fd_sendMail()
{
    if (!validateTellAFriend()) return;

    ret = getElement(arguments[0]);
    var from = getAuthenticated() ? '' : getElementValue(getParamMyEmail());
    var to = getElementValue(getParamEmail());
    var copy = getElementChecked(getParamEmailCopy()) ? 'on' : 'off';
    var msg = getElementValue(getParamMessage());
    var template = getElementValue(getParamTemplate());
    var view = getElementValue(getParamView());
    var filmId = getElementValue(getParamFilmId());
    ret.innerHTML = getFilmDetailsSendingEmail();

    send(fd_handleTellAFriendSent,
         getParamAction(), getActionTellAFriendAjax(),
         getParamMyEmail(), from,
         getParamEmail(), to,
         getParamEmailCopy(), copy,
         getParamMessage(), msg,
         getParamTemplate(), template,
         getParamView(), view,
         getParamFilmId(), filmId == '' ? '0' : filmId);

    return false;
}

function fd_handleTellAFriendSent(response, args)
{
    ret.innerHTML = response;

    // only reset the values if there wasn't an error
    if (response.toLowerCase().indexOf('problem') == -1)
    {
        setElementValue(getParamEmail(), tellAFriendEmailSav);
        setElementValue(getParamMessage(), tellAFriendMessageSav);
        if (!getAuthenticated()) setElementValue(getParamMyEmail(), tellAFriendMyEmailSav);
    }
}
