//-----------------------------------------------------------------
// Licensed Materials - Property of IBM
//
// WebSphere Commerce
//
// (C) Copyright IBM Corp. 2008, 2011 All Rights Reserved.
//
// US Government Users Restricted Rights - Use, duplication or
// disclosure restricted by GSA ADP Schedule Contract with
// IBM Corp.
//-----------------------------------------------------------------

/** 
 * @fileOverview This file provides the common controller variables and functions, 
 * and links these controllers to listen to the defined render contexts in CommonContextsDeclarations.js.
 *
 * It is based on V7FEP3 Madisons.sar, MadisonsEnhancements.sar with the controllers not currently
 * in use removed.
 */

dojo.require("wc.render.common");

/** 
 * @class The CommonControllersDeclarationJS class defines all the common variables and functions 
 * for the controllers of the defined render contexts across all store pages.
 */
CommonControllersDeclarationJS = {
       /**
        * This variable stores the ID of the language that the store is currently using.
        * @private
        */
       langId: "-1",
       
       /**
        * This variable stores the ID of the current store.
        * @private
        */       
       storeId: "",
       
       /**
        * This variable stores the ID of the catalog that is used in the store.
        * @private
        */           
       catalogId: "",
       
       /**
        * This variable indicates whether the Ajax CheckoutOut flex flow is enabled or not.
        * @private
        */           
       ajaxCheckOut: true,
       
       /**
        * Sets the common ids used in the store - language id, store id, and catalog id.
        * 
        * @param {string} langId The id of the store language.
        * @param {string} storeId The id of the store.
        * @param {string} langId The id of the catalog used in the store.
        */
       setCommonParameters:function(langId,storeId,catalogId){
              this.langId = langId;
              this.storeId = storeId;
              this.catalogId = catalogId;
       },
       
       /**
        * Sets the URL of the specified controller.
        * 
        * @param {string} controllerId The id of the target controller.
        * @param {string} url The link to specify for the controller.
        */       
       setControllerURL:function(controllerId,url){
              wc.render.getRefreshControllerById(controllerId).url = url;
       }

};

/**
 * Declares a new render context for the AutoSuggest display.
 */
wc.render.declareContext("AutoSuggest_Context",null,"");

/**
 * Declares a new render context for the Cached Suggestions.
 */
wc.render.declareContext("CachedSuggestions_Context",null,"");

/** 
 * Declares a new refresh controller for Auto Suggest
 */
wc.render.declareRefreshController({
       id: "AutoSuggestDisplayController",
       renderContext: wc.render.getContextById("AutoSuggest_Context"),
       url: "",
       formId: ""

       /** 
        * Displays the keyword suggestions from the search index
        * This function is called when a render context changed event is detected. 
        * 
        * @param {string} message The render context changed event message
        * @param {object} widget The registered refresh area
        */
       ,renderContextChangedHandler: function(message, widget) {
              var controller = this;
              var renderContext = this.renderContext;
              widget.refresh(renderContext.properties);
       }

       /** 
        * Display the results.
        * 
        * @param {object} widget The registered refresh area
        */
       ,postRefreshHandler: function(widget) {
              var controller = this;
              var renderContext = this.renderContext;
			  var response = document.getElementById('suggestedKeywordResults');
			  if(response == null) {
			  	// No response or an error page.   Clear the contents.
			  	document.getElementById("autoSuggestDynamic_Result_div").innerHTML = "";
			  }
			  showAutoSuggestIfResults();
       }
});

/** 
 * Declares a new refresh controller for Cached Suggestions
 */
wc.render.declareRefreshController({
       id: "AutoSuggestCachedSuggestionsController",
       renderContext: wc.render.getContextById("CachedSuggestions_Context"),
       url: "",
       formId: ""

       /** 
        * Retrieves the cached suggestions used in the autosuggest box.
        * This function is called when a render context changed event is detected. 
        * 
        * @param {string} message The render context changed event message
        * @param {object} widget The registered refresh area
        */
       ,renderContextChangedHandler: function(message, widget) {
              var controller = this;
              var renderContext = this.renderContext;
              widget.refresh(renderContext.properties);
       }

       /** 
        * Updates the cached suggestions.
        * 
        * @param {object} widget The registered refresh area
        */
       ,postRefreshHandler: function(widget) {
              var controller = this;
              var renderContext = this.renderContext;
              var response = document.getElementById('cachedSuggestions');
              if(response == null) {
                     // No response or an error page.   Clear the contents.
                     document.getElementById("autoSuggestCachedSuggestions_div").innerHTML = "";
              }
              else {
                     var scripts = response.getElementsByTagName("script");
                     var j = scripts.length;
                     for (var i = 0; i < j; i++){
                            var newScript = document.createElement('script');
                            newScript.type = "text/javascript";
                            newScript.text = scripts[i].text;
                            document.getElementById('autoSuggestCachedSuggestions_div').appendChild (newScript);
                     }
                     retrievedCachedSuggestions = true;
                     var searchTerm = document.getElementById("SearchBox").value;
                     if(searchTerm.length > AUTOSUGGEST_THRESHOLD) {
                            doStaticAutoSuggest(searchTerm);
                     }
              }
       }
});


