var Cart = {
  init: function() {
    this._setup();
    this._bindEvents();
  },

  _getTemperature: function() {
//    jQuery.ajax({
//        cache: false,
//        dataType: 'text',
//        url: 'http://weather.yahooapis.com/forecastrss?p=USNY0996&u=f',
//        success: function(feed) {
//           console.log(feed);
//           regex = '<yweather:condition.*temp="(.*?)".*/>';

//           matches = feed.match(regex);
//           console.log(matches[1]);
//        }
//    });
  },

  _setup: function() {
    // remove category name
    $('#recalculate').find('div.item_title').each(function() {
      // remove category
      $(this).find('b').remove();
      $(this).find('br').remove();
      var text = $(this).html();

      // remove price portion
      var price_pos = text.indexOf(' - $');
      if (price_pos != -1) {
        text = text.substr(0, price_pos);
      }

      // space out text so it can overflow 
      var pieces =  text.split('\&nbsp;'); 
      if(pieces.length > 1) {
        var text = pieces[0];// + '</div><div style="margin-bottom: 0px;" class="no_wrap description">' + pieces[1] + '</div>';
        $(this).html(text);
        //pieces[1]
        $('#' + $(this).attr('data') + '_description').html(pieces[1]);
      }
    });

    this._showTempIfNotAdded();
  },

  _showTempIfNotAdded: function() {
    if($('a.cart_line_item[data="BRRR"]').length === 0) {
      $('.weather_balloon').fadeIn(1250);
    }
  },

  _bindEvents: function() {
    var obj = this;

    $('#add_brrr_product').ajaxForm({
      iframe: 'form_reception',
      success: function(responseText) {
        obj._showUpdatedCart();
      }
    });

    $('#add_brrr').click(function(e) {
      $('#add_brrr_product').submit();
    });

    //bind suggestions and cart items to open in a popup
    $('a.suggested_product, a.cart_line_item').click(function (e) {
        // prevent default link action
        e.preventDefault();
        // load the popup for a Product
        GaspareStore._loadPopup(GsProduct, $(this).attr('href'));

        return false;
    });

    $('.remove_item').click( function(e) {
      e.preventDefault();
      var input = $(this).attr('data');
      $('.item input[name="' + input + '"]').val(0);
      $('#recalculate').submit();
      $('.' + input).each(function() {
        $(this).fadeOut();
      });
    });

    $('.qty_select').each(function() {
      var input = $(this).attr('data');
      var val = $('.item input[name="' + input + '"]').val();
      $(this).val(val);
    });
    $('.qty_select').change(function() {
      var val = $(this).val();
      var input = $(this).attr('data');
      $('.item input[name="' + input + '"]').val(val);

      $('#recalculate').submit();

    });

    $('.continue_shopping').click(function(e) {
      e.preventDefault();
      GaspareStore._removePopup($('.popup_container.cart'));
      return false;
    });

    $('#recalculate').ajaxForm({
      success: function(responseText) {
        obj._showUpdatedCart();
      }
    });
  },

  _showUpdatedCart: function() {
      // show "updated" popup
      var current_cart = $('.popup_container.cart');
      var zIndex = current_cart.css('zIndex');

      var options = {
        url: '/view_cart.asp',
        callback: function(popup_page) {
            var newCart = $(popup_page).find('.popup_container');

            newCart.css('zIndex', zIndex);
            current_cart.replaceWith(newCart);

            // activate close window
            newCart.find('#popup_close a').click( function(e) {
              e.preventDefault();
              GaspareStore._removePopup($(this).parent().parent()); 
            });

            Cart.init();
        }
      };

      GaspareStore.doAjaxEncodedRequest(options);
  }
};

