/*
 * Copyright (C) 2008-2010 APN, LLC. All rights reserved
 * References to external sources embedded in the code.
 * 
 * Ask.com Toolbar - Options JavaScript module.
 * Author: Vishal V. Shah, Christine Hodges, FF team
 * Description: Contains toolbar options dialog processing and logic.
 * 
 * - Options are modeled as objects with a special API in ATB_Options.Model.getOptions
 * - MVC Note: ATB_Options itself fills some parts of the controller role. ATB_Options is the public (inter)face.
 * 
 * See also ATB.Core.showOptionsWindow (which checks hasLocaleChangedViaOptions)
 * TODO how to jsdoc this style? does it just work?
 */
var ATB_Options;if(!ATB_Options){ATB_Options={ATB:null,m:null,v:null,c:null}}ATB_Options.XUL_OPTIONS_PREFWINDOW="asktb-dialog-prefs";ATB_Options.Model=function(){};ATB_Options.Model.prototype.getOptions=function(){var b=ATB_Options.ATB,a=b.Utils.hitch;return[{controlId:"asktb-option-display-recent-searches-entry",initializeOption:ATB_Options.v.hideOptionFromFirefox2,getSavedValue:a(b.Prefs,b.Prefs.saveSearches),setSavedValue:a(b.Prefs,b.Prefs.setSaveSearches),getControlValue:ATB_Options.c.getCheckboxValue},{controlId:"asktb-option-clear-searches-on-exit-entry",initializeOption:ATB_Options.v.hideOptionFromFirefox2,getSavedValue:a(b.Prefs,b.Prefs.clearSearchesOnExit),setSavedValue:a(b.Prefs,b.Prefs.setClearSearchesOnExit),getControlValue:ATB_Options.c.getCheckboxValue},{controlId:"asktb-option-show-labels-entry",initializeOption:ATB_Options.v.logNoInitializationMessage,getSavedValue:a(b.Prefs,b.Prefs.showLabels),setSavedValue:ATB_Options.m.saveShowLabelsOption,getControlValue:ATB_Options.c.getCheckboxValue},{controlId:"asktb-option-enable-search-assist",initializeOption:ATB_Options.v.initializeCheckboxWithSavedValue,getSavedValue:a(b.Prefs,b.Prefs.isSAEnabled),setSavedValue:ATB_Options.m.saveSearchAssistOption,getControlValue:ATB_Options.c.getCheckboxValue},{controlId:"asktb-supported-locales-list",initializeOption:ATB_Options.v.populateMenuList,getSavedValue:a(b.Prefs,b.Prefs.getToolbarCountryOrRegion),setSavedValue:ATB_Options.m.saveCountryOrRegionOption,getControlValue:ATB_Options.c.getListValue,menuPopupId:"asktb-supported-locales-popup",getMenuListContent:a(b.Locale,b.Locale.getSupportedToolbarLocales)},{controlId:"asktb-supported-langs-list",initializeOption:ATB_Options.v.populateMenuList,getSavedValue:a(b.Prefs,b.Prefs.getToolbarLanguage),setSavedValue:ATB_Options.m.saveLanguageOption,getControlValue:ATB_Options.c.getListValue,menuPopupId:"asktb-supported-langs-popup",getMenuListContent:a(b.Locale,b.Locale.getSupportedToolbarLangs)}]};ATB_Options.Model.prototype.saveShowLabelsOption=function(d,c){var a=c.getSavedValue();if(a!=d){ATB_Options.ATB.Prefs.setShowLabels(d);ATB_Options.ATB.Core.updateToolbarLayoutPerPreferences();try{var e=ATB_Options.ATB.Utils.getAvailableBrowserWidth();if(d){ATB_Options.ATB.Events.handleTbButtonAreaNarrowing(e)}else{ATB_Options.ATB.Events.handleTbButtonAreaWidening(e)}}catch(b){ATB_Options.ATB.Logger.error("Error (de-)activating showLabels option: ",b)}ATB_Options.ATB.Logger.debug("Option CHANGED: Show labels option: ",c.controlId," changed to: ",d)}else{ATB_Options.ATB.Logger.debug("Options: NO CHANGE to show labels option: ",c.controlId," Value is: ",d)}return false};ATB_Options.Model.prototype.saveSearchAssistOption=function(c,b){var a=b.getSavedValue();if(a!=c){if(c){ATB_Options.ATB.Logger.debug("Option CHANGED: Enabling Search Assist.");ATB_Options.ATB.Prefs.setSAEnabledPref(true);ATB_Options.ATB.Utils.updateKeywordUrlForSearchAssist()}else{ATB_Options.ATB.Logger.debug("Options CHANGED: DISABLING Search Assist.");ATB_Options.ATB.Prefs.setSAEnabledPref(false);ATB_Options.ATB.Utils.unsetKeywordUrlForSearchAssist()}}return false};ATB_Options.Model.prototype.saveCountryOrRegionOption=function(b,a){return this._saveLocaleOption(b,a,ATB_Options.ATB.Prefs.setToolbarCountryOrRegion)};ATB_Options.Model.prototype.saveLanguageOption=function(b,a){return this._saveLocaleOption(b,a,ATB_Options.ATB.Prefs.setToolbarLanguage)};ATB_Options.Model.prototype._saveLocaleOption=function(d,c,a){var b=c.getSavedValue();if(b!=d){ATB_Options.ATB.Logger.debug("Option CHANGED: lang/locale changing from: ",b," to: ",d);a.call(ATB_Options.ATB.Prefs,d,"options");return true}return false};ATB_Options.onloadOptionsDialog=function(c){ATB_Options.ATB=(typeof window.opener.ATB=="undefined")?window.opener.opener.ATB:window.opener.ATB;ATB_Options.ATB.Logger.info("Loading Options dialog. Initializing localization specific attributes. Current locale: ",ATB_Options.ATB.Prefs.getToolbarLocale());ATB_Options.m=new ATB_Options.Model();ATB_Options.c=new ATB_Options.Controller();ATB_Options.v=new ATB_Options.View(ATB_Options.c,ATB_Options.m);var a=ATB_Options.m.getOptions();for(var b=0;b<a.length;b++){a[b].initializeOption.call(ATB_Options.v,a[b])}ATB_Options.ATB.Locale.updateLocaleSpecificAttributes(null,ATB_Options.XUL_OPTIONS_PREFWINDOW,document,ATB_Options.ATB.Utils.getDocObjHostingATB());ATB_Options.ATB.Logger.info("DONE Loading Options dialog.")};ATB_Options.onunloadOptionsDialog=function(a){ATB_Options.ATB.Logger.info("Unloading Options dialog. Ok, DONE.")};ATB_Options.saveOptions=function(){ATB_Options.ATB.Logger.info("Options OK button clicked. Saving changes...");var e=false;for(var c=0,b=ATB_Options.m.getOptions();c<b.length;c++){var d=b[c];try{var f=d.getControlValue.call(ATB_Options.c,d);ATB_Options.ATB.Logger.debug("Processing option pref: ",d.controlId," Value: ",f);e=d.setSavedValue.call(ATB_Options.m,f,d)||e}catch(a){ATB_Options.ATB.Logger.warn("Error changing option change for: ",d.controlId,". ERROR: ",a)}}if(e){ATB_Options.c.executeSoftRestart()}else{ATB_Options.ATB.Logger.debug("No (soft) restart needed after saving Option changes")}ATB_Options.ATB.Logger.info("DONE Saving Options changes. Closing Options window.");if(ATB_Options.ATB.Observer){ATB_Options.ATB.Observer.publish(ATB_Options.ATB.Observer.Events.OPTIONS_SAVE)}window.close()};ATB_Options.cancelOptions=function(){ATB_Options.ATB.Logger.info("Options CANCEL button clicked. Closing Options window.");window.close()};ATB_Options.restoreOptionsDefaults=function(){};ATB_Options.getOptionsHelp=function(b,a){ATB_Options.ATB.Logger.info("Options help button clicked..");var c=ATB_Options.ATB.Utils.getWindowObjHostingATB(),d=ATB_Options.ATB.Core.getHelpUrl(ATB_Options.ATB.Utils.getDocObjHostingATB());if(d){if(a&&typeof(a)!="undefined"&&a.length>0){d+=("#"+a)}ATB_Options.ATB.Logger.info("Identified help url from toolbar.xul and using the specific entry attribute: ",d);ATB_Options.ATB.Core.navigateToUrl(b,d,c)}else{ATB_Options.ATB.Logger.error('Unable to retrieve the oncommand attribute of item with id "asktb-help-item" in toolbar.xul. It is used to retrieve the faq url. Resetting it to http://www.ask.com..');ATB_Options.ATB.Core.navigateToUrl(b,"http://www.ask.com",c)}window.close()};ATB_Options.get=function(a){return document.getElementById(a)};ATB_Options.Controller=function(){};ATB_Options.Controller.prototype.executeSoftRestart=function(){ATB_Options.ATB.Logger.info("executeSoftRestart: Popping new window!");var f=ATB_Options.ATB.Utils.getWindowObjHostingATB(),e=this._getTabs(f);ATB_Options.ATB.LifeCycle.markMasterAsDying();if(e.length>0){f.open(e[0])}else{f.open()}ATB_Options.ATB.Logger.info("Identified parent tabs: ",e);if(e.length>1){var d=Components.classes["@mozilla.org/appshell/window-mediator;1"].getService(Components.interfaces.nsIWindowMediator),a=d.getMostRecentWindow("navigator:browser");ATB_Options.ATB.Logger.info("Identified main window: ",a);if(a&&a.getBrowser){var c=a.getBrowser();for(var b=1;b<e.length;++b){ATB_Options.ATB.Logger.info("Opening tab for url: ",e[b]);c.addTab(e[b])}}}};ATB_Options.Controller.prototype._getTabs=function(f){ATB_Options.ATB.Logger.info("_getTabs: Collecting current tabs");var b=[],d=f.getBrowser();for(var e=0;e<d.browsers.length;e++){var c=d.getBrowserAtIndex(e),a=c.currentURI.spec;if(a&&a.length>0){ATB_Options.ATB.Logger.debug("Identified url (parent tab): ",a);b.push(a)}}return b};ATB_Options.Controller.prototype.getCheckboxValue=function(a){var b=ATB_Options.get(a.controlId);if(b){return b.checked}return null};ATB_Options.Controller.prototype.getListValue=function(a){var b=ATB_Options.get(a.controlId);if(b){return b.selectedItem.value}return null};ATB_Options.View=function(a,b){this._controller=a;this._model=b};ATB_Options.View.prototype._controller=null;ATB_Options.View.prototype._model=null;ATB_Options.View.prototype.hideOptionFromFirefox2=function(a){ATB_Options.ATB.Logger.debug("Initializing Option: ",a.controlId," -- Hide this option if FF 2.x");if(ATB_Options.ATB.Utils.getBrowserVersion()<3){ATB_Options.get(a.controlId).style.display="none"}};ATB_Options.View.prototype.initializeCheckboxWithSavedValue=function(b){ATB_Options.ATB.Logger.debug("Initializing Option: ",b.controlId," -- initializeCheckboxWithSavedValue");var c=ATB_Options.get(b.controlId),a=b.getSavedValue();if(c&&(a!=null)){c.checked=a}};ATB_Options.View.prototype.populateMenuList=function(g){var d=null,b=ATB_Options.get(g.menuPopupId),a=g.getSavedValue(),f=g.getMenuListContent();ATB_Options.ATB.Logger.debug("Initializing Option: ",g.controlId," Populating menu with current selection: ",a," and supported values: ",ATB_Options.ATB.Utils.jsonStringify(f));for(var e in f){var c=document.createElement("menuitem");c.setAttribute("label",f[e]);c.setAttribute("value",e);b.appendChild(c);if(e==a){d=c}}var h=ATB_Options.get(g.controlId);if(h){h.selectedItem=d;ATB_Options.ATB.Logger.debug("DONE populating menu: ",g.controlId,". Selected menuitem: ",h.selectedItem.getAttribute("label"))}};ATB_Options.View.prototype.logNoInitializationMessage=function(a){ATB_Options.ATB.Logger.debug("Initializing Option: ",a.controlId," -- (no special init needed)")};