﻿/**
    This library carries functions that are useful for embedding or controlling a Flash movie within a Web page.
    
    Priority Functions (see the function definitions for more information):

      Creates code to embed a Flash movie/object.
        insertFlash()

      Creates code to embed a Flash movie/object, including code for an enclosing layer.
        insertFlashLayer()

      Creates code to embed a Flash movie/object on an existing layer.
      Notes: A target "div" element (layer) should exist before invoking this function.
        updateFlashLayer()

      Controls the playback of a Flash movie.
        playFlashMovie()
        stopFlashMovie()
        rewindFlashMovie()
  **/

// - - - - - - - - - - - - - - - - Default Flash Movie Values

// Assign values to the following variables that represent the MOST COMMON NEEDS of the course utilizing them.
// Functions that embed Flash movies can OVERRIDE THESE VALUES when deviation from the defaults is required.

// Identifies the location of a folder containing Flash files used in the course.
// Obs: This only gets prepended to a default Flash movie file name (not a specified custom path and file name).
var str_movieFolder = '../flash/';

// Identifies a window or frame to contain primary Flash objects by specifying its name.
var str_defaultWindowName = 'contentFrame';

// Identifies a "div" element (layer) to contain primary Flash objects by specifying its "id" attribute value.
var str_defaultFlashLayerID = 'FlashLayer';

// Identifies the minimum version of the Flash player required to view this course.
// Obs: The minor version number occupies the third list element position, as in "6,0,65,0" for v6.65.
var str_playerVersion = '6,0,0,0';

// Maintains default values for the attributes of an embedded Flash object.
// Obs: Fixed attributes -- "classid," "codebase," "pluginspage," and "type" -- are assigned within #getFlashCode.
//      The omitted "name" and "src" attributes adopt values from the "id" and "movie" attributes.
var obj_defaultFlashProperties = {
  align:'', // left|top|right|bottom|middle|l|t|r|b
  allowscriptaccess:'', // always|never|samedomain
  base:'.', // relative or absolute path
  bgcolor:'#FFFFFF', // formatted hexadecimal number
  flashvars:'', // url-encoded query string
  height:'416', // whole number or percentage (Should match the height of top.contentFrame for most situations.)
  id:'FlashMovie', // legal object identifier
  loop:'false', // true|false
  menu:'false', // true|false
  movie:'', // relative or absolute path (default value assigned within #getFlashCode)
  play:'true', // true|false
  quality:'high', // low|high|best|autolow|autohigh
  salign:'', // bl|l|tl|t|tr|r|br|b
  scale:'default', // default|showall|noborder|exactfit
  swliveconnect:'true', // true|false
  width:'792', // whole number or percentage (Should match the width of top.contentFrame for most situations.)
  wmode:'window' // window|opaque|transparent
};

// Identifies a window or frame that will display primary embedded Flash movies by specifying a reference to it.
var obj_defaultWindow = null; // assigned within #assignDefaultWindow

var FlashCode = self;

// - - - - - - - - - - - - - - - - Code to Embed Flash Movies

// Adds content to a page by writing HTML that creates a Flash object.
function insertFlash(obj_customFlashProperties)
{
  var str_flashCode = self.getFlashCode(obj_customFlashProperties);
  obj_defaultWindow.document.write(str_flashCode);
}

// Adds content to a page by writing HTML that creates both a Flash object and a layer to contain it.
function insertFlashLayer(obj_customFlashProperties, str_flashLayerID, obj_customWindow)
{
  var str_flashCode = self.getFlashCode(obj_customFlashProperties);
  obj_customWindow = self.getFlashWindow(obj_customWindow);
  str_flashLayerID = self.getFlashLayerID(str_flashLayerID);
  obj_customWindow.document.write('<div id="' + str_flashLayerID + '">');
  obj_customWindow.document.write(str_flashCode);
  obj_customWindow.document.write('</div>');
}

// Redefines the content of an existing layer by writing HTML that creates a Flash object.
function updateFlashLayer(obj_customFlashProperties, str_flashLayerID, obj_customWindow)
{
  var obj_flashLayer;
  var str_flashCode = self.getFlashCode(obj_customFlashProperties);
  obj_customWindow = self.getFlashWindow(obj_customWindow);
  str_flashLayerID = self.getFlashLayerID(str_flashLayerID);
  if (obj_flashLayer = obj_customWindow.document.getElementById(str_flashLayerID)) // assignment, not a comparison
  {
    obj_flashLayer.innerHTML = str_flashCode;
  }
}

// - - - - - - - - - - - - - - - - Code to Control Flash Movie Playback

// Wrapper. See #controlFlashMovie.
function playFlashMovie(str_flashMovieID)
{
  self.controlFlashMovie(str_flashMovieID, 'play')
}

// Wrapper. See #controlFlashMovie.
function stopFlashMovie(str_flashMovieID)
{
  self.controlFlashMovie(str_flashMovieID, 'stop')
}

// Wrapper. See #controlFlashMovie.
function rewindFlashMovie(str_flashMovieID)
{
  self.controlFlashMovie(str_flashMovieID, 'rewind')
}

// Controls the basic playback of a Flash movie using built-in Flash methods.
function controlFlashMovie(str_flashMovieID, str_controlType)
{
  var obj_flashMovie;
  str_flashMovieID = self.getFlashMovieID(str_flashMovieID);
  self.assignDefaultWindow();
  if (obj_flashMovie = obj_defaultWindow.document.getElementById(str_flashMovieID)) // assignment, not a comparison
  {
    switch (str_controlType.toLowerCase())
    {
      case 'play' :
        if (obj_flashMovie.isPlaying() == false)
        {
          obj_flashMovie.Play();
        }
        break;
      case 'stop' :
//        obj_flashMovie.Stop();
        obj_flashMovie.StopPlay();
        break;
      case 'rewind' :
        obj_flashMovie.Rewind();
        break;
    }
  }
}

// - - - - - - - - - - - - - - - - Auxiliary Function Code

// Assigns a default window/frame to use for Flash movie display based on the #str_defaultWindowName value.
function assignDefaultWindow()
{
  if (self.obj_defaultWindow instanceof Object == false)
  {
    if (typeof self.str_defaultWindowName != 'string' || self.str_defaultWindowName.length == 0 || self[self.str_defaultWindowName] === undefined)
    {
      self.obj_defaultWindow = self;
    }
    else
    {
      self.obj_defaultWindow = self[self.str_defaultWindowName];
    }
  }
}

// Validates a target Flash window reference.
function getFlashWindow(obj_customWindow)
{
  if(!obj_customWindow)
  {
    obj_customWindow = self.obj_defaultWindow;
  }
  return obj_customWindow;
}

// Validates a Flash movie "id" attribute value, assigning a default value when needed.
function getFlashMovieID(str_flashMovieID)
{
  if (typeof str_flashMovieID != 'string' || str_flashMovieID.length == 0)
  {
    str_flashMovieID = self.obj_defaultFlashProperties.id;
  }
  return str_flashMovieID;
}

// Validates a Flash layer "id" attribute value, assigning a default value when needed.
function getFlashLayerID(str_flashLayerID)
{
  if (typeof str_flashLayerID != 'string' || str_flashLayerID.length == 0)
  {
    str_flashLayerID = self.str_defaultFlashLayerID;
  }
  return str_flashLayerID;
}

function getFlashMovieReference(str_flashMovieID)
{
  var str_flashMovieID = self.getFlashMovieID(str_flashMovieID);
  self.assignDefaultWindow(); // Just in case.
  return obj_defaultWindow.document.getElementById(str_flashMovieID);
}

function getFlashLayerReference(str_flashLayerID)
{
  var str_flashLayerID = self.getFlashLayerID(str_flashLayerID);
  self.assignDefaultWindow(); // Just in case.
  return obj_defaultWindow.document.getElementById(str_flashLayerID);
}

function focusFlashMovie(str_flashMovieID)
{
  self.getFlashMovieReference(str_flashMovieID).focus();
}

function resizeFlashLayerToMatchClient()
{
  var obj_movie = getFlashMovieReference();
  var obj_layer = getFlashLayerReference();
  var num_movieWidth = obj_movie.width;
  var num_movieHeight = obj_movie.height;
  var num_clientWidth = self.document.body.clientWidth;
  var num_clientHeight = self.document.body.clientHeight;
  var num_newMovieWidth = num_clientWidth;
  var num_newMovieHeight = Math.round((num_clientWidth * num_movieHeight) / num_movieWidth);
  obj_movie.width = num_newMovieWidth;
  obj_movie.height = num_newMovieHeight;
  obj_layer.style.width = num_newMovieWidth.toString();
  obj_layer.style.height = num_newMovieHeight.toString();
}

// Retrieves Flash object HTML that incorporates default and customized Flash object attributes.
// Obs: To override specified default Flash movie attributes for a situation requiring customization:
//      Pass in a reference to a formal Object (data type) instance or "object initializer" literal.
//      The specified argument should ONLY carry properties for the individual Flash attributes to be customized.
//      Example => {movie:'course.swf', flashparams:'first=Fulano&last=de+Tal', play:'false'}
function getFlashCode(obj_customFlashProperties)
{
  var str_flashCode = '';
  var obj_properties = self.getMergedProperties(self.obj_defaultFlashProperties, obj_customFlashProperties, true);

  obj_properties.classid = 'clsid:D27CDB6E-AE6D-11cf-96B8-444553540000';
  obj_properties.codebase = 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=' + self.str_playerVersion;
  obj_properties.pluginspage = 'http://www.macromedia.com/go/getflashplayer/';
  obj_properties.type = 'application/x-shockwave-flash';

  self.assignDefaultWindow();

  // Assigns a default Flash movie (when needed) based on the name of the HTML document that will display it.
  if (typeof obj_properties.movie != 'string' || obj_properties.movie.length == 0)
  {
    if (typeof self.str_movieFolder == 'string' && self.str_movieFolder.length > 0)
    {
      if (self.str_movieFolder.charAt(self.str_movieFolder.length - 1) != '/')
      {
        self.str_movieFolder += '/';
      }
    }
    obj_properties.movie = self.str_movieFolder + obj_defaultWindow.document.location.href.match(/(.*[:\\\/])?(\w+)\..*/)[2] + '.swf';
  }

  str_flashCode += '<object align="' + obj_properties.align + '" classid="' + obj_properties.classid + '" codebase="' + obj_properties.codebase + '" height="' + obj_properties.height + '" id="' + obj_properties.id + '" width="' + obj_properties.width + '">\n';
//  str_flashCode += '<param name="allowscriptaccess" value="' + obj_properties.allowscriptaccess + '" />\n';
  str_flashCode += '<param name="base" value="' + obj_properties.base + '" />\n';
  str_flashCode += '<param name="bgcolor" value="' + obj_properties.bgcolor + '" />\n';
  str_flashCode += '<param name="flashvars" value="' + obj_properties.flashvars + '" />\n';
  str_flashCode += '<param name="loop" value="' + obj_properties.loop + '" />\n';
  str_flashCode += '<param name="menu" value="' + obj_properties.menu + '" />\n';
  str_flashCode += '<param name="movie" value="' + obj_properties.movie + '" />\n';
  str_flashCode += '<param name="play" value="' + obj_properties.play + '" />\n';
  str_flashCode += '<param name="quality" value="' + obj_properties.quality + '" />\n';
  str_flashCode += '<param name="salign" value="' + obj_properties.salign + '" />\n';
  str_flashCode += '<param name="scale" value="' + obj_properties.scale + '" />\n';
  str_flashCode += '<param name="wmode" value="' + obj_properties.wmode + '" />\n';
//  str_flashCode += '<embed align="' + obj_properties.align + '" allowscriptaccess="' + obj_properties.allowscriptaccess + '" base="' + obj_properties.base + '" bgcolor="' + obj_properties.bgcolor + '" flashvars="' + obj_properties.flashvars + '" height="' + obj_properties.height + '" loop="' + obj_properties.loop + '" menu="' + obj_properties.menu + '" name="' + obj_properties.id + '" play="' + obj_properties.play + '" pluginspage="' + obj_properties.pluginspage + '" quality="' + obj_properties.quality + '" salign="' + obj_properties.salign + '" scale="' + obj_properties.scale + '" src ="' + obj_properties.movie + '" swliveconnect="' + obj_properties.swliveconnect + '" type="' + obj_properties.type + '" width="' + obj_properties.width + '" wmode="' + obj_properties.wmode + '" />\n';
  str_flashCode += '<embed align="' + obj_properties.align + '" base="' + obj_properties.base + '" bgcolor="' + obj_properties.bgcolor + '" flashvars="' + obj_properties.flashvars + '" height="' + obj_properties.height + '" loop="' + obj_properties.loop + '" menu="' + obj_properties.menu + '" name="' + obj_properties.id + '" play="' + obj_properties.play + '" pluginspage="' + obj_properties.pluginspage + '" quality="' + obj_properties.quality + '" salign="' + obj_properties.salign + '" scale="' + obj_properties.scale + '" src ="' + obj_properties.movie + '" swliveconnect="' + obj_properties.swliveconnect + '" type="' + obj_properties.type + '" width="' + obj_properties.width + '" wmode="' + obj_properties.wmode + '" />\n';
  str_flashCode += '</object>\n';

  return str_flashCode;
}

// Retrieves a "merged" properties object that coalesces values from "default" and "custom" properties objects.
function getMergedProperties(obj_defaultProperties, obj_customProperties)
{
  if (obj_defaultProperties instanceof Object)
  {
    var obj_mergedProperties = new Object();
    for (var str_propertyName in obj_defaultProperties)
    {
      obj_mergedProperties[str_propertyName] = obj_defaultProperties[str_propertyName];
    }
    for (var str_propertyName in obj_customProperties)
    {
      if (obj_mergedProperties[str_propertyName] !== undefined)
      {
        obj_mergedProperties[str_propertyName] = obj_customProperties[str_propertyName];
      }
    }
  }
  return obj_mergedProperties;
}
