var offerRss = {
  container:null,
  interests:null,
  brands:null,
  win:null,
  init:function() {
    var self = this;
    this.container = yuiDom.get("rssOffersContent");
    this.interests = yuiDom.getElementsByClassName("offerItem", "input", this.container);
    this.brands = yuiDom.getElementsByClassName("brandItem", "input", this.container);
    yuiDom.getElementsByClassName("rssLink", "li", this.container).forEach(function(el) {
      yuiEvent.addListener(el, "click", self.processFeedBridge);
    });
    var whatItIsLink = yuiDom.get("whatIsRssLink");
    if (whatItIsLink) {
      yuiEvent.addListener(whatItIsLink, "click", self.popUpBridge);
    }
  },
  getFeedUrl:function(fullUrl, allOffers) {
    var output = [];
    var queryString = "?searchType=COMMONSEARCH";
    var baseUrl = document.location.protocol + "//" + document.location.hostname + (document.location.port != "" ? ":" + document.location.port : "") + "/dp/en_US/common/offers/rss_feed.jsp";
    if (!allOffers) {
      this.interests.forEach(function(input) {
        if (input.checked) {
          output.push("amenities=" + input.value);
        }
      });
      this.brands.forEach(function(input) {
        if (input.checked) {
          output.push("brand=" + input.value);
        }
      });
    }
    if (output.length > 0) {
      queryString += "&interestFilterType=AND&" + output.join("&");
    }
    if (fullUrl) {
      return baseUrl + queryString;
    } else {
      return queryString;
    }
  },
  processFeed:function(el) {
    var isStandard = yuiDom.hasClass(el, "standardRss");
    var allOffers = yuiDom.hasClass(el, "allOffers");
    var url = this.getFeedUrl(!isStandard, allOffers);
    if (isStandard) {
      if (!this.win || this.win.closed) {
        this.win = window.open('/lemeridien/offers/results/detail/special_offers_rss.html' + url, null, 'width=430,height=300,screenX=157,screenY=140,top=157,left=140,scrollbars=no,resizable=no');
      } else {
        this.win.location.href = '/lemeridien/offers/results/detail/special_offers_rss.html' + url;
        this.win.focus();
      }
    } else {
      if (yuiDom.hasClass(el, "googleRss")) {
        document.location.href = "http://fusion.google.com/add?feedurl=" + escape(url);
      } else if (yuiDom.hasClass(el, "yahooRss")) {
        document.location.href = "http://add.my.yahoo.com/content?url=" + escape(url);
      } else if (yuiDom.hasClass(el, "bloglinesRss")) {
        document.location.href = "http://www.bloglines.com/sub/" + escape(url);
      }
    }
  },
  initBridge:function(e) {
    offerRss.init();
  },
  processFeedBridge:function(e) {
    var self = offerRss;
    self.processFeed(this);
  },
  popUpBridge:function(e) {
    window.open(this.href, null, 'width=440,height=600,screenX=50,screenY=50,top=50,left=50,scrollbars=yes,resizable=yes');
    yuiEvent.preventDefault(e);
  }
};
yuiEvent.onDOMReady(offerRss.initBridge);
