
$(document).ready(function () {
	// Add to cart
	$("div#penseNetAjoutRapide").mouseover(function () {
		$(this).addClass("hover");
	});
	$("div#penseNetAjoutRapide").mouseout(function () {
		$(this).removeClass("hover");
	});
	$("input#inputPNAjoutRapide").mouseover(function () {
		return false;
	});

	$("div#penseNetAjoutRapide").click(function () {
		var quickAddInput = $("input#inputPNAjoutRapide");
		addToCart(quickAddInput.val(), 1, quickAddInput, null, ajouteRapideUnknownArticle);
	});

	$("input#inputPNAjoutRapide").keydown(function (event) {
		if (event.keyCode == 13) {
			$("div#penseNetAjoutRapide").click();
			event.stopImmediatePropagation();
			return false;
		} else {
			$("div#penseNetAjoutRapide").removeClass("articleError");
			return true;
		}
	})

	function ajouteRapideUnknownArticle() {
		$("div#penseNetAjoutRapide").addClass("articleError");
	}

	$("input#inputPNAjoutRapide").click(function (event) {
		// Don't pass the click event up to the search button behind.
		event.stopPropagation();
	})
});

// Adds en item to the cart and gives a graphical indication
function addToCart(codeArticle, quantity, sourceElement, successCallback, unknownArticleCallback, bannerCode) {
	if (codeArticle.length = 6) {
		var elements = [[codeArticle, ((quantity && quantity != null) ? quantity : '1')]];
		return addMultipleToCart(elements, sourceElement, successCallback, unknownArticleCallback, bannerCode);
	}else{
		return false;
	}
}

// Adds en item to the cart and gives a graphical indication
function addMultipleToCart(elements, sourceElement, successCallback, unknownArticleCallback, bannerCode) {
	// Send the list of articles to AddToCart.ashx in the form articles=code|quantity,code|quantity...
	var elementsString = "articles=";
	for(var i = 0; i<elements.length; i++){
		if(i>0){
			elementsString += ",";
		}
		elementsString += elements[i][0] + "|" + elements[i][1];
	}
	if (bannerCode != undefined) {
		elementsString += "&bannerCode=" + bannerCode;
	}
	$.ajax({
		type: 'GET',
		url: '/services/rest/AddToCart.ashx',
		data: elementsString,
		dataType: "json",
		error: function (obj, status, error) {
			alert("Impossible de communiquer avec le serveur.  Vous avez peut-être perdu votre connexion internet");
		},
		success: function (jsonData) {
			if (jsonData == null) {
				alert("Impossible de communiquer avec le serveur.  Vous avez peut-être perdu votre connexion internet");
			} else {
				if (!jsonData.codesOk && unknownArticleCallback != undefined && unknownArticleCallback != null) {
					unknownArticleCallback();
					return;
				}
				if (jsonData.addedAll) {
					showArticleAdded(sourceElement, jsonData.newArticleCount, jsonData.newCartTotal);
					if (successCallback != undefined && successCallback != null) {
						successCallback();
					}
				} else {
					alert("Impossible d'ajouter l'article au panier");
				}
			}
		}
	});
}


function showArticleAdded(sourceElement, newArticleCount, newCartTotal) {
	//$(".ui-effects-transfer").fadeTo(0.5);
	sourceElement.effect("sendToCart",
											{ to: $("img.imgPanier") },
											3000,
											function () {
												updatePanier(newArticleCount, newCartTotal);
												$("img.imgPanier").attr("src", "/Extranet/images/Commun/Bandeau/caddieAnime.gif");
											});
}

function updatePanier(newArticleCount, newCartTotal) {
	// Update the summary details.  We do this even if the panier is visible, just in case it takes a while to refresh the 
	// page... at least the PensNet will be up-to-date.
	$("span.nbArticles").text(newArticleCount);
	$("input.valeurPanier").val(newCartTotal + " \u20AC");
	if (typeof (panierVisible) != "undefined" && panierVisible === true) {
		__doPostBack();
	}
}

// Copied from jQueryUI Transfer
(function ($, undefined) {

	$.effects.sendToCart = function (o) {
		return this.queue(function () {
			var elem = $(this),
			target = $(o.options.to),
			endPosition = target.offset(),
			animation = {
				top: endPosition.top,
				left: endPosition.left,
				height: target.innerHeight(),
				width: target.innerWidth()
			},
			startPosition = elem.offset(),
			transfer = $('<div class="ui-effects-transfer"><img src="/Extranet/images/Commun/Bandeau/ColisCentravet.png" style="height:100%; vertical-align:middle; text-align:center;" /></div>')
				.appendTo(document.body)
				.addClass(o.options.className)
				.css({
					top: startPosition.top,
					left: startPosition.left,
					height: elem.innerHeight(),
					width: elem.innerWidth(),
					position: 'absolute'
				})
				.animate(animation, o.duration, o.options.easing, function () {
					transfer.remove();
					(o.callback && o.callback.apply(elem[0], arguments));
					elem.dequeue();
				});
		});
	};

})(jQuery);


