

/////////////////////////////////
// Retail locator behavior w/ multimap
// author paul irish
////////////////////////////////

/*
TODO:
- re-enable verbose inputs..

*/

/*
* Coremetrics: Sending page view and event conversion tags
* using country variable (declared in the template) to get country from ${userLocale.country.ISO};
* Sending page view tags for successful or unsuccessful search results
* Sending conversion tag for user searches
*/

// searcher is a global.

	

var locator = {
  rfs               : ['client_id', 'name', 'street', 'town', 'pc','state', 'lat', 'lon','GeoQuality','country','telephone'], // fields we want to get back from the web service.
  results_per_page  : 5,
  maxResults        : 25,
  mapviewer         : null, // set in init()
  markers           : [],
  highlightColor    : '#99c6ea',
  requested_page    : 1, // default
  isInputVerbose    : false,
  geothreshold      : 8,
  livequeryCount    : 0,
  isFirstTime       : true,
  isFormEnabled     : true,
  lastUsedAddy      : null, // set in resultsLoaded()

  urlquerykeys      : ['address1','cc','rad'],  // MUST MATCH whats coming into the URL from the serverside
  elemLookup        : { address1 : '#map-address', cc : '#map-country', rad : '#map-radius' },
  
  multipleMatchFirstResult : null, // holds the first result in a multiple match for re-searching.
  
  fixCountryCode        : function(code){ 
                            code = code.toLowerCase();
                            if (code == 'hk'){
								if(RBK.i18n.retailLocator.languageCode == 'zh') return 'cn_03'
								 else return 'hongkong';  // cuz there was a db name conflict.. this gets a diff name
							}  // cuz there was a db name conflict.. this gets a diff name
							
                            if (code == 'gb') return 'uk';  // tweak
							if (code == 'cn') return 'cn_03'; // For China
                            else return code;
                          },

  
  /*
  isGeoQualityGood()
  o	Location with geoquality of 0,7: the location will not appear in search results nor be on the map.
  o	Location with geoquality of 1,2,8: the directions button will read "Get Directions to Store". 
  o	Location with geoquality of 3,4,5,6,9: the directions button will read "Get Directions to City". 
  */
  isGeoQualityGood      : function(record) { var qual = parseInt(record.GeoQuality,10); return ( qual == 1 || qual == 2 || qual == 8 || qual == 9) },
  isCountrySame         : function(record) { return (record.country == $('#map-country option:selected').text() || record.country == $('#map-country').val() );  }, 
  
  
  get_result_container  : function(){ document.getElementById('mapresults').innerHTML="";return document.getElementById('mapresults')},
  get_search_radius     : function(){ return parseInt(  $('#map-radius').val()  ) },
  get_search_addy       : function(){ return new MMAddress({ qs : $('#map-address').val(), country_code : $('#map-country').val() })  },
  get_verbose_addy      : function(){ return new MMAddress({ street : $('#street-address').val(), city : $('#city-town').val(), region : $('#state-province-input').val(), postalCode : $('#zip-code').val(), country_code : $('#map-country').val() })  },
  
  
  init : function(){ // fired with MMAttachEvent( window, 'load')..
  

        locator.mapviewer = new MultimapViewer( document.getElementById( 'mapviewer' ) );
        //Do not display map while loading the page, should be displayed only when user searches for something.
        
         $('#mapviewer').hide();
	$('#mapimage').show();
          
  }
  
};



locator.redoSearch = function(){
  
  $('#loading').show();
  

  
  $.each(locator.markers, function(){ // remove the existing location markers
    locator.mapviewer.removeOverlay( this );
  });
  
  locator.initSearch();
  
  return false;
}

locator.initSearch = function () {
  
 	 $('#mapviewer').show();
	$('#mapimage').hide();
	
    $('#results #directions-destination').hide().appendTo('#directions-destination-table tbody:first'); // move it so we can get it back.
  
    $.each(locator.markers, function(){ // remove the existing location markers
      locator.mapviewer.removeOverlay( this );
    });
    
    $('#error,#geo-error,#timeout-error').hide(); 
    
    $('#noresults').remove(); 
       
    
    locator.cleanUp('locations');
     $('div.pagination,div.pagingstatus').hide();
    searcher = new MMSearchRequester( locator.resultsLoaded );
    
    // Set return fields and maximum distance to search within:
    var search = new MMSearch(); 
    search.return_fields = locator.rfs;
    //search.distance_units = 'miles'; 
    search.max_distance = locator.get_search_radius();  //disable radius for now. multimap has 1000km radius built in.
    search.compoundThreshold  = locator.maxResults;

     
    // Specify the address we will search around
    // if the verbose guys are visible, use them, otherwise use the qs 
    locator.isInputVerbose = $('fieldset.verbose:first').is(':visible');
    
    
    
    function hideMap(bool){
      bool && $('#mapviewer').hide();
      !bool && $('#mapviewer').show();
      search.compoundThreshold = searcher.maxNumResults = bool ? 400 : locator.maxResults; // lots more results if CJK RH.
      search.count = searcher.resultsPerPage = bool ? (locator.results_per_page * 2) : locator.results_per_page;
    }
    
    var currentCountry = $('#map-country').val();
    
    //PS: Supress Maps for HK,CN,RU,KR
    //For these countries do not show the map. we will just view the listing of stores.
    var supressCounty = 'false';
    if((currentCountry == 'HK') || (currentCountry == 'CN') ||(currentCountry == 'RU') ||(currentCountry == 'KR') ){
    	supressCounty = 'true';
    }
    
    if (false && currentCountry.match(/KR|CN|RU|HK/) ) { // if CJK, we do a non-spatial filtered search (TEMPORARILY DISABLED)
      
      hideMap(true);
      
      var lookUp = { JP:'Japan', KR : 'KR', CN : 'China', RU : 'Russia', HK : 'HK' }; // cuz the val()s dont match what's in the DB
      search.filters = [ new MMSearchFilter('country', 'eq', lookUp[currentCountry] ), new MMSearchFilter('lon', 'not', '0.000000' ) ];  // don't map unmappables.
      search.logic = 'AND';

    }else if (locator.multipleMatchFirstResult){                    // if we're searching again with the first result from multiple matches.
      hideMap(false); 
      search.point = locator.multipleMatchFirstResult;
      locator.multipleMatchFirstResult = null;
    }else{                                                    // business as usual
      hideMap(false);
      search.address =  locator.isInputVerbose ? locator.get_verbose_addy() : locator.get_search_addy();      
      locator.lastUsedAddy = search.address;  
    }
    //PS: Supress Maps for HK,CN,RU,KR
    if(supressCounty == 'true'){
       	     	
       	     	 $('#mapviewer').hide();
	

    }
    var country = locator.fixCountryCode(locator.multipleMatchCountry || search.address.country_code); // fix for hongkong
    var closeCountries = locator.getCloseCountries(country); // returns an array
    for (var x in closeCountries){       closeCountries[x] = 'mm.clients.rbk_easytone_'+closeCountries[x].toLowerCase();    }  //prepend database name prefix
    search.data_source = closeCountries.join(',');    
    
    
    // look in URL for a datasource override
    // something like: http://qa.reebok.com/US/retail-locator?DS=rbk_spaceracerz
    var qk = parseUri(document.location.href).queryKey
    if (qk.DS) search.data_source = 'mm.clients.'+qk.DS;
       
    locator.multipleMatchCountry = null; // clear this after each use.
    
    // Determine the start index to be used.
    // We take selected page number less one, multiply this by the results per page, and then add one:
    search.start_index =  ( ( locator.requested_page - 1 ) * searcher.resultsPerPage )    + 1 ;
    search.start_index = parseInt(search.start_index);
    
    
    locator.timeoutTimer.start(); // protect against MM's slow server
    
    searcher.search( search );
   
}  




locator.resultsLoaded = function( ) {
    
    locator.timeoutTimer.clear();
            
    
    $('#mapresults').show(); // needed only when app bugs out.
    $('#timeout-error').hide(); // probably hidden but can sometimes be there..
      
    // If an error code has been produced, display the explanation:
    if ( searcher.error_code ) {
        
         //Coremterics: since the error has occured we assume unsuccessful search and send a page view tag
        cm.createPageViewForRetailLocatorSearch(country+":Retail Locator:Unsuccessful","/"+ country+"/retail");
        
        // if geocoding failed then we'll break out the input to multiple things.
        // error codes listed: http://www.multimap.com/share/documentation/api/1.2/web_service/generalerrorcodes.htm
        if ((/MM_GEOCODE_FAILED/).test(searcher.error_code)){ 
          $('#error').show();
          //locator.createVerboseInputs(); disable this for now....
          return;
        }
        
        // if multiple matches.. search again with the first result. :)
        if ((/MM_GEOCODE_MULTIPLE_MATCHES/).test(searcher.error_code)){ 
          
          var point = searcher.result_set[0]; // grab the first one.
          locator.multipleMatchFirstResult = new MMLatLon( point.coords.lat, point.coords.lon );
          locator.multipleMatchCountry = point.address.country_code;
          $('#loading').show();
          locator.initSearch(); // do it again!
          return;
        }
        
        var err =  '';
        if ( searcher.error_explanation ) { err =  searcher.error_explanation;  } 
        else {  err =  searcher.error_code; }
        
        $('#geo-error').show();
        return;
        
    } 
    
    /*  data source specific errors.. i dont think we want this..
    
    // If an error was returned for the record set, display details and return:
    if ( searcher.all_record_sets[0].error ) {
        var err;
        if ( searcher.record_sets[0].error.error_explanation ) {  err =  searcher.record_sets[0].error.error_explanation; } 
        else { err =  RBK.i18n.retailLocator.errorCodeMsg + searcher.record_sets[0].error.error_code;  }
    
        //Coremterics: since the error has occured we assume unsuccessful search and send a page view tag
        cm.createPageViewForRetailLocatorSearch(country+":Retail Locator:Unsuccessful","/"+ country+"/retail");
        $('#geo-error').show();
        return;  
    }
    */
     
    // if you are here, there is no error.


    // Determine the index of our first returned record, based on page number and results per page:
    var start_index_value = ( Number(searcher.resultsPerPage) * ( Number(locator.requested_page) - 1 ) ) + 1;      
    var results_returned = 1;
    
    var $result_table = $('<table id="results" class="txt_normal StoreLocator_ResultTable"/>');
    
    // Start our numbering of results at the start index:
    $result_table.attr('start',start_index_value);
    
    
    // lets merge any record_sets into one
    searcher.all_records = [];
    searcher.total_record_count = 0;
    
    for (var j = 0; j< searcher.record_sets.length; j++){
      var rArr  = j;
      if (searcher.record_sets[rArr].records && searcher.record_sets[rArr].records.length){    // make sure that we actually have results...
        searcher.all_records = searcher.all_records.concat( searcher.record_sets[rArr].records );
        searcher.total_record_count += searcher.record_sets[rArr].total_record_count;
      }
    }
      

    // check to see if individual records have been returned:
    if ( searcher.all_records.length ) {
        // we got results! show em.      
        
        //$result_table.append( locator.createResultsHeader() ); 
        
        /* does not work.. :)
        // sort them all by distance
        searcher.all_records.sort(function(a, b) {
            var x = a.distance.km;
            var y = b.distance.km;
            return ((x < y) ? -1 : ((x > y) ? 1 : 0));
        });
        */
        
        //Coremetrics: tags sent for a conversion event tag
        cm.createEventCompletionForRetailLocator();
        
        //Coremterics: since no error returned, we set successful page view tag
        cm.createPageViewForRetailLocatorSearch(country+":Retail Locator:Successful","/"+ country+"/retail");
        
       
        // Loop through each record in the record set, and add it to the list below the map,
        //  and populate the infobox text:           
        for (var record_count = 0, rl = searcher.all_records.length; record_count < rl; record_count++ ) 
        {
            var $tbody = $('<tbody/>').addClass((record_count % 2) ? 'StoreLocator_ResultEvenRows' : 'StoreLocator_ResultRowHeight').appendTo($result_table);
            
            var record = searcher.all_records[record_count];
                             
            var resultrow = locator.handleRecord(record, start_index_value + record_count);
            
            $tbody.html(resultrow);
            
            $result_table.append($tbody);
           
        }
        
        
        locator.setUpPoorGeocodeTooltips('#mapviewer div.MMMarker');
              
        $('#printpage').show();
        
    } else {
    	//Coremterics: since no records returned we assume unsuccessful search and send a page view tag
        cm.createPageViewForRetailLocatorSearch(country+":Retail Locator:Unsuccessful","/"+ country+"/retail");
        	 
        // No records have been returned. Display a message noting that no records were returned by the search:        
       
        $("<p id='noresults' />").text(RBK.i18n.retailLocator.noResults).prependTo( locator.get_result_container() );
        results_returned = 0;

    }
    
    
    $('#loading').hide();
    
    if ( searcher.all_records.length ){  
    
      
        // add the table to the DOM. Then: clicking the business name reveals the infobox.
       
        	
		        $( locator.get_result_container() ).append(locator.createResultsHeader()).append($result_table)
		          .find('tr td:first-child')
		            .find('a')
		              .each(function(i,elem){
		                $(elem).click(function(){                   
		                       locator.openInfoBox ( 'click', locator.markers[i] ); // assume each() gets them sequentially.
		                       return false; 
		                  });  
		              });
                             
        
        if ( results_returned == 1 ) {
            locator.mapviewer.goToPosition ( locator.mapviewer.getAutoScaleLocation( locator.markers ) ); 
         }  
         
         // set up the pagination
         locator.num_results = searcher.total_record_count > searcher.maxNumResults ? searcher.maxNumResults : searcher.total_record_count;
         
         $('div.pagination')
            .show()
            .makePagination( {
                  records_per_page  : searcher.resultsPerPage,
                  total_records     : locator.num_results, // set our own maxresults.
                  requested_page    : locator.requested_page    
            });
         var records = searcher.all_records;
         $('div.pagingstatus').show().html('Showing '+records[0].counter + '-'+records[records.length-1].counter+' of '+ locator.num_results + ' locations');   // more counter stuff.. uh oh.
           
        
    }     // end of if (has records) 
  } // end of locator.resultsLoaded()



locator.unHighlightRow = function(type,overlay){ 
  var count = overlay.getAttribute('count');
  $('#results tbody').eq( count ).find('td').css({backgroundColor: ''}); // clear any highlight for the correct row
  $('#results td').css({backgroundColor: ''}); // clear any highlight at all.. this is sometimes neccesary...
  

};

locator.markInfoBox = function(type,overlay,marker){  // called when an infobox Opens.
  for (obj in marker){
    if (typeof marker[obj] == 'object' && marker[obj].className && marker[obj].className == "MMMarker"){  // walk the object.
      var num = $(marker[obj]).text();
      var newcount = (num-1) % locator.results_per_page; // normalize number to be 0 through 4;
      overlay.setAttribute('count',  newcount); // setting an attribute on the object called count.
      
      if ($.browser.msie && $.browser.version < 7) {
          $('#results tbody').eq(newcount).find('td').css({ backgroundColor : '#99c6ea' });
          break;
      } 
      
      if ($.browser.msie && $.browser.version < 7) break; // don't animate for ie6.
      
      $('#results tbody').eq(newcount).find('td').animate({ backgroundColor : '#99c6ea' }, 1400); // and animate that row.
      break;  
    }
  }
};

locator.createVerboseInputs = function(){
  $('#map-search fieldset.terse').hide();
  $('#map-search fieldset.verbose').removeClass('hidden').show();
  $('#error').show();
  
  var verboseFields = $('#map-search fieldset.verbose input');
  
  // this splits up the original query by comma and throw it into the new boxes.
  $.each( 
    $('#map-address').val().split(','), 
    function(i,str){  verboseFields.eq(i).val(str); }
  );

}


locator.handleRecord = function( record, num ) {
  	
  	// Pavitra: Added variable to distinguish between Asian Countries (Japan,Hongkong,Korea,China)
	var market = '';
	
	if ((record.country == 'JP') ||(record.country == 'KR') ||(record.country == 'HK') ||(record.country == 'China')) {
	
		market = 'Asian';
		
	} 
  
    // Create marker text for the infobox for this record:    
    var markerText = '<h1>' + record.name + '<' + '/h1>'; 
    markerText += '<div class="adr">' +  locator.writeAddressHtml(record) + '</div>';    
    
    //Pavitra: For Asian Markets, as well as for markets with poor Geo Code do not show distance when the address is displayed on the map.
    if ((market != 'Asian')&& (locator.isGeoQualityGood(record))){
	    if (RBK.i18n.retailLocator.isKm && record.distance) {
	        markerText += '<p>' + RBK.i18n.retailLocator.distance + ': '+ record.distance.km +' '+RBK.i18n.retailLocator.kilometers +'</p>';
	    } else if (record.distance) { // probably mi
	        markerText += '<p>' + RBK.i18n.retailLocator.distance + ': '+ record.distance.miles +' '+RBK.i18n.retailLocator.miles +'</p>'; 
	    }
    }
    if ( record.point ) {
        var marker = locator.createMarker( new MMLatLon(record.point.lat, record.point.lon), markerText, num, locator.isGeoQualityGood(record) );
        locator.markers.push(marker);
    } 
    

    return locator.createResultRow(record, marker);
      
} // EO handlerecord()


locator.writeAddressHtml = function(record){
  var cb = locator.checkblanks;
  
  var adrHTML = cb(record.street)+'<br>';
  if (record.country != 'KR'){
        adrHTML += cb(record.town)+'<br>'+cb(record.state)+","+cb(record.pc);
  }
  //if (! locator.isCountrySame(record)) {  adrHTML += '<div class="country-name">'+cb(record.country)+'</div>'; }
  return adrHTML;
}



locator.createResultRow = function(record, marker){
	
	// Pavitra: Added variable to distinguish between Asian Countries (Japan,Hongkong,Korea,China)
	var market = '';
		
	if ((record.country == 'JP') ||(record.country == 'KR') ||(record.country == 'HK') ||(record.country == 'China')) {
	
		market = 'Asian';
		
	} 
	
   
    function roundToTens(num){ // rounding because multimap's hundreds place is just too much
      return (Math.round(num*10)/10);
    }   
   
    var cb = locator.checkblanks; // shorthand
   
    var rowStr = 
    '<tr class="location-result">'+
      '<td class="StoreLocator_Number"><a class="NUMbutton"><span>'+ record.counter +'</span></a></td>'+
      '<td class="StoreLocator_AdressDetails" ><b>'+cb(record.name)+'</b><br>'+locator.writeAddressHtml(record);
      rowStr += '</td>'+
      '<td class="Storelocator_ContactNumber" >'+
        cb(record.telephone)+
      '</td>'  +
      '<td class="StoreLocator_DirectionDetails" >'; 
        
        //Pavira: As per business requirement directions should be shown only for non Asian Countries.
        //Pavitra: Modified the table view to hide the distance if the GeoQuality is not good.
        if (market != 'Asian')
        {
           if (locator.isGeoQualityGood(record)){
		        
		        if (RBK.i18n.retailLocator.isKm && record.distance) {
		            rowStr += cb(roundToTens(record.distance.km)) +   
		            ' '+RBK.i18n.retailLocator.kilometers +'';
		        } else if (record.distance) { 
		            rowStr += cb(roundToTens(record.distance.miles))+
		            ' '+RBK.i18n.retailLocator.miles +''; 
		        }
	        } else {
	        	rowStr +='';
	        }
	        // show the Directions link IF:
	        // 1. the geoquality is good
	        // 2. the map is visible
	        // 3. if its india.. don't even show anything
	        // 4. For Asian countries (Japan,Korea,China,HK) don't show anything.
	        if (record.country != 'India'){
	            if ( locator.isGeoQualityGood(record) &&  $('#mapviewer').is(':visible') && !(record.GeoQuality==8 && record.country == 'India') ) {   
	            rowStr += '<br><A href="#" onclick="return routing.viewDirectionsFor(this,'+record.counter+')">'+RBK.i18n.retailLocator.getdirStore+'</a>'; 
	          } else {
	            rowStr += RBK.i18n.retailLocator.poorGeocodeDirections;  
	          }
	        }
	    }
	     else 
	     {
	     	
	    	rowStr += RBK.i18n.retailLocator.poorGeocodeDirections;
	    }
      rowStr += '</td></tr>';
    return rowStr;
}


locator.checkblanks = function(str){
  if (str !== '' && str !== undefined){
    if (str == 'KR') str = 'Korea';
    return str;
  } 
  else{
    return '';
  }
}

locator.createResultsHeader = function(){
    
    var headerStr = 
    '<div class="searchResultTitle">'+
      '<div class="searchResultTitleContent StoreLocator_Map" >'+RBK.i18n.retailLocator.srchresultMap+'</div>'+
      '<div class="searchResultTitleContent StoreLocator_NameAddress" >'+RBK.i18n.retailLocator.srchresultNameAdd+'</div>'+
      '<div class="searchResultTitleContent StoreLocator_ContactInfo" >'+RBK.i18n.retailLocator.srchresultCntct+'</div>'+
      '<div class="searchResultTitleContent StoreLocator_Direction" >'+RBK.i18n.retailLocator.srchresultDir+'</div>'+
    '</div>';
    
   return headerStr;
}


locator.openInfoBox = function ( type, target) {
    if( target.infoBoxOpened() ) { target.closeInfoBox(); }
    else { target.openInfoBox( ); }
}

locator.createMarker = function (location, display_name, num, isGeoQualityGood) {
  
    var marker = locator.mapviewer.createMarker(location, 
      {'text' : num, 
       'label' : isGeoQualityGood ? '' : RBK.i18n.retailLocator.poorGeocodeDirections
      });
    marker.setInfoBoxContent('<p>' + display_name + '<' + '/p>');
    return marker;
}


locator.cleanUp = function(mode) {
  // Clean up the HTML containers
  
  if (mode == 'locations') {
    
    $('#results').remove(); //$(locator.get_result_container()).empty();
    
    
    //$.each(routing.markers, function(){
    //  this.remove();
    //});
    $.each(locator.markers, function(){
      this.remove();
    });
    locator.markers = []; // clear it out     
  }
  else if (mode == 'directions'){
    // kill the table except for the selected line
  }
  
  
  //locator.mapviewer.removeAllOverlays();
  
}

locator.alphabetizeCountryList = function(){
  var arr=[];
  $('#map-country option').each(function(){
     arr.push($(this).text());
  });
  //arr.sort(); // ASSUMES: countries will always be Capital Case. 
  
  arr.sort(function(a,b){

    function normalize(str){
       return str
               .toLowerCase()
               .replace(/è|é|ê|ë/,'e').replace(/ò|ó|ô|õ|ö/,'o').replace(/ì|í|î|ï/,'i').replace(/à|á|â|ã|ä|å|æ/,'a').replace(/ù|ú|û|ü/,'u');
    }
  
    a = normalize(a);
    b = normalize(b);
  
    return ((a < b) ? -1 : ((a > b) ? 1 : 0));
  });

  $.each(arr, function(i,val){
    $('#map-country option').filter(':contains("' + arr[i] +'")').appendTo('#map-country');
  });

}        
        
locator.setUpPoorGeocodeTooltips = function(selector){
  
  $(selector).livequery(function(){ 
    
      var eachTitle = $(this).children().attr('title');
      
      $(this).find('*').removeAttr('title').removeAttr('alt'); // remove so its own tooltip doesnt show.
      
      $(this)
        .attr('title', eachTitle || '' )  // grab it from the A tag where we set it in createMarker()
        .Tooltip({ 
          track: true, 
          delay: 0, 
          showURL: false, 
          showBody: " - ", 
          opacity: 0.85 
        });
      
      
      locator.livequeryCount++;
      
      if (locator.livequeryCount == locator.markers.length){
        locator.livequeryCount = 0;
        $(selector).expire(); // and kill the livequery.  
      }
      
   });
}        
        

locator.correctLoad = function(){  
  if (locator.isFirstTime) {  //IE has some trouble geocoding the country on load.. so we basicaly do it twice sequentially.
    locator.isFirstTime = false;
    
    
    var defaultLocation = new MMLocation (new MMAddress({ country_code :  RBK.i18n.retailLocator.countryCode.toUpperCase()   }));
    
    // special handling for JP.. Don't really know why, but it defaults to Asia sometimes...
    if (RBK.i18n.retailLocator.countryCode.toUpperCase() == "JP") { defaultLocation =  new MMLocation( new MMLatLon( 35.21449, 133.8951 ),5 );  }

    locator.mapviewer.goToPosition( defaultLocation );      
    locator.mapviewer.removeEventHandler('endGeocode', locator.correctLoad);
  }
}

locator.getCloseCountries = function(countryCode){
  // note:
  // the following countries are grouped at the database level:
  //   PT - Portugal, Açores, Madeira
  //   ES - Spain, Canarys
  //   NL - Netherlands, Holland
  //   UK - UK, England, Scotland
  //   SE - Sweden, Sweeden
  
  // and here i'm grouping the following:
  //   Benelux (Belgium, Luxembourg, Netherlands)
  //   Scandanavia (Norway, Denmark, Sweden, Finland) 
  //   Latin America (minus Chile and Mexico)
  
  var groupings = {
    'Benelux' : ['BE','LU','NL'],
    'Scandanavia' : ['NO','DK','SE','FI'],
    'LAR' : ['BR','CR','SV','GT','PA','UY']   // Brazil, Costa Rica, El Salvador, Guatamala, Panama
  }
  
  var closeCs = [];
  
  /*  FOR NOW we will not be searching neighboring countries. we will only deliver results in the country you searched.
  
  for (var i in groupings){
    var curr = groupings[i];

    for (var code in curr){
       if (countryCode == curr[code])
           closeCs = curr;  // returns an array of all matching
    }
  }
  */

  if (! closeCs.length){ // country not found in the table above
    closeCs.push(countryCode);
  } 

  return closeCs;

}
        
locator.i18izeMultiMap = function(){
  
  $('#mapviewer div.MMMapTypeWidget').livequery(function(){
    $(this).find('a.MM_WORLD_MAP').text(RBK.i18n.retailLocator.map);
    $(this).find('a.MM_WORLD_AERIAL').text(RBK.i18n.retailLocator.aerial);
    $(this).find('a.MM_WORLD_HYBRID').text(RBK.i18n.retailLocator.hybrid);
    $('#mapviewer div.MMMapTypeWidget').expire(); // and kill it.
  });
  
  /* for some reason this doesnt work.. no idea.
    
  $('#mapviewer div.MMContextMenuWidget').livequery(function(){
    $(this).find('a.MMzoomin').text(RBK.i18n.retailLocator.zoomInToHere);
    $(this).find('a.MMzoomout').text(RBK.i18n.retailLocator.zoomOutFromHere);
    $(this).find('a.MMrecenter').text(RBK.i18n.retailLocator.moveMapToHere);
    
  });  
  */
}        
      
        
locator.timeoutTimer = {
  
  duration : 20 * 1000, // 16 seconds good enough?
  id       : null, // this will hold the setTimeout ID
  
  start : function(){
    locator.timeoutTimer.id = window.setTimeout( locator.timeoutTimer.expired , locator.timeoutTimer.duration );
  },
  expired : function(){  // what do we want to do when the timer expires
    
    $('#timeout-error').show(); 
    
    if (routing.inRoutingState){
      $('#mapresults, div.pagination').show(); 
  		$('tr#directions-destination').appendTo('#oldTbody').show();
    } 
    
    locator.timeoutTimer.clear();

  },
  clear : function(){
    window.clearTimeout( locator.timeoutTimer.id );
    
    routing.inRoutingState = false;
    locator.isFormEnabled = true; // clear the temporary disabling.
    $('#loading').hide();     // Results are now loaded, remove spinning icon:
    
  }
};




/////////////////////////////////////
// onload events!  
///////////////////////////////////////

MMAttachEvent( window, 'load', locator.init ); // using multimap's because jquery might be too quick for it.


$( function(){
  
  RBK.i18n.retailLocator.isKm = (RBK.i18n.retailLocator.systemOfMeasure == "SI") ? true : false; // will be "IMPERIAL"  for the US and stuff.

  //process the query keys and add them in as default
  var querykeys = parseUri(document.location.href).queryKey;
  if ( querykeys[ locator.urlquerykeys[0] ]){ // if one of our defined keys is in the URL...
    for (key in querykeys){
      if ($( locator.elemLookup[key] ).length){ // if this dom element is on the page (catch for radius)
        $( locator.elemLookup[key] ).val( decodeURIComponent(querykeys[key]) ); // set the form value of the element to the extracted value from the URL
      }
    }
  }


  
  $('#map-country').val( RBK.i18n.retailLocator.countryCode.toUpperCase() ); // set default country
  
  locator.alphabetizeCountryList(); // alphabetize for other countries.
 
  $('form#map-search #map-submit').click( function(){
  
    $(this).parents('form:first').submit();
    return false;
  });
  
  $('form#map-search').submit(function(){
   
    if (!locator.isFormEnabled) return; // dont let a user Clickyclicky. this clears inside resultsLoaded
    locator.isFormEnabled = false; 
    $("#search-results").show();
    
    locator.requested_page = 1; // reset to first page.
    $('#loading').show();
    locator.initSearch();
    return false;
  });
  
  
  $('#map-search input[type="text"],#map-search select').keydown(function(e){
        if (e.keyCode == 13) {
             locator.isInputVerbose = $('fieldset.verbose:first').is(':visible');
             if (!locator.isInputVerbose){
               $('form#map-search').submit();
               
             }
             return false;    
        }
  });
  

  locator.i18izeMultiMap();

 
  $('div.pagination').hide();
  
  // this is image preloading.. with a twist. -- REMOVED. there is a better way (below) but i'll keep this here for humor's sake... -pi
  //$('#loading').css({visibility: 'hidden', position: 'absolute'}).show();
  //setTimeout(function(){  $('#loading').hide().css({visibility: '', position: '' }).hide();    },500);
  (new Image()).src = "../images/rich/ajax-loader-sm.gif"; 
   
  if ($.browser.msie && $.browser.version < 7){  // a little weirdness to shake the Find button for ie6.
      (function(){
        if ( $('#map-submit').offset().left == 0 ){
          $('#map-submit').hide().show();
          setTimeout(arguments.callee,50);
        }
      })();
  }

});

/*
	parseUri 1.2.1
	(c) 2007 Steven Levithan <stevenlevithan.com>
	http://blog.stevenlevithan.com/archives/parseuri
	MIT License
*/
function parseUri(str){var o=parseUri.options,m=o.parser[o.strictMode?"strict":"loose"].exec(str),uri={},i=14;while(i--)uri[o.key[i]]=m[i]||"";uri[o.q.name]={};uri[o.key[12]].replace(o.q.parser,function($0,$1,$2){if($1)uri[o.q.name][$1]=$2});return uri};parseUri.options={strictMode:false,key:["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],q:{name:"queryKey",parser:/(?:^|&)([^&=]*)=?([^&]*)/g},parser:{strict:/^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,loose:/^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/}};



/*
 color animations jquery plugin
 http://plugins.jquery.com/project/color
*/
(function(jQuery){jQuery.each(['backgroundColor','borderBottomColor','borderLeftColor','borderRightColor','borderTopColor','color','outlineColor'],function(i,attr){jQuery.fx.step[attr]=function(fx){if(fx.state==0){fx.start=getColor(fx.elem,attr);fx.end=getRGB(fx.end)}fx.elem.style[attr]="rgb("+[Math.max(Math.min(parseInt((fx.pos*(fx.end[0]-fx.start[0]))+fx.start[0]),255),0),Math.max(Math.min(parseInt((fx.pos*(fx.end[1]-fx.start[1]))+fx.start[1]),255),0),Math.max(Math.min(parseInt((fx.pos*(fx.end[2]-fx.start[2]))+fx.start[2]),255),0)].join(",")+")"}});function getRGB(color){var result;if(color&&color.constructor==Array&&color.length==3)return color;if(result=/rgb\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*\)/.exec(color))return[parseInt(result[1]),parseInt(result[2]),parseInt(result[3])];if(result=/rgb\(\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*\)/.exec(color))return[parseFloat(result[1])*2.55,parseFloat(result[2])*2.55,parseFloat(result[3])*2.55];if(result=/#([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})/.exec(color))return[parseInt(result[1],16),parseInt(result[2],16),parseInt(result[3],16)];if(result=/#([a-fA-F0-9])([a-fA-F0-9])([a-fA-F0-9])/.exec(color))return[parseInt(result[1]+result[1],16),parseInt(result[2]+result[2],16),parseInt(result[3]+result[3],16)];return colors[jQuery.trim(color).toLowerCase()]}function getColor(elem,attr){var color;do{color=jQuery.curCSS(elem,attr);if(color!=''&&color!='transparent'||jQuery.nodeName(elem,"body"))break;attr="backgroundColor"}while(elem=elem.parentNode);return getRGB(color)};var colors={aqua:[0,255,255],azure:[240,255,255],beige:[245,245,220],black:[0,0,0],blue:[0,0,255],brown:[165,42,42],cyan:[0,255,255],darkblue:[0,0,139],darkcyan:[0,139,139],darkgrey:[169,169,169],darkgreen:[0,100,0],darkkhaki:[189,183,107],darkmagenta:[139,0,139],darkolivegreen:[85,107,47],darkorange:[255,140,0],darkorchid:[153,50,204],darkred:[139,0,0],darksalmon:[233,150,122],darkviolet:[148,0,211],fuchsia:[255,0,255],gold:[255,215,0],green:[0,128,0],indigo:[75,0,130],khaki:[240,230,140],lightblue:[173,216,230],lightcyan:[224,255,255],lightgreen:[144,238,144],lightgrey:[211,211,211],lightpink:[255,182,193],lightyellow:[255,255,224],lime:[0,255,0],magenta:[255,0,255],maroon:[128,0,0],navy:[0,0,128],olive:[128,128,0],orange:[255,165,0],pink:[255,192,203],purple:[128,0,128],violet:[128,0,128],red:[255,0,0],silver:[192,192,192],white:[255,255,255],yellow:[255,255,0],transparent:[255,255,255]}})(jQuery);

