// Endeca
// Helper functions for CoreMetrics
// Called when we first start processing a response from the server.
// Can/should be used to init data for this page view.
var endeca_tags={};

var ecatCmBeginProcessing = function() {

   endeca_tags['pcount'] = 0;
   endeca_tags['ccount']=0;
   endeca_tags['term']='';
   endeca_tags['filter_list']=  ''; 
   endeca_tags['current_page']=0;
   endeca_tags['page_count']=0;
}

// Set current search term for CM.
// "sterm" is the search term (string).
var ecatCmSetSearchTerm = function(sterm) {
       //console.log("TERM ",sterm);
   endeca_tags['term'] = sterm;
 
}

// Add another filter name to the CM list of filters.
// Will be called once for each filter used in the request.
// "fname" is the value of the filter.
var ecatCmAddFilter = function(dim,fname) {
      //console.log("Filter list ",fname,dim);
      //console.log("Existing Filters ", endeca_tags['filter_list']);
   var sf = {
      "Skin Type" : "Typ",
      "Skin Tone" : "Ton"
  };
  if (typeof sf[dim] != "undefined"){
    dim = sf[dim];
       //console.log("SF ran",dim);
  }else{
    var filter_words = dim.split(' ');
         //console.log("filter words ", filter_words); 
       if (filter_words.size() > 1){
            dim = filter_words[0].substr(0,3);
            for (i=1; i<=filter_words.size() - 1;i++){
              dim = dim + filter_words[i].substr(0,1);
            } 
       }else{
          dim = dim.substr(0,3);
       }
  } 
   if (fname){
      if (endeca_tags['filter_list'].length > 1 ){
        endeca_tags['filter_list'] = endeca_tags['filter_list'] +' > '+ dim + ':'+ fname;
       }else{
       endeca_tags['filter_list'] = dim + ':' +fname;
       }
   } 
}

// Called when we know how many results we have.
// Indicates total number of products and content items found.
var ecatCmResultCount = function(countProducts,countContent) {
   endeca_tags['pcount'] = countProducts;
   endeca_tags['ccount'] = countContent;
  console.log("ccount ", countContent);
  console.log("pcount ", countProducts);
}

// Called to indicate which page we're on and how many total pages.
var ecatCmPageNumber = function(currentPage,totalPages) {
   endeca_tags['current_page']=currentPage;
   endeca_tags['page_count']=totalPages;

}

// Called when we're done processing results.
// Can/should be used to send the actual page view tag
// using the data provided by the above calls.
var ecatCmFinishProcessing = function() {
  var page_count = endeca_tags['current_page'] || '1';
  var PAGE_ID = 'Search Results ' + page_count;
  //if (page_count < 1 && endeca_tags['ccount'] == 0){
   //   PAGE_ID = PAGE_ID + " BestSellers";
  //}
  if (endeca_tags['filter_list'].length > 0){
      PAGE_ID = 'Search Results Filtered ' + page_count;
  }
  var CATID = '2200';
  var KEYWORDS = endeca_tags['term'];

   if (endeca_tags['ccount'] > 0 && endeca_tags['pcount'] == 0){
           KEYWORDS = '*' + KEYWORDS;
   }

  var RESULTS = endeca_tags['ccount'] + endeca_tags['pcount'] || '0';
  
  cmCreatePageviewTag(PAGE_ID, KEYWORDS,CATID,RESULTS,endeca_tags['filter_list']);
   

}

var ecatCmContentClick  = function() {
  cmCreatePageElementTag("CONTENT", "SEARCH DROPDOWN");
}
var ecatCmProductClick = function() {
 cmCreatePageElementTag("PRODUCTS", "SEARCH DROPDOWN");
} 
var ecatCmSeeAll = function() {
  cmCreatePageElementTag("SEE ALL","SEARCH DROPDOWN");
}

// end Endeca

