basket = {

	url: 'index.php?eID=tx_butik_eid1',

	buttonOffset: null,

	messageBox: null,

	init: function () {
		jQuery.ajaxSetup({cache:false});
		$('.add-to-basket').click(basket.add)
		//$('.top-basket-data').load(basket.url,{request: 'smallBasket'})
		$('#basket').load(basket.url,{request: 'smallBasket'})
		$('.display-basket input').keyup(basket.updateQty);
	},

	add: function () {
		basket.buttonOffset = $(this).offset();
		//$.get(basket.url,{request: 'add', productId: $(this).attr('butik:productid')} ,basket.update);
		$.get(basket.url,{request: 'add', productId: $(this).attr('name')} ,basket.update);

	},
	
	add2: function(vnr,quantity,leverandoer) {
		$.get(basket.url,{request: 'add', productId: vnr,qty: quantity,lev: leverandoer} ,basket.update);
	},
	
	addmany: function(vnr,quantity) {
		$.get(basket.url,{request: 'addmany', productId: vnr,qty: quantity} ,basket.update);
	},

	pickup: function(vnr,quantity) {
		$.get(basket.url,{request: 'updateBasketAndDisplay2', productId: vnr,qty: quantity}, basket.updateBasketAndDisplay);
		//$.get(basket.url,{request: 'pickupstatus', productId: vnr,qty: quantity} ,basket.updateBasketAndDisplay);
	},

	update: function (data) {
		//$('.top-basket-data').html(data);
		//console.log(data);
		$('#basket').html(data);
		basket.messageBox = $('#msg');
		if(data.indexOf('<!-- SOLD OUT -->') > -1){
			basket.msgShow(1);
		}
		else if(data.indexOf('<!-- IN BASKET -->') > -1){
			basket.msgShow(2);
		}
		else if(data.indexOf('<!-- PRODUCT ADDED -->') > -1){
			basket.msgShow(0);
		}
		else{
			//Do nothing
		}

	},
	
	updateBasketAndDisplay: function(data) {
		//console.log(data);
		basket.update(data);
		// should be handled in a similar manner, see comment in ButikBasketDisplayView

		$('.display-basket').load(butik_url.basketUrl+' .display-basket > *',function(){$('.display-basket input').keyup(basket.updateQty);});

	},

	updateQty: function (event) {
		value = parseInt($(this).val());


		if(!isNaN(value)) {
				//Changed the algorith, so updateBasketAndDisplay simply returns Raw HTML, not JSOn encodend stuff.
				//$.getJSON(basket.url,{request: 'updateBasketAndDisplay', productId: $(this).attr('name').substr(2),qty: $(this).val()}, basket.updateBasketAndDisplay);
				$.get(basket.url,{request: 'updateBasketAndDisplay', productId: $(this).attr('name').substr(2),qty: $(this).val()}, basket.updateBasketAndDisplay);
			}
	},

	empty: function() {
		$.get(basket.url,{request: 'clear'},basket.update);
	},

	msgShow: function(what) {
		switch(what){
			case 0:
				theText = 'Vare tilføjet til kurv';
				break;
			case 1:
				theText = 'Varen er desværre udsolgt';
				break;
			case 2:
				theText = 'Varen findes allerede i kurven<br />Vis kurv for at ændre antallet af en vare.';
				break;
		}
		basket.messageBox.html(theText);
		basket.messageBox.show('fast');
		/**
		 * @todo move css declaration to view
		 */
		basket.messageBox.css({display:'block',top:basket.buttonOffset.top+10, left:basket.buttonOffset.left+10});
		basket.messageBox.click(function(){basket.messageBox.hide()});
		basket.messageBox.fadeOut(5000);
	}
}

$(basket.init);