//=========================================================================================================
//
// Copyright 2002, 2003 Macromedia, Inc. All rights reserved.
//
// Feature: Contributor Flash Editing
// Author:  JDH
// Module:  EditFlashMovie.js
// Purpose:	Flash editing dialog.
// Updates:
//	7/30/02 - Started file control
//
//=========================================================================================================

var helpDoc = MM.HELP_objFlash;

function receiveArguments()
{
}

function canAcceptCommand()
{
	if ( dw.getDocumentDOM().getCCSharedSetting_TextOnlyInNonTemplates() )
		return false;
	return true;
}

function commandButtons()
{
   /*return new Array(MM.BTN_OK,         "run()",
                    MM.BTN_Cancel,     "window.close()",
                    MM.BTN_Help,       "displayHelp()" );

	*/

	return new Array( "PutButtonsOnBottom", "OkButton", MM.BTN_OK, "run()",
					  "PutButtonOnLeft", MM.BTN_Help,    "displayHelp()",
					  "CancelButton", MM.BTN_Cancel,  "window.close()");


}

var fileWidth;
var fileHeight;
var origWidth;
var origHeight;
var aspectRatio;

function initializeUI()
{
	var play = "";
	var loop = "";
	var src = "";
	var wmode = "";

	// Look for the loop and play params attached to the flash
	// movie

	selectedNode = dw.getDocumentDOM().getSelectedNode();
        selectedNode.outerHTML = selectedNode.outerHTML;
   
	if ( selectedNode.childNodes )
	{
		for ( var index = 0; index < selectedNode.childNodes.length; index++ )
		{
			var node = selectedNode.childNodes[ index ];
                        if ( node.tagName == "PARAM" )
			{
				var nodeName = node.getAttribute( "name" );
				nodeName = nodeName.toLowerCase();
				if ( nodeName == "loop" )
					loop = node.getAttribute( "value" );
				if ( nodeName == "play" )
					play = node.getAttribute( "value" );
				if ( nodeName == "movie" )
				    src = node.getAttribute("value");
				if( nodeName == "wmode" )
				    wmode = node.getAttribute("value"); 
			}
		}
	}

	// Set the check boxes

	if ( !play || play.toLowerCase() != "false" )
		document.theForm.play.checked = true;

	if ( !loop || loop.toLowerCase() != "false" )
	    document.theForm.loop.checked = true;

	//set the wmode selection
	if (wmode.toLowerCase() == "opaque") {
	    document.theForm.wmodeSelector.options[1].selected = true;
	}
	else if (wmode.toLowerCase() == "transparent") {
	    document.theForm.wmodeSelector.options[2].selected = true;
	}
	else {
	    document.theForm.wmodeSelector.options[0].selected = true;	    
	}   	

	// set the movie title
	if ( src )
	{
		// before we do anything else, remove any query string
		var index = src.lastIndexOf("?");
		if( index != -1 )
			src = src.substr( 0, index );

		var filePath = unescape( src );
		index = filePath.lastIndexOf("/");
		if( index != -1 )
			filePath = filePath.substr( index+1, filePath.length );

		document.fileName.innerHTML = filePath;

		var movieURL = dwscripts.getAbsoluteURL(unescape(src));
		if (movieURL != null)
		{
			var moviePath = dwscripts.localURLToFilePath(movieURL);

			if (moviePath != null)
			{
				var widthHeight = dw.extractWidthAndHeightFromSWF(moviePath);
				if (widthHeight != null)
				{
					fileWidth = widthHeight[0];
					fileHeight = widthHeight[1];										
				}
			}
		}
	}

	origWidth = selectedNode.getAttribute("width");
	origHeight = selectedNode.getAttribute("height");	

	if (fileWidth == null || fileHeight == null)
		document.theForm.resetsize.disabled=true;
	else
	{
		document.theForm.resetsize.disabled=false;
		if (origWidth == null)
			origWidth = fileWidth;
		if (origHeight == null)
			origHeight = fileHeight;
	}

	if (origWidth == null || origHeight == null)
	{
		document.theForm.width.disabled="true";
		document.theForm.widthselector.disabled="true";
		document.theForm.height.disabled="true";
		document.theForm.heightselector.disabled="true";
		document.theForm.constraint.disabled="true";
		document.theForm.resetsize.disabled="true";
	}
	else
	{
		document.theForm.width.value = origWidth;
		document.theForm.height.value = origHeight;
	}
			
    if (fileWidth==null || fileHeight==null) 
    {        
        fileWidth = origWidth;
        fileHeight = origHeight;        
    }
    
    aspectRatio = fileWidth / fileHeight;		
}

function widthInPixels()
{
	var width = document.theForm.width.value;
	if (document.theForm.widthselector.selectedIndex == 1)
		width = Math.round((width / 100) * fileWidth);
	return width
}

function setWidthInPixels(width)
{
		if (document.theForm.widthselector.selectedIndex == 1)
			document.theForm.width.value = Math.round(100 * width / fileWidth);
		else
			document.theForm.width.value = width;
}

function heightInPixels()
{
	var height = document.theForm.height.value;
	if (document.theForm.heightselector.selectedIndex == 1)
		height = Math.round((height / 100) * fileHeight);
	return height
}

function setHeightInPixels(height)
{
		if (document.theForm.heightselector.selectedIndex == 1)
			document.theForm.height.value = Math.round(100 * height / fileHeight);
		else
			document.theForm.height.value = height;
}

function widthSet()
{
    var width = parseInt(document.theForm.width.value);
    if (document.theForm.width.value != ""+width)  //if width is not a number  
    {
        alert(dw.loadString("EditFlashVideo/invalid width"));
    }
	else if (document.theForm.constraint.checked)
	{
		var width = widthInPixels();
		var height = Math.round(width / aspectRatio);

		setHeightInPixels(height);
	}
}

function heightSet()
{
    var height = parseInt(document.theForm.height.value);
    if (document.theForm.height.value!=""+height) //if bad height
    {
        alert(dw.loadString("EditFlashVideo/invalid height"));
    }
	else if (document.theForm.constraint.checked)
	{
		var height = heightInPixels();
		var width = Math.round(height * aspectRatio);

		setWidthInPixels(width);
	}
}

function widthSelectorChange()
{      
	if (document.theForm.widthselector.selectedIndex == 1)
		document.theForm.width.value =
			Math.round(100 * document.theForm.width.value / fileWidth);
	else
		document.theForm.width.value =
			Math.round(fileWidth * document.theForm.width.value / 100);
}

function heightSelectorChange()
{
	if (document.theForm.heightselector.selectedIndex == 1)
		document.theForm.height.value =
			Math.round(100 * document.theForm.height.value / fileHeight);
	else
		document.theForm.height.value =
			Math.round(fileHeight * document.theForm.height.value / 100);
}

function resetSize()
{
	setWidthInPixels(fileWidth);
	setHeightInPixels(fileHeight);
}


function run()
{   
    var width = parseInt(document.theForm.width.value);
    var height = parseInt(document.theForm.height.value);
    if (document.theForm.width.value!=""+width || width<0 )  //if width is not a number  
    {
        alert(dw.loadString("EditFlashVideo/invalid width"));
    }
    else if (document.theForm.height.value != ""+height || height<0) //if bad height
    {
        alert(dw.loadString("EditFlashVideo/invalid height"));
    }
    else
    {
	    // Set that we have not found the param tags
    
        var playObj = null;
        var loopObj = null;
        var embedObj = null;
        var secondObjectObj = null;
        var wmodeObj = null;
	    var src;

	    // Look for the param tags as well as the embed tags,
	    // when you find them, alter them to the new values
    
    	selectedNode = dw.getDocumentDOM().getSelectedNode();
            selectedNode.outerHTML = selectedNode.outerHTML;
       
    	if ( selectedNode.childNodes )
    	{
            for ( var index = 0; index < selectedNode.childNodes.length; index++ )
		    {
    			var node = selectedNode.childNodes[ index ];
    			if ( node.tagName == "PARAM" )
			    {
    				var nodeName = node.getAttribute( "name" );
				    nodeName = nodeName.toLowerCase();
				    if ( nodeName == "loop" )
				        loopObj = node;
				    if ( nodeName == "play" )
				        playObj = node;
				    if (nodeName == "wmode")
				        wmodeObj = node;
				    if ( nodeName == "movie" )
					    src = node.getAttribute( "value" );
			    }
                if ( node.tagName == "EMBED" )
                    embedObj = node;
                if (node.tagName == "OBJECT")
                    secondObjectObj = node;                       
		    }
		}

		var secondPlayObj = null;
		var secondLoopObj = null;
		var secondWmodeObj = null;
		
		if (secondObjectObj) {
		    if (secondObjectObj.childNodes) {
		        for (var index = 0; index < secondObjectObj.childNodes.length; index++) {
		            var node = secondObjectObj.childNodes[index];
		            if (node.tagName == "PARAM") {
		                var nodeName = node.getAttribute("name");
		                nodeName = nodeName.toLowerCase();
		                if (nodeName == "loop")
		                    secondLoopObj = node;
		                if (nodeName == "play")
		                    secondPlayObj = node;
		                if (nodeName == "wmode")
		                    secondWmodeObj = node;		               
		            }
		        }
		    }
		}
			      
	    // Get the values of the check boxes
      	var play = "false";
    	if ( document.theForm.play.checked )
		    play = "true";
    
    	var loop = "false";
    	if ( document.theForm.loop.checked )
    	    loop = "true";

    	//get the value for wmode
    	var selected = document.theForm.wmodeSelector.selectedIndex;
    	var wmode = document.theForm.wmodeSelector.options[selected].value;
    	    
    	// If we haven't found the param tags then we need to add them
        if ( embedObj )
        {
            embedObj.setAttribute( "play", play );
            embedObj.setAttribute("loop", loop);
            embedObj.setAttribute("wmode", wmode);
        }

        if ( loopObj )
            loopObj.setAttribute( "value", loop );
        if ( playObj )
            playObj.setAttribute("value", play);
        if (wmodeObj)
            wmodeObj.setAttribute("value", wmode);         
        
        if ( loopObj == null )
		    selectedNode.innerHTML = "<param name=\"loop\" value=\"" + loop + "\">\n" + selectedNode.innerHTML;
        if ( playObj == null )
            selectedNode.innerHTML = "<param name=\"play\" value=\"" + play + "\">\n" + selectedNode.innerHTML;
        if (wmodeObj == null && wmode != "window")
            selectedNode.innerHTML = "<param name=\"wmode\" value=\"" + wmode + "\" >\n" + selectedNode.innerHTML;

        if (secondLoopObj)
            secondLoopObj.setAttribute("value", loop);
        if (secondPlayObj)
            secondPlayObj.setAttribute("value", play);
        if (secondWmodeObj)
            secondWmodeObj.setAttribute("value", wmode);
            
        if (secondObjectObj) {
            if (secondLoopObj == null) {
                if (secondObjectObj.innerHTML.search("endif") != -1) {
                    secondObjectObj.innerHTML = secondObjectObj.innerHTML.substring(secondObjectObj.innerHTML.search("<param"));
                    secondObjectObj.innerHTML = "<!--<![endif]--><param name=\"loop\" value=\"" + loop + "\">\n" + secondObjectObj.innerHTML;
                }
                else {
                    secondObjectObj.innerHTML = "<param name=\"loop\" value=\"" + loop + "\">\n" + secondObjectObj.innerHTML;
                }
            }
            if (secondPlayObj == null) {
                if (secondObjectObj.innerHTML.search("endif") != -1) {
                    secondObjectObj.innerHTML = secondObjectObj.innerHTML.substring(secondObjectObj.innerHTML.search("<param"));
                    secondObjectObj.innerHTML = "<!--<![endif]--><param name=\"play\" value=\"" + play + "\">\n" + secondObjectObj.innerHTML;
                }
                else {
                    secondObjectObj.innerHTML = "<param name=\"play\" value=\"" + play + "\">\n" + secondObjectObj.innerHTML;
                }
            }
            if (secondWmodeObj == null && wmode != "window") 
            {
                if (secondObjectObj.innerHTML.search("endif") != -1) 
                {
                    secondObjectObj.innerHTML = secondObjectObj.innerHTML.substring(secondObjectObj.innerHTML.search("<param"));
                    secondObjectObj.innerHTML = "<!--<![endif]--><param name=\"wmode\" value=\"" + wmode + "\" >\n" + secondObjectObj.innerHTML;
                }
                else 
                {
                    secondObjectObj.innerHTML = "<param name=\"wmode\" value=\"" + wmode + "\" >" + secondObjectObj.innerHTML;
                }
            }
        }

        var newWidth = widthInPixels();
	    var newHeight = heightInPixels();	

	    if (newWidth != origWidth || newHeight != origHeight)
	    {
    		selectedNode.setAttribute("width", newWidth);
		    selectedNode.setAttribute("height", newHeight);
		    if ( embedObj )
            {
                embedObj.setAttribute( "width", newWidth );
                embedObj.setAttribute( "height", newHeight );
            }
            if (secondObjectObj) {
                secondObjectObj.setAttribute("width", newWidth);
                secondObjectObj.setAttribute("height", newHeight);
            }
        }

        //Remove the wmode param if its value is 'window'
	    if(wmode == "window")
	    {
	        if(wmodeObj)
	            wmodeObj.outerHTML = "";
	        if(secondWmodeObj)
	            secondWmodeObj.outerHTML = "";
	        if(embedObj)
	            embedObj.removeAttribute("wmode");
	    }       
            
    	window.close();
    }
}
