﻿function StoreLocatorSearchBox(id, options){
    this.ID = id;
    this.SearchBoxID = String.format('{0}_search_box', id);
    this.WatermarkID = String.format('{0}_watermark', id);
    this.SearchButtonID = String.format('{0}_search_button', id);
    this.CookieName = options.CookieName;
    
    this.init();
}


StoreLocatorSearchBox.prototype = {
    init: function(){
        var oThis = this;
        var objSuggestions = new Suggestions(StationWebsites.StoreLocatorSearchBox, StationWebsites.StoreLocatorSearchBox.aac, function (res){
            var aSuggestions = [];
            if(res && res.value && res.value.ResponseCode == 1){
                var rows = res.value.Payload;
                if(rows){
                    for(var i=0,l=Math.min(10, rows.length);i<l;i++){
                        var row = rows[i];
                        var str;
                        if(isBlank(row.City)){
                            str = trim(row.State)
                        }
                        else{
                            str = String.format('{0}, {1}', trim(row.City), trim(row.State));
                        }                            
                        var sWord = new SuggestionWord(str, str);
                        aSuggestions.push(sWord);
                    }
                }                   
            }
            objSuggestions.updateSuggestions(aSuggestions);
        });
        
        var strCookieName = oThis.CookieName;
        if(!isBlank(strCookieName)){    
            //deleteCookie(strCookieName);
            var strSLRecentSearches = getCookie(strCookieName);
            if(strSLRecentSearches){
                var recentSearchesArr = eval(String.format('[{0}]', unescape(strSLRecentSearches)));
                if(recentSearchesArr){
                    var i = recentSearchesArr.length;
                    while(i--){
                    var sWord = new SuggestionWord(recentSearchesArr[i], recentSearchesArr[i]);
                        objSuggestions.recentWords.push(sWord);
                    }
                }
            }
        }
    
        var txtSearchBox = $(oThis.SearchBoxID);
        var spnWatermark = $(oThis.WatermarkID);
        var btnSearch = $(oThis.SearchButtonID);
        if(txtSearchBox){            
            var oTextbox2 = new AutoSuggestControl(txtSearchBox, objSuggestions, function(){
                oThis.search.call(oThis);
            });
            addTxtboxOnFocus(txtSearchBox, function(){toggleVisibility(oThis.WatermarkID, false);});
            addTxtboxOnBlur(txtSearchBox, function(){
                if(isBlank(txtSearchBox.value)){
                    toggleVisibility(oThis.WatermarkID, true);
                }
            });
            spnWatermark.onclick = function(){txtSearchBox.focus()};
            
            if(isBlank(txtSearchBox.value)){
                toggleVisibility(oThis.WatermarkID, true);
            }
            
            btnSearch.onclick = function(){
                oThis.search();
            }
        }
    },
    
    search: function(){
        var oThis = this;
        var txtSearchBox = $(oThis.SearchBoxID);
        if(!isBlank(txtSearchBox.value)){
            var item = new RoutingObjects.StoreLocatorAddress(txtSearchBox.value);
            if(item){              
                window.top.location.href = item.GetURL();
            }
        }
    }
}
