//=========================================================================================================
//
<!--
/*************************************************************************
*
* 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: Drop PDF
// Author:  GSR
// Module:  DropPDF.js
// Purpose:	Called by the drag-drop and Insert->PDF menu option.
// Source control start : 7/27/06	
// Updates:
// 	
//  
//
//=========================================================================================================

var helpDoc = MM.HELP_objDropPDF;   

var g_file = null;
var g_return = null;
var pdfEmbeddingSetting = true;
var g_rememberPrefs = true;


// ----------------------------------------

function getSelected()
{
	var i;
	for (i = 0; i < document.theForm.opmode.length; ++i)
	{
		if (document.theForm.opmode[i].checked)
		{
			return document.theForm.opmode[i].value;
		}
	}

	return null;
}

// ----------------------------------------

function setSelected(v)
{
	var i;
	for (i = 0; i < document.theForm.opmode.length; ++i)
	{
		if (document.theForm.opmode[i].value == v)
		{
			document.theForm.opmode[i].checked = true;
			document.theForm.opmode[i].focus();
			return true;
		}
	}

	// just choose the first one, whatever it is.
	document.theForm.opmode[0].checked = true;
	document.theForm.opmode[0].focus();
	return false;
}

// ----------------------------------------

function getExtension(url)
{
	var lastDot = url.lastIndexOf(".");
	if (lastDot >= 1)
		return url.slice(lastDot).toLowerCase();
	else
		return "";
}

// ----------------------------------------

function insertPDF(sourceURL)
{
	/*const*/ var warnUserIfNotAvailable = true;
	
	/*if (!dw.isPDFDocument(sourceURL))		//gsrupdate hitch - well is it necessary to check if the doc is pdf?
	{
		return false;
	}*/

	if (sourceURL == null || sourceURL == "") 
	{
		return '';
	}

	
	var isBlogSite = false;
	var ok = true;
	if (dw.appName == "Contribute")
	{
	
		var dom = dw.getDocumentDOM();
	        if(dom)
	        {
	             if(CCWorkspaceManager.getManager(dom))
	              { // if its a blog, do not display the accessibility warning
	                    if(CCWorkspaceManager.getManager(dom).isBlogSite())        
	                        isBlogSite = true;
	              }
	        }
         }
         
         if(isBlogSite == false)
	 {
	      ok = dw.displayOptionalDialog("PDF Accessibility Warning Dialog", 
	      dw.loadString("insert doc dialog/pdf accessibility warning"),true);
	 }
	 if (!ok)
	 {
	 	return '';
	 }
	
	//  a "real" pathname, not a fileURL
	var sourceFile_s = MMNotes.localURLToFilePath(dw.doURLDecoding(sourceURL));

	//aspect Ration of 4:3
	
	var width = 70;  //fixed bug 211640
	var height = 99;
	var winWidth = dw.getDocumentDOM().parentWindow.innerWidth;
	
	if(isBlogSite)
	{
		winWidth = 200;
	}
	
	if (width > 0 && height > 0 && winWidth > 0)
	{
		///*const*/ var PERCENT_OF_WIDTH = 0.90;
		/*const*/ var PERCENT_OF_WIDTH = 0.50;  // changing to 50% of the editable region

		var aspectRatio =  width/height;
		width = Math.round(winWidth * PERCENT_OF_WIDTH);
		height = Math.round(width / aspectRatio);
	}

	
				
	rtnStr = '<OBJECT CLASSID="clsid:CA8A9780-280D-11CF-A24D-444553540000"' + 
				'  WIDTH="' + width.toString() + '" HEIGHT="' + height.toString() + '">\n' + 
				'<PARAM NAME="src" VALUE="' + sourceURL + '"> \n' +
				'<EMBED SRC="' + sourceURL + '" ' +
				'TYPE="application/pdf" PLUGINSPAGE="http://www.adobe.com/products/acrobat/readstep2.html" WIDTH="' + width.toString() + '" HEIGHT="' + height.toString() + '">'+
				'</EMBED></OBJECT>';	

	prefsAccessibilityOption = dw.getPreferenceString("Accessibility", "Accessibility Media Options", "");

	if (prefsAccessibilityOption == '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));	
}


// ----------------------------------------
function receiveArguments()
{
	
	
	g_file = arguments[0];
	g_return = arguments[1];
	g_rememberPrefs = arguments[2];
	
	
	if (dw.appName == "Contribute")
	{
	
		pdfEmbeddingSetting = dw.isPDFEmbeddingEnabled();
		
	}
	
	//gsrupdate read the pref setting and set the defVal
	var defVal ="link";
	
	
	var isTextOnlyMode = dw.getDocumentDOM().getCCSharedSetting_TextOnlyInNonTemplates();

	if (isTextOnlyMode)
	{
			defVal = "link";

	}

	
	setSelected(defVal);
	//onOptionChanged();
	
	if(!pdfEmbeddingSetting)
	{
		
		document.theForm.opmode[1].disabled = true;
		document.theForm.opmode[0].checked = true;
		document.theForm.opmode[0].focus();
		var whatToDo = dw.getPreferenceString("General Preferences", "Drop PDF Action", "prompt");
		if(whatToDo == "link")
		{
			setSelected(whatToDo);
			onOptionChanged();
			document.theForm.remember.checked = true;
			window.close();
			run();
			return 'abort';
		}
		
	}
	else
	{
	
		findObject("WarningText").outerHTML = "";
	
		if (g_rememberPrefs)
		{
			// if g_rememberPrefs is false, always show the dialog, regardless of this pref
			var whatToDo = dw.getPreferenceString("General Preferences", "Drop PDF Action", "prompt");
			
			if (whatToDo != "prompt")
			{
				setSelected(whatToDo);
				onOptionChanged();
				document.theForm.remember.checked = true;
				window.close();
				run();
				return 'abort'; 
			}
			else
			{
				document.theForm.opmode[0].focus();
				document.theForm.opmode[0].checked = true;
				document.theForm.remember.checked = false;
			}
		}
		else
		{
			findObject("RememberCheckbox").outerHTML = "";
		}	
	}
}


// ----------------------------------------

function commandButtons()
{
	return new Array( "PutButtonsOnBottom", 
					  "OkButton",			MM.BTN_OK,		"if (checkValid()) { window.close(); run(); }",
				      "CancelButton",		MM.BTN_Cancel,	"window.close();",
					  "PutButtonOnLeft",	MM.BTN_Help,	"displayHelp();");
}

// ----------------------------------------
function initializeUI()
{
}

// ----------------------------------------
function checkValid()
{
/*	const var warnUserIfNotAvailable = true;
	if (getSelected() == "embed" && !isPDFAvailable())
	{
		return false;
	} */
	
	return true;
}


// ----------------------------------------
function onOptionChanged()
{
	return true;
}

// ----------------------------------------



// ----------------------------------------
function run()
{	
	var selected = getSelected();
	if(!pdfEmbeddingSetting)
	{
		if(g_return)
			g_return[0] = 'link';
	}
	else
	{
		
	
		var result;
		if(selected == 'link')
		{
			
			if(g_return)
				g_return[0] = 'link';
		}
		else if(selected == 'insert')
		{
			
			if(g_return)
				g_return[0] = 'insert';
		}
	
		
	}

	if (g_rememberPrefs)
	{
		if (document.theForm.remember.checked)
		{
			
			dw.setPreferenceString("General Preferences", "Drop PDF Action", selected);
			
		}
		else
		{
			dw.setPreferenceString("General Preferences", "Drop PDF Action", "prompt");
		}
	}

	
	
	
	
}

function linkname(filepath)
{
	var url = filepath;
	var lastSlash = url.lastIndexOf("/");
	
	if (lastSlash >= 1)
	{
		url = url.slice(lastSlash+1);
	}
	
	
	var lastDot = url.lastIndexOf(".");
	
	if(lastDot >= 1)
	{
		
		return url.slice(0,(lastDot));
	}
		
		return url;
	
}
