//=========================================================================================================
//
<!--
/*************************************************************************
*
* ADOBE CONFIDENTIAL
* ___________________
*
* Copyright 2006 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 may be covered by U.S. and Foreign Patents,
* patents in process, 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.
**************************************************************************/
-->
//
// Feature: Contributor PDF Editing
// Author:  GSR
// Module:  EditPDF.js
// Purpose:	PDF property dialog.
// Updates:
//	7/20/06 - Started file control
//
//=========================================================================================================

var helpDoc = MM.HELP_objEditPDF;

function receiveArguments()
{
}

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

function commandButtons()
{
   return new Array( "PutButtonsOnBottom", "OkButton", MM.BTN_OK, "window.close(); 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 src = "";

	
	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 == "src" )
					src = node.getAttribute( "value" );

			}
		}
	}

	
	// 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];										
				}*/
				fileWidth = 700;
				fileHeight= 990;
			}
		}
	}

	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("EditPDF/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("EditPDF/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("EditPDF/invalid width"));
    }
    else if (document.theForm.height.value != ""+height || height<0) //if bad height
    {
        alert(dw.loadString("EditPDF/invalid height"));
    }
    else
    {
	    // Set that we have not found the param tags
    
        var embedObj = 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 == "src" )
					    src = node.getAttribute( "value" );
			    }
                    if ( node.tagName == "EMBED" )
                        embedObj = node;
		    }
	    }

	
        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 );
            }

	    }
    
    	//window.close();
    }
}
