
/*************************************************************************
*
* ADOBE CONFIDENTIAL
* ___________________
*
*  Copyright 2008 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.
*
* $DateTime: 2008/04/10 13:35:00 $
*
* Adobe Output Module 2.0
*
* MediaGallery Originally Written by Quality Process Incorporated, Enhanced by
* Adobe System China. Contact Sheet written by Adobe System China.
* 
*
**************************************************************************/

/**
  * Paths class
  * Define application program pathes
  *
  *  @Date: 2008-1-7
*/
AOM.CS = {};

AOM.CS.Paths = function() {};

AOM.CS.Paths.mainApp = Folder.commonFiles.fullName + "/Adobe/Bridge CS4 Extensions/Adobe Output Module/";
AOM.CS.Paths.app = Folder.commonFiles.fullName + "/Adobe/Bridge CS4 Extensions/Adobe Output Module/contactsheet/";
AOM.CS.Paths.resources = AOM.CS.Paths.app + "resources/";
AOM.CS.Paths.templates = AOM.CS.Paths.resources + "templates/";
AOM.CS.Paths.scripts = AOM.CS.Paths.resources + "scripts/";
AOM.CS.Paths.images = AOM.CS.Paths.resources + "images/";

//TODO , templately set user path as same as app path
AOM.CS.Paths.user = Folder.userData + "/Adobe/Bridge CS4/Adobe Output Module/";
AOM.CS.Paths.preview = AOM.CS.Paths.user + "preview/";
AOM.CS.Paths.log = AOM.CS.Paths.user + "contactsheet.log";
AOM.CS.Paths.userData = AOM.CS.Paths.user + "ContactSheet/";
AOM.CS.Paths.defaultPersistedUserData = AOM.CS.Paths.resources + "datas/userInputDefault.json";
AOM.CS.Paths.persistedUserData = AOM.CS.Paths.userData + "userInput.json";
AOM.CS.Paths.customTemplate = AOM.CS.Paths.resources + "datas/customtemplate/";

/**
  * Log class
  * Define Log class
  *
  *  @Date: 2007-1-7
*/


AOM.CS.Log =function() {};

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//if debug is verbose mode, app can write log file, otherwise can not write log file
AOM.CS.Log.debugVerbose = false;

AOM.CS.Log.logFile = undefined;

AOM.CS.Log.checkFolder= function(path) {
	var hasExists = true;
	var folder = new Folder(path);
	
	if (!folder.exists) {
		var hasCreated = folder.create();
		if (!hasCreated) {
			hasExists = false;
		}
	}

	return hasExists;
}

//TODO
AOM.CS.Log.checkFolder(AOM.CS.Paths.user);
AOM.CS.Log.checkFolder(AOM.CS.Paths.userData);

AOM.CS.Log.initializeLogFile = function() {
	if (AOM.CS.Log.debugVerbose == false) return;
	
	this.logFile = new File(AOM.CS.Paths.log);
	
	if (this.logFile == undefined) {
		alert("Log file cann't be created.");
		return;
	} 
	
	if (this.logFile.open("w")) {
		this.logFile.close();
		return;
	} else {
		alert("Log file cann't be opened. - " + this.logFile.fsName);
		this.logFile = undefined;
	}
}

//init log file
AOM.CS.Log.initializeLogFile();

AOM.CS.Log.writeLine = function(line)  {
	if (this.logFile == undefined) {
		return;
	}
	if (!this.logFile.open("e")) {
		return;
	}
	this.logFile.seek(0, 2);
	var date = new Date();
	this.logFile.writeln(date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds() + "	"+ line);
	this.logFile.close();
}

AOM.CS.Log.errorsDetected = false;
AOM.CS.Log.writeError = function (error)
{
	if (AOM.CS.Log.debugVerbose == false)  return ;
	this.writeLine("Error:"  + error);
	AOM.CS.Log.errorsDetected = true;
	return undefined;
}

AOM.CS.Log.writeInfo= function(info) {
	if (AOM.CS.Log.debugVerbose == false)  return ;
		
	this.writeLine("Info:" + info);
	return undefined;
}


AOM.CS.Log.dumpObject = function (object, text)
{
	if (AOM.CS.Log.debugVerbose == false)  return ;
		
	if (object == undefined)
	{
		AOM.CS.Log.writeError("dumpObject - object was undefined");
		return;
	}

	AOM.CS.Log.writeLine(">>>>>dumpObject");

	if (text != undefined)
		AOM.CS.Log.writeLine(text);

	var properties = object.reflect.properties;
	for (var i = 0; i < properties.length; i++)
	{
		if (properties[i] != undefined)
			AOM.CS.Log.writeLine("	" +object.reflect.name + "." + properties[i].name + " == " + object[properties[i].name]);
	}
	var methods = object.reflect.methods;
	for (i = 0; i < methods.length; i++)
	{
		if (methods[i] != undefined)
			AOM.CS.Log.writeLine("	" + object.reflect.name + "." + methods[i].name + "()");
	}

	AOM.CS.Log.writeLine("<<<<<dumpObject");
}


/**
  * CString class
  * Define application program pathes
  *
  *  @Date: 2007-1-7
*/
AOM.CS.CString = function() {};


AOM.CS.CString.trim = function(s) {
	if (s == undefined || s.length == 0) return s;
	
	var re = /\s*((\S+\s*)*)/;
	s = s.replace(re, "$1");
	
	re = /((\s*\S+)*)\s*/;
	return s.replace(re, "$1");
};


/*
AOM.CS.CString.trim = function(s) 
{ 
	if (s == undefined || s.length == 0) {
		return s;
	}
	if (s instanceof String) {
		return s.replace(/(^\s*)|(\s*$)/g, ""); 
	}
} 
*/

AOM.CS.CString.ltrim = function(s) 
{ 
	return this.replace(/(^\s*)/g, ""); 
} 

AOM.CS.CString.rtrim = function(s) 
{ 
	return this.replace(/(\s*$)/g, ""); 
} 


AOM.CS.CString.badPathCharacters = function(os) {
	if (os == undefined) {
		os = File.fs;
	} 
	var re = new RegExp();
	switch(os) {
	case "Windows" :
		re = /[\/*?"<>|]/;
		re.toString = function() {
			return "/*?\"<>|";
		}
		break;
	case "Macintosh" :
		re  = /:/;
		re.toString = function() {
			return ":";
		}
		break;
	default:
		alert(AOM.localize("$$$/ContactSheet/badPathCharactersUnknownOs=AOM.CS.CString.badPathCharacters - unknown os."));
		break;
	}
	return re;
};

AOM.CS.CString.badFileCharacters = function(os) {
	if (os == undefined) {
		os = File.fs;
	}

	var re = new RegExp();
	switch(os) {
	case "Windows":
		re  =/[\/\\:*?"<>|]/;
		re.toString = function () { return "/\\:*?\"<>|"; }
		break;
	case "Macintosh":
		re = /:/;	
		re.toString = function () { return ":"; }
		break;
	default:
		alert(AOM.localize("$$$/ContactSheet/badFileCharactersUnknownOs=AOM.CS.CString.badFileCharacters - unknown os."));
		break;
	}
	return re;
};

AOM.CS.CString.strToBoolean = function (str) {
	if (str) {
		if (str == "false") {
			return false;
		} else {
			return true;
		}
	} else {
		return false;
	}
}

AOM.CS.CString.strToNumber = function(str) {
	if (str == str * 1.0) {
		return parseFloat(str);
	} else {
		return 0;
	}
}


/**
  * CommonFuncs 
  * Define application program pathes
  *
  *  @Date: 2007-1-7
*/
AOM.CS.CommonFuncs = function(){};

AOM.CS.CommonFuncs.unitConvertion = function(from, to , value,  realPixelPerInch) {
	if (from == undefined || to == undefined || value == undefined) {
		throw new Error("AOM.CS.CommonFuncs.unitConvertion() throw an error: the arguments 'from, to, value' must not be empty. from="
									+ from + ",to=" + to + ",value=" + value);
	}

	if (from == to )  return value;
	
	//TODO, to get more precise value
	var myVal = undefined;
	var newValue = -1;
	
	value = AOM.CS.Locale.parseFloat(value);
	if (from == AOM.CSConstant.UNIT_PIXES) {
		//
	} else {
		myVal = UnitValue(value, from);
		myVal.convert(AOM.CSConstant.UNIT_PIXELS);
		value = Math.round(myVal.value);
	}
	
	myVal = UnitValue(value, AOM.CSConstant.UNIT_PIXELS);
	myVal.convert(to);
	var newValue = myVal.value;
	
	if (to == AOM.CSConstant.UNIT_PIXELS) {
		newValue = Math.round(newValue);
	} else {
		newValue = AOM.CS.CommonFuncs.roundTwo(newValue); 
	}
	
	return AOM.CS.Locale.toLocaleString(newValue);
}


AOM.CS.CommonFuncs.parseFloatRound2 = function(str) {
	var numberValue = parseFloat(str);
	return AOM.CS.CommonFuncs.roundTwo(numberValue);
}

AOM.CS.CommonFuncs.roundTwo = function(number) {
	return (Math.round(number * 100)) / 100;
}


//return [0, length)  number.
AOM.CS.CommonFuncs.randomInt = function(length) {
	var randNum = Math.random();
	randNum = Math.round(randNum*length);
	return randNum;
}


/**
  * UIUtil
  * Define application program pathes
  *
  *  @Date: 2007-1-7
*/
AOM.CS.UIUtils = function() {};


/**
  * setDropDownList
  * Thinking about two types of dropdownlist: one is single, another is parent-children.
  * if current dropdownlist is single, check if items has no empty, don't remove current items,  (Just for performance consider)
  * if current dropdownlist is parent-children, always remove children items, and add new items in the controller. 
  *
  * @param:dropDownList	current DropDownList control element.
  * @param: newItems	set new items into dropdownlist
  * @param: value	equal to selected item text value
  * @param: isAlwaysSelected	if it is true,  it's always selected one item, even if the item value is not equal the argument value.
  */


AOM.CS.UIUtils.setDropDownList = function(dropDownListObj, newItems, value, isAlwaysSelected, isStringArray) {
	if (dropDownListObj == undefined) {
		return undefined;
	}

	if (dropDownListObj.refParent == undefined) {
		if (dropDownListObj.items.length <= 0) {
			AOM.CS.UIUtils.addDropDownListItems(dropDownListObj, newItems, isStringArray);
		}
	} else {
		dropDownListObj.removeAll();
		AOM.CS.UIUtils.addDropDownListItems(dropDownListObj, newItems, isStringArray);
	}

	//set selection
	var test = AOM.CS.UIUtils.setDropDownListSelection(dropDownListObj, value, isAlwaysSelected);
	
	return test;
}



AOM.CS.UIUtils.rsetDropDownList = function(dropDownListObj, newItems, value, isAlwaysSelected, isStringArray) {
	if (dropDownListObj == undefined) {
		return undefined;
	}

	dropDownListObj.removeAll();
	AOM.CS.UIUtils.addDropDownListItems(dropDownListObj, newItems, isStringArray);
	
	var test = AOM.CS.UIUtils.setDropDownListSelection(dropDownListObj, value, isAlwaysSelected);
	return test;
}


AOM.CS.UIUtils.addDropDownListItems = function(dropDownListObj, newItems, isStringArray) {
	if (newItems != undefined) {		
		for(var idx = 0; idx < newItems.length; idx++) {
			var itm = newItems[idx];
			
			var newItem;
			if (isStringArray == undefined || isStringArray == false) {
				if (itm.value == "-") {
					newItem = dropDownListObj.add("separator", "-");
				} else {
					newItem = dropDownListObj.add("item", itm.text);
				}
				newItem.value = itm.value;
			} else {
				//is string array is true
				var pair = itm.split("|||");
				if (pair.length != 2) {
					newItem = dropDownListObj.add("item", itm);
					newItem.value = itm;
				}
				else {
					newItem = dropDownListObj.add("item", pair[0]);
					newItem.value = pair[1];
				}
			}
		}
	}
}

AOM.CS.UIUtils.setDropDownListSelection = function(dropDownListObj, value, isAlwaysSelected) {
	var items = dropDownListObj.items; 
	var selectedValue = undefined;
	if (items == undefined || items.length ==0) {
		dropDownListObj.enabled = false;
		dropDownListObj.selection = undefined;
	} else {
		dropDownListObj.enabled = true;
		if(value != undefined) {
			for (var idx = 0; idx < items.length; idx++) {
				if (items[idx].value == value) {
					dropDownListObj.selection = items[idx];
					break;
				}
			}
		}
		
		if (dropDownListObj.selection == undefined && isAlwaysSelected) {
			dropDownListObj.selection = items[0];
		}
		selectedValue = dropDownListObj.selection.value;
	}
	dropDownListObj.gsEnabled = dropDownListObj.enabled;

	return selectedValue;
}

AOM.CS.UIUtils.setDropDownListSelectionByIdx = function(dropDownListObj, idx, isAlwaysSelected){
	var items = dropDownListObj.items;
	var selectedValue = undefined;
	if (items == undefined || items.length == 0) {
		dropDownListObj.enabled = false;
		dropDownListObj.selection = undefined;
	} else {
		dropDownListObj.enabled = true;
		if (idx < items.length) {
			dropDownListObj.selection = items[idx];
		}
	
		if (dropDownListObj.selection == undefined && isAlwaysSelected) {
			dropDownListObj.selection = items[0];
		}
		selectedValue = dropDownListObj.selection.value;
	}
	dropDownListObj.gsEnabled = dropDownListObj.enabled;
	

	return selectedValue;
}

/*
AOM.CS.UIUtils.getColorFromColorPicker = function(selectedColor) {
	if (selectedColor < 0) {
		return undefined;
	}

	if (selectedColor == 0) {
		return [0, 0, 0]; 
	}

	var hexString = selectedColor.toString(16);
	
	var rval = gval = bval = 0;
	if (hexString.length == 2) {
		bval = parseInt(hexString.substr(0, 2), 16);
	}
	if (hexString.length == 4) {
		gval = parseInt(hexString.substr(0, 2), 16);
		bval = parseInt(hexString.substr(2, 2), 16);
	} 
	if (hexString.length == 6) {
		rval = parseInt(hexString.substr(0, 2), 16);
		gval = parseInt(hexString.substr(2, 2), 16);
		bval = parseInt(hexString.substr(4, 2), 16);
	}

	var rgb = [rval/255, gval/255, bval/255];
	return rgb;
}
*/

AOM.CS.UIUtils.getColorFromColorPicker = function(color) {
	if (color < 0) {
		return undefined;
	}

	var blue  = (color & 0xFF)/255.0; color = color >>> 8;
	var green = (color & 0xFF)/255.0; color = color >>> 8;
	var red   = (color & 0xFF)/255.0;
	
	return [red, green, blue];
}

AOM.CS.UIUtils.setColorToColorPicker = function(rgb) {
	rgb[0] = Math.round(rgb[0] * 255);
	rgb[1] = Math.round(rgb[1] * 255);
	rgb[2] = Math.round(rgb[2] * 255);
	
	return AOM.CS.UIUtils.rgbToHex(rgb[0], rgb[1], rgb[2]);
}

AOM.CS.UIUtils.rgbToHex = function(r, g, b) { 
	var r_string= r.toString(16); 
	var g_string = g.toString(16); 
	var b_string= b.toString(16); 
	r_string = (r_string.length < 2) ? ("0" + r_string) : r_string; 
	g_string = (g_string.length < 2) ? ("0" + g_string) : g_string; 
	b_string = (b_string.length < 2) ? ("0" + b_string) : b_string; 
	var rgbHex = parseInt((r_string + g_string + b_string), 16);
	return rgbHex;
} 


AOM.CS.UIUtils.confirmLocale = function(msg, arg1, arg2, arg3, arg4) {
	return confirm(AOM.localizeWithArgs(msg, arg1, arg2, arg3, arg4));
};

AOM.CS.UIUtils.alertLocale= function(msg, arg1, arg2, arg3, arg4) {
	alert(AOM.localizeWithArgs(msg, arg1, arg2, arg3, arg4));
}

