/*************************************************************************
*
* ADOBE CONFIDENTIAL
* ___________________
*
*  Copyright 2010 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.
**************************************************************************/


//************************GLOBALS**************************

var HELP_DOC = MM.HELP_jQueryMobile;

//Radio buttons for the link type.
var LINKTYPE = new RadioGroup("linkTojQuery");

//Paths for the source of the asset files. (Absolute)
var JQM_JS_SOURCE = "";
var JQM_CSS_SOURCE = "";
var JQ_JS_SOURCE = "";

//Path for the destination of the asset files. (Relative to site root).
var JQM_JS_DEST = "";
var JQM_CSS_DEST = "";
var JQ_JS_DEST = ""; 
 
//Source input boxes
var JQMJS_LOCATION_FIELD = null;
var JQMCSS_LOCATION_FIELD = null;
var JQ_LOCATION_FIELD = null;
var JQ_LIB_SOURCE_FIELD = null;

//Object to hold clicked value.
var result;

//Library source elements
var jQueryBrowse = null;
var jqLibSourceLabel = null;


//******************* API **********************

//--------------------------------------------------------------------
// FUNCTION:
//   updateSettings
//
// DESCRIPTION:
//   Check that input is valid before moving forward with inserting markup.
//--------------------------------------------------------------------
function updateSettings() {
    var linkTypeValue = LINKTYPE.getSelectedIndex();
    dw.setPreferenceString(PREF_SECTION, PREF_LINK_TYPE, linkTypeValue);

	var jQMjsLoc = JQMJS_LOCATION_FIELD.value;
	var jQMcssLoc = JQMCSS_LOCATION_FIELD.value;
	var jQLoc = JQ_LOCATION_FIELD.value;
	
	//Any problems with the file?
	var fileSanity = checkFileSanity(linkTypeValue);
	if (fileSanity != "") {
		alert(fileSanity);
	} else {
		//Files are good!
		var jqSrc, jsSrc, cssSrc;
		
		//Save modifications in rmeote case. Local case saved during browse.
		if (linkTypeValue == PREF_LINK_REMOTE) {    // link to files via CDN
			dw.setPreferenceString(PREF_SECTION, REMOTE_JS, jQMjsLoc);
			dw.setPreferenceString(PREF_SECTION, REMOTE_CSS, jQMcssLoc);
			dw.setPreferenceString(PREF_SECTION, REMOTE_JQ, jQLoc);
		} else {
			//Check that site root is writable/unlocked.
			if (!isSiteRootSane()) {
				alert(dw.loadString("Commands/jQM/files/alert/lockedFolder"));
				return;
			}
		}

		result.returnValue = "OK";
		window.close();
	}
}

//--------------------------------------------------------------------
// FUNCTION:
//   isSiteRootSane
//
// DESCRIPTION:
//   Check to see if site root is writable or unlocked if it exists.
//
// RETURNS:
//   folderSane - Boolean of whether or not the folder is OK to copy assets into.
//--------------------------------------------------------------------
function isSiteRootSane() {
	var siteRoot = dw.getSiteRoot();
	var folderSane = true;

	//Is site root defined?
	if (siteRoot != "file:///") {
		//Is defined site root writable/unlocked?
		folderSane = dwscripts.isFileWritable(siteRoot);
	}
	return folderSane;
}

//--------------------------------------------------------------------
// FUNCTION:
//   checkFileSanity
//
// DESCRIPTION:
//   Check to see if file path or URL is valid and if it matches naming pattern.
//	 Alert the user if any problems arise.
//
// ARGUMENTS:
//   linkType - Number indicating which view the user is in (0 for remote, 1 for local).
//
// RETURNS:
//   String containing error message if there are problems, nothing otherwise.
//--------------------------------------------------------------------
function checkFileSanity(linkType) {
	var jQMjsLoc = JQMJS_LOCATION_FIELD.value;
	var jQMcssLoc = JQMCSS_LOCATION_FIELD.value;
	var jQLoc = JQ_LOCATION_FIELD.value;
	
	var ok = MM.BTN_OK;
	var invalidURL = dw.loadString("Commands/jQM/files/alert/invalidURL");
	var invalidFile = dw.loadString("Commands/jQM/files/alert/invalidFile");
	var message = "";
	var jsError = ok;
	var cssError = ok;
	var jqError = ok;
	
	var http = "http://";
	
	switch (linkType) {
		case PREF_LINK_REMOTE:
			if (jQMjsLoc.indexOf(http) != 0) {
				jsError = invalidURL;
			}
			if (jQMcssLoc.indexOf(http) != 0) {
				cssError = invalidURL;
			}
			if (jQLoc.indexOf(http) != 0) {
				jqError = invalidURL;
			}

			if (!validFileName(getFileName(jQMjsLoc), 'js')) {
				jsError = invalidFile;
			}
			if (!validFileName(getFileName(jQMcssLoc), 'css')) {
				cssError = invalidFile;
			}
			if (!validFileName(getFileName(jQLoc), 'jq')) {
				jqError = invalidFile;
			}
			
			//Find any invalid file names?
			message += getStatusMessage("error", jsError, cssError, jqError);
			
			break;
		case PREF_LINK_LOCAL:
			var libPath = dw.getConfigurationPath() + "/" + assetDir;
			//Check source is valid (In case file was deleted)
			var libSrc = dw.getPreferenceString(PREF_SECTION, PREF_JQLIB_SOURCE_FOLDER, libPath);
			
			if (!dwscripts.isFolder(libSrc)) {
				message = dw.loadString("Commands/jQM/files/alert/folderProblem");
			} else {
				//Folder exists, check to see if files are valid.
				var locJS = libPath + localJS;
				var locCSS = libPath + localCSS;
				var locJQ = libPath + localJQ;
				
				var iconSrc = libSrc;
				if (libSrc[libSrc.length-1] != "/") {
					iconSrc += "/";
				}
				iconSrc += "images/";
				
				//Get preferences
				var jsFile = dw.getPreferenceString(PREF_SECTION, PREF_JQM_JS_SRC, locJS);
				var cssFile = dw.getPreferenceString(PREF_SECTION, PREF_JQM_CSS_SRC, locCSS);
				var jqFile = dw.getPreferenceString(PREF_SECTION, PREF_JQ_JS_SRC, locJQ);

				var iconDir = libSrc + "images/";
				var notFound = dw.loadString("Commands/jQM/files/alert/notFound");
				
				if (!dwscripts.isFile(jsFile)) {
					jsError = notFound;
				}
				if (!dwscripts.isFile(cssFile)) {
					cssError = notFound;
				}
				if (!dwscripts.isFile(jqFile)) {
					jqError = notFound;
				}
				
				//Find any invalid files?
				message += getStatusMessage("error", jsError, cssError, jqError);
			}
			
			break;
	}
	
	return message;
	
}

//--------------------------------------------------------------------
// FUNCTION:
//   getStatusMessage
//
// DESCRIPTION:
//   Returns a message corresponding to the given status and status messages.
//
// ARGUMENTS:
//   status - Type of status message (Success or error)
//	 jsStatus - Status for the jQuery Mobile js file.
//	 cssStatus - Status for the jQuery Mobile css file.	
//	 jqStatus - Status for the jQuery js file.
//
// RETURNS:
//   String containing the message.
//--------------------------------------------------------------------
function getStatusMessage(status, jsStatus, cssStatus, jqStatus) {
	var ok = MM.BTN_OK;
	var message = "";
	
	if (jsStatus != ok || cssStatus != ok || jqStatus != ok) {
		var jsStr = dw.loadString("Commands/jQM/files/jqmJS");
		var cssStr = dw.loadString("Commands/jQM/files/jqmCSS");
		var jqStr = dw.loadString("Commands/jQM/files/jquery");
		
		var jsSpacing = " : ";
		var cssSpacing = "          ";
		var jqSpacing = "                            ";
		
		//Tab spacing differs between Mac and Win for alignment
		if (dwscripts.IS_WIN) {
			cssSpacing += " ";
			jqSpacing += "    ";
		}
		
		var colonSpace = ": ";
		cssSpacing += colonSpace;
		jqSpacing += colonSpace;
		
		//Trim only if it's a file name.
		if (jsStatus.indexOf(".js") != -1) {
			jsStatus = trimFile(jsStatus);
			cssStatus = trimFile(cssStatus);
			jqStatus = trimFile(jqStatus);
		}
		
		jsStr += jsSpacing + jsStatus + "\n";
		cssStr += cssSpacing + cssStatus + "\n";
		jqStr += jqSpacing + jqStatus + "\n\n";
		
		message = jsStr + cssStr + jqStr;
		
		var msgHead;
		if (status == "success") {
			msgHead = dw.loadString("Commands/jQM/files/alert/fileSuccess");
		} else {
			msgHead = dw.loadString("Commands/jQM/files/alert/fileProblem");
		}
		msgHead += ":\n\n";
		message = msgHead + message;
	}
	
	return message;
}

//--------------------------------------------------------------------
// FUNCTION:
//   trimFile
//
// DESCRIPTION:
//   Cuts file name down to fit on one line if it's too long, appending
//   ellipses at the end.
//
// ARGUMENTS:
//   srcStr - String of the file name.
//
// RETURNS:
//   String that is a shortened from it's original length, with ellipses at the end.
//--------------------------------------------------------------------
function trimFile(srcStr) {
	//Max number of characters possible for file in single line in dialogs for both platforms.
	var WIN_STR_LEN = 40;
	var MAC_STR_LEN = 24;
	
	//Number we want to cut off at.
	var THRESHOLD = 3;
	
	var maxLen = dwscripts.IS_WIN ? WIN_STR_LEN : MAC_STR_LEN;
	var strLen = srcStr.length;
	
	if (strLen > maxLen) {
		var srcStr = srcStr.substring(0,maxLen-4);
		for (var i = 0; i < THRESHOLD; i ++) {
			srcStr += '.';
		}
	}
	
	return srcStr;
}

//--------------------------------------------------------------------
// FUNCTION:
//   initializeUI
//
// DESCRIPTION:
//   Sets up all of the initial UI for the dialog.
//--------------------------------------------------------------------
function initializeUI() {
	//Init controls
	JQMJS_LOCATION_FIELD = document.getElementById("jqmjsURL");
	JQMCSS_LOCATION_FIELD = document.getElementById("jqmcssURL");
	JQ_LOCATION_FIELD = document.getElementById("jqueryURL");

	JQ_LIB_SOURCE_FIELD = document.getElementById("jQuerySourceLibrary");

	jQueryBrowse = document.getElementById("jQueryBrowse");

	jqLibSourceLabel = document.getElementById("jqLibSourceLabel");
   
	//Disable the input buttons if remote or local is checked.
	var remote = document.getElementById("linkTojQuery_1");
	var local = document.getElementById("linkTojQuery_0");
               
    var linkTypeValue = dw.getPreferenceString(PREF_SECTION, PREF_LINK_TYPE, PREF_LINK_REMOTE);
    // set layout type to previous choice
    LINKTYPE.setSelectedIndex(linkTypeValue);

	toggleLinkType(linkTypeValue);
                       
	//Event handlers to browse file.
	remote.onclick = 'toggleLinkType(' + PREF_LINK_REMOTE + ')';
	local.onclick = 'toggleLinkType(' + PREF_LINK_LOCAL + ')';
   
	jQueryBrowse.onclick = 'browseFolder()';
}

//--------------------------------------------------------------------
// FUNCTION:
//   toggleLinkType
//
// DESCRIPTION:
//   Toggles the input fields and loads the correct values and UI.
//
// ARGUMENTS:
//   linkType - Number indicating which view the user is in (0 for remote, 1 for local). 
//-------------------------------------------------------------------- 
function toggleLinkType(linkType) {
	var toggle = linkType == PREF_LINK_REMOTE ? "false" : "true";

	//Toggle input fields.
	JQMJS_LOCATION_FIELD.disabled = toggle;
	JQMCSS_LOCATION_FIELD.disabled = toggle;
	JQ_LOCATION_FIELD.disabled = toggle;

	if (toggle == "true") {     // LOCAL
		toggleSrcLib("block");
		
		jqDef = jqmDir + localJQ;
		jqSrc = PREF_JQ_JS_DEST;
	   
		jsDef = jqmDir + localJS;
		jsSrc = PREF_JQM_JS_DEST;
	   
		cssDef = jqmDir + localCSS;
		cssSrc = PREF_JQM_CSS_DEST;

		JQ_LIB_SOURCE_FIELD.value = dw.getPreferenceString(PREF_SECTION, PREF_JQLIB_SOURCE_FOLDER, getTrueConfigurationPath()+assetDir);
	} else {                    // REMOTE case
		toggleSrcLib("none");
		
		jqDef = jqmJquerySource;
	   
		jqSrc = REMOTE_JQ;
		jqDef = jqmJquerySource;
	   
		jsDef = jqmJavascriptSource;
		jsSrc = REMOTE_JS;
	   
		cssDef = jqmCSSSource;
		cssSrc = REMOTE_CSS;
	}
	//Set file locations.
	
  JQMJS_LOCATION_FIELD.value = dw.getPreferenceString(PREF_SECTION, jsSrc, jsDef);
  JQMCSS_LOCATION_FIELD.value = dw.getPreferenceString(PREF_SECTION, cssSrc, cssDef);
  JQ_LOCATION_FIELD.value = dw.getPreferenceString(PREF_SECTION, jqSrc, jqDef);
}

//--------------------------------------------------------------------
// FUNCTION:
//   getTrueConfigurationPath()
//
// DESCRIPTION:
//   Get user configuration path if assets there exist, otherwise return the application configuration path.
//
// ARGUMENTS:
//   String representing desired configuration path.
//--------------------------------------------------------------------
function getTrueConfigurationPath() {
	var userConfig = dw.getUserConfigurationPath();
	var userAssets = userConfig + assetDir;
	var appConfig = dw.getConfigurationPath() + '/';
	var appAssets = appConfig + assetDir;
	
	//Workaround DW's config path mechanism to force check existence of user config folder.
	return DWfile.listFolder(userAssets, "files") == "" ? appConfig : userConfig;
}

//--------------------------------------------------------------------
// FUNCTION:
//   toggleSrcLib
//
// DESCRIPTION:
//   Toggle the field for the jQuery Library source path, browse button and its label.
//
// ARGUMENTS:
//   display - String of the display style that the fields should be set as.
//--------------------------------------------------------------------
function toggleSrcLib(display) {
	JQ_LIB_SOURCE_FIELD.style.display = display;	// the edit field
	jqLibSourceLabel.style.display = display; 		// the Label "jQuery Library Source: "
	jQueryBrowse.style.display = display; 			// the browse button
}

//--------------------------------------------------------------------
// FUNCTION:
//   isInCurrentSite
//
// DESCRIPTION:
//   If there is a site currently selected and the path is in the currently
//   selected site returns true.
//
// ARGUMENTS:
//   path url to be checked if it is in the current site
//
// RETURNS:
//   dom object
//--------------------------------------------------------------------
function isInCurrentSite(path) {
    var siteRoot = dw.getSiteRoot();
    if (!siteRoot)
        return false;
    var siteRootForURL = dwscripts.filePathToLocalURL(site.getSiteRootForURL(path));
    return (siteRoot == siteRootForURL);
}

//--------------------------------------------------------------------
// FUNCTION:
//   getFolderName
//
// DESCRIPTION:
//   Given a full path to a folder returns just the folder name with a separator appended. 
//
//--------------------------------------------------------------------
function getFolderName(fileURL) {
	var retVal;
	if (dwscripts.isFolder(fileURL)) {
		if (fileURL[fileURL.length - 1] == dwscripts.FILE_SEP) {
			fileURL = fileURL.substring(0, fileURL.length - 1);
		}
		retVal = fileURL.substring(fileURL.lastIndexOf(dwscripts.FILE_SEP) + 1, fileURL.length);
		return(retVal + dwscripts.FILE_SEP);
	}	
}

//--------------------------------------------------------------------
// FUNCTION:
//   browseFolder
//
// DESCRIPTION:
//   Allows user to browse to jQuery mobile library folder. Verifies that
//   the required jQuery files are in the folder. 
//
//--------------------------------------------------------------------
function browseFolder() 
{
	//Is the folder writable/unlocked?
	if (isSiteRootSane()) {
		// Call Dw to bring up the browse for folder dialog
	  var browseRoot = dw.getPreferenceString(PREF_SECTION, PREF_JQLIB_SOURCE_FOLDER, dw.getConfigurationPath()+"/"+assetDir);
	  var jQuerySourceFolder = dw.browseForFolderURL(dw.loadString("Commands/jQM/files/alert/browseFile"), browseRoot, false);
	  var inCurrentSite = false;  // the jQuery files are located within the current site if this is true
	  
		if (jQuerySourceFolder != "") {
			//Add trailing slash if non-existent.
			if (jQuerySourceFolder[jQuerySourceFolder.length-1] != '/') {
				jQuerySourceFolder += "/";
			}

			if (isInCurrentSite(jQuerySourceFolder)) {  // the user selected a folder inside the current site
				siteRelativePath = dw.absoluteURLToDocRelative(jQuerySourceFolder, dw.getSiteRoot(), jQuerySourceFolder);
				inCurrentSite = true;
			} else {
				inCurrentSite = false;
				siteRelativePath = getFolderName(jQuerySourceFolder);
			}
			

			var jQMJSRegEx = /jquery[.-]mobile.*\.js/;
			var jQMJSMinRegEx = /jquery[.-]mobile.*\.min\.js/;
			
			var jQMCSSRegEx = /jquery[.-]mobile.*\.css/;
			var jQMCSSMinRegEx = /jquery[.-]mobile.*\.min\.css/;

			var jQueryJSRegEx = /jquery.*\.js/;
			var jQueryJSMinRegEx = /jquery.*\.min\.js/;
			
			var fileMask = "*.js";
			var jQMJSFile = null;
			var jQueryJSFile = null;
			var jQMcssFile = null;
			var jQMIcons = null;
			
			//Arrays to hold file matches.
			var jqmFullAssets = new Array();
			var jqmMinAssets = new Array();
			var jqFullAssets = new Array();
			var jqMinAssets = new Array();
			
			//Variables for list element usage.
			var listItem, listLen;
			
			// look for all *.js files
			var list = DWfile.listFolder(jQuerySourceFolder + "/" + fileMask, "files");
			if (list) {
				listLen = list.length;
				for (i = 0; i < listLen; i++) {
					listItem = list[i];
					//Look for minified versions first.
					if (listItem.match(jQMJSMinRegEx))
						jqmMinAssets.push(listItem);
					else if (listItem.match(jQMJSRegEx))
						jqmFullAssets.push(listItem);
					// match first form jquery.moble(.*).js if we don't find mobile then look for jquery(.*).js
					else if (listItem.match(jQueryJSMinRegEx))
						jqMinAssets.push(listItem);
					else if (listItem.match(jQueryJSRegEx))
						jqFullAssets.push(listItem);
				}
			}
			
			//Pick JS files if any.
			jQMJSFile = pickSourceFile(jQMJSFile, jqmMinAssets, jqmFullAssets);
			jQueryJSFile = pickSourceFile(jQueryJSFile, jqMinAssets, jqFullAssets);
			
			//Reset jqm arrays for reuse.
			jqmFullAssets = new Array();
			jqmMinAssets = new Array();

			// look for all *.css files to 
			fileMask = "*.css";
			list = DWfile.listFolder(jQuerySourceFolder + "/" + fileMask, "files");
			if (list) {
				listLen = list.length;
				for (i = 0; i < listLen; i++) {
					listItem = list[i];
					if (listItem.match(jQMCSSMinRegEx))
						jqmMinAssets.push(listItem);
					else if (listItem.match(jQMCSSRegEx))
						jqmFullAssets.push(listItem);
				}
			}
			
			//Pick CSS file if any.
			jQMcssFile = pickSourceFile(jQMcssFile, jqmMinAssets, jqmFullAssets);
			
			//Copy image folder.
			fileMask = "*.png";
			list = DWfile.listFolder(jQuerySourceFolder + localIconDir + fileMask, "files");

			if (list != "") {
				//Get parent folder of images directory.
				var dirNames = jQuerySourceFolder.split("/");
				jQMIcons = dirNames[dirNames.length-2] + '/' + localIconDir;
			}
			
			//Did we find all the files?
			if (jQMJSFile && jQueryJSFile && jQMcssFile) {
				var confirmDialog = true;
				//Are any of the files in our current site?
				if (!inCurrentSite) {
					message = getStatusMessage("success", jQMJSFile, jQMcssFile, jQueryJSFile);
					message += dw.loadString("Commands/jQM/files/alert/fileNoExist") + "\n\n";
					
					confirmDialog = confirm(message);
				}
				
				//Don't update path if user cancels
				if (confirmDialog) {
					/** For each of the files, set the corresponding fields to match preference information.
						Also, update the input field with file name. */
					if (jQMJSFile) {
						JQMJS_LOCATION_FIELD.value = siteRelativePath + jQMJSFile;
						JQM_JS_DEST = JQMJS_LOCATION_FIELD.value;
						JQM_JS_SOURCE = jQuerySourceFolder + jQMJSFile;
						dw.setPreferenceString(PREF_SECTION, PREF_JQM_JS_SRC, JQM_JS_SOURCE);
						dw.setPreferenceString(PREF_SECTION, PREF_JQM_JS_DEST, JQM_JS_DEST);
					}
					if (jQueryJSFile) {
						JQ_LOCATION_FIELD.value = siteRelativePath + jQueryJSFile;
						JQ_JS_DEST = JQ_LOCATION_FIELD.value;
						JQ_JS_SOURCE = jQuerySourceFolder + jQueryJSFile;
						dw.setPreferenceString(PREF_SECTION, PREF_JQ_JS_SRC, JQ_JS_SOURCE);
						dw.setPreferenceString(PREF_SECTION, PREF_JQ_JS_DEST, JQ_JS_DEST);
					}
					if (jQMcssFile) {
						JQMCSS_LOCATION_FIELD.value = siteRelativePath + jQMcssFile;
						JQM_CSS_DEST = JQMCSS_LOCATION_FIELD.value;
						JQM_CSS_SOURCE = jQuerySourceFolder + jQMcssFile;
						dw.setPreferenceString(PREF_SECTION, PREF_JQM_CSS_SRC, JQM_CSS_SOURCE);
						dw.setPreferenceString(PREF_SECTION, PREF_JQM_CSS_DEST, JQM_CSS_DEST);
					}
					
					//Do the same for the library source folder.
					JQ_LIB_SOURCE_FIELD.value = jQuerySourceFolder;
					dw.setPreferenceString(PREF_SECTION, PREF_JQLIB_SOURCE_FOLDER, JQ_LIB_SOURCE_FIELD.value);
					
					//Again with the icons to preserve directory name. Throw an alert if not found.
					if (jQMIcons) {
						dw.setPreferenceString(PREF_SECTION, PREF_ICON_DIR, jQMIcons);
					} else {
						alert(dw.loadString("Commands/jQM/files/alert/imageNoExist"));
					}
				}
			} else {
				//Some files are not found.
				var ok = MM.BTN_OK;
				var notFound = dw.loadString("Commands/jQM/files/alert/notFound");
				
				var jsError = jQMJSFile ? ok : notFound;
				var cssError = jQMcssFile ? ok : notFound;
				var jqError = jQueryJSFile ? ok : notFound;
			
				message = getStatusMessage("error", jsError, cssError, jqError);
				alert(message);
			}
		}
	} else {
		alert(dw.loadString("Commands/jQM/files/alert/lockedFolder"));
	}
}

//--------------------------------------------------------------------
// FUNCTION:
//   pickSourceFile
//
// DESCRIPTION:
//   Chooses the latest minified assets over all full assets,
//
// ARGUMENTS:
//   file - String containing name of the picked file.
//	 minAssets - Array of minified file names for given file.
//	 fullAssets - Array of uncompressed file names for given file.
//
// RETURNS:
//   String of just the chosen file name.
//-------------------------------------------------------------------- 
function pickSourceFile(file, minAssets, fullAssets) {
	if (minAssets.length > 0) {
		file = minAssets.pop();
	} else {
		//No min file, try to get latest full file.
		if (fullAssets.length > 0) {
			file = fullAssets.pop();
		}
	}
	
	return file;
}

//--------------------------------------------------------------------
// FUNCTION:
//   getFileName
//
// DESCRIPTION:
//   Obtain the name of the file, given a URL or filepath.
//
// ARGUMENTS:
//   fileName - String containing the name of the absolute file.
//
// RETURNS:
//   String of just the file name.
//-------------------------------------------------------------------- 
function getFileName(fileName) {
	var fileArr = fileName.split('/');
	return fileArr[fileArr.length-1];
}

//--------------------------------------------------------------------
// FUNCTION:
//   setDefaults
//
// DESCRIPTION:
//   Reset preference fields of current selection to program defaults.
//
// ARGUMENTS:
//   linkType - Integer specifying which type of resource we're using
//				(Remote or Local). This is optional.
//--------------------------------------------------------------------
function setDefaults(linkType) {
	var linkTypeValue = linkType ? linkType : LINKTYPE.getSelectedIndex();
	
	if (linkTypeValue == PREF_LINK_REMOTE) {   // DEFAULT LINK is Remote
	    //Remote cases
		dw.setPreferenceString(PREF_SECTION, REMOTE_JS, jqmJavascriptSource);
		dw.setPreferenceString(PREF_SECTION, REMOTE_JQ, jqmJquerySource);
		dw.setPreferenceString(PREF_SECTION, REMOTE_CSS, jqmCSSSource);
	} else {
		//Local cases
		var libPath = getTrueConfigurationPath() + assetDir;
		
		//Local source folder
		dw.setPreferenceString(PREF_SECTION, PREF_JQLIB_SOURCE_FOLDER, libPath);
		
		//Local icon directory
		dw.setPreferenceString(PREF_SECTION, PREF_ICON_DIR, jqmDir+localIconDir);
		
		//Create default local source paths.
		var locJS = libPath + localJS;
		var locCSS = libPath + localCSS;
		var locJQ = libPath + localJQ;
		
		//Set paths
		dw.setPreferenceString(PREF_SECTION, PREF_JQ_JS_SRC, locJQ);
		dw.setPreferenceString(PREF_SECTION, PREF_JQM_JS_SRC, locJS);
		dw.setPreferenceString(PREF_SECTION, PREF_JQM_CSS_SRC, locCSS);
		dw.setPreferenceString(PREF_SECTION, PREF_JQ_JS_DEST, jqmDir+localJQ);
		dw.setPreferenceString(PREF_SECTION, PREF_JQM_JS_DEST, jqmDir+localJS);
		dw.setPreferenceString(PREF_SECTION, PREF_JQM_CSS_DEST, jqmDir+localCSS);
    }
	toggleLinkType(linkTypeValue);
}

/*********************************************************
*                                             DW COMMAND SPECIFIC FUNCTIONS                                                    *
**********************************************************/
function commandButtons() {
   return new Array(MM.BTN_OK, "updateSettings()",
   					dw.loadString("Commands/MultiscreenPreview_EditViewSizes/Label_ResetToDefaults"), "setDefaults()",
   					MM.BTN_Cancel, "window.close()",
   					MM.BTN_Help, "displayHelp()"
                   );
}


//--------------------------------------------------------------------
// FUNCTION:
//   displayHelp
//
// DESCRIPTION:
//   Displays the built-in Dreamweaver help.
//
//--------------------------------------------------------------------
function displayHelp() {
    dwscripts.displayDWHelp(HELP_DOC);
}

//--------------------------------------------------------------------
// FUNCTION:
//   receiveArguments
//
// DESCRIPTION:
//   Sets value for which widget to fire off upon configuration completion.
//
// ARGUMENTS:
//   opt - String containing the type of widget to invoke.
//--------------------------------------------------------------------
function receiveArguments(opt) {
	result = opt;
}