// Copyright 2004 Macromedia, Inc. All rights reserved.
//**************  GLOBAL CONSTANTS  *************************

//Supported extensions. NOTE: changes here won't affect the remote file chooser,
//so please change Source/Titan/contributor_dreamweaver/RemoteFileBrowser.cpp to match.
var EXTENSIONS_QUICKTIME = "*.mov;*.qt;";
var EXTENSIONS_WINDOWSMP_WEBSITE = "*.avi;*.mpg;*.mpeg;*.mp4;*.flv;*.wmv;";
var EXTENSIONS_WINDOWSMP_BLOGGING = "*.avi;*.mpg;*.mpeg;*.mp4;*.wmv;*.flv";

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

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

function isDOMRequired() { 
  // return true.  This will insert the object into the design view.
  return true;
}

function isAsset() {
  return true;
}

function objectTag(assetArgs)
{ 
  var rtnStr='', theMovie='';

  var bDialogState = dw.getShowDialogsOnInsert();
  // Return the html tag that should be inserted
  
  var isBlogSite = 0;
  var EXTENSIONS = EXTENSIONS_WINDOWSMP_WEBSITE;
  var dom = dw.getDocumentDOM();
  if(dom)
  {
    if(CCWorkspaceManager.getManager(dom))
    { // todo - make a single extension list and use it
      if(CCWorkspaceManager.getManager(dom).isBlogSite())
      {
        EXTENSIONS = EXTENSIONS_WINDOWSMP_BLOGGING;
        isBlogSite = 1;
      }
    }
  }

  if (arguments.length == 0)
  {
    if (MM.InsertType == "file")
    {
     //prompt user for file, only allow extensions .mov, .qt, .avi, .mpg, .mpeg, .wmv
      var niceExtList = (EXTENSIONS_QUICKTIME + EXTENSIONS).replace(/;/g,",");  //nice list should be comma separated
      var filter = new Array(dw.loadString("insert doc dialog/movie doc desc")+ " (" + niceExtList + ")|" + EXTENSIONS_QUICKTIME+EXTENSIONS + "||");

      if (MM.InsertType == "file")
      {
        theMovie = dw.browseForFileURL("open", dw.loadString("insert doc dialog/movie title"), false, true, filter, "", "", "desktop");
            
	    if (dw.appName == "Contribute" && dw.isGreaterThanMaxDependentFileSize(theMovie))
		  return '';
      }
    }
    else if (MM.InsertType == "website")
    {
      theMovie = dw.browseForVideoOnWebsite ();  //launch remote file browser, look for those types
    }
    
    else if (MM.InsertType == "internet")
    {
      theMovie = dw.BrowseToInternetVideo ();  //launch internet file browser, look for those types
      if(theMovie.length == 0)
	     return; // No selection made
	  var retval = dw.validateMediaFile(theMovie);
	  
	  if(!retval)
		return;    
    }    
  }
  
  var sourceURL = theMovie;
  	
	//block double byte ot high ascii filenames
	if (dw.appName == "Contribute") 
	{
		var fileName = dwscripts.getFileName(theMovie);

		if (hasDoubleByteChar(fileName))
		{
			alert(dw.loadString("file naming/doublebyte filename error"));
			return '';
		}
		else if (hasHighAsciiChar(fileName))
		{
			alert(dw.loadString("file naming/highascii filename error"));
			return '';
		}
	}
	
	if (theMovie != '')
	{
		theMovie = dw.doURLEncoding(theMovie);
	}
	
	if (assetArgs)
	{
		theMovie = assetArgs;
	}
		
  //if we have a file name with an extension...
  if (theMovie.length > 4 && theMovie.indexOf(".") != -1)
	{
    var defaultWidth = 320;
 	var defaultHeight= 240;
    

    var fileExt = theMovie.substring(theMovie.lastIndexOf(".")).toLowerCase();  //extract the file extension
    
    if(fileExt == ".flv")
    {          
        var url = sourceURL;
        var dom = dw.getDocumentDOM();
        if(dom)
        {
            // if its not a direct edit site, get only filename
            // we do this because for non direct edit sites, the path will
            // change on publish and only the filename will be the same
            if(!dom.isDirectEditSite())
            {
                url = dwscripts.getFileName(url);
            }        
         }
         
       	// for .flv files we do not allow spaces or special characters
		// as Flash Player will not play the .flv file if they have them
		if((url != '') && (!assetArgs))
		{
			if(/^(\.flv)/i.test(url) || !(/(\.flv)$/i.test(url)) || 
				(url.indexOf("*") != -1) || (url.indexOf("?") != -1) || 
				(url.indexOf("<") != -1) || (url.indexOf(">") != -1) || 
				((index = url.lastIndexOf("|")) != -1 && url.indexOf("file:///") == -1 && index > 9) || 
				(url.indexOf("\"") != -1) || (url.indexOf(" ") != -1))
			{
			    if(url == sourceURL)
				    alert(dw.loadString("file naming/invalid path chars"));        
				else
				    alert(dw.loadString("file naming/invalid filename chars"));    
				     
				return '';
			}      
		}
		
		// we do the rest of the processing for flash video in a seperate file
		// as we need to have a hidden JS-Dialog up when we detect the flash video
		// dimensions
	    var cmdFile = dreamweaver.getConfigurationPath() + "/Commands/FlashVideo.htm";
	    var cmdDOM = dreamweaver.getDocumentDOM(cmdFile);	    
	    cmdDOM.parentWindow.setVideoUrl(theMovie);	    	    
	    dreamweaver.popupCommand("FlashVideo.htm");	    
	    rtnStr = cmdDOM.parentWindow.GetGenHtml();	    	    
    }
    else if (EXTENSIONS_QUICKTIME.indexOf(fileExt) != -1)  //for Quicktime, use the QT CLASSID and parameters
    {
      rtnStr = '' +
        '<object classid="clsid:02bf25d5-8c17-4b23-bc80-d3488abddc6b"' +
        ' codebase="http://www.apple.com/qtactivex/qtplugin.cab"' +
        ' width="' + defaultWidth + '"' +
        ' height="' + (defaultHeight+CONTROL_SIZE_QT) + '"' +  //add controller height to height
        ' border="0"' +
        ' align="baseline">\n' +
        '  <param name="src"         value="' + theMovie + '">\n' +
        '  <param name="autoplay"    value="true">\n' +
        '  <param name="controller"  value="true">\n' +
        '  <param name="scale"       value="aspect">\n' +
        '  <param name="bgcolor"     value="white">\n' +
        '  <param name="pluginspage" value="http://www.apple.com/quicktime/download/">\n' +
        '  <embed' +
        ' width="' + defaultWidth + '"' +
        ' height="' + (defaultHeight+CONTROL_SIZE_QT) + '"' +
        ' border="0"' +
        ' align="baseline"' +
        ' src="' + theMovie + '"' +
        ' autoPlay="true"' +
        ' controller ="true"' +
        ' scale="aspect"' +
        ' bgcolor="white"' +
        ' pluginspage="http://www.apple.com/quicktime/download/">\n' +
        '  </embed>\n' +
        '</object>';

    } 
    else if (EXTENSIONS.indexOf(fileExt) != -1)  //if Windows ext, use Windows Media Player CLASSID and parameters
    {
      rtnStr = '' +
        '<object classid="clsid:22d6f312-b0f6-11d0-94ab-0080c74c7e95"' +
        ' codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab"' +
        ' width="' + defaultWidth + '"' +
        ' height="' + (defaultHeight+CONTROL_SIZE_WMP) + '"' +  //add controller height to height
        ' border="0"' +
        ' align="baseline">\n' +
        '  <param name="FileName"             value="' + theMovie + '">\n' +
        '  <param name="AutoStart"            value="true">\n' +
        '  <param name="ShowControls"         value="true">\n' +
        '  <param name="ShowPositionControls" value="false">\n' +
        '  <param name="ShowAudioControls"    value="true">\n' +
        '  <param name="ShowTracker"          value="true">  \n' +
        '  <param name="ShowStatusBar"        value="false">\n' +
        '  <param name="AutoSize"             value="true">\n' +
        '  <param name="AutoRewind"           value="false">\n' +
        '  <param name="AllowScan"            value="true">\n' +
        '  <param name="EnableContextMenu"    value="true">\n' +
        '  <param name="bgcolor"              value="white">\n' +
        '  <embed' +
        ' width="' + defaultWidth + '"' +
        ' height="' + (defaultHeight+CONTROL_SIZE_WMP) + '"' +
        ' border="0"' +
        ' align="baseline"' +
        ' src="' + theMovie + '"' +
        ' autostart="true"' +
        ' showcontrols="true"' +
        ' controller="true"' +  //this is here for Mac compatibility
        ' showpositioncontrols="false"' +
        ' showaudiocontrols="true"' +
        ' showtracker="true"' +
        ' showstatusbar="false"' +
        ' autosize="true"' +
        ' scale="aspect"' +  //this is here for Mac compatibility
        ' autorewind="false"' +
        ' allowscan="true"' +
        ' enablecontextmenu="true"' +
        ' pluginspage="http://www.microsoft.com/Windows/MediaPlayer/download/">\n' +
        '  </embed>\n' +
        '</object>';
    }
  
	  if (rtnStr && dw.getPreferenceString("Accessibility", "Accessibility Media Options", "") == 'TRUE')
	  {
		  rtnStr = addAccessibility(rtnStr);
	  }
  }
    
  return rtnStr;
}


function addAccessibility(rtnStr) {
   var cmdFile = dreamweaver.getConfigurationPath() + "/Commands/Object Options.htm";
   var cmdDOM = dreamweaver.getDocumentDOM(cmdFile);
 
   cmdDOM.parentWindow.setFormItem(rtnStr);
   dreamweaver.popupCommand("Object Options.htm");
   return (cmdDOM.parentWindow.returnAccessibilityStr(rtnStr));	
}