// Copyright 2003-2006 Adobe Macromedia Software LLC and its licensors. All rights reserved.

//---------------   GLOBAL VARIABLES   ---------------
var  helpDoc = MM.HELP_objVideo;
var please_select = dw.loadString('Objects/HTML5/video2/please_select_source');

//---------------     API FUNCTIONS    ---------------
function isDOMRequired()
{
	return true;
}

function commandButtons()
{
   return new Array( "PutButtonsOnBottom", MM.BTN_OK, "insertVideo()", MM.BTN_Cancel, "window.close()",
                     "PutButtonOnLeft",	MM.BTN_Help,   "displayHelp()");
}

//---------------    LOCAL FUNCTIONS   ---------------
function canAcceptCommand()
{
	return true;
}

function insertVideo()
{
	var video1=document.form1.video1.value;
    var video2=document.form1.video2.value;
    var video3=document.form1.video3.value;
	
   if( (video1 && video1 != "") || (video2 && video2 != "") || (video3 && video3 != "") )
   {
	   var strOut = initializeUI();
	   if(isValidWidth() && isValidHeight())
	   {
		  insertAtSelection(strOut);
	   	  window.close();
	   }
   }
   else
   {
		alert(please_select);
		document.form1.video1.focus();
		document.form1.video1.select();
   }
}

/*

 function getVideoFormat(videoFilePath)

 Returns the value to be used for type tag of <video> given a file path

*/
function getVideoFormat(videoFilePath)
{
    var extensionDotIndex = videoFilePath.lastIndexOf(".");
    if( extensionDotIndex == -1 )
    {
        return "";
    }

    var videoFilePathLength = videoFilePath.length;
    var videoFileExtension = videoFilePath.substr(extensionDotIndex,videoFilePathLength - extensionDotIndex);

	if(videoFileExtension.indexOf(".mp4") != -1 
        || videoFileExtension.indexOf(".m4v") != -1 )
	{
		return "video/mp4";
	}else if(videoFileExtension.indexOf(".ogg") != -1
             || videoFileExtension.indexOf(".ogv") != -1 )
	{
		return "video/ogg";
	}else if(videoFileExtension.indexOf(".3gp") != -1
             || videoFileExtension.indexOf(".3g2") != -1)
	{
		return "video/3gp";
	}else if(videoFileExtension.indexOf(".webm") != -1 )
    {
        return "video/webm";
    }
	return "";
}


function initializeUI() 
{    
   
	var video1=document.form1.video1.value;
	var video2=document.form1.video2.value;
	var video3=document.form1.video3.value;   
    var swf=document.form1.swf.value;
    var opttext=document.form1.opttext.value;

    var poster =document.form1.posterimg.value;
    var controls = document.form1.ctrlFld.checked
    var autoplay = document.form1.autoplayFld.checked;

	var height = document.form1.height.value;
    var width = document.form1.width.value;

    
        strOut = '<video' + " ";
        if( poster && poster != "" )
           strOut = strOut + ' poster="' + poster + '" '; 
        if( width != "" )
			strOut = strOut + ' width="' + width + '" ';
		if( height != "" )
            strOut = strOut + ' height="' + height + '" ';
        if( controls )
            strOut = strOut + ' controls="controls" ';
		if( autoplay )
            strOut = strOut + ' autoplay="autoplay" ';
        strOut = strOut + '>'; //End video tag
        if( video1 != "" )
        {
            strOut = strOut + '<source src="' + video1 +'" type="' + getVideoFormat(video1) + '" />';
        }
        if( video2 != "" )
        {
            strOut = strOut + '<source src="' + video2 +'" type="' + getVideoFormat(video2) + '" />';
        }
        if( video3 != "" )
        {
            strOut = strOut + '<source src="' + video3 +'" type="' + getVideoFormat(video3) + '" />';
        }
        if( swf != "" )
        {
            strOut = strOut + '<embed src="' + swf + '" type="application/x-shockwave-flash" />';
        }
        if( opttext != "" )
        {
            strOut = strOut + '<p>'+opttext+'</p>';
        }
        strOut = strOut + '</video>';
        return (strOut);

	
}
function insertAtSelection(strOut)
{
	var theObj = dw.getDocumentDOM();
	if( theObj ){
		var offs = theObj.source.getSelection();
		if( isValidiOffSets(offs))
		{
			var docElement = theObj.documentElement;
			docElement.outerHTML = docElement.outerHTML.substring(0, offs[0]) +  strOut + docElement.outerHTML.substr(offs[1]);	
		}
	}
        
}
function isValidiOffSets(offs)
{
	if(!offs || offs[0]<0 || offs[0] > offs[1])
		return false;
	return true;
}
function isValidWidth()
{
	var width = document.form1.width.value;	
	if(!isValidValue(width))
	{
		var errStr = dw.loadString("Inspectors/video/invalidValue");
		alert("'" + width + "' " + errStr);
		document.form1.width.focus() ;
		document.form1.width.select();
		return false;
	}
	return true;
}
function isValidHeight()
{
	var height = document.form1.height.value;	
	if(!isValidValue(height))
	{
		var errStr = dw.loadString("Inspectors/video/invalidValue");
		alert("'" + height + "' " + errStr);
		document.form1.height.focus() ;
		document.form1.height.select();
		return false;
	}
	return true;
}

function isValidValue(sText)
{
   var ValidChars = "0123456789";
   var IsNumber=true;
   var Char;
   var length = sText.length;

   for (i = 0; i < length && IsNumber == true; i++) 
   { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
   }
   return IsNumber;
}


function browseForVideo(srcField)
{
	var filePath = null;
	if( srcField ){
		switch( srcField ){
			case "one":
				filePath = document.form1.video1.value;
				break;
			case "two":
				filePath =  document.form1.video2.value;
				break;
			case "three":
				filePath = document.form1.video3.value;
				break;			
			default:
				break;
		 }
	 }
	var supportedMediaTypes = new Array("*.ogg; *.mp4; *.m4v; *.webm; *.ogv;  *.3gp");
	
	var selVideoStr = dw.loadString("Inspectors/video/selectVideo");
	
	var  fileName= dw.browseForFileURL("select", selVideoStr, false, true, supportedMediaTypes, "",true, filePath);	
	return dw.doURLEncoding(fileName);
}

function browseForImage()
{
	var supportedMediaTypes = new Array("*.gif; *.jpg; *.jpeg; *.png; *.psd");
	var filePath = document.form1.posterimg.value;
	var selVideoStr = dw.loadString("Inspectors/video/selectImage");
	var  fileName= dw.browseForFileURL("select", selVideoStr, true, false, supportedMediaTypes, "",true, filePath);
	 
	return dw.doURLEncoding(fileName);
}

function browseForSwf()
{
	var supportedMediaTypes = new Array("*.swf");
	var filePath = document.form1.swf.value; 
	var selVideoStr = dw.loadString("Inspectors/video/selectVideo");
	var  fileName= dw.browseForFileURL("select", selVideoStr, false, false,"" ,"" ,true, filePath);
	 
	return dw.doURLEncoding(fileName);
}