var basketTimeout = null;

bizplace.cartShow = function(callback) {
	clearTimeout(basketTimeout);
//	J('#wholeBasket').addClass('basketActive');
//	J('#mainBasket').slideDown('normal');
//	J('#wholeBasket').addClass('open');
	if (typeof(callback) == 'function')
		callback();
}
bizplace.cartHide = function(callback) {
//	var wholeBasket = J('#wholeBasket');
//	if (wholeBasket.hasClass('add')) {
//		clearTimeout(basketTimeout);
//		if(J('#basketCover').hasClass('active'))	//remove cover
//			J('#basketCover').fadeOut('fast', function(){
//				J(this).removeClass('active');
//			});
//		J('#newService').slideUp('normal');   //remove small info
//		wholeBasket.removeClass('add');
//	}
//	else if (wholeBasket.hasClass('open')) {
//		J('#mainBasket').slideUp('normal', function() {
//			J('#wholeBasket').removeClass('basketActive');
//		});
//		wholeBasket.removeClass('open');
//	}
	if (typeof(callback) == 'function')
		callback();
}
	
// dodaj do koszyka
bizplace.cartAdd = function(button, planID, options, parent, callback) {
	
	//cover dimensions
//	var coverWidth = J('#body2').width();
//	var coverHeight = Math.max(J('body').height(), J('#body1').height());
//	var oldWidth = J('#content').width();
//	var topOffset = J('#header').height();
	
//	J('#basketCover').addClass('active').width(coverWidth).height(coverHeight).css({'top': 0 - topOffset, 'left': oldWidth - coverWidth}).fadeTo('fast',0.6);
	
	bizplace.JSON({cart: {action: 'cartadd', id: planID, options: options, parent: parent}}, function(data) {
		bizplace.orderCheckout();
		bizplace.cartUpdate(data.cart);
//		bizplace.cartShow();
//		bizplace.cartHide(function() {
//			var helperPosition = J(button).offset();
//			helperPosition.top -= 100;
//			var wholeBasket = J('#wholeBasket');
//			var basketHelper = J('#basketHelper');
//			var basketPosition = wholeBasket.offset();
//			basketHelper.css(helperPosition)
//			.addClass(bizplace.plans[planID].cssclass)
//			.fadeIn('normal')
//			.animate({
//				left: (basketPosition.left + 20) + 'px',
//				top: (basketPosition.top - 20) + 'px'
//			}, 800, 'swing', function() {
//				J('#wholeBasket').addClass('basketActive').addClass('add');
//				bizplace.cartShow();
//				J('#newService').slideDown(0, function() {
//					basketTimeout = setTimeout(function() {
//						bizplace.cartHide();
//					}, 5000);
//				});
//				J(this).fadeOut('fast').removeClass(bizplace.plans[planID].cssclass);
//			});
//			if (typeof callback == 'function')
//				callback();
//		});
//		bizplace.cartUpdate(data.cart);
	});
}

//usun z koszyka
bizplace.cartRemove = function(elem) {
	var typeOfService = elem.attr('class');
	var mtc = elem.attr('id').match(/^(remove)-(\d+)(-(.+))?/);
	bizplace.JSON({cart: {action: 'cartremove', id: mtc[2], options: {domain_name: mtc[4]}}}, function(data) {
		if(typeOfService == 'hostingRemove'){
			elem.parents('.chosenHosting').fadeOut(500, function(){
				bizplace.cartUpdate(data.cart);	
			});
		}
		else if(typeOfService == 'removeDomain'){
			elem.parent().fadeOut(500, function(){
				bizplace.cartUpdate(data.cart);
			});
		}
	});
}

// aktualizacja html koszyka
bizplace.cartUpdate = function(jsonData) {
	// zapamietaj
	bizplace.cart = jsonData;
	// update podsumowania
//	J('#wholeBasket .smallSummary').html(': ' + jsonData.totalcount + ' Usług za ' + (jsonData.total + ' ' + bizplace.config.currency.iso_alfa));
	// update kwoty do zaplaty
//	J('.cashToPay div .rgh').eq(0).html(jsonData.value + ' ' + bizplace.config.currency.iso_alfa);
//	J('.cashToPay div .rgh').eq(1).html(jsonData.taxvalue + ' ' + bizplace.config.currency.iso_alfa);
//	J('.sumedUp strong').html(jsonData.total + ' ' + bizplace.config.currency.iso_alfa);
	// content koszyka
	var content = J('#wholeBasket .items').html('');
	$p.renderTemplate(
		'<div id="insertionDiv">' +
			'<div class="sectionBotBorder">' + 
				'<h4></h4>' +
			'</div>' +
		'</div>',
		content, jsonData, {
			'div.sectionBotBorder': {
				'element<-items': {
					'.': function(arg){
							var html='';
							html = '<div class="chosenHosting ' + bizplace.plans[arg.item.id].cssclass + '">' +
										'<span class="icon-bkg"><span class="icon-image"></span></span>' +
										'<span class="hostingName">' + arg.item.name + (arg.item.for_trial ? ' ' + '<a class="button-micro button-dark">Test</a>' : '') + '<span>' + arg.item.period_conv + ' ' + arg.item.period_desc + '</span></span>' +
//										'<span class="hostingDetails">' + bizplace.plans[arg.item.id].summary + '</span>' +
										'<span class="hostingInfo hostingVal"><label>Netto</label>' + arg.item.value + ' <span>' + bizplace.config.currency.iso_alfa + '</span></span>' +
										'<span class="hostingInfo hostingVat"><label>+VAT</label>' + arg.item.taxvalue + ' <span>' + bizplace.config.currency.iso_alfa + '</span></span>' +
										'<span class="hostingInfo hostingSum">' + arg.item.total + ' <span>' + bizplace.config.currency.iso_alfa + '</span></span>' +
										'<div class="cleared"></div>' +
									'</div>';
						return html;
					}
				}
			}
	});
	J('.hostingName span, span.removeDomain').unbind('click') //na wszelki wypadek unbind
	.click(function(){
		bizplace.cartRemove(J(this));
	});
	
}
	


