﻿/*
movieSwap.js

    This file contains functions for swapping YouTube movies.
*/

/*

Example:

// Declare the movie data list
var _movieDataList = new Array(
    {'title':'Movie1','desc':'This is my favorite movie','hash':'...', 'url':'http://...'},
    {'title':'Movie2', 'desc':'This is an ok movie','hash':'...','url':'http://...'});

[...]

<body>
    <div id='movie_container'></div>
    <div id='movie_title'></div>
    <div id='movie_desc'></div>
    
    <a href="javascript:void(0)" onclick="nextMovie('movie_container','movie_title','movie_desc', _movieDataList)">Next &gt;</a>
</body>

*/



// Function: swapMovie
//  Description: inserts a movie into a block element
//  Parameters:
//      containerId -- id of the iframe where the movie will be inserted
//      titleId -- id of the element where the title will be displayed (can be empty string)
//      descId -- id of the element where the description will be displayed (can be empty string)
//      movieData -- an associative array of the following information:
//          'title' -- the movie title
//          'desc' -- the movie description
//          'url' -- the url of the movie
function swapMovie(containerId, titleId, descId, movieData)
{
    var container = document.getElementById(containerId);
    if (!container) return false;
    
    var title = document.getElementById(titleId);
    var desc = document.getElementById(descId);
    
    if (title) title.innerHTML = movieData["title"];
    if (desc) desc.innerHTML = movieData["desc"];
    
    container.src = movieData["url"];
    return true;
}

// Function: swapMovie_Hash
// Description: similar to swapMovie, but takes a the movie hash value rather than the url
// Parameters:
//      containerId
//      titleId
//      descId 
//      movieData -- assoc. array with the follow values:
//          'title' -- movie title
//          'desc' -- movie description
//          'hash' -- hash locator for the movie

function swapMovie_Hash(containerId, titleId, descId, movieData)
{
    var container = document.getElementById(containerId);
    if (!container) return false;
    
    var title = document.getElementById(titleId);
    var desc = document.getElementById(descId);
    
    if (title) title.innerHTML = movieData["title"];
    if (desc) desc.innerHTML = movieData["desc"];
    
    var movieHash = movieData["hash"];
    
    //container.innerHTML = "<object width=\"425\" height=\"350\"><param name=\"movie\" value=\"http://www.youtube.com/v/"+movieHash+"\"></param><param name=\"wmode\" value=\"transparent\"></param><embed src=\"http://www.youtube.com/v/"+movieHash+"\" type=\"application/x-shockwave-flash\" wmode=\"transparent\" width=\"425\" height=\"350\"></embed></object>";
    container.src = "http://www.youtube.com/v/"+movieHash;
    
    return true;
}

// Function: nextMovie
// Description: Loads the next movie in the list of movies using an internal
//      index variable.
// Parameters:
//      containerId
//      titleId
//      descId
//      movieDataList -- an array of associative arrays containing information
//          needed to load the movie.
var _g_nextMovieIdx = 0;
function nextMovie(containerId, titleId, descId, movieDataList)
{
    if (_g_nextMovieIdx >= movieDataList.length || _g_nextMovieIdx < 0)
        _g_nextMovieIdx = 0;
    
    swapMovie(containerId, titleId, descId, movieDataList[_g_nextMovieIdx++]);
}

// Function: setCurrentMovie
// Description: Sets the current movie to synchronize the "nextMovie" function
//      for the case when the user did not use "nextMovie" to load the movie.
// Parameters:
//      idx -- index of the current movie that was loaded
function setCurrentMovie(idx)
{
    _g_nextMovieIdx = idx + 1;
}