function add_to_cart_list (did, fast) {
  var deal = all_deals[did];
  if (! deal) return false;
  $('#cart-list-start').after('<div class="cart-item ui-widget-content" id="cart-item-'+did+'" style="display:none;"><h4><div class="cart-item-del ui-state-default ui-corner-all" id="cart-item-del-'+did+'"><span class="ui-icon ui-icon-closethick"></span></div>'+
      deal.customer+'</h4>'+
      deal.txt+
      (deal.discount ? ('<br><b>get '+deal.discount+' off</b>'):'') +
      '</div>'
  );
  $('#cart-item-del-'+did).click(function () {
      cart_item_remove(did);
  });
  setTimeout(function () {
    if (fast) $('#cart-item-'+did).show();
    else      $('#cart-item-'+did).fadeIn('fast');
    deal_button_label(did, true /* adding */);
  }, 200);
  return true;
}
function cart_item_remove (did) {
  $('#cart-item-'+did).fadeOut('fast', function () {$(this).remove();});
  var deals = $.cookie(cookie);
  if (deals) {
    var d={};
    var deal_list = [];
    deals = deals.split(',');
    for (var i = deals.length - 1; i >= 0; i--) {
      if (deals[i] == did) continue;
      if (d[deals[i]]) continue;
      deal_list.push(deals[i]);
      d[deals[i]] = 1;
    }
    deals = deal_list.join(',');
  }
  if (deals) {
    $.cookie(cookie, deals, { path: '/' });
  }
  else {
    $.cookie(cookie, null, { path: '/' });
    setTimeout(cart_hide, 300);
  }
  deal_button_label(did, false /* removing */);
}
function init_cart () {
  var deals = $.cookie(cookie);
  var fast=true;
  if (deals) {
    var d={};
    deals = deals.split(',');
    for (var i = deals.length - 1; i >= 0; i--){
      add_to_cart_list(deals[i], fast);
    }
    $('#cart').show().css({right:'0'});
    setTimeout(function () {
      cart_close();
    }, 600);
  }
}
function add_to_cart (did) {
  var deals = $.cookie(cookie);
  var new_deal = null;
  if (deals) {
    var d={};
    var deal_list = [];
    deals = deals.split(',');
    for (var i = deals.length - 1; i >= 0; i--) {
      if (d[deals[i]]) continue;
      deal_list.push(deals[i]);
      d[deals[i]] = 1;
    }
    if (! d[did]) {
      new_deal = did;
      deal_list.push(did);
    }
    deals = deal_list.join(',');
  }
  else {
    deals = did;
    new_deal = did;
  }
  cart_open();
  if (new_deal !== null) {
    add_to_cart_list(new_deal);
  }
  var ci = '#cart-item-'+did;
  $('#cart-list').scrollTo($(ci), 200);
  setTimeout(function () {
    $(ci).animate({
      opacity: 0.2
    }, 200, function () {
      $(ci).animate({
        opacity: 1.0
      });
    });
  }, 300);
  $.cookie(cookie, deals, { path: '/' });
}
function cart_close () {
  $('#cart').animate({right:'-380'}, 'fast');
}
function cart_hide () {
  $('#cart').fadeOut('fast');
  cart_close();
  $('#top-get-deal-link').remove();
}
function cart_open (cart) {
  if (cart) {
    cart.fadeIn('fast').animate({right:'0'}, 'fast');      
  }
  else {
    $('#cart').fadeIn('fast').animate({right:'0'}, 'fast');      
  }
}
function cart_leave () {
  setTimeout(
    function () {
      if (! cart_close_cancel) {
        cart_close();
      }
    },
    400
  );
}

function deal_button_label (did, adding) {
  if (adding) {
    $('#dealbox-get-'+did+' span').html('Deal is selected');
    $('#dealbox-get-'+did+' h1').html('Click to show');
  }
  else {
    $('#dealbox-get-'+did+' span').html('Click to');
    $('#dealbox-get-'+did+' h1').html('Get this deal');      
  }
}
function deal_animate (id, dir) {
  var div=$('#'+id);
  var props = sliders[id];
  props.current += dir;
  if (props.current <= 0) {
    props.current = 0;
    $('#'+id+'-left').fadeOut();
  }
  else {
    $('#'+id+'-left').fadeIn();
  }
  if (props.current >= props.s-1) {
    $('#'+id+'-right').fadeOut();
    props.current = props.s-1;
  }
  else {
    $('#'+id+'-right').fadeIn();        
  }
  $('ul', div).animate({marginLeft:0-(props.w*props.current)});
}
function popeye_init () {
  $('#cart').bind('mouseenter', function () {
    cart_close_cancel=true;
    cart_open($(this));
  });
  $('#cart').bind('mouseleave', function () {
    cart_close_cancel=false;
    cart_leave($(this));
  });
  $('.dealbox-get').each(function(i) {
    var x=$(this);
    var did = x.attr('id').replace('dealbox-get-', '');
    var deal = all_deals[did];
    if (! deal) return;
    if (remove_more_info) {
      $('#deal-more-info-' + did).remove();
    }
    else {
      $('#deal-more-info-' + did).ajaxify({
        link:deal.moreinfo,
        onSuccess:function (options,data) {
          $('<div title="'+deal.customer+'"><div style="height:500px;width:auto;" class="more-info-widget">'+data+'</div></div>'
           ).dialog({modal:true, width: 700,
                     buttons: { "Close": function() { $(this).dialog("close"); } }
                    });
          //.click(function() {$(this).dialog("close");});
        }
      });      
    }
    x.click(function () { add_to_cart(did); });
  });
  $('#cart-close').click(cart_close);
  $('#cart-open').click(cart_open);
  init_cart();
  $('.make_me_slider').each(function () {
    var div=$(this);
    var id=div.attr('id');
    // for <li> we get the dimensions of the first
    var props = {
      id: id,
  	  s: $("li", div).length,
		  w: $("li", div).width(),
		  h: $("li", div).height(),
		  current: 0
    };
    div.width(props.w);
    div.height(props.h);
    $("li", div).css('float','left');
    div.css('overflow', 'hidden');
    $('ul', div).css('width', props.s * props.w);
    sliders[id] = props;
    $('#'+id+'-left').click(function () {
      deal_animate(id, -1);
    });
    $('#'+id+'-right').click(function () {
      deal_animate(id, 1);
    });
    if (props.s > 1) {
      $('#'+id+'-right').fadeIn();
    }
  });
}
$(document).ready(popeye_init);
