// START Class
var Cart = Class.create({

  initialize: function() {
  
  	// ACE Service
  	this.cart_service = new CartService();
  	
  	this.items = $H();
  	
  	// cart elements
	this.cart_link = $('cart-link');
	this.cart_button = $('cart-button');
	this.cart_new_button = $('cart-new-button');
	this.cart_dropdown = $('cart-dropdown');
	this.cart_dropdown_list = $('cart-dropdown-list');
	this.cart_dropdown_link = $('cart-dropdown-link');
	this.throbber = $('cart-throbber');
	
	this.target = $('target-dropdown') ? true : false;
	
	// observe cart button hover
	if(this.cart_button) {
		this.cart_button.observe('mouseenter', this.show_dropdown.bind(this))
		this.cart_button.observe('mouseleave', this.hide_dropdown.bind(this));
	} else if(this.cart_new_button) {
		this.cart_new_button.observe('mouseenter', this.show_new_dropdown.bind(this));
		this.cart_new_button.observe('mouseleave', this.hide_new_dropdown.bind(this));
	}
	
	// get the total
	this.get_total();
	
	this.get_cart_items();
	
	// resize dropdown
	if(this.cart_button) {
		this.resize_dropdown();
	}

  },
  
  get_total: function(removed) {
  
  	this.cart_service.get_total.call();
	this.cart_service.get_total.result = function(result) {
		
		if(result){
		
			this.total = result.total;
			this.qty = result.qty;
			
			if(this.qty){
			
				var item = ( !nx.unboxed && nx.partner_id!=36 ? 'My Trade-In' : 'Item' );
				
				if(nx.donate_partner) {
					var item = 'My Donation';
				} 

				if(this.qty > 1){
					
					item += 's';
				}
				
				if(nx.unboxed) {
					var cart_link_text =  this.qty + ' ' + item + ': ' + Math.format_number(this.total,{currency:nx.currency});
				}
				else {
					var cart_link_text = item + ': ' + Math.format_number(this.total,{currency:nx.currency}) +  '<span id="cart-qty">(' + this.qty + ")</span>";
				}
				
			}else{
			
				var cart_link_text = ( !nx.unboxed && nx.partner_id!=36 ? 'My Trade-In: $0.00' : '0 Items: $0.00' );
				
				if(nx.donate_partner) {
					var cart_link_text = 'My Donation: $0.00';
				} 
				
			}
			
			this.cart_link.update(cart_link_text);
			
		} 
		
	}.bind(this); // END ACE call
  
  },
  
  remove_item: function(cart_item_id) {
	
	this.throbber.show();

	this.cart_service.remove_item.call(cart_item_id);
	this.cart_service.remove_item.result = function(success) {

		if(success) {
	
			this.get_cart_items();
			this.throbber.hide();

		}

	}.bind(this);
	
	
  },
  
  // get cart items from ACE
  get_cart_items: function() {
   
	this.cart_service.get_items.call();
	this.cart_service.get_items.result = function(cart_items) {
	
		if(cart_items) {
			
			// clear the list
			this.cart_dropdown_list.update('');
		
			// if there are no cart items
			if(cart_items.size()==0) {
		
				if(!nx.unboxed) {
					this.cart_dropdown_link.update('Your cart is empty...add some items!');
				}
				this.cart_dropdown_link.href = "/search";
			
			// else build the list
			} else {
		
				if(this.target) {
				
					this.cart_dropdown_link.update('proceed to checkout <span class="blue-arrow"></span>');
					
				} else {
				
					if(nx.unboxed) {
						this.cart_dropdown_link.update('Proceed to checkout...');
					}
					
					this.cart_dropdown_link.href = "/checkout";
					
				}
			
				// loop through the cart items
				cart_items.each(function(cart_item) {
					
					//build html
					var cart_item_html = Builder.node('div',{id:"cart-item-"+cart_item.cart_item_id,className:'cart-item'},[
						Builder.node('img',{src:'/inc/scripts/image.php?trim=1&w=30&h=50&file_id='+cart_item.file_id}),
						Builder.node('span',{className:'title'}, [
							Builder.node('a',{href:'/product/'+cart_item.product_id+"/"},cart_item.title)
						]), 
		 		    	Builder.node('span',{className:'price'}, Math.format_number((cart_item.display_price ? cart_item.display_price : cart_item.original_price),{currency:nx.currency})),
		 		    	Builder.node('button',{
		 		    		id:"cart-item-"+cart_item.cart_item_id+"-delete",
		 		    		onClick:"nx.cart.remove_item("+parseInt(cart_item.cart_item_id)+")"
		 		    	}, 'X')
		 		    ]);	
		 		    
		 		    this.cart_dropdown_list.appendChild(cart_item_html);									
				
				}.bind(this));
				// END EACH
				
			}	
		
		}
		
		if(this.throbber){
		
			this.throbber.hide();	
		}
		
		if(this.cart_button) {
			
			this.resize_dropdown();
		}
		
	}.bind(this); // END ACE
	
	
	this.get_total();

  },
  
  show_new_dropdown: function() {
  
  	clearTimeout(this.dropdown_timeout);
  	
  	if(!this.dropdown_open){
  		this.dropdown_timeout = setTimeout(function() { Effect.Appear(this.cart_dropdown, { duration: 0.2 }) }.bind(this),200);
  	}
  },
  
  hide_new_dropdown: function() {
  	
  	clearTimeout(this.dropdown_timeout);
  	
  	this.dropdown_timeout = setTimeout(function(){ Effect.Fade(this.cart_dropdown, { duration: 0.2 }) }.bind(this),200);
  },
  
  show_dropdown: function() {
		
		clearTimeout(this.dropdown_timeout);
		
		if(!this.dropdown_open){
		
			this.dropdown_timeout = setTimeout(function(){
				
				this.resize_dropdown();

				this.dropdown_open = true;
				
				if(this.items_count > 5) {
				
					this.cart_dropdown.show();
	
					new Effect.Morph(this.cart_dropdown, {
						style: 'height: '+this.dropdown_height+'px;',
						duration: 0.2
					});
				
				} else {
				
					Effect.BlindDown(this.cart_dropdown, {duration: 0.2});
				
				}	

			}.bind(this),200);
			
		}
		
	},
	
	hide_dropdown: function() {
		
		clearTimeout(this.dropdown_timeout);
		
		if(this.dropdown_open){
		
			this.dropdown_timeout = setTimeout(function(){
				
				this.dropdown_open = false;
				
				Effect.BlindUp(this.cart_dropdown, {duration: 0.2});

			}.bind(this),200);
			
		}
		
	},
	
	resize_dropdown:function() {
	
		// count cart items
	  	this.items_count = 0;
	  
	  	$$('.cart-item').each(function(cart_item) {
	  	
	  		this.items_count++;	
	
		}.bind(this));
	
		// if more than 5 items, alter height of cart dropdown
		if(this.items_count >= 5) {
 		    	
			this.list_height = 355;
			this.dropdown_height = (!nx.unboxed ? 410 : 390);
			this.cart_dropdown_list.setStyle('overflow-y:auto;');
			
	
		// else if is greater than 0
		} else if(this.items_count >= 1) {
		
			this.list_height = this.items_count * 70;
			this.dropdown_height = this.list_height+(!nx.unboxed ? 56 : 35);
			this.cart_dropdown_list.setStyle('overflow-y:hidden;');
			
		// else if there are no items		
		} else if(this.items_count==0)  {
		
			this.list_height = 0;
			this.dropdown_height = (!nx.unboxed ? 56 :35);
	
		}
		
		// set heights
		this.cart_dropdown_list.setStyle('height:'+this.list_height+'px;');
		this.cart_dropdown.setStyle('height:'+this.dropdown_height+'px;');
	
	}
  
}); // END Class

