/*
@@@BUILDINFO@@@ st10Preferences.jsx 466 16-March-2007
*/

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

if( ( stockphoto.timeKeeperOn != undefined )&&( stockphoto.timeKeeperOn ) )
{
	stockphoto.timeKeeper.st10Preferences        = {};
	stockphoto.timeKeeper.st10Preferences.StTime = new Date();
}

// Preferences namespace and zstringCache instance
stockphoto.prefs	= {};
stockphoto.prefs.zc = new stockphoto.zstringCache("st10Preferences");

//--------------------------------------------------------------------
// GLOBAL VARIABLES
//--------------------------------------------------------------------

stockphoto.prefs.PrefDlgName = stockphoto.prefs.zc.get("$$$/Preferences/Dialog/Name=Adobe Stock Photos Search Preferences");
stockphoto.prefs.PrefDlgTitle = "Adobe Stock Photos";
stockphoto.prefs.ProxyButtonTitle = stockphoto.prefs.zc.get("$$$/Preferences/Dialog/ProxyButtonTitle=Enter Proxy Details");

// default settings
stockphoto.prefs.default_maxHits			= "300";
stockphoto.prefs.thumbPerGroup				= ["25", "50", "75", "100", "200", "500"];
stockphoto.prefs.default_thumbPerGroup		= 4;  // 200
stockphoto.prefs.thumbnailSizes				= ["128x128", "256x256"];
stockphoto.prefs.default_thumbnailSize		= 0;
stockphoto.prefs.languages					= {};
stockphoto.prefs.regions					= {};
stockphoto.prefs.regionsSpecified			= false;
stockphoto.prefs.default_languageCode		= "en";
stockphoto.prefs.default_countryCode		= "US";
stockphoto.prefs.default_advSearchAutoOpen  = true;

stockphoto.prefs.default_prefsPanel = {};

if (stockphoto.isMac)
{ 
	stockphoto.prefs.stdCntTab = 
	{
		START_X                : "5",
		START_Y                : "12",
		TITLE_HEIGHT           : "12",
		COL1_BEGIN             : "10",
		ROW_BEGIN              : "10",
		RAW_HEIGHT             : "20",
		RAW_SPACE              : "5",
		COL_WIDTH              : "125",
		REGION_LIST_BEGIN      : "-80",
		REGION_LIST_WIDTH_ADD  : "0",
		BROWSE_BTN_WIDTH       : "140",
		BROWSE_BTN_OFFSET      : "0",
		RESET_BTN_WIDTH        : "70",
		CLEAR_BTN_WIDTH	       : "100",
		CLEAR_BTN_OFFSET       : "0",
		MARGIN                 : "10",
		LINE_PANEL_BOUNDS      : "[5, 26, 515, 26]",
		SEARCH_PANEL_BOUNDS    : "[5, 36, 515, 111]",
		FOLDER_PANEL_BOUNDS    : "[5, 121, 515, 196]",
		PURCHASE_PANEL_BOUNDS  : "[5, 206, 515, 378]",
		PURCHASE_PANEL1_BOUNDS : "[5, 206, 515, 401]",
		MAX_CHAR_COUNT         : "70",
		PROXY_WIDTH            : "8"
	};

}
else if (stockphoto.isWin)
{
	stockphoto.prefs.stdCntTab = 
	{
		START_X                : "5",
		START_Y                : "10",
		TITLE_HEIGHT           : "12",
		COL1_BEGIN             : "10",
		ROW_BEGIN              : "10",
		RAW_HEIGHT             : "20",
		RAW_SPACE              : "4",
		COL_WIDTH              : "110",
		REGION_LIST_BEGIN      : "-80",
		REGION_LIST_WIDTH_ADD  : "0",
		BROWSE_BTN_WIDTH       : "120",
		BROWSE_BTN_OFFSET      : "0",
		RESET_BTN_WIDTH        : "70",
		CLEAR_BTN_WIDTH        : "80", 
		CLEAR_BTN_OFFSET       : "0",
		MARGIN                 : "10",
		LINE_PANEL_BOUNDS      : "[5, 23, 455, 23]",
		SEARCH_PANEL_BOUNDS    : "[5, 33, 455, 106]",
		FOLDER_PANEL_BOUNDS    : "[5, 116, 455, 189]",
		PURCHASE_PANEL_BOUNDS  : "[5, 199, 455, 367]",
		PURCHASE_PANEL1_BOUNDS : "[5, 199, 455, 388]",
		MAX_CHAR_COUNT         : "70",
		PROXY_WIDTH            : "8"
	};
};

stockphoto.prefs.SetStandardCnvTab=function() 
{
	if ( stockphoto.prefs.locale != stockphoto.core.getValidLocale() ) 
	{
		if ( stockphoto.core.util.loadLocaleData( "st10Preferences.par" ) )
			stockphoto.prefs.locale = stockphoto.core.getValidLocale();
	};
}

stockphoto.prefs.SetStandardCnvTab();

stockphoto.prefs.getCurrentPrefsPanel=function()
{
    if ((app.document != undefined) && (app.document.id != undefined)
        && (stockphoto.documents != undefined)
        && (stockphoto.documents[app.document.id] != undefined))
    {
        return stockphoto.documents[app.document.id].prefsPanel;
    }
    else
    {
        return stockphoto.prefs.default_prefsPanel;
    }
}

stockphoto.prefs.setCurrentPrefsPanel=function(prefsPanel)
{
    if ((app.document != undefined) && (app.document.id != undefined)
        && (stockphoto.documents != undefined))
    {
        stockphoto.documents[app.document.id].prefsPanel = prefsPanel;
    }
    else
    {
        stockphoto.prefs.default_prefsPanel = prefsPanel;
    } 
}

// *****************************************************************
// *****************************************************************
//			Get Functions
// *****************************************************************
// *****************************************************************

stockphoto.prefs.GetMaxHits = function()
{
	if (app.preferences.bm_maxHits == undefined)
	{
		app.preferences.bm_maxHits = stockphoto.prefs.default_maxHits;
	}
	return app.preferences.bm_maxHits;
}

stockphoto.prefs.GetThumbPerGroup_Body=function()
{
	return stockphoto.prefs.thumbPerGroup[stockphoto.prefs.GetThumbPerGroupIndex()];
}

stockphoto.prefs.GetThumbPerGroupIndex = function()
{
    var iCurrentTPG = app.preferences.bm_thumbPerGroupIndex;
    var iCurrentVersion = app.preferences.bm_versionInstalled;
    
	if ( (iCurrentTPG == undefined) ||  // If TPG doesn't exist yet
         ( (iCurrentTPG == 1)  // Index 1 is 50 (default for GM/BC1/BC2)
                && ((iCurrentVersion == undefined) || (iCurrentVersion < stockphoto.core.version)) ) )
	{
		app.preferences.bm_thumbPerGroupIndex = stockphoto.prefs.default_thumbPerGroup;
	}
	
	app.preferences.bm_versionInstalled = stockphoto.core.version;
	
	return app.preferences.bm_thumbPerGroupIndex;
}

stockphoto.prefs.GetThumbnailSize = function()
{
	if (app.preferences.bm_thumbnailSize == undefined)
	{
		app.preferences.bm_thumbnailSize = stockphoto.prefs.default_thumbnailSize;
	}
	return app.preferences.bm_thumbnailSize;
}

stockphoto.prefs.GetAdvancedSearchAutoOpen = function()
{
    if (app.preferences.bm_advSearchAutoOpen == undefined)
    {
        app.preferences.bm_advSearchAutoOpen = stockphoto.prefs.default_advSearchAutoOpen;
    }
    return app.preferences.bm_advSearchAutoOpen;
}

stockphoto.prefs.GetLanguage = function()
{
	if (app.preferences.bm_search_language == undefined)
	{
		// default search language is based on the default UI language
		var uiCode = stockphoto.core.getValidLocale();
		var code = stockphoto.prefs.default_languageCode + "_" + 
				 stockphoto.prefs.default_countryCode;
		var i;

		if (uiCode != undefined)
		{
			for( i = 0; stockphoto.LangCountry.LanguageTable[i].c != ""; i++)
			{
				if (uiCode == stockphoto.LangCountry.LanguageTable[i].c)
				{
					code = uiCode;
					break;
				}
			}
		}
		
		app.preferences.bm_search_language = code;
	}
	return app.preferences.bm_search_language;
}

stockphoto.prefs.GetSearchLanguageIndex = function()
{
	var code = stockphoto.prefs.GetLanguage();
	var name = stockphoto.LangCountry.GetNameFromSearchLanguageCode(code);
	var i;
	
	for( i = 0; stockphoto.prefs.languages[i] != undefined; i++)
	{
		if (name == stockphoto.prefs.languages[i])
		{
			return i;
		}
	}
	
	return 0;
}

stockphoto.prefs.GetSearchLanguageCode = function()
{
	var lc = stockphoto.prefs.default_languageCode + "-" + 
			 stockphoto.prefs.default_countryCode;
	if( stockphoto.DL.demandJsxLoading( stockphoto.DL.jsx_LangCountry ) )
	{
		var code = stockphoto.prefs.GetLanguage();
		if (code != undefined)
		{
			var l_c = code.split("_");
			lc = l_c[0] + "-" + l_c[1];
		}
	}
	return lc;
}

stockphoto.prefs.GetLanguageCode_Body = function()
{
	var langCode = stockphoto.prefs.default_languageCode;
	if( stockphoto.DL.demandJsxLoading( stockphoto.DL.jsx_LangCountry ) )
	{
		e = stockphoto.LangCountry.LoadLangCountries();
		if( e != "" )
		{
			stockphoto.prefs.langCountries = eval( e );
		}
		var fullCode = stockphoto.prefs.GetLanguage();
		if (fullCode.length == 5)
		{
			langCode = fullCode.substr(0,2);
		}
	}
	return langCode;
}

stockphoto.prefs.GetCurrencyRegion = function()
{
  return app.preferences.bm_billing_region;
}

stockphoto.prefs.GetBillingCountryIndex = function()
{
	var code = stockphoto.prefs.GetCurrencyRegion();

	if (code != undefined)
	{
		var name = stockphoto.LangCountry.GetNameFromBillingCountryCode(code);
	
		for(var i = 0; stockphoto.prefs.regions[i] != undefined; i++)
		{
			if (name == stockphoto.prefs.regions[i])
			{
				return i;
			}
		}
	}
	
	return -1;
}

stockphoto.prefs.GetCountryCodeFromLanguageCode_Body = function()
{
	var countryCode = stockphoto.prefs.default_countryCode;
	if( stockphoto.DL.demandJsxLoading( stockphoto.DL.jsx_LangCountry ) )
	{
		e = stockphoto.LangCountry.LoadLangCountries();
		if( e != "" )
		{
			stockphoto.prefs.langCountries = eval( e );
		}
		var fullCode = stockphoto.prefs.GetLanguage();
		if (fullCode.length == 5)
		{
			countryCode = fullCode.substr(3,2);
		}
	}
	return countryCode;
}

stockphoto.prefs.CreateMyFolder = function(subFolder)
{
	var myFolder = Folder(app.preferences.bm_myFolder + subFolder);
	if (!myFolder.exists)
	{
		if (!myFolder.create())
		{
			throw ("Cound not create folder : " + myFolder.fsName);
		}
	}
}

stockphoto.prefs.GetMyFolder = function(defaultPathOnly)
{
	return stockphoto.core.folder.getMyFolder(defaultPathOnly);
}

stockphoto.prefs.GetTruncatedFolderPath = function(myFolder)
{
	if (myFolder != undefined)
	{
		var MaxCharCount = "MaxCharCount=" + stockphoto.prefs.stdCntTab.MAX_CHAR_COUNT;
		eval(MaxCharCount);
		if (myFolder.length > MaxCharCount)
		{
			myFolder = myFolder.substring(0, MaxCharCount-1) + zc.get("$$$/Preferences/Panel/DotDotDot=...");
		}
	}
	return myFolder;
}

stockphoto.prefs.GetMySearchesFolder = function()
{
	return stockphoto.core.folder.getMySearchesFolder();
}

stockphoto.prefs.GetMyCompFolder = function()
{
	return stockphoto.core.folder.getMyCompFolder();
}

stockphoto.prefs.GetMyPurchasesFolder = function()
{
	return stockphoto.core.folder.getMyPurchasesFolder();
}

stockphoto.prefs.GetCurrencyType_Body = function()
{
	var countryCode = stockphoto.prefs.GetCurrencyRegion();
	var currency = stockphoto.DL.LangCountry.StockCurrency(countryCode);
	return (currency);
}

stockphoto.prefs.GetCurrencySymbol_Body = function()
{
	var currType = stockphoto.prefs.GetCurrencyType_Body();
	var currSymbol = "US$";
	if (currType != "USD")
	{
    if (currType == "GBP")
  	{
  		currSymbol = "&pound;";
  	}
  	else if (currType == "JPY")
  	{
  		currSymbol = "&yen;";
  	}
  	else if (currType == "EUR")
  	{
  		currSymbol = "&euro;";
  	}
  }
	return currSymbol;
}


//added by aman for ValueCollection

stockphoto.prefs.GetValueCollectionFlag = function()
{
  return app.preferences.PrevValueCollection;
}
//end of added for ValueCollection




// *****************************************************************
//			Set functions
// *****************************************************************

//added by aman for ValueCollection

stockphoto.prefs.SetValueCollectionFlag = function( bCollectionFlag )
{
    var prevValueCollectionFlag = stockphoto.prefs.GetValueCollectionFlag();

	app.preferences.PrevValueCollection = bCollectionFlag; 	
	
	return ((undefined == prevValueCollectionFlag) || (prevValueCollectionFlag != bCollectionFlag));
}


stockphoto.prefs.SetMaxHits = function(maxHits)
{
	app.preferences.bm_maxHits = maxHits;
}

stockphoto.prefs.SetThumbPerGroupIndex = function(thumbPerGroup)
{
	for (var i = 0; stockphoto.prefs.thumbPerGroup[i] != undefined; i++)
	{
		if (stockphoto.prefs.thumbPerGroup[i].toString() == thumbPerGroup.toString())
		{
			app.preferences.bm_thumbPerGroupIndex = i;
			break;
		}
	}
}

stockphoto.prefs.SetThumbnailSize = function(thumbnailSize)
{
	for (var i = 0; stockphoto.prefs.thumbnailSizes[i] != undefined; i++)
	{
		if (stockphoto.prefs.thumbnailSizes[i].toString() == thumbnailSize.toString())
		{
			app.preferences.bm_thumbnailSize = i;
			break;
		}
	}
}

stockphoto.prefs.SetAdvancedSearchAutoOpen = function(value)
{
    app.preferences.bm_advSearchAutoOpen = value;
}

stockphoto.prefs.SetLanguage = function(language)
{
	app.preferences.bm_search_language = stockphoto.LangCountry.GetCodeFromSearchLanguageName(language);
}

stockphoto.prefs.SetCurrencyRegion = function(region)
{
	app.preferences.bm_billing_region = stockphoto.LangCountry.GetCodeFromBillingCountryName(region);
	stockphoto.DL.MiscDlgs.BillingCountry_Status = 2;
}

stockphoto.prefs.SetMyFolder = function(folderName)
{
	if (app.preferences.bm_myFolder == Folder(folderName).fsName)
	{
		return;
	}
	app.preferences.bm_myFolder = Folder(folderName).fsName;

	stockphoto.prefs.GetMyFolder();
	if (stockphoto.thumbs.initialized)
	{
		stockphoto.thumbs.setPaths();
	}
	if (stockphoto.places.initialized)
	{
		stockphoto.places.setPaths();
		stockphoto.places.addStockItems();
		var docs = app.documents;
		var	n = docs.length;
		for (i = 0; i < n; i++)
		{
			var	doc = docs[i];
			with (stockphoto.places)
			{
				var	where;
				switch (doc.thumbnail.path)
				{
					case comps.path:
						where = stockphoto.core.consts.comps;
						break;
					case purchases.path:
						where = stockphoto.core.consts.purchases;
						break;
					case recentSearches.path:
						where = stockphoto.core.consts.recentSearches;
						break;
					default:
						continue;
				}
				var script =
				'\
					doc = stockphoto.documents[' + doc.id + '].document;\
					doc.thumbnail = stockphoto.places.' + where + ';\
					doc.refresh();\
				';
				var taskId = app.scheduleTask(script, 0, false);
			}
		}
	}
}	

// *****************************************************************
// *****************************************************************
//	 Event handler
// *****************************************************************
// *****************************************************************

stockphoto.prefs.isInteger = function(input) 
{
	var i = input.indexOf(".");
	var j = input.indexOf("-");
	var s = isNaN(input);
	return (!(s || (!s && ((i >=0) || (j >=0)))));
}

stockphoto.prefs.isValidFolderName = function(inFolder) {
	var f = Folder(inFolder);
	if (f.exists)
	{
		return true;
	}
	
  return f.create();	 
}

stockphoto.prefs.OkBtnClicked = function()
{
	var dlg = stockphoto.prefs.getCurrentPrefsPanel();
	if (dlg == undefined)
	{
		return;
	}

	var zc = stockphoto.prefs.zc;
	if (!stockphoto.prefs.isValidFolderName(dlg.folderPanel.myFolder.fulltext))
	{
		stockphoto.DL.generalAlertBox(zc.get("$$$/Preferences/Alert/IllegalFolderName=Illegal Folder Name"), 2);
		return;
	}

	stockphoto.prefs.SetThumbPerGroupIndex(dlg.searchPanel.thumbs.thumbPerGroup.selection);
	stockphoto.prefs.SetMyFolder		 (dlg.folderPanel.myFolder.fulltext);
	stockphoto.prefs.SetLanguage		 (dlg.searchPanel.lang.language.selection);

	if ((0 < dlg.purchasePanel.billing.region.selection) || (stockphoto.prefs.regionsSpecified == true))
	{
		if((undefined != app.preferences.bm_PrevBillingRegion) && (undefined != app.preferences.bm_billing_region))
		{	
			app.preferences.bm_PrevBillingRegion = app.preferences.bm_billing_region;
		}
		stockphoto.prefs.SetCurrencyRegion(dlg.purchasePanel.billing.region.selection);
		stockphoto.places.setPaths();
//		stockphoto.places.addMainStockFavs();
		
	}
	
	// store dialog display checkbox values
	// (Note: first parameter matches alertType parameter in st16AlertBox.jsx)
	with (stockphoto.core.consts)
	{
		stockphoto.core.ui.setAlertDisplayPrefs(MsgAfterDownloadComp,		dlg.purchasePanel.messageAfterCompCheck.value);
		stockphoto.core.ui.setAlertDisplayPrefs(MsgAfterAddingImageToCart,	dlg.purchasePanel.messageAfterAddCheck.value);
		stockphoto.core.ui.setAlertDisplayPrefs(AutoDlImagesAfterCheckout,	dlg.purchasePanel.autoDownloadCheck.value);
		stockphoto.core.ui.setAlertDisplayPrefs(ResumeDlsWhenBridgeStarts,	dlg.purchasePanel.messageBridgeLaunchCheck.value);
	}
	if( stockphoto.DL.isAlreadyLoaded( stockphoto.DL.jsx_NavBar ) && (app.document != undefined) )
	{
		stockphoto.NavExtFnc.PlaceBMNavBarScheduled( app.document.id );
	}  
	delete stockphoto.prefs.getCurrentPrefsPanel();
	stockphoto.prefs.setCurrentPrefsPanel(undefined);
}

stockphoto.prefs.CancelBtnClicked = function() 
{
}

stockphoto.prefs.preferenceBrowseBtnClicked = function() 
{
}


// *****************************************************************
//		Dialog
// *****************************************************************
stockphoto.prefs.ResetLangContTab = function() 
{
	if ( stockphoto.DL.demandJsxLoading( stockphoto.DL.jsx_LangCountry ) ) 
	{
		var e;
		e = stockphoto.LangCountry.LoadLanguages();
		if( e != "" )
		{
			stockphoto.prefs.languages = eval( e );
			stockphoto.prefs.languages.sort();
		}
		e = stockphoto.LangCountry.LoadCountries();
		if( e != "" )
		{
			stockphoto.prefs.regions = eval( e );
			stockphoto.prefs.regions.sort();
		}

		var idx = stockphoto.prefs.GetBillingCountryIndex();

		if (idx < 0)
		{
			var len = stockphoto.prefs.regions.length;
			var ary = new Array(len + 1);

			var tmpzc = new stockphoto.zstringCache("st20MiscDlgs");
			tmpzc.dict = "st20miscDlgs.txt";

			ary[0] = tmpzc.get("$$$/MiscDlgs_FT/SelLC=Select a country or region");

			for (var i = 1; i <= len; i++)
			{
				ary[i] = stockphoto.prefs.regions[i - 1];
			}
			stockphoto.prefs.regions = ary;
			stockphoto.prefs.regionsSpecified = false;
		}
		else
		{
			stockphoto.prefs.regionsSpecified = true;
		}
	}
}

stockphoto.prefs.ResetLangContTab();

stockphoto.prefs.GetCurrencyTypeFromRegion = function(regionName)
{
	var currencyTypeStr = "";

	if ((0 < regionName) || (stockphoto.prefs.regionsSpecified == true))
	{
		var countryCode = stockphoto.LangCountry.GetCodeFromBillingCountryName(regionName);
		currencyTypeStr = stockphoto.DL.LangCountry.StockCurrency(countryCode);
	}

	return currencyTypeStr;
}

stockphoto.prefs.OnCurrencyRegionListChange = function()
{
	var zc = stockphoto.prefs.zc;
	var regionName = this.parent.region.selection;
	if("undefined" == typeof(regionName) || null == regionName)
	{
	   return ;
  	}
	var currencyTypeStr = stockphoto.prefs.GetCurrencyTypeFromRegion(regionName);
	this.parent.currencyType.text = zc.get("$$$/Preferences/Panel/Currency/CurrencyType=Currency=") + currencyTypeStr;
}

stockphoto.prefs.PreferenceMain = function(dlg)
{
	var zc = stockphoto.prefs.zc;
	var dlgBounds = dlg.bounds;
	stockphoto.prefs.setCurrentPrefsPanel(dlg);

	var Margin = "Margin=" + stockphoto.prefs.stdCntTab.MARGIN;
	eval(Margin);


	var Col1Begin = "Col1Begin=" + stockphoto.prefs.stdCntTab.COL1_BEGIN;
	eval(Col1Begin);

	var RawBegin = "RawBegin=" + stockphoto.prefs.stdCntTab.ROW_BEGIN; 
	eval(RawBegin);


	var RawHeight = "RawHeight=" +	stockphoto.prefs.stdCntTab.RAW_HEIGHT;
	eval(RawHeight);

	var StartX = "StartX=" + stockphoto.prefs.stdCntTab.START_X; 
	eval(StartX);

	var StartY = "StartY=" + stockphoto.prefs.stdCntTab.START_Y; 
	eval(StartY);

	var TitleHeight = "TitleHeight=" + stockphoto.prefs.stdCntTab.TITLE_HEIGHT; 
	eval(TitleHeight);

	var Margin = "Margin=" + stockphoto.prefs.stdCntTab.MARGIN;
	eval(Margin);
	
	var subPanelRight = dlgBounds.right - Margin;
	
	
	if (!isNaN(subPanelRight))
	{ 
		subPanelRight = subPanelRight-188;
	}
	
	var ColWidth;

	ColWidth = Math.floor((dlgBounds.right - dlgBounds.left - Margin - StartX) / 4);

	if (isNaN(ColWidth))
	{
			ColWidth = "ColWidth=" + stockphoto.prefs.stdCntTab.COL_WIDTH; 
			eval(ColWidth);
	}

	var regionListBegin = "regionListBegin=" + stockphoto.prefs.stdCntTab.REGION_LIST_BEGIN;
	eval(regionListBegin);

	var regionListWidthAdd = "regionListWidthAdd=" + stockphoto.prefs.stdCntTab.REGION_LIST_WIDTH_ADD;
	eval(regionListWidthAdd);

	var Col2Begin = Col1Begin + ColWidth;
	var Col3Begin = Col2Begin + ColWidth;
	var Col4Begin = Col3Begin + ColWidth;

	var RegionBegin = Col3Begin + regionListBegin;
	var RegionWidth = ColWidth + regionListWidthAdd;


	
// Title
	dlg.orientation = 'column';
	dlg.alignChildren='fill';

	// To leave a margin above the title..
	dlg.dummyGp = dlg.add('group');
	dlg.dummyGp.margins = Margin;

	dlg.ASPtitle = dlg.add('group', undefined);
	dlg.ASPtitle.orientation = 'column';
	dlg.ASPtitle.alignChildren='fill';
	
	dlg.ASPtitle.ASP = dlg.ASPtitle.add( 'statictext', undefined, stockphoto.prefs.PrefDlgTitle);
	dlg.ASPtitle.ASP.alignment = ['left', 'bottom'];
	dlg.ASPtitle.add( 'panel', undefined, "");
	
// Searching panel
	dlg.searchPanel = dlg.add( 'panel', undefined, zc.get("$$$/Preferences/Panel/Search/Title=Searching"));
	dlg.searchPanel.orientation  = 'column';
	dlg.searchPanel.alignChildren = 'fill' 

	// Thumbs per group
	dlg.searchPanel.thumbs = dlg.searchPanel.add('group');
	dlg.searchPanel.thumbs.orientation = 'row';
	dlg.searchPanel.thumbs.thumbPerGroupTitle = dlg.searchPanel.thumbs.add( 'statictext',	undefined, zc.get("$$$/Preferences/Panel/Search/ThumbnailPerGroup=Thumbnails per search group:"));
	dlg.searchPanel.thumbs.thumbPerGroup = dlg.searchPanel.thumbs.add( 'dropdownlist', undefined, stockphoto.prefs.thumbPerGroup);
	dlg.searchPanel.thumbs.thumbPerGroup.selection = stockphoto.prefs.GetThumbPerGroupIndex();

	// Language title.
	dlg.searchPanel.lang = dlg.searchPanel.add('group');
	dlg.searchPanel.lang.orientation = 'row';
	dlg.searchPanel.lang.languageTitle = dlg.searchPanel.lang.add( 'statictext', undefined, zc.get("$$$/Preferences/Panel/Search/Language=Search Language:"));
	dlg.searchPanel.lang.language = dlg.searchPanel.lang.add( 'dropdownlist', undefined, stockphoto.prefs.languages);	
	dlg.searchPanel.lang.language.selection = stockphoto.prefs.GetSearchLanguageIndex();

// Folders panel
	dlg.folderPanel = dlg.add( 'panel', undefined, zc.get("$$$/Preferences/Panel/Folder/Title=Adobe Stock Photos Folder Location"));
	dlg.folderPanel.orientation  = 'column';
	dlg.folderPanel.alignChildren = 'left';
	
	var mySearchFolder = stockphoto.prefs.GetMyFolder();
	var mySearchFolder1 = stockphoto.prefs.GetTruncatedFolderPath(mySearchFolder);
	dlg.folderPanel.myFolder = dlg.folderPanel.add( 'statictext', undefined, mySearchFolder1);
	
	dlg.folderPanel.folderOpBtns = dlg.folderPanel.add('group');
	dlg.folderPanel.folderOpBtns.orientation='row';
	dlg.folderPanel.folderOpBtns.alignment = 'right';
	dlg.folderPanel.folderOpBtns.alignChildren = ['right', 'top'] ;
	dlg.folderPanel.folderOpBtns.browseBtn	= dlg.folderPanel.folderOpBtns.add( 'button', undefined, zc.get("$$$/Preferences/Panel/Folder/ChangeLocation=Change Location...") );
	dlg.folderPanel.folderOpBtns.resetBtn	= dlg.folderPanel.folderOpBtns.add( 'button', undefined, zc.get("$$$/Preferences/Panel/Folder/Reset=Reset"));
	dlg.folderPanel.myFolder.fulltext = mySearchFolder;

	dlg.folderPanel.folderOpBtns.browseBtn.onClick = function ()
	{ 
		var zc = stockphoto.prefs.zc;
		var savePathSpec = app.zStringPathSpecifier;
		var pathSpec = stockphoto.core.util.getLocaleFolderPath("{STARTUP}", "st10Preferences.txt");
		app.zStringPathSpecifier = pathSpec;
		mySearchFolder = Folder.selectDialog(zc.get("$$$/Preferences/Panel/Folder/Title=Adobe Stock Photos Folder Location") + ":");
		app.zStringPathSpecifier = savePathSpec;
		if (mySearchFolder != null)
		{
			mySearchFolder = Folder(mySearchFolder.fsName + "/" + "AdobeStockPhotos").fsName;
			dlg.folderPanel.myFolder.text = stockphoto.prefs.GetTruncatedFolderPath(mySearchFolder);
				 
			dlg.folderPanel.myFolder.fulltext = mySearchFolder;
		}
		dlg.layout.layout(true);
	};
	dlg.folderPanel.folderOpBtns.resetBtn.onClick = function ()
	{
		mySearchFolder = stockphoto.prefs.GetMyFolder(true);
			 
		dlg.folderPanel.myFolder.text = stockphoto.prefs.GetTruncatedFolderPath(mySearchFolder);
		dlg.folderPanel.myFolder.fulltext = mySearchFolder;
	};

// Purchasing & Downloading Panel
	var purchasePanelBounds;
	var itemInShoppingCart;
	
	if ((app.preferences.bm_ShoppingCartCount == undefined) || (app.preferences.bm_ShoppingCartCount == 0))
	{
		itemInShoppingCart = false;
	}	
	else
	{
		itemInShoppingCart = true;
	}	

	dlg.purchasePanel = dlg.add( 'panel', undefined, zc.get("$$$/Preferences/Panel/Purchase/Title=Purchasing and Downloading"));
	dlg.purchasePanel.orientation = 'column';
	dlg.purchasePanel.alignChildren = 'left';

	// Billing country/regsion	
	dlg.purchasePanel.billing = dlg.purchasePanel.add('group');
	dlg.purchasePanel.billing.orientation = 'row';
	
	dlg.purchasePanel.billing.regionTitle = dlg.purchasePanel.billing.add( 'statictext', undefined, zc.get("$$$/Preferences/Panel/Purchase/Region=Billing Country or Region:"));
	
	dlg.purchasePanel.billing.region = dlg.purchasePanel.billing.add( 'dropdownlist', [RegionBegin-5, RawBegin, RegionBegin + RegionWidth -15 , RawBegin + RawHeight], stockphoto.prefs.regions);
	dlg.purchasePanel.billing.region.selection = stockphoto.prefs.GetBillingCountryIndex();
	if (dlg.purchasePanel.billing.region.selection == null)
	{
		dlg.purchasePanel.billing.region.selection = 0;
	}
	dlg.purchasePanel.billing.region.onChange = stockphoto.prefs.OnCurrencyRegionListChange;

	var currencyTypeStr = stockphoto.prefs.GetCurrencyTypeFromRegion(dlg.purchasePanel.billing.region.selection);
	dlg.purchasePanel.billing.currencyType = dlg.purchasePanel.billing.add( 'statictext', [RegionBegin + RegionWidth + 4, RawBegin,  subPanelRight - 18, RawBegin + RawHeight], zc.get("$$$/Preferences/Panel/Currency/CurrencyType=Currency=") + currencyTypeStr);

	// only enable the region dropdown list if shopping cart is empty
	if (!itemInShoppingCart)
	{
		dlg.purchasePanel.billing.region.enabled = true;
	}
	else
	{
		dlg.purchasePanel.billing.region.enabled = false;
		dlg.purchasePanel.add( 'statictext', undefined , zc.get("$$$/Preferences/Panel/Currency/CurrencyNote=To change this setting, you must first empty your shopping cart."));
	}
	
	dlg.purchasePanel.messageAfterCompCheck = dlg.purchasePanel.add( 'checkbox', undefined, zc.get("$$$/Preferences/Panel/Purchase/AfterComp=Display Message after Downloading Comp"));
	dlg.purchasePanel.messageAfterAddCheck = dlg.purchasePanel.add( 'checkbox', undefined, zc.get("$$$/Preferences/Panel/Purchase/AfterAdd=Display Message after Adding Image to Shopping Cart"));
	dlg.purchasePanel.autoDownloadCheck = dlg.purchasePanel.add( 'checkbox', undefined, zc.get("$$$/Preferences/Panel/Purchase/AutoDownload=Auto-download Images after Purchasing Them"));
	dlg.purchasePanel.messageBridgeLaunchCheck = dlg.purchasePanel.add( 'checkbox', undefined, zc.get("$$$/Preferences/Panel/Purchase/Resume=Resume Interrupted Downloads When Bridge Starts"));

	dlg.purchasePanel.messageAfterCompCheck.value = stockphoto.core.ui.getAlertDisplayPrefs("MessageAfterDownloadComp");
	dlg.purchasePanel.messageAfterAddCheck.value = stockphoto.core.ui.getAlertDisplayPrefs("MessageAfterAddingImageToCart");
	dlg.purchasePanel.autoDownloadCheck.value = stockphoto.core.ui.getAlertDisplayPrefs("AutoDownloadImagesAfterCheckout");
	dlg.purchasePanel.messageBridgeLaunchCheck.value = stockphoto.core.ui.getAlertDisplayPrefs("ResumeDownloadsWhenBridgeStarts");

	// Clear Purchase list
	dlg.purchasePanel.clearPurchaseImg = dlg.purchasePanel.add('group');
	dlg.purchasePanel.clearPurchaseImg.orientation = 'row';
	
	var clearNowString = zc.get("$$$/Preferences/Panel/Purchase/ClearNow=Clear purchased images from the Download Status:");
	var clearNowBtnString = zc.get("$$$/Preferences/Panel/Purchase/ClearBtnNow=Clear Now");
	
	dlg.purchasePanel.clearPurchaseImg.clearListText = dlg.purchasePanel.clearPurchaseImg.add( 'statictext', undefined, clearNowString);
	dlg.purchasePanel.clearPurchaseImg.clearListButton = dlg.purchasePanel.clearPurchaseImg.add( 'button', undefined, clearNowBtnString, { alignment:['center','top'] } );

	dlg.purchasePanel.clearPurchaseImg.clearListButton.onClick = function()
	{
		if (stockphoto.DL.alertDialog(5000, 0, 2, undefined) == 1)
		{
			stockphoto.DL.doclearAllItemsInList();
		}
	}
	
// Adding proxy Panel
	if( "undefined" != typeof(stockphoto.impl) &&  "undefined" != typeof(stockphoto.impl.ProxyExists()) && 1 == stockphoto.impl.ProxyExists() )
	{
		dlg.ProxyPrefButton = dlg.add('button', undefined, stockphoto.prefs.ProxyButtonTitle);
		dlg.ProxyPrefButton.alignment = 'left';

		dlg.ProxyPrefButton.onClick = function()
		{
			this.enabled = false;
			stockphoto.core.ShowProxyDialog(false);
			this.enabled = true;
		}
	}
	
	dlg.layout.layout(true);
}

stockphoto.prefs.Preference_Body = function(dialog) 
{
	stockphoto.prefs.PreferenceMain(dialog);
}

stockphoto.prefs.getProviderTabCount = function()
{
	if (app.preferences.bm_ProviderTabCount == undefined)
	{
		app.preferences.bm_ProviderTabCount = 0;
	}
	return app.preferences.bm_ProviderTabCount;
}

stockphoto.prefs.setProviderTabCount = function(iValue)
{
	app.preferences.bm_ProviderTabCount = iValue;
}

stockphoto.prefs.incrementProviderTabCount = function ()
{
	if (app.preferences.bm_ProviderTabCount == undefined)
	{
		app.preferences.bm_ProviderTabCount = 0;
	}
	else
	{
		app.preferences.bm_ProviderTabCount += 1;
	}
}

stockphoto.prefs.setSurveySwitch = function (bSurveyFlag)
{
	if (stockphoto.prefs.getSurveySwitch() != undefined && stockphoto.prefs.getSurveySwitch() != bSurveyFlag)
	{
 		// this means the flag is changed from false to true
		// So reset the providerTabCount to zero
		stockphoto.prefs.setProviderTabCount(0);
	}
	app.preferences.bm_SurveyFlag = bSurveyFlag;
}

stockphoto.prefs.getSurveySwitch = function ()
{
	return app.preferences.bm_SurveyFlag;
}

stockphoto.prefs.getSurveySubmitted = function ()
{
    if (app.preferences.bm_SurveySubmitted == undefined)
    {
        app.preferences.bm_SurveySubmitted = 0;
    }
    
	return app.preferences.bm_SurveySubmitted;
}

stockphoto.prefs.setSurveySubmitted = function (iSurveySubmitted)
{
    app.preferences.bm_SurveySubmitted = iSurveySubmitted;
}

if( ( stockphoto.timeKeeperOn != undefined )&&( stockphoto.timeKeeperOn ) )
{
	stockphoto.core.util.edTime( stockphoto.DL.jsx_Preference );
}

