var cart = {
	dragWidth: null,
	
	cart: null,
	toggler: null,
	items: null,
	buyables: null,
	
	init: function() {
		cart.cart= $('#cart');
		if(cart.cart.length == 0) return;
		
		cart.dragWidth= 100;
		
		cart.toggler= $('#cart #togglecart');
		cart.items= $('#cart .internal');
		cart.submit= $('input[type=image]', cart.items);
		cart.buyString= 'img';
		cart.buyables= $(cart.buyString);
		cart.products= $('#products .product');
		cart.message= $('p.empty', cart.items);
		cart.bought= $('.item', cart.items);
		cart.total= $('.actions .total', cart.items);
		
		cart.toggler.click(function() {
			if(cart.cart.hasClass('open')) {
				cart.close();
			} else {
				cart.open();
			}
			
			return false;
		});
		
		cart.buyables.draggable({
					helper: 'clone',
					delay: 50,
					cursorAt: {left: cart.dragWidth/2, top: cart.dragWidth/2},
					start: function(e, ui) {
						ui.helper.slug= $('span.slug', ui.helper.parent().parent()).text();
						ui.helper.attr('width', cart.dragWidth).attr('height', cart.dragWidth);
						ui.helper.addClass('product-dragging');
						cart.open();
					},
					revert: true
				});
				
		cart.items.droppable({
			accept: cart.buyString,
			activeClass: 'ready',
			hoverClass: 'droppable-hover',
			hover: function(e, ui) {
				// console.log(ui);
				// ui.helper.remove();
			},
			drop: function(e, ui) {
				cart.add(ui.helper.slug);
				ui.helper.remove();
			}
		});
				
		$('a.buy', cart.products).click(function() {
			slug= $('.slug', $(this).parent().parent()).text();
			cart.add(slug);
			return false;
		});
		
		cart.bought.each(function() {
			cart.applyEvents($(this));
		});
		
		// cart.open();
	},
	applyEvents: function(item) {
		$('a.remove', item).click(function() {
			slug= $('.slug', $(this).parent()).text();
			cart.remove(slug);
			return false;
		});
		item.hover(
			function() {
				$(this).addClass('hover');
			},
			function() {
				$(this).removeClass('hover');
			}
		);
	},
	open: function() {
		cart.cart.addClass('open');
		cart.toggler.attr('title', 'Close cart');
		cart.items.show().animate({
			height: '70px'
		});
	},
	close: function() {
		cart.cart.removeClass('open');
		cart.toggler.attr('title', 'Open cart');
		cart.items.show().animate({
			height: '0'
		});
	},
	add: function(slug) {				
		$.get(
			urls.cart,
			{action: "add", slug: slug},
			function(response) {
				cart.items.html(response);
				
				$('.item', cart.items).each(function() {
					cart.applyEvents($(this));
				});
			}
		);
				
		humanMsg.displayMsg('<strong>Success:</strong> <span class="indent">Added one to cart</span>');
				
		cart.open();
	},
	remove: function(slug) {
		$.get(
			urls.cart,
			{action: "remove", slug: slug},
			function(response) {
				cart.items.html(response);
				
				$('.item', cart.items).each(function() {
					cart.applyEvents($(this));
				});
			}
		);
	},
	checkout: function() {
		cart.submit.click();
	},
	url: function(slug) {
		url= 'http://oakleighvermont.com/user/files/products/';
		url= url + slug + '/';
		url= url + 'num1.thumb.png';
		
		return url;
	}
}

$(document).ready(function() {
	cart.init();
});