var cartOpen = false;
$(document).ready(function() {
	if($('#CartFullViewContainer').length > 0) {
		$('.submit').click(function(e) {
			if((!$.browser.msie && e.button == 0) || ($.browser.msie && e.button == 1)) {
				$.get('http://be.bruneautech.com/nl/ajax/cartFullViewActions.php', { action: 'refreshCart' }, function(data) {
					$('#CartFullViewContainer').replaceWith(data);
				}, "html");
			}
		});
		deleteAllGifts();
	}
});

function cartPopUpOpen(prodId, openingElement, quantity, award) {
	var openingElement  = (openingElement == null) ? false : openingElement;
	if (quantity == null) {
		if (openingElement) {
			var placingElementWidth = 204;
			popUpPlace(openingElement, placingElementWidth);
		}
	}
	var data = {
		prodId: prodId
	};
	if (quantity != null) {
		data['q'] = quantity;
	}
	if (award != null) {
		data['award'] = '';
	}

	$.get('http://be.bruneautech.com/nl/ajax/cartAddPopUp.php', data, function(data) {
		if (quantity) {
			window.location = '/cart.php';
		} else {
			$('#thePopUp').html(data);
			popUpOpen();
		}
	}, 'html');
}

function cartPopUpNext(prodId,q) {
	loadFile('get', 'http://be.bruneautech.com/nl/ajax/cartAddPopUp.php', 'prodId='+prodId+'&q='+q, 'text', cartPopUpLastStep);
	$('#addToCartPopUpStepTwoQ').html(q);
	$('#addToCartPopUpStepTwoTotal').html($('#priceTotalOrderPopup').html());
	$('#addToCartPopUpStepTwo').css({ 'display': 'block'});
	$('#addToCartPopUpStepOne').css({ 'display': 'none' });
}
/*Return of popup after ajax call (last step)*/
/*place data (confirmation) in thePopUP and autoclose the popup*/
function cartPopUpLastStep(data) {
	$('#thePopUp').html(data);
  popUpClose(2, 'cartAddPopUpCounter', true);
	$.get('http://be.bruneautech.com/nl/ajax/cartSmallViewActions.php', {}, function(data){
		$('#cart').replaceWith(data);
	},'text');

}
function cartDelete( cartRowId, award ) {
	var data = {
		action    : 'delete',
		cartRowId : cartRowId
	};
	if (award != null) {
		data.award = 1;
	}
	$.get('http://be.bruneautech.com/nl/ajax/cartFullViewActions.php', data, function( data ) {
		$('#CartFullViewContainer').replaceWith(data);
		setTimeout('reloadSmallCart()', 500);
	});

	//loadFile('get', 'http://be.bruneautech.com/nl/ajax/cartFullViewActions.php', 'action=delete&cartRowId='+cartRowId + extraParam, 'text', 'CartFullViewContainer');
	//setTimeout( 'reloadSmallCart()' , 500 );
}
function reloadSmallCart() {
	$.get('http://be.bruneautech.com/nl/ajax/cartSmallViewActions.php', {}, function(data){
		$('#cart').replaceWith(data);
	},'text');
}

function calculateTotalsOfCartFullView(prodId, award) {
	var prodId = (prodId == null) ? false : prodId;
	extraParam = "";

	if(award != null) {
		extraParam = "award";
		if(prodId) {
			var quantity = makeInt($('#quantity'+prodId).val());
			if (quantity < 1) quantity = 1;
		}
	} else {
		if(prodId) {
			var quantity = makeInt($('#quantity'+htmlIdIfy(prodId)).val());
			if (quantity < 1) quantity = 1;
		}
	}

	// Do not bitch at me for not using Atalanta. It b0rks on '#' in prodId. (Passing an object instead of having to assemble your own string is much more logical, anyway.)
	var data = {
		action    : 'changeQ',
		reference : prodId,
		quantity  : quantity,
		locale    : brutech.locale
	};

	if(extraParam) data.award = '';

	$.get('http://be.bruneautech.com/nl/ajax/cartFullViewActions.php', data, function(data) {
			$('#CartFullViewContainer').replaceWith(data);
		}, 'html');
	setTimeout('reloadSmallCart()', 500);
}

function deleteAllGifts() {
	$('.deleteAllGifts').live('click', function(e) {
		if((!$.browser.msie && e.button == 0) || ($.browser.msie && e.button == 1)) {
			$.get('http://be.bruneautech.com/nl/ajax/cartFullViewActions.php', { action: 'deleteAllGifts' }, function(data) {
				$('#CartFullViewContainer').replaceWith(data);
				setTimeout( 'reloadSmallCart()' , 500 );
			}, "html");
		}
	});
}

function closeNotification() {
	popUpClose(0, false, true);
}

