//=========================================================================================================
//
// Copyright 2002, 2003 Macromedia, Inc. All rights reserved.
//
// Feature: Contribute movie tag editing
// Author:  KPS
// Module:  EditMovie.js
// Purpose:	Movie editing dialog.
//
// This editing dialog is invoked for <object> tags that use the classID for either Quicktime
// or Windows Media Player. It gets and sets basic properties common to both tags.
// Note that most users of the <object> tag also include the <embed> tag for Netscape (and other)
// browser compatibility, so this command also writes redundant settings to that nested tag.
// Here are the minimal settings that are edited (Windows Media Player example):
//
// <object classid="CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95"
//   codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab"
//   width   ="320"
//   height  ="256"
//  <param name="FileName"             value="media/wmvMovie.wmv">
//  <param name="AutoStart"            value="true">
//  <param name="ShowControls"         value="true">
//  <embed
//    width               ="320"
//    height              ="256"
//    src                 ="media/wmvMovie.wmv" 
//    autostart           ="true"
//    showcontrols        ="true"
//  </embed>
//
//  The classid attribute, and the FileName/src parameters are read-only. The other values shown here are
//  both read and written. Also note that many are different for Quicktime:
//    FileName     => src (in quicktime object)
//    AutoStart    => autoplay
//    ShowControls => controller
//=========================================================================================================

var helpDoc = MM.HELP_objEditMovie;

//********************** GLOBAL CONSTANTS ****************************

var CLASSID_QUICKTIME = "clsid:02bf25d5-8c17-4b23-bc80-d3488abddc6b";
var CLASSID_WINDOWSMP = "clsid:22d6f312-b0f6-11d0-94ab-0080c74c7e95";

var CONTROL_SIZE_QT  = 16;  //height of play controls for Quicktime (in pixels)
var CONTROL_SIZE_WMP = 40;  //height of play controls for Windows Media Player

var MAX_FILENAME = 40;  //max chars to display of filename string

var MOVOBJ = null;
var UIOBJ = null;

//********************************************************************


function receiveArguments()
{
}

function canAcceptCommand()
{
  return !( dw.getDocumentDOM().getCCSharedSetting_TextOnlyInNonTemplates() );
}

function commandButtons()
{
	return new Array( "PutButtonsOnBottom", "OkButton", MM.BTN_OK, "okClicked()",
					          "PutButtonOnLeft", MM.BTN_Help,    "displayHelp()",
					          "CancelButton", MM.BTN_Cancel,  "cancelClicked()"
					          );
}



function initializeUI()
{
  MOVOBJ = new MovieObject();  //initialize movie object. Get handles to tags, and read in values
  UIOBJ  = new UiObject();   //initialize UI object. Get handles to form controls.

  UIOBJ.setFileName   (MOVOBJ.fileName);   //set file name in UI
  UIOBJ.setWidth      (MOVOBJ.width);      //set width, etc.
  UIOBJ.setHeight     (MOVOBJ.height);
  UIOBJ.setbController(MOVOBJ.bController);
  UIOBJ.setbAutoPlay  (MOVOBJ.bAutoPlay);
}


function okClicked()
{
  if (UIOBJ.getWidth() < 0)   //if bad width
  {
    alert(dw.loadString("EditMovie/invalid width"));
  }
  else if (UIOBJ.getHeight() < 0) //if bad height
  {
    alert(dw.loadString("EditMovie/invalid height"));
  }
  else 
  {
    MOVOBJ.setWidth      (UIOBJ.getWidth());    //get width from UI, and write it back to the object tag
    MOVOBJ.setHeight     (UIOBJ.getHeight(), UIOBJ.getbController());
    MOVOBJ.setbController(UIOBJ.getbController());
    MOVOBJ.setbAutoPlay  (UIOBJ.getbAutoPlay());
    window.close();
  }
}


function cancelClicked()
{
  window.close();
}





//Constructor for movie object. This object manages the <object> tag on the page.
function MovieObject()
{
  //public properties
  this.type        = "";  //either "qt" or "wmp"
  this.fileName    = "";;
  this.width       = 0;
  this.height      = 0;    //this height doesn't include extra pixels for controller
  this.bController = true;
  this.bAutoPlay   = true;

  //private properties: these are handles to different tags on the page
  this.activexObj         = dw.getDocumentDOM().getSelectedNode(); //parent object tag attribs: width, height
  this.embedObj           = null; //embed tag attribs: width, height, src, autoPlay, controller 
  this.paramFileNameObj   = null; //param src (qt) or FileName (wmp)
  this.paramAutoObj       = null; //param autoplay (qt) or AutoStart (wmp)
  this.paramControllerObj = null; //param controller (qt) or ShowControls (wmp)

  //initialize
  this.getType();
  this.getValues();
}
//public methods
MovieObject.prototype.setWidth       = MovieObject_setWidth;
MovieObject.prototype.setHeight      = MovieObject_setHeight;
MovieObject.prototype.setbController = MovieObject_setbController;
MovieObject.prototype.setbAutoPlay   = MovieObject_setbAutoPlay;
//private methods
MovieObject.prototype.getType        = MovieObject_getType;
MovieObject.prototype.getValues      = MovieObject_getValues;


//Method to initialize the UI object.
//Reads values from web page <object> tag,
//and gets handles to param and embed tags for future rewriting.
function MovieObject_getValues()
{
  var i, node, tag, paramName;

  if (this.type)  //if it's one of the movie types we care about (quicktime, windows media), proceed
  {
    if (this.activexObj.childNodes)
    {
      for (i=0; i<this.activexObj.childNodes.length; i++)  //with each child of <object>
      {
        node = this.activexObj.childNodes[i];
        if (node.tagName)  //if child is a tag
        {
          tag = node.tagName.toLowerCase(); //force lower
          if (tag == "param")  //if param tag
          {
            paramName = node.getAttribute("name");  //get the name of the param tag
            if (paramName)
            {
              paramName = paramName.toLowerCase();
              if (paramName == "src" || paramName == "filename") //if src or filename param
              {
                this.paramFileNameObj = node;                 //get handle to src/FileName param tag
                this.fileName = this.paramFileNameObj.value;  //get src value
              }
              else if (paramName == "controller" || paramName == "showcontrols")
              {
                this.paramControllerObj = node;
                this.bController = (this.paramControllerObj.value.toLowerCase() == "true");  //set as boolean
              }
              else if (paramName == "autoplay" || paramName == "autostart")
              {
                this.paramAutoObj = node;
                this.bAutoPlay = (this.paramAutoObj.value.toLowerCase() == "true");
              }
            }
          }
          else if (tag == "embed")
          {
            this.embedObj = node;  //get handle to embed tag
          }
        }
      }
    }
    //get width from object tag. If not a number (or doesn't exist), set to zero
    this.width  = parseInt(this.activexObj.width); //read width off the object tag
    if (this.activexObj.width != ""+this.width)     //if width is not a number
    {
      this.width = -1;  //set to -1 as a flag to other methods
    }
    
    //get height from object tag. If not a number (or doesn't exist), set to zero. 
    this.height = parseInt(this.activexObj.height);
    if (this.activexObj.height != ""+this.height)  //if height is not a number
    {
      this.height = -1;  //set to -1 as a flag to other methods
    }
    if (this.bController && this.height > 0)  //if controller displayed, subtract control height (if height)
    {
      this.height -= ((this.type == "qt")? CONTROL_SIZE_QT : CONTROL_SIZE_WMP);
    }
  }
}

//Determine what type of object tag it is (from classId): either Quicktime or Windows Media Player.
function MovieObject_getType()
{
  if (this.activexObj)
  {
    var classId = this.activexObj.classId;  //get the CLASSID from the selected object tag
    if (classId)
    {
      classId = classId.toLowerCase();
      if (classId == CLASSID_QUICKTIME) //if quicktime classID
      {
        this.type = "qt";
      }
      else if (classId == CLASSID_WINDOWSMP)  //if windows media player classID
      {
        this.type = "wmp";
      }
    }
  }
}

function MovieObject_setWidth(theWidth)
{
  if (theWidth > 0)  //if positive value, set it
  {
    this.activexObj.setAttribute("width",theWidth);
    if (this.embedObj)
    {
      this.embedObj.setAttribute("width",theWidth);
    }
  }
  else //if width was < 1 or not a number
  {
    //maybe we should remove it if they leave the value blank?
    //this.activexObj.removeAttribute("WIDTH");
    //this.embedObj.removeAttribute("WIDTH");
  }
}

function MovieObject_setHeight(theHeight,bController)
{
  if (theHeight > 0)
  {
    if (bController)  //if displaying controller, add the height
    {
      theHeight += ((this.type == "qt")? CONTROL_SIZE_QT : CONTROL_SIZE_WMP);
    }
    this.activexObj.setAttribute("height",theHeight);
    if (this.embedObj)
    {
      this.embedObj.setAttribute("height",theHeight);
    }
  }
  else //if height was < 1 or not a number
  {
    //maybe we should remove it if they leave the value blank?
    //this.activexObj.removeAttribute("HEIGHT");
    //this.embedObj.removeAttribute("HEIGHT");
  }
}

//If passed boolean true, sets value of param tag to "true"
function MovieObject_setbController(bController)
{
  var bValue = (bController)? "true" : "false";

  if (this.paramControllerObj)  //if parameter exists
  {
    this.paramControllerObj.setAttribute("value",bValue);  //set the parameter value
  }

  if (this.embedObj)
  {
    this.embedObj.setAttribute("controller",bValue);  //set this for QT *and* WMP
    if (this.type == "wmp") //if Windows Media Player
    {
      this.embedObj.setAttribute("showcontrols",bValue);
    }
  }
}

function MovieObject_setbAutoPlay(bAutoPlay)
{
  var bValue = (bAutoPlay)? "true" : "false";

  if (this.paramAutoObj)  //if parameter exists
  {
    this.paramAutoObj.setAttribute("value",bValue);
  }

  if (this.embedObj)
  {
    if (this.type == "qt") //if Quicktime
    {
      this.embedObj.setAttribute("autoplay",bValue);
    }
    else if (this.type == "wmp") //if Windows Media Player
    {
      this.embedObj.setAttribute("autostart",bValue);
    }
  }
}



//Constructor for UI object. This object manages the dialog UI, correcting or 
//converting values as needed.
function UiObject()
{
  //private properties
  this.fileNameObj = document.theForm.fileName;
  this.widthObj    = document.theForm.theWidth;
  this.heightObj   = document.theForm.theHeight;
  this.controlsObj = document.theForm.displayControls;
  this.autoPlayObj = document.theForm.autoPlay;
}
//public methods
UiObject.prototype.setFileName    = UiObject_setFileName;
UiObject.prototype.setWidth       = UiObject_setWidth;
UiObject.prototype.setHeight      = UiObject_setHeight;
UiObject.prototype.setbController = UiObject_setbController;
UiObject.prototype.setbAutoPlay   = UiObject_setbAutoPlay  ;

UiObject.prototype.getWidth       = UiObject_getWidth;
UiObject.prototype.getHeight      = UiObject_getHeight;
UiObject.prototype.getbController = UiObject_getbController;
UiObject.prototype.getbAutoPlay   = UiObject_getbAutoPlay  ;


//Display the filename. If too long, truncate it smartly.
function UiObject_setFileName(fileName)
{
  if (fileName.length > MAX_FILENAME)
  {
    fileName = fileName.substring(fileName.length - (MAX_FILENAME-3));  //first, get last MAX chars (-3 for ellipses)
    var firstSlash = fileName.indexOf("/");
    if (firstSlash != -1)  //if there's a slash, remove stuff before it
    {
      fileName = fileName.substring(firstSlash);
    }
    fileName = "..." + fileName;  //prepend ellipses
    fileName = fileName.substring(fileName.length - (MAX_FILENAME-3));  //first, get last MAX chars (-3 for ellipses)
  }
  this.fileNameObj.innerHTML = fileName;
}

function UiObject_setWidth(width)
{
  if (width < 1) width = "";
  this.widthObj.value = width;
}

function UiObject_setHeight(height)
{
  if (height < 1) height = "";
  this.heightObj.value = height;
}

function UiObject_setbController(bController)
{
  this.controlsObj.checked = bController;
}

function UiObject_setbAutoPlay(bAutoPlay)
{
  this.autoPlayObj.checked = bAutoPlay;
}


//Get the width from the UI. If non-number, or negative, set to zero.
function UiObject_getWidth()
{
  var width = parseInt(this.widthObj.value);
  if (this.widthObj.value != ""+width)  //if width is not a number
  {
    width = -1;  //set to -1 as flag to ignore value
  }
  return width;
}

//Get the height from the UI. If non-number, or negative, set to zero.
function UiObject_getHeight()
{
  var height = parseInt(this.heightObj.value);
  if (this.heightObj.value != ""+height)  //if height is not a number
  {
    height = -1;  //set to -1 as flag to ignore value
  }
  return height;
}

function UiObject_getbController()
{
  return this.controlsObj.checked;
}

function UiObject_getbAutoPlay()
{
  return this.autoPlayObj.checked;
}
