var def_makeMovie = "V2.0";			// Make sure this is here, richtext.js will check for it

var stdWidth      = -1;				// Change here or overwrite in page (-1 keeps original size)
var movieImage    = "/art/blank.gif";		// Image to use in editor as movie placeholder - now blank, see textpages.css
var movieAssetDir = "/assets/movies";		// Directory to find movies in
var moviePlayer   = "/art/player.swf"		// Player swf file
var swfAssetDir   = "/assets/swf";

function makeMovie( type, movieId, width, height )
{
  var newHeight = ( ( stdWidth == -1 || width < stdWidth ) ? height : Math.round( height / width * stdWidth ) );
  var newWidth  = ( ( stdWidth == -1 || width < stdWidth ) ? width  : stdWidth );

  var movieAlign = "";

  if( movieId.indexOf( "|" ) != -1 )
  {
    movieAlign = movieId.substring( movieId.indexOf( "|" ) + 1 );
    movieId    = movieId.substring( 0, movieId.indexOf( "|" ) );
  }

  if( type == "dlm" )
  {
    var moviePath = ( ( movieId.charAt( 0 ) >= 0 || movieId.charAt( 0 ) <= 9 ) ? "0" : movieId.charAt( 0 ) ) + "/" + movieId + ".flv";

    document.write( '<div id="flashPlay_' + movieId + '"' + ( ( movieAlign != "" ) ? ' class="float' + movieAlign + '"' : '' ) + '></div>\n' +
      '<script type="text/javascript">\n' +
      '  var so_' + movieId + ' = new SWFObject("' + moviePlayer + '","mediaplayer_' + movieId + '","' + newWidth + '","' + newHeight + '","9","#f0f0f0");\n' +
      '  so_' + movieId + '.addParam("allowfullscreen","true");\n' +
      '  so_' + movieId + '.addVariable("file","' + movieAssetDir + '/' + moviePath + '");\n' +
//      '  so_' + movieId + '.addVariable("backcolor","0x000000");\n' +
//      '  so_' + movieId + '.addVariable("frontcolor","0xc0c0c0");\n' +
//      '  so_' + movieId + '.addVariable("lightcolor","0xf0f0f0");\n' +
      '  so_' + movieId + '.addVariable("controlbar","over");\n' +
      '  so_' + movieId + '.addVariable("autostart","false");\n' +
      '  so_' + movieId + '.write("flashPlay_' + movieId + '");\n' +
      '</script>\n' );
  }
  else if( type == "swf" )
  {
    var moviePath = ( ( movieId.charAt( 0 ) >= 0 || movieId.charAt( 0 ) <= 9 ) ? "0" : movieId.charAt( 0 ) ) + "/" + movieId + ".swf";
    document.write( '<div id="flashPlay_' + movieId + '"' + ( ( movieAlign != "" ) ? ' class="float' + movieAlign + '"' : '' ) + '></div>\n' +
      '<script type="text/javascript">\n' +
      '  var so_' + movieId + ' = new SWFObject("' + swfAssetDir + '/' + moviePath + '","mediaplayer_' + movieId + '","' + newWidth + '","' + newHeight + '","9","#f0f0f0");\n' +
      '  so_' + movieId + '.addVariable("autostart","false");\n' +
      '  so_' + movieId + '.write("flashPlay_' + movieId + '");\n' +
      '</script>\n' );
  }
  else if( type == "youtube" )
  {
    document.write( '<div id="youTube_' + movieId + '"' + ( ( movieAlign != "" ) ? ' class="float' + movieAlign + '"' : '' ) + '><object width="' + newWidth + '" height="' + newHeight + '"><param name="movie" value="http://www.youtube.com/v/' + movieId + '"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/' + movieId + '" type="application/x-shockwave-flash" wmode="transparent" width="' + newWidth + '" height="' + newHeight + '"></embed></object></div>' );
  }
  else if( type == "gametrailers" )
  {
    document.write( '<div id="gameTrailers_' + movieId + '"' + ( ( movieAlign != "" ) ? ' class="float' + movieAlign + '"' : '' ) + '><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" id="gtembed" width="' + newWidth + '" height="' + newHeight + '"><param name="allowScriptAccess" value="sameDomain" /><param name="movie" value="http://www.gametrailers.com/remote_wrap.php?mid=' + movieId + '"/> <param name="quality" value="high" /> <embed src="http://www.gametrailers.com/remote_wrap.php?mid=' + movieId + '" swLiveConnect="true" name="gtembed" align="middle" allowScriptAccess="sameDomain" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="' + newWidth + '" height="' + newHeight + '"></embed> </object></div>' );
  }
}

function getEndIndexOf( text, match )
{
  return text.indexOf( match ) + match.length;
}

function getBetween( text, start, end )
{
  var p1 = getEndIndexOf( text, start );
  return text.substring( p1, text.indexOf( end, p1 ) );
}

function dlmMovie( movieName, assetId, width, height, align )
{
  align = ( ( typeof( align ) == "undefined" ) ? "" : align );

  var movieId = movieName + "_" + assetId + ( ( align != '' ) ? "|" + align : '' );

  return '<img src="' + movieImage + '" class="moviePlaceholder' + ( ( align != '' ) ? ' float' + align : '' ) + '" alt="dlm:' + movieId + ':' + width + ':' + height + '" width="' + width + '" height="' + height + '"/>';
//  return '<img src="' + movieImage + '" alt="dlm:' + movieId + ':' + width + ':' + height + '"/>';
}

function parseMovie( rawHtml, align )
{
  align = ( ( typeof( align ) == "undefined" ) ? "" : align );

  var type;
  var movieId;
  var width;
  var height;

  if( rawHtml.toLowerCase().indexOf( "www.youtube.com" ) != -1 )
  {
    type = "youtube";
    movieId = getBetween( rawHtml, "www.youtube.com/v/" , '"' );
  }
  else if( rawHtml.toLowerCase().indexOf( "www.gametrailers.com" ) != -1 )
  {
    type = "gametrailers";
    movieId = getBetween( rawHtml, "www.gametrailers.com/remote_wrap.php?mid=", '"' );
  }
  else
  {
    alert( 'That movie HTML is currently unsupported.\nPlease contact support via the "email support" link to have it enabled.' );
    return "";
  }

  width  = getBetween( rawHtml, 'width="',  '"' );
  height = getBetween( rawHtml, 'height="', '"' );

  return '<img src="' + movieImage + '" class="moviePlaceholder' + ( ( align != '' ) ? ' float' + align : '' ) + '" alt="' + type + ':' + movieId + ( ( align != '' ) ? '|' + align : '' ) + ':' + width + ':' + height + '" width="' + width + '" height="' + height + '"/>';
//  return '<img src="' + movieImage + '" alt="' + type + ':' + movieId + ':' + width + ':' + height + '"/>';
}
