
var cartFunctions = {
  
  cart : null,
  hideNotes : false,
  
  addItem: function(itemId, rfId) {
    var query = 'id=' + itemId + '&rfid=' + rfId;
    var myAjax = new Ajax('/cart/add_to_cart', { method: 'POST', data: query, update: 'cartsize', onComplete: cartFunctions.updateSidebar}).request();
  },
  
  getCart: function() {
    var myAjax = new Ajax('/cart/display_cart', {method: 'GET', update: 'cart', onComplete: cartFunctions.openCart}).request();
  },
  
  openCart: function() {
    var cartHeight = $('cart').offsetHeight;
    var cartboxHeight = $('cartbox').offsetHeight;
    var top = 255 - ((cartHeight-cartboxHeight)/2);
    cartFunctions.cart.style.top = top + 'px';
    mySlide2.slideIn();
    var cartTop = cartFunctions.cart.offsetTop;
    var cartBottom = cartFunctions.cart.offsetTop + cartFunctions.cart.offsetHeight;
    var notes = $('noteHeader');
    cartFunctions.hideNotes = (notes && (notes.offsetTop >= cartTop) && (notes.offsetTop <= cartBottom));
    if( cartFunctions.hideNotes ) {
      $('noteHeader').style.opacity = '0';
    }
  },
  
  closeCart: function() {
    mySlide2.slideOut().chain(function(){ if(cartFunctions.hideNotes) { $('noteHeader').style.opacity = '1' } });
  },
  
  updateSidebar: function() { 
    var myAjax = new Ajax('/cart/get_size', {method: 'get', update: 'cartsize'}).request();
    var box = $('cartbox');
    box.style.display = 'block';
    var fx = box.effects({duration: 300, transition: Fx.Transitions.Quart.easeOut});
    fx.start({'background-color': '#999'}).chain(function(){ this.start({'background-color': '#BEC8D1'})});
  }
  
};

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	}
	else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

var mySlide2 = null;

function defSlide() {
  mySlide2 = new Fx.Slide('cartouter', {mode: 'horizontal'}).hide();
  cartFunctions.cart = $('cartouter');
}

function setupTips() {
  /* setup tooltips */
	var as = [];
	$$('a').each(function(a){
		if (a.getAttribute('title')) as.push(a);
	});
	new Tips($$('.addtip'));
}


/*addLoadEvent(defSlide);*/
addLoadEvent(setupTips);

