basket = {

	url: 'index.php?eID=tx_butik_eid1',
	basketSelector: '#basket',
	buttonOffset: null,
	language: 0,
	messageBox: null,
	paymentOngoing: false,
	forceFolding: false,
	registerInitCallback: function(func) {
		basket.initCallback = func;
	},

	init: function () {
		basket.url = basket.url + "&L=" + basket.language;
		jQuery.ajaxSetup({cache:false,type: "POST"});

		jQuery.post(basket.url,{request: 'smallBasket'},function(data){
			jQuery(basket.basketSelector).html(data.basketHtml);
			if(jQuery.isFunction(basket.initCallback)){
				basket.initCallback();
			}
		},"json");
		jQuery('.add-to-basket').click(basket.add);

		/**
		 * @todo next lines should be callback to load, to avoid rac conditions
		 */
		//jQuery('.display-basket input').keyup(basket.updateQty);
		jQuery('.remove-from-basket').click(basket.remove);



	},
	onsiteInit: function(){
		jQuery('.remove-from-basket').click(basket.remove);
	},
	add: function () {
		jQuery.post(basket.url,{request: 'add', productId: jQuery(this).attr('butik:productid')} ,basket.update, "json");
	},

	update: function (data) {
		
		jQuery(basket.basketSelector).html(data.basketHtml);
		if(data.message) {
			basket.msgShow(data.message);
		}
	},

	updateBasketAndDisplay: function(data) {
		basket.update(data);
		// should be handled in a similar manner, see comment in ButikBasketDisplayView
		jQuery('.display-basket').load(butik_url.basketUrl+' .display-basket > *',function(){jQuery('.display-basket input').keyup(basket.updateQty);});

	},

	updateQty: function (event) {
		value = parseInt(jQuery(this).val());
		if(!isNaN(value)) {
				//Changed the algorith, so updateBasketAndDisplay simply returns Raw HTML, not JSOn encodend stuff.
				//jQuery.getJSON(basket.url,{request: 'updateBasketAndDisplay', productId: jQuery(this).attr('name').substr(2),qty: jQuery(this).val()}, basket.updateBasketAndDisplay);
				jQuery.post(basket.url,{request: 'updateBasketAndDisplay', productId: jQuery(this).attr('name').substr(2),qty: jQuery(this).val()}, basket.updateBasketAndDisplay,"json");
		}
	},

	empty: function() {
		jQuery.post(basket.url,{request: 'clear'},basket.update,"json");
	},

	remove: function(){
		if(jQuery(this).hasClass('in_checkout')){			
			jQuery.post(basket.url, {request: 'remove', productId: jQuery(this).attr('butik:productId')}, function(){document.location.reload(document.location.href);}, "json");
		}else{
			jQuery.post(basket.url, {request: 'remove', productId: jQuery(this).attr('butik:productId')}, basket.update, "json");
		}
	},

 	msgShow: function(message) {
		// open a welcome message as soon as the window loads
		alert(message);
		/*
		Shadowbox.open({
			//content:    "<div id='lightbox'><h1>"+theText+"</h1><img src='/typo3temp/GB/4a5e7df868.png' width='216' height='192' /><p>DKK 499,95-<em>Approximately &euro;30</em></p><span><a href='#'>Tilbage til shoppen</a></span><span><a href='#'>Gå til kassen</a></span></div>",
		    content: message,
			player:     "html",
		    height:     350,
		    width:      450
		});
		*/

	},

	hideOrderlines : function(){
		jQuery('#basketWrap').html('<div class="payment_ongoing">Betaling i gang...</div><div id="msg"></div>');
	}
};

jQuery(basket.init);

