var offers = {
     request: null,
     initialize: function() {
       yuiEvent.addListener(yuiDom.get("offersregions"), "change", offers.getCountries);
       yuiEvent.addListener(yuiDom.get("offerscountry"), "change", offers.getStates);
       yuiEvent.addListener(window, "load", offers.getCountries);
       if(offers.text.selectedCountry != "null" && yuiDom.get("offersregions").value != ""){
          yuiEvent.addListener(window, "load", offers.getStates);
       }
       return true;
     },
     getCountries: function(){
        if(!yuiDom.get("offersregions").value){
          yuiDom.get("offerscountry").disabled = true;
          yuiDom.get("offerscountry").options.length = 1;
          yuiDom.get("offerscountry").selectedIndex = 0;
          yuiDom.get("offersstateProvince").disabled = true;
          yuiDom.get("offersstateProvince").options.length = 1;
          yuiDom.get("offersstateProvince").selectedIndex = 0;
          return;
        }
        yuiDom.get("offerscountry").options[0].text = offers.text.loading;
        offers.request = yuiConnect.asyncRequest('GET','/ajax/countryandstatelocations?brandCode=&offerRegionCode='+yuiDom.get("offersregions").value, {success: offers.populateCountries, argument: ["offerscountry", "Country"]});
        yuiDom.get("offerscountry").disabled = true;
        yuiDom.get("offerscountry").options.length = 1;
        yuiDom.get("offerscountry").selectedIndex = 0;
        yuiDom.get("offersstateProvince").disabled = true;
        yuiDom.get("offersstateProvince").options.length = 1;
        yuiDom.get("offersstateProvince").selectedIndex = 0;

     },
     getStates: function(){
        yuiDom.get("offersstateProvince").options[0].text = offers.text.loading;
        yuiDom.get("offersstateProvince").disabled = true;
        yuiDom.get("offersstateProvince").options.length = 1;
        yuiDom.get("offersstateProvince").selectedIndex = 0;
        if(!yuiDom.get("offerscountry").value && offers.text.selectedCountry != "null") {
           aCountryCode = offers.text.selectedCountry;
        }else{
           aCountryCode = yuiDom.get("offerscountry").value;
        }
        offers.request = yuiConnect.asyncRequest('GET','/ajax/countryandstatelocations?brandCode=&requestType=STATE&countryCode='+aCountryCode+'&timestamp='+new Date().getTime(), {success: offers.populateStates, argument: ["offersstateProvince"]});
     },
     populateCountries: function(xhr){
         var resetCountry = false;
          var countryOptions = xhr.responseXML.getElementsByTagName("entry");
          yuiDom.get("offerscountry").options[0].text = offers.text.countryLabel;
          if(countryOptions.length>0) {
            yuiDom.get(xhr.argument[0]).disabled = false;
            var selectOptions = xhr.responseXML.getElementsByTagName("entry");
            for(var i=0; i<selectOptions.length; i++) {
                if(offers.text.selectedCountry == selectOptions[i].getAttribute("key")){
                    resetCountry = true;
                    var option = new Option(selectOptions[i].firstChild.nodeValue, selectOptions[i].getAttribute("key"), '', true);
                }else{
                    var option = new Option(selectOptions[i].firstChild.nodeValue, selectOptions[i].getAttribute("key"));
                }
                yuiDom.get(xhr.argument[0]).options[yuiDom.get(xhr.argument[0]).options.length] = option;
            }
            if(resetCountry){
              offers.text.selectedCountry = "null";
            }
          }
     },
     populateStates: function(xhr){
          var stateOptions = xhr.responseXML.getElementsByTagName("entry");
          yuiDom.get("offersstateProvince").options[0].text = offers.text.stateLabel;
          if(stateOptions.length>0) {
            yuiDom.get(xhr.argument[0]).disabled = false;
            var selectOptions = xhr.responseXML.getElementsByTagName("entry");
            for(var i=0; i<selectOptions.length; i++) {
                if(offers.text.selectedState == selectOptions[i].getAttribute("key")){
                    var option = new Option(selectOptions[i].firstChild.nodeValue, selectOptions[i].getAttribute("key"), '', true);
                }else{
                    var option = new Option(selectOptions[i].firstChild.nodeValue, selectOptions[i].getAttribute("key"));
                }
                yuiDom.get(xhr.argument[0]).options[yuiDom.get(xhr.argument[0]).options.length] = option;
            }
          }
          else {
            yuiDom.get(xhr.argument[0]).disabled = true;
          }
     }
    ,
    offerFinder: {
         baseURL: document.location.href,
         init: function(aParam){
            var url = aParam.url;
            yuiEvent.addListener(window,'load', function(){offers.offerFinder.updateOffersCount(url);});
            yuiEvent.addListener(window,'load', offers.offerFinder.getAmenities);
            yuiEvent.addListener(yuiDom.get('displayAmenities'), 'click', function(){offers.offerFinder.toggleAmenities("open")});
            yuiEvent.addListener(yuiDom.get('closeFilter'), 'click', function(){offers.offerFinder.toggleAmenities("close","Y")});
            yuiEvent.addListener(yuiDom.get('clearAmenities'), 'click', offers.offerFinder.clearAmenities);
         },
         getAmenities:function(){
            var amentiesArray = ["amenities","brandFilters"];
            amentiesArray.forEach(function(amenity){
              offers.offerFinder.checkAmenities("&"+amenity);
            });
            return true;
         },
         checkAmenities:function(parm) {
             // Parse url and return amenities to check.
             var url = offers.offerFinder.baseURL;
             if(url.indexOf(parm) > -1){
                var urlParams = url.substr(url.indexOf(parm), url.length);
                url = url.substr(0, url.indexOf(parm)); // reset url string
                offers.offerFinder.checkChecked(urlParams);
             }
         },
         checkChecked:function(urlParams) {
             // Repopulate Amenities
             var checked = urlParams.match(/(?:&(amenities|brandFilters)=)(\w+)/g);
             offers.offerFinder.toggleAmenities("close");
             if(checked){
                for ( var i = 0; i < checked.length; i++ ){
                    var val = checked[i].match(/=(\w+)/);
                    if(val[1] == "Spa"){
                        val[1] = "Spa/Fitness";
                    }
                    try {
                        var checkMe = document.getElementById(val[1]);
                        checkMe.checked = true;
                        offers.offerFinder.toggleAmenities("open");
                    } catch(e) { /* error / e */ }
                }       
             }
         },
        toggleAmenities: function(status, reload){
            if(status == "open"){
              yuiDom.removeClass(yuiDom.get("refineSearch"),"showAmenities");
              yuiDom.addClass(yuiDom.get("refineSearch"),"hideAmenities");
              yuiDom.removeClass(yuiDom.get("offerFilter"),"hideAmenities");
              yuiDom.addClass(yuiDom.get("offerFilter"),"showAmenities");

            }else{
              yuiDom.removeClass(yuiDom.get("refineSearch"),"hideAmenities");
              yuiDom.addClass(yuiDom.get("refineSearch"),"showAmenities");
              yuiDom.removeClass(yuiDom.get("offerFilter"),"showAmenities");
              yuiDom.addClass(yuiDom.get("offerFilter"),"hideAmenities");
              if(reload){
                 offers.offerFinder.clearAmenities();
              }
            }
        },
        clearAmenities: function(){
            for(var i = 0; i < document.offerFilterForm.amenities.length; i++ ) {
                if(document.offerFilterForm.amenities[i].checked){
                   document.offerFilterForm.amenities[i].checked = false;
                   document.offerFilterForm.amenities[i].value = "";
                }
            }
            for(var x = 0; x < document.offerFilterForm.brandFilters.length; x++ ) {
                if(document.offerFilterForm.brandFilters[x].checked){
                   document.offerFilterForm.brandFilters[x].checked = false;
                   document.offerFilterForm.brandFilters[x].value = "";
                }
             }
             document.offerFilterForm.submit();
        },
        updateOffersCount:function(currentUrl){
            if(currentUrl) {
               var ampersandPairs = currentUrl.split("&");
               var splitPairs = [];
               for (var zI=0; zI<ampersandPairs.length; zI++) {
                  splitPairs = ampersandPairs[zI].split('=');
                  if (splitPairs.length == 2) {
                     if(splitPairs[0] == "offersCount"){
                        document.getElementById("offersCount").value = splitPairs[1];
                        yuiDom.get("totalOfferCount").innerHTML = splitPairs[1];
                        break;
                     }
                  }
               }
            }
        }
    }
};

yuiEvent.onDOMReady(offers.initialize);