/**
 * @file $File: //depot/saas/BWI/main/src/BuzzwordBrowser.jsx $
 * @author $Author: lxia $
 * @date $DateTime: 2010/01/12 14:02:14 $
 * @version	$Revision: #1 $
 * 
 * ADOBE CONFIDENTIAL
 *
 * Copyright 1997-2007 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.
*/

function BWBrowserUtilities() {
}

//********************************************************************************************
// Utils.GetBoolAttr
//
// Returns the boolean value of the passed in XML attribute
//********************************************************************************************
BWBrowserUtilities.GetBoolAttr = function(attr)
{
	return (attr.toString() == "true");
}

BWBrowserUtilities.GetIntAttr = function(attr)
{
	if (attr != null)
		return parseInt(attr.toString());
	else
		return 0;
}


//********************************************************************************************
// Utils.callMainFunction
//
// this is a wrapper that takes care of logging, handling exceptions and return values
//********************************************************************************************
BWBrowserUtilities.callMainFunction = function(entryPointFunction) 
{
	try 
	{
		// construct a new argument array for the function
		var args = [];
		for(var i = 1; i<arguments.length; i++) {
			args.push (arguments[i]);
		}
	
		// call the entry point function with the remaining arguments
		var result = entryPointFunction.apply(undefined, args);
			
	} 
	catch (err) 
	{
		return "<string><![CDATA[<error>" + err +  "</error>]]></string>";
	}
	return "<string><![CDATA[" + result +  "]]></string>";
}

//********************************************************************************************
// Utils.fixPathIfNeeded
//
// Windows-specific file path fix
//********************************************************************************************

BWBrowserUtilities.fixPathIfNeeded = function(pathIn)
{
	var path = pathIn;
	if (File.fs == 'Windows' && RegExp("^file:///").test(path)) 
	{
		// On Windows, paths of the form 'file:///c:/etc' will fail -- it's that third 
		// slash that throws it off. So replace the /// with //.
		path = path.substr(0,7) + path.substr(8); 
	}
	return path;
}

//********************************************************************************************
// placeBuzzwordDocs
//
// Place the Buzzword document in InDesign.
//********************************************************************************************
var placeBuzzwordDocs = function (args)
{
	return BWBrowserUtilities.callMainFunction(_placeBuzzwordDocs, args);
}

var _placeBuzzwordDocs = function (args)
{
	if (app.documents.length > 0)
	{
		var arguments = new XML(args);
		var doc = app.activeDocument;
		
		doc.placeBuzzword(arguments.@uriStrings.toString(), BWBrowserUtilities.GetBoolAttr(arguments.@showOptions), 
			BWBrowserUtilities.GetBoolAttr(arguments.@createLink), BWBrowserUtilities.GetBoolAttr(arguments.@replaceSelected), 
			BWBrowserUtilities.GetBoolAttr(arguments.@applyGrid));
	}

	return "<success />";
}

//********************************************************************************************
// getProductInfo
//
// Gets InDesign product info.
//********************************************************************************************
var getProductInfo = function ()
{
	return BWBrowserUtilities.callMainFunction(_getProductInfo);
}

var _getProductInfo = function ()
{
	var productInfo = new XML('<productInfo></productInfo>');
	productInfo.@name = app.name;
	productInfo.@featureSet = (app.featureSet == FeatureSetOptions.JAPANESE)? "Japanese" : "Roman";
	
	return productInfo.toXMLString();
}

//********************************************************************************************
// hasActiveDocument
//
// Checks if InDesign has active document.
//********************************************************************************************
var hasActiveDocument = function ()
{
	return BWBrowserUtilities.callMainFunction(_hasActiveDocument);
}

var _hasActiveDocument = function ()
{
	if (app.documents.length > 0 && app.activeDocument != null)
	{
		return "true";
	}
	else
	{
		return "false";
	}
}

//********************************************************************************************
// exportStoryToRtf
//
// Export selected story to RTF
//********************************************************************************************
var exportStoryToRtf = function (filePath)
{
	return BWBrowserUtilities.callMainFunction(_exportStoryToRtf, filePath);
}

var _exportStoryToRtf = function (filePath)
{
	var exportedFile;

	if (app.documents.length > 0)
	{
		var doc = app.activeDocument;
		if (doc.selection.length > 0)
		{
			switch (doc.selection[0].constructor.name)
			{
				case "Text":
				case "InsertionPoint":
				case "Character":
				case "Word":
				case "Line":
				case "TextStyleRange":
				case "Paragraph":
				case "TextColumn":
				case "TextFrame":
				{
					var selectedStory = doc.selection[0].parentStory;
					
					// filePath already contains the file name
					var exportedFilePath = BWBrowserUtilities.fixPathIfNeeded(filePath);
					exportedFile = new File(exportedFilePath);
					selectedStory.exportFile(ExportFormat.RTF, exportedFile);
					break;
				}
			
				default:
					break;
			}
		
		}
	}
	
	return exportedFile.fsName;
}

//********************************************************************************************
// updateLinkState
//
// Update a Buzzword document's state in InDesign.
//********************************************************************************************
var updateLinkState = function (args)
{
	return BWBrowserUtilities.callMainFunction(_updateLinkState, args);
}

var _updateLinkState = function (args)
{
	var arguments = new XML(args);
	
	var docID = arguments.@id.toString();
	var status = LinkStatus.normal;
	var linkStatusInfo = arguments.@statusInfo.toString();
	var result;
	
	if (arguments.@state == "missing")
	{
		status = LinkStatus.linkMissing;
		result = app.updateBuzzwordLinkInfo(docID, status, linkStatusInfo);
	}
	else if (arguments.@state == "inaccessible")
	{
		status = LinkStatus.linkInaccessible;
		result = app.updateBuzzwordLinkInfo(docID, status, linkStatusInfo);
	}
	else
	{	
		// these parameters are required for the available case
		var docTitle = arguments.@title.toString();
		var docVersion = arguments.@docVersion.toString();
		var contentVersion = arguments.@contentVersion.toString();
		var modYear = BWBrowserUtilities.GetIntAttr(arguments.@modYear);
		var modMonth = BWBrowserUtilities.GetIntAttr(arguments.@modMonth);
		var modDay = BWBrowserUtilities.GetIntAttr(arguments.@modDay);
		var modHour = BWBrowserUtilities.GetIntAttr(arguments.@modHour);
		var modMinute = BWBrowserUtilities.GetIntAttr(arguments.@modMinute);
		var modSecond = BWBrowserUtilities.GetIntAttr(arguments.@modSecond);
	
		result = app.updateBuzzwordLinkInfo(docID, status, linkStatusInfo, docTitle, docVersion, contentVersion,
			modYear, modMonth, modDay, modHour, modMinute, modSecond); 
	}
	
	return result;
}
