/*************************************************************************
*
* ADOBE CONFIDENTIAL
* ___________________
*
*  Copyright 2012 Adobe Systems Incorporated
*  All Rights Reserved.
*
* NOTICE:  All information contained herein is, and remains
* the property of Adobe Systems Incorporated and its suppliers,
* if any.  The intellectual and technical concepts contained
* herein are proprietary to Adobe Systems Incorporated and its
* suppliers and are protected by trade secret or copyright law.
* Dissemination of this information or reproduction of this material
* is strictly forbidden unless prior written permission is obtained
* from Adobe Systems Incorporated.

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

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

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

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

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

function insertAudio()
{
	var audio1=document.form1.audio1.value;
    var audio2=document.form1.audio2.value;
    var audio3=document.form1.audio3.value;
	
   if( (audio1 && audio1 != "") || (audio2 && audio2 != "") || (audio3 && audio3 != "") )
   {
	   insertAtSelection(initializeUI());
	   window.close();	   
   }
   else
   {
		alert(please_select);
		document.form1.audio1.focus();
		document.form1.audio1.select();
   }
}

/*
 function getAudioFormat(audioFilePath)
 Returns the value to be used for type tag of <audio> given a file path
*/

function getAudioFormat(audioFilePath)
{
    var extensionDotIndex = audioFilePath.lastIndexOf(".");
    if( extensionDotIndex == -1 )
    {
        return "";
    }

    var audioFilePathLength = audioFilePath.length;
    var audioFileExtension = audioFilePath.substr(extensionDotIndex,audioFilePathLength - extensionDotIndex);

	if(audioFileExtension.indexOf(".mp3") != -1 )
	{
		return "audio/mpeg";
	}else if(audioFileExtension.indexOf(".ogg") != -1)
	{
		return "audio/ogg";
	}
	else if(audioFileExtension.indexOf(".wav") != -1 )
    {
        return "audio/wav";
    }	
	return "";
}

/*
  Used to compose the <audio> tag based on the input parameters provided by the user
*/
function initializeUI() 
{    
    var audio1=document.form1.audio1.value;
    var audio2=document.form1.audio2.value;
    var audio3=document.form1.audio3.value;
    var opttext=document.form1.opttext.value;	
    
    var controls = document.form1.ctrlFld.checked
    var autoplay = document.form1.autoplayFld.checked;
	var loop = document.form1.loopAudioFld.checked;
	var muted = document.form1.mutedFld.checked;
    var preloadSelector = document.form1.preload; 

	if (audio1 && audio1 != "" || audio2 && audio2 !="" || audio3 && audio3 !="" )
    {

        strOut = '<audio'+ " ";
        if( controls )
            strOut = strOut +  ' controls="controls" ';
        if( autoplay )
            strOut = strOut + ' autoplay="autoplay" ';
		if( loop )
			strOut = strOut + ' loop ="loop" ';
		if( muted )
			strOut = strOut + ' muted ="muted" ';
				
		if( preloadSelector )
		{
			var selIndex = preloadSelector.selectedIndex;
			if(selIndex >0 )
			{
				strOut = strOut + " preload=" +  preloadSelector.options[selIndex].text + " ";
			}
		}
					
        strOut = strOut + '>'; //End audio tag

        if( audio1 != "" )
        {
            strOut = strOut + '<source src="' + audio1 +'" type="' + getAudioFormat(audio1) + '"/>';
        }
        if( audio2 != "" )
        {
            strOut = strOut + '<source src="' + audio2 +'" type="' + getAudioFormat(audio2) + '"/>';
        }
		if( audio3 != "" )
        {
            strOut = strOut + '<source src="' + audio3 +'" type="' + getAudioFormat(audio3) + '"/>';
        }
		
        if( opttext != "" )
        {
            strOut = strOut + '<p>'+opttext+'</p>';
        }
        strOut = strOut + '</audio>';		
        return (strOut);
    }

    //Return blank if no audio specified
    return('');

}
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 browseForAudio(srcField)
{
	var filePath = null;
	if( srcField ){
		switch( srcField ){
			case "one":
				filePath = document.form1.audio1.value;
				break;
			case "two":
				filePath =  document.form1.audio2.value;
				break;
			case "three":
				filePath = document.form1.audio3.value;
				break;			
			default:
				break;
		 }
	 }
	var supportedMediaTypes = new Array("*.ogg; *.wav; *.mp3");
	
	var selAudioStr = dw.loadString("Inspectors/audio/selectAudio");
	
	var  fileName= dw.browseForFileURL("select", selAudioStr, false, false, supportedMediaTypes, "",true, filePath);
	 
	return dw.doURLEncoding(fileName);
}
