if (!window.alice) {
  alice = {};
}

(function() {
  document.write(unescape("<link href=\"http://www.alice.com/sifr/sifr.css?1277823723\" media=\"screen\" rel=\"stylesheet\" type=\"text/css\" />"));
  document.write(unescape("<link href=\"http://www.alice.com/stylesheets/jScrollPane.css?1268954235\" media=\"screen\" rel=\"stylesheet\" type=\"text/css\" />"));
  document.write(unescape("<link href=\"http://www.alice.com/super_shelf.css\" media=\"screen\" rel=\"stylesheet\" type=\"text/css\" />"));

      document.write(unescape("<script src=\"http://www.alice.com/javascripts/jScrollPane-1.2.3.min.js?1279742770\" type=\"text/javascript\"></script>"));
      document.write(unescape("<script src=\"http://www.alice.com/javascripts/jquery.mousewheel.pack.js?1268954234\" type=\"text/javascript\"></script>"));
      document.write(unescape("<script src=\"http://www.alice.com/javascripts/jquery.tools.scrollable.min.js?1277741362\" type=\"text/javascript\"></script>"));
      document.write(unescape("<script src=\"http://www.alice.com/super_shelf.js\" type=\"text/javascript\"></script>"));
      document.write(unescape("<script src=\"http://www.alice.com/javascripts/three_sixty.js?1279912689\" type=\"text/javascript\"></script>"));
      document.write(unescape("<script src=\"http://www.alice.com/javascripts/jquery.ui.js?1268954234\" type=\"text/javascript\"></script>"));
    document.write(unescape("<div id=\"fb-root\"></div><script src=\"http://www.alice.com/javascripts/facebook.connect.js?1279912689\" type=\"text/javascript\"></script>"));
})();

alice.initialize = function(options) {

  // alice.jQuery = jQuery.noConflict(true); // <-- The right thing
  alice.jQuery = window.jQuery;              // <-- The rude thing :/
  alice.API_TOKEN = options.api_token;

  alice.jQuery(document).bind('alice::sharing_event', function(event, response) {
    var coupon_panel = alice.jQuery('#alice #coupon_panel');
    var congrats = alice.jQuery('<h3></h3>');
    congrats.text(response.message);
    coupon_panel.find('.discount').text('$'+response.discount+' off');
    coupon_panel.find('.message').addClass('thanks');
    coupon_panel.find('.message').empty().append(congrats);
    coupon_panel.find('.share_bonus').empty();
    var footer = alice.jQuery('#alice .footer');
    footer.find('.discount').text('$'+response.discount);
    footer.find('.total').text('$'+response.net_price);
    return false;
  });
};

alice.ViewPort = {
  center: function(targetWidth, targetHeight) {
    var win = alice.jQuery(window),
    target = {
      top: Math.max(0, Math.round((win.height()-targetHeight)/2)+win.scrollTop()),
      left: Math.max(0, Math.round((win.width()-targetWidth)/2)+win.scrollLeft())
    }
    return target;
  }
};

alice.showFooter = function(options) {
  if(alice.jQuery('#alice_footer').is('*')) {
    alice.jQuery('#alice_footer').remove();
  }
  var $iframe = alice.jQuery('<iframe></iframe>');

  $iframe
    .attr('frameborder', 0)
    .attr('height', 64)
    .attr('id', 'alice_footer')
    .attr('marginwidth', 0)
    .attr('marginheight', 0)
    .attr('scrolling', 'no')
    .attr('src', "http://www.alice.com/coupons.html?api_token="+alice.API_TOKEN)
    .attr('style', "position:absolute; left:0; bottom:0; margin:0;")
    .attr('width', '100%');

  alice.jQuery('body').append($iframe);
}

alice.__viewProduct = function(settings, html) {
  alice.jQuery('#alice_wrapper').remove();
  alice.jQuery('#alice_mask').remove();
  var mask = alice.jQuery('<div id="alice_mask"></div>');
  mask.click(function() { alice.jQuery(this).hide(); });
  mask.css({
    background: settings.maskColor,
    border: 0,
    display: 'none',
    opacity: settings.maskOpacity,
    position: 'fixed',
    _position: 'absolute',  
    top: 0,
    left: 0,
    width: '100%',  
    height: '100%',
    'z-index': 9999
  });

  var wrapper = alice.jQuery('<div id="alice_wrapper"></div>'),w = 650, h = 486;
  wrapper.html(html);
  wrapper.css({
    border: 0,
    display: 'none',
    position: 'absolute',
    top: alice.ViewPort.center(w,h).top,
    left: alice.ViewPort.center(w,h).left,
    width:  w,
    height: h
  });
  wrapper.find('.basictabs').basictabs();
  wrapper.find('.panel_link').each(function() {
    alice.jQuery(this).click(function() {
      alice.jQuery(document).trigger("alice::shelf::activating", this.hash);
      wrapper.find('.panel_link').removeClass('active');
      alice.jQuery(this).addClass('active');
      wrapper.find('.panel').hide();
      var panel = wrapper.find(this.hash);
      panel.show();
      if(panel.is('.basictabs')) {
        panel.find('ul:first li:first a:first').click();
      }
      if(this.hash == '#review_panel' || this.hash == '#description_panel') {
        wrapper.find(this.hash).find(' .body').jScrollPane({
          dragMinHeight:22,
          dragMaxHeight:22,
          scrollbarWidth:10
        });
      };
      return false;
    });
  });
  wrapper.find('.close_link').click(function() {
    mask.remove();
    wrapper.remove();
		alice.show_selects_ie6();
    return false;
  });
  wrapper.find('.footer > .labels').each(function() {
    alice.jQuery(this)
      .css({ cursor: 'pointer' })
      .click(function() { wrapper.find('a[href="#description_panel"]').click(); });
  });
  wrapper.find('a.desc_tab').each(function() {
    alice.jQuery(this)
      .css({ cursor: 'pointer' })
      .click(function() {
        wrapper.find('#nutrition_panel').hide();
        wrapper.find('#description_panel').show();
        wrapper.find('#description_panel').find(' .body').jScrollPane({
          dragMinHeight:22,
          dragMaxHeight:22,
          scrollbarWidth:10
        });
        return false;
      });
  });
  wrapper.find('a.nutri_tab').each(function() {
    alice.jQuery(this)
      .css({ cursor: 'pointer' })
      .click(function() {

        wrapper.find('#description_panel').hide();
        wrapper.find('#nutrition_panel').show();
        wrapper.find('#nutrition_panel').find(' .body').jScrollPane({
          dragMinHeight:22,
          dragMaxHeight:22,
          scrollbarWidth:10
        });
        return false;
      });
  });
  wrapper.find('.footer > .review_overview').each(function() {
    alice.jQuery(this)
      .css({ cursor: 'pointer' })
      .click(function() { wrapper.find('a[href="#review_panel"]').click(); });
  });
  wrapper.find('form.new_subscription').submit(function() {
    var jsonUrl = this.action + ".js?api_token="+alice.API_TOKEN+"&callback=?";
    alice.jQuery.getJSON(jsonUrl, alice.jQuery(this).serialize(), function(response) {});
    return false;
  });
  alice.jQuery('body').append(mask);
  alice.jQuery('body').append(wrapper);

  wrapper.find('#buy_now_panel').css({ display:'none'});
  mask.show("fast", function() {
    wrapper.show("fast", function() {
      alice.jQuery.each(alice.jQuery.makeArray(settings.initialView), function() {
        var panel = this;
        wrapper.find('a.panel_link').filter(function() {
          var href = this.href;
          return href && href.indexOf(panel) > -1; 
        }).click();
      });
      // make sure we're always showing something.
      if(!alice.jQuery('.panel:visible').is('*')) {
        wrapper.find('a.panel_link.image').click();
      }
    });
  });
};

alice.hide_selects_ie6 = function(){
	if (!window.XMLHttpRequest) {
    $('select').css({visibility:'hidden'});
  }
};
alice.show_selects_ie6 = function(){
	if (!window.XMLHttpRequest) {
    $('select').css({visibility:'visible'});
  }
};

alice.viewProduct = function(options) {
  var settings = alice.jQuery.extend({}, {
    initialView: ['#image_panel'],
    maskColor: 'white',
    maskOpacity: 0.56
  }, options);


  var superShelfPath = "http://www.alice.com/super_shelf/upc/"+settings.upc+"?api_token="+alice.API_TOKEN;

  var panel = null; 
  try { panel = $.makeArray(settings.initialView).pop().split("#").pop().split('_panel').shift(); } catch(err) {}
  if(panel) { superShelfPath = superShelfPath+"&panel="+panel; }

  var $link = $('<a></a>');
  $link.attr('class', 'alice_super_shelf');
  $link.attr('href', superShelfPath);
  $link.trigger('click');
	return false;
};
