function getWindowHeight() {
	var windowHeight = 0;
	if (typeof(window.innerHeight) == 'number') {
		windowHeight = window.innerHeight;
	}
	else {
			if (document.documentElement && document.documentElement.clientHeight) {
				windowHeight = document.documentElement.clientHeight;
			}
	}
	return windowHeight;
}

function addCompare(idProduct) {
	$.ajax({
		type: 'GET',
		url: baseDir+ 'modules/compareproducts/compare.php',
		async: true,
		cache: false,
		data: 'add&ajax=true&id_product=' + idProduct + '&token=' + static_token,
		success: function(data)
		{
			//apply 'transfert' effect
			var elementToTransfert = null;
			elementToTransfert = $('#productCompare_' + idProduct);
			elementToTransfert.TransferTo({
						to: $('#contentDockHeader').get(0),
						className:'transferProductCompare',
						duration: 800,
						complete: function () {
							datas = data.split('|');
							if(datas[0] == "NOK")
								window.alert(datas[1]);
							else
								updateVisuel();
						}
			});
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {
			alert("TECHNICAL ERROR: unable to add the product.\n\nDetails:\nError thrown: " + XMLHttpRequest + "\n" + 'Text status: ' + textStatus);
		}
	});
}
function updateVisuel() {
	$.ajax({
		type: 'GET',
		url: baseDir+ 'modules/compareproducts/compare.php',
		async: true,
		cache: false,
		data: 'visuel=true&token=' + static_token,
		beforeSend: function() {
			var loading = '<div style="margin-top: 15px;"><img src="'+baseDir+ 'modules/compareproducts/img/loading.gif"></div>';
			$('#contentDockHeader').html(loading)
		},
		success: function(data)
		{
			$('#contentDockHeader').html(data);
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {
			alert("TECHNICAL ERROR: unable to display products.\n\nDetails:\nError thrown: " + XMLHttpRequest + "\n" + 'Text status: ' + textStatus);
		}
	});
}
function deleteProductComparaison(idProduct, type) {
	if(!type) type="";
	$.ajax({
		type: 'GET',
		url: baseDir+ 'modules/compareproducts/compare.php',
		async: true,
		cache: false,
		data: 'delete=true&id_product=' + idProduct + '&token=' + static_token,
		success: function(data)
		{
			if(type=="")
				updateVisuel()
			else {
				updateVisuel()
				$.ajax({
					type: 'GET',
					url: baseDir+ 'modules/compareproducts/compare.php',
					async: true,
					cache: false,
					data: 'displayCompare=true&token=' + static_token,
					beforeSend: function() {
						var loading = '<table style="width: 100%; height:100%"><tr><td align="center"><img src="'+baseDir+ 'modules/compareproducts/img/loading.gif"></td></tr></table>';
						$('#resultCompare').html(loading)
					},
					success: function(data)
					{
						if(data=="NOK")
							closeCompare()
						else
							$('#resultCompare').html(data)
					},
					error: function(XMLHttpRequest, textStatus, errorThrown) {
						alert("TECHNICAL ERROR: unable to delete the product.\n\nDetails:\nError thrown: " + XMLHttpRequest + "\n" + 'Text status: ' + textStatus);
					}
				});
			}
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {
			alert("TECHNICAL ERROR: unable to delete the product.\n\nDetails:\nError thrown: " + XMLHttpRequest + "\n" + 'Text status: ' + textStatus);
		}
	});
}
function deleteComparaisonList() {
	$.ajax({
		type: 'GET',
		url: baseDir+ 'modules/compareproducts/compare.php',
		async: true,
		cache: false,
		data: 'deleteList=true&token=' + static_token,
		success: function(data)
		{
			updateVisuel()
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {
			alert("TECHNICAL ERROR: unable to delete the product.\n\nDetails:\nError thrown: " + XMLHttpRequest + "\n" + 'Text status: ' + textStatus);
		}
	});
}
function compareProducts() {
	$('#resultCompare').html("")
	$.ajax({
		type: 'GET',
		url: baseDir+ 'modules/compareproducts/compare.php',
		async: true,
		cache: false,
		data: 'countCompare=true&token=' + static_token,
		success: function(data)
		{
			if(data=="OK") {
				var windowHeight = getWindowHeight();
				if(windowHeight>document.body.offsetHeight)
					var HeightMasque = windowHeight;
				else
					var HeightMasque = document.body.offsetHeight;
				$("#masqueCompare").css("display", "block");
				$("#masqueCompare").animate({ 
					opacity: 0.4 
					}, 200, function() {
						$("#resultCompare").height(windowHeight - 20)
						$("#resultCompare").css("top", "10px");
						$("#resultCompare").css("display", "block");
						$("#resultCompare").animate({ 
						opacity: 1
					}, 200, function() {
						var windowHeight = getWindowHeight();
						if(windowHeight>document.body.offsetHeight)
							var HeightMasque = windowHeight;
						else
							var HeightMasque = document.body.offsetHeight;
						$("#masqueCompare").height(HeightMasque)
						$.ajax({
							type: 'GET',
							url: baseDir+ 'modules/compareproducts/compare.php',
							async: true,
							cache: false,
							data: 'displayCompare=true&token=' + static_token,
							beforeSend: function() {
								var loading = '<table style="width: 100%; height:100%"><tr><td align="center"><img src="'+baseDir+ 'modules/compareproducts/img/loading.gif"></td></tr></table>';
								$('#resultCompare').html(loading)
							},
							success: function(data)
							{
								if(data=="NOK")
									closeCompare()
								else
									$('#resultCompare').html(data)
							},
							error: function(XMLHttpRequest, textStatus, errorThrown) {
								alert("TECHNICAL ERROR: unable to delete the product.\n\nDetails:\nError thrown: " + XMLHttpRequest + "\n" + 'Text status: ' + textStatus);
							}
						});
					});
				});
			}
			else {
					alert(data);
			}
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {
			alert("TECHNICAL ERROR: unable to delete the product.\n\nDetails:\nError thrown: " + XMLHttpRequest + "\n" + 'Text status: ' + textStatus);
		}
	});
}
function closeCompare() {
	$("#resultCompare").fadeTo("fast", 0, function() {
		$("#masqueCompare").fadeTo("normal", 0, function() {
			$("#masqueCompare").css("display", "none");
			$("#resultCompare").css("display", "none");
		});
	});
}
function printComparaisonList() {
	$.ajax({
		url: baseDir+ 'modules/compareproducts/compare.php',
		type: 'post', 
		async: true,
		cache: false,
		data: 'displayCompare=true&token=' + static_token,
		success: function(data)
		{
			var f = $('#bcp_iframe');
			f.contents().find('head').append('<link href="' + baseDir + 'modules/compareproducts/css/compareproducts.css" rel="stylesheet" type="text/css" />');
			f.contents().find('body').css('color', '#000000');
			f.contents().find('body').css('background-color', '#FFFFFF');
			f.contents().find('body').css('padding', '0');
			f.contents().find('body').html(data);
			window.frames['bcp_iframe'].print();
		}
	});
}
$(document).ready(function() {
	updateVisuel()
	window.onresize = function() {
		var windowHeight = getWindowHeight();
		if(windowHeight>document.body.offsetHeight)
			var HeightMasque = windowHeight;
		else
			var HeightMasque = document.body.offsetHeight;
		$("#masque").height(HeightMasque);
		$("#resultCompare").height(windowHeight - 20);
	}
});


