//=========================================================================================================
//
// Copyright 2005 Macromedia, Inc. All rights reserved.
//
// Feature: Contribute Flash Video
// Module:  EditFlashVideo.js
// Purpose:    Flash Video editing dialog.
//
//=========================================================================================================

<!--
//--------------- LOCALIZEABLE GLOBALS---------------
var clear_skin_1 = "Enveloppe transparente 1";
var clear_skin_2 = "Enveloppe transparente 2";
var clear_skin_3 = "Enveloppe transparente 3";
var halo_skin_1 = "Enveloppe Halo 1";
var halo_skin_2 = "Enveloppe Halo 2";
var halo_skin_3 = "Enveloppe Halo 3";
var corona_skin_1 = "Enveloppe Corona 1";
var corona_skin_2 = "Enveloppe Corona 2";
var corona_skin_3 = "Enveloppe Corona 3";
var MIN_WIDTH = "Largeur min.: %d";
//--------------- END LOCALIZEABLE   ---------------
// -->   

var helpDoc = MM.HELP_objEditMovie;

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

var videoControlSourceFolder = dwscripts.filePathToLocalURL(dw.getConfigurationPath()
                    + dwscripts.FILE_SEP + "Templates" + dwscripts.FILE_SEP + "Video_Controls");

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

var FLVOBJ = 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()
{
  FLVOBJ = new FlashVideoObject();  //initialize FlashVideo object. Get handles to tags, and read in values
  UIOBJ  = new UiObject();   //initialize UI object. Get handles to form controls.

  // set values for objects in the UI
  UIOBJ.setFileName   (FLVOBJ.fileName);   
  UIOBJ.setWidth      (FLVOBJ.width);      
  UIOBJ.setHeight     (FLVOBJ.height);     
  UIOBJ.setbLoopVideo (FLVOBJ.bLoopVideo);
  UIOBJ.setbAutoPlay  (FLVOBJ.bAutoPlay);
  UIOBJ.setSkinName   (FLVOBJ.skinName);
}


function okClicked()
{
  if (UIOBJ.getWidth() < 0)   //if bad width
  {
    alert(dw.loadString("EditFlashVideo/invalid width"));
  }
  else if (UIOBJ.getHeight() < 0) //if bad height
  {
    alert(dw.loadString("EditFlashVideo/invalid height"));
  }
  else 
  {
    // we need to  setup the value of flash vars atrribute 
    var flashVars = "&amp;MM_ComponentVersion=$COMP_VER$&amp;skinName=$SKIN_NAME$&amp;streamName=$STREAM_NAME$&amp;autoPlay=$AUTO_PLAY$&amp;autoRewind=$AUTO_REWIND$";
    
    // version
    flashVars = flashVars.replace(/\$COMP_VER\$/g, FLVOBJ.compVer);
    
    // skin
    var skin  = UIOBJ.getSkinName();
    if(/\.swf$/i.test(skin))
        skin = skin.replace(/\.swf$/i,"");
    var videoControlsFileSourceFolder = "";
    var lowerCaseSkin = skin.toLowerCase();        
    var isSkinChanged = true;
    if( FLVOBJ.skinName.indexOf(lowerCaseSkin) != -1 )
        isSkinChanged = false;  
         
    if(( FLVOBJ.OriginalSkinFile.indexOf("file:///") == 0 ) || isSkinChanged)
    {
        var videoControlsFileSourceFolder = dwscripts.filePathToLocalURL(dw.getConfigurationPath()
                + dwscripts.FILE_SEP + "Templates" + dwscripts.FILE_SEP + "Video_Controls" + "/");
        skin = videoControlsFileSourceFolder + skin;
    }
    else
        skin = FLVOBJ.OriginalCaseSkinPath;
    
    flashVars = flashVars.replace(/\$SKIN_NAME\$/g,skin);
    
    // autoplay
    var bAutoPlay = UIOBJ.getbAutoPlay();
    var bAutoPlayValue = (bAutoPlay)? "true" : "false";
    flashVars = flashVars.replace(/\$AUTO_PLAY\$/g, bAutoPlayValue);
    
    // auto rewind
    var bLoopVideo = UIOBJ.getbLoopVideo();
    var bLoopVideoValue = (bLoopVideo)? "true" : "false";
    flashVars = flashVars.replace(/\$AUTO_REWIND\$/g, bLoopVideoValue);
    
    // flv filename
    if(/\.flv$/i.test(FLVOBJ.fileName))
        FLVOBJ.fileName = FLVOBJ.fileName.replace(/\.flv$/i,"");
    flashVars = flashVars.replace(/\$STREAM_NAME\$/g, FLVOBJ.fileName);
    
    FLVOBJ.setFlashVars(flashVars);
    FLVOBJ.setWidth      (UIOBJ.getWidth());    //get width from UI, and write it back to the object tag
    FLVOBJ.setHeight     (UIOBJ.getHeight(), UIOBJ.getbLoopVideo());   
       
    window.close();
  }
}


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


//Constructor for FlashVideo object. This object manages the <object> tag on the page.
function FlashVideoObject()
{
  //public properties
  this.fileName    = "";;
  this.width       = 0;
  this.height      = 0;    //this height doesn't include extra pixels for halo skins
  this.bLoopVideo = true;
  this.bAutoPlay   = true;
  this.compVer       = 1;
  this.skinName    = "";
  this.aspectRatio = 1;
  this.OriginalSkinFile = "";
  this.OriginalCaseSkinPath = "";

  //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,
  this.secondObjectObj    = null; //second object tag for non-IE browsers 
  this.paramFileNameObj   = null; //embec src
  this.paramFlashVars     = null; //has flashvars properties

  //initialize
  this.getValues();
  
  // setup the aspect ratio
  this.aspectRatio = this.width / this.height;
  if ( (this.aspectRatio != ""+ this.aspectRatio) || (this.aspectRatio < 0) )
    this.aspectRatio = 1.33;  // use 1.33 ( 320/240 ) as default aspect ratio if we are unable to form it
}

//public methods
FlashVideoObject.prototype.setWidth       = FlashVideoObject_setWidth;
FlashVideoObject.prototype.setHeight      = FlashVideoObject_setHeight;
FlashVideoObject.prototype.setFlashVars   = FlashVideoObject_setFlashVars;

//private methods
FlashVideoObject.prototype.getValues      = FlashVideoObject_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 FlashVideoObject_getValues()
{
  var i, node, tag, paramName;
  if (this.activexObj)
  {
    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 == "embed")
          {
            this.embedObj = node;  //get handle to embed tag
          }
          
          else if (tag = "object")
          {
            this.secondObjectObj = node; //get handle to second object tag
          }

          else if (tag == "param")  //if param tag
          {
            paramName = node.getAttribute("name");  //get the name of the param tag
            if (paramName)
            {
              paramName = paramName.toLowerCase();

              if(paramName == "flashvars")                // get values from flashVars
              {
                this.paramFlashVars = node;
                var flvParams = this.paramFlashVars.value.toLowerCase().split('&');
                var flvOriginalParams = this.paramFlashVars.value.split('&');
                var j = 0;
                var tmpStr;
                for(var c in flvParams)
                {
                  if(( j = flvParams[c].indexOf('=')) > 0)
                  {    
                    if(flvParams[c].substring(0, j) == 'streamname') // fileName
                      this.fileName = flvOriginalParams[c].substring( j+1, flvOriginalParams[c].length) + ".flv"; 
                    
                    else if(flvParams[c].substring(0, j) == 'skinname') //skinName
                    {
                      this.OriginalCaseSkinPath = flvOriginalParams[c].substring( j+1, flvOriginalParams[c].length);
                      this.skinName = flvParams[c].substring( j+1, flvParams[c].length);
                      this.OriginalSkinFile = this.skinName;
                      var lastSlash = this.skinName.lastIndexOf('\\');
                      if (lastSlash == -1)
                      lastSlash = this.skinName.lastIndexOf('/');
                      if(lastSlash != -1)
                        this.skinName = this.skinName.substring(++lastSlash, this.skinName.length) 
                    }
                            
                    else if(flvParams[c].substring(0, j) == 'autoplay')  //autoPlay
                      this.bAutoPlay = (flvParams[c].substring( j+1, flvParams[c].length).toLowerCase() == "true");
                    else if(flvParams[c].substring(0, j) == 'autorewind') //autoRewind
                      this.bLoopVideo = (flvParams[c].substring( j+1, flvParams[c].length).toLowerCase() == "true");
                    else if(flvParams[c].substring(0, j) == 'componentversion') //componentVersion
                      this.compVer = flvParams[c].substring( j+1, flvParams[c].length);
                    else
                      ;
                  }
                }
             }
          } 
        }
      }
    }
  }
   
  //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    
   }
    
  }
}

function FlashVideoObject_setWidth(theWidth)
{
  if (theWidth > 0)  //if positive value, set it
  {
    if(this.activexObj)
    this.activexObj.setAttribute("width",theWidth);
    
    if (this.embedObj)
    this.embedObj.setAttribute("width",theWidth);
    
    if (this.secondObjectObj)
      this.secondObjectObj.setAttribute("width",theWidth);
  }
}

function FlashVideoObject_setHeight(theHeight,bLoopVideo)
{
  if (theHeight > 0)
  {
    if (this.activexObj)
          this.activexObj.setAttribute("height",theHeight);
            
    if (this.embedObj)
      this.embedObj.setAttribute("height",theHeight); 
    
    if (this.secondObjectObj)
      this.secondObjectObj.setAttribute("height",theHeight);       
  }
}


function FlashVideoObject_setFlashVars(str)
{
  if(this.paramFlashVars)
     this.paramFlashVars.value = str;
                
  if (this.embedObj)
     this.embedObj.setAttribute("FlashVars", str);
   
  if (this.secondObjectObj)
  {
    if (this.secondObjectObj.childNodes)
    {
      for (i=0; i<this.secondObjectObj.childNodes.length; i++)  //with each child of <object>
      {
        node = this.secondObjectObj.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 == "flashvars")                // get values from flashVars
              {
                node.value = str;
              }
            }
          }
        }
      }
    }
  }     
}



//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.displayLoopVideo;
  this.autoPlayObj = document.theForm.autoPlay;
  this.constraint  = document.theForm.constraint; 
  
   
  // initialize skin list control
  this.listSkinNames = new ListControl("skinList");
  var fileMask = "*.swf";
  this.skinValues = DWfile.listFolder(videoControlSourceFolder + "/" + fileMask,"files");
  var skinNames = new Array();
  var minWidths = new Array(clear_skin_1,140,clear_skin_2,160,clear_skin_3,260,halo_skin_1,180,halo_skin_2,180,halo_skin_3,280,corona_skin_1,130,corona_skin_2,141,corona_skin_3,258);
   for (var i=0; i < this.skinValues.length; i++)
  {
    var skinName = this.skinValues[i];
    skinName = skinName.replace(/_/g, " ");
    skinName = skinName.replace(/\.swf/, "");
    for (var w=0; w < minWidths.length; w=w+2){
      if (minWidths[w] == skinName){
        var minWidth = MIN_WIDTH.replace(/%d/,minWidths[w+1]);
        skinName += "   (" + minWidth + ")";
        break;
       }
    }
    skinNames.push(skinName);
  }
  this.listSkinNames.setAll(skinNames,this.skinValues); 
}
//public methods
UiObject.prototype.setFileName    = UiObject_setFileName;
UiObject.prototype.setWidth       = UiObject_setWidth;
UiObject.prototype.setHeight      = UiObject_setHeight;
UiObject.prototype.setbLoopVideo = UiObject_setbLoopVideo;
UiObject.prototype.setbAutoPlay   = UiObject_setbAutoPlay  ;
UiObject.prototype.setSkinName      = UiObject_setSkinName;

UiObject.prototype.getWidth       = UiObject_getWidth;
UiObject.prototype.getHeight      = UiObject_getHeight;
UiObject.prototype.getbLoopVideo = UiObject_getbLoopVideo;
UiObject.prototype.getbAutoPlay   = UiObject_getbAutoPlay  ;
UiObject.prototype.getSkinName      = UiObject_getSkinName;

UiObject.prototype.updateUI          = UiObject_updateUI;
UiObject.prototype.widthSet          = UiObject_widthSet;
UiObject.prototype.heightSet      = UiObject_heightSet;


//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_setbLoopVideo(bLoopVideo)
{
  this.controlsObj.checked = bLoopVideo;
}

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

function UiObject_setSkinName(name)
{
  var tmp = name + '.swf';
  for (var i=0; i < this.skinValues.length; i++)
  {
    if( tmp == this.skinValues[i].toLowerCase())
    {
      this.listSkinNames.setIndex(i);
      break;
    }
  }
  updateUI();
}


//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_getbLoopVideo()
{
  return this.controlsObj.checked;
}

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

function UiObject_getSkinName()
{
  return this.listSkinNames.getValue();
}

function UiObject_widthSet()
{
  var width = this.getWidth();
  var height = this.getHeight();
  
  if(width < 1 || height<1)
  	return;
  	
  if( this.widthObj && this.heightObj && document.theForm.constraint.checked )
        this.heightObj.value = Math.round(width / FLVOBJ.aspectRatio);        
  
}

function UiObject_heightSet()
{
  var height = this.getHeight();
  var width = this.getWidth();
  
  if( height < 1 || width<1)
  	return;
  if( this.widthObj && this.heightObj && document.theForm.constraint.checked)
      this.widthObj.value = Math.round(height * FLVOBJ.aspectRatio);
  
}

function UiObject_updateUI(element)
{
  var previewImgSrc = "../Templates/images/" + this.listSkinNames.getValue();
  previewImgSrc = previewImgSrc.replace(/\.swf/, ".png");
  var previewImgObj = dwscripts.findDOMObject("skinPreview");      
  previewImgObj.src = previewImgSrc;
}

function updateUI()
{
  UIOBJ.updateUI();
}

function widthSet()
{
  UIOBJ.widthSet();
}

function heightSet()
{
  UIOBJ.heightSet();
}
