basket = {

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

	buttonOffset: null,

	messageBox: null,
	paymentOngoing: false,
	registerInitCallback: function(func) {
		basket.initCallback = func;
	},

	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'},function(){

			if($.isFunction(basket.initCallback)){
				basket.initCallback();
			}

		});
		/**
		 * @todo next lines should be callback to load, to avoid rac conditions
		 */
		$('.display-basket input').keyup(basket.updateQty);
		$('.remove-from-basket').click(basket.remove);
		basket.checkBasketState(0, false);
	},
	onsiteInit: function(){
		$('.remove-from-basket').click(basket.remove);
		userloginBasket.init(); //another special setting for enb
		basket.checkBasketState(500);
		if(basket.paymentOngoing){
			basket.hideOrderlines();
		}
	},
	add: function () {
		basket.buttonOffset = $(this).offset();
		if(!basket.paymentOngoing){
			$.get(basket.url,{request: 'add', productId: $(this).attr('butik:productid')} ,basket.update);
		}else{
			basket.messageBox = $('#msg');
			basket.msgShow(3);
		}
	},

	update: function (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);
			//Do nothing (special Ennnybog)
		}
		else if(data.indexOf('<!-- PRODUCT ADDED -->') > -1){
			//basket.msgShow(0);
			 //used only in enb shop (should perhaps be moved elsewhere)
			 if($('.wishlist-co-container').length > 0){
			 	document.location.reload(document.location.href);
			 }else{
			 	basket.foldOut(500, true);
			 }
		}
		else{
			 //used only in enb shop (should perhaps be moved elsewhere)
			if($('.basketItem').size()==0) {
				basket.foldIn(500);
			}
			//Do nothing
		}

	},

	updateBasketAndDisplay: function(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);
	},

	remove: function(){
		if($(this).hasClass('in_checkout')){
			$.get(basket.url, {request: 'remove', productId: $(this).attr('butik:productId')}, function(){document.location.reload(document.location.href)});
		}else{
			$.get(basket.url, {request: 'remove', productId: $(this).attr('butik:productId')}, 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.';
				break;
			case 3:
				theText = 'Du kan ikke tilføje varer til kurven når betaling er i gang.';
				break;
		}
		basket.messageBox.html(theText);
		basket.messageBox.show('fast');
		/**
		 * @todo move css declaration to view
		 */

		basket.messageBox.css({display:'block', left: basket.buttonOffset.left - 913, top: basket.buttonOffset.top-190});
		basket.messageBox.click(function(){basket.messageBox.hide()});
		basket.messageBox.fadeOut(5000);
	},

//Used in enb shop
	foldOut: function(speed, newproduct){
		if($("#basketHolder").css("left")=="646px"){
			var newProd = newproduct;
			$("#basketHolder").animate( { left:"810px"}, speed, 'linear', function(){
				if(newProd){
					basket.msgShow(0);
				}
			});
		}else if(newproduct){
			basket.msgShow(0);
		}
		$.cookie('ennybogBasket','open', {path : '/'});
	},
	foldIn: function(speed){
		if($("#basketHolder").css("left")=="810px"){
			$("#basketHolder").animate( { left:"646px"}, speed, 'linear');
		}
		if($('#basketItems').children('.basketItem').length == 0){
			$.cookie('ennybogBasket','', {path : '/'})
		}else{
			$.cookie('ennybogBasket','open', {path : '/'});
		}
	},
	checkBasketState: function(speed, newproduct){
		if($.cookie('ennybogBasket')!='open'){
			basket.foldIn(0);
		}else{
			basket.foldOut(0);
		}
	},

	toggle : function() {
		if($.cookie('ennybogBasket')!='open'){
			basket.foldOut(500);
		}else{
			if($('#basketItems').children('.basketItem').length == 0){
				basket.foldIn(500);
			}
		}
	},
	hideOrderlines : function(){
		$('#basketWrap').html('<div class="payment_ongoing">Betaling i gang...</div><div id="msg"></div>');
	}
}

$(basket.init);