var cl = "ajaxccart";

function ccartClick(id_product, index, is_ete)
{
	var div = $('#'+cl+'-id'+id_product);
	div.ccartdata = '';

	ccartClose();
	if (div.css('display') == 'block')
		return false;
	else
	{
		hlToProduct(id_product);
		ccartCallAjax(id_product, div, index, is_ete);
	}
	return false;
}

function ccartCallAjax(id_product, div, index, is_ete)
{
	ccartDisp(1, div, index);
	if (div.ccartstop != true)
	{
		$.ajax({
			url: url,
			type: 'POST',
			data: 'id_product='+parseInt(id_product)+'&id_category='+id_category_main+(is_ete ? '&ete=1' : ''),
			dataType: 'json',
			success: function (data, status) {
				if (data.error != '')
					div.ccartdata = 'Error: '.data.error;
				else
				{
					div.ccartdata = data.data;
					div.ccartstop = true;
				}
				ccartAjaxDisp(div);
			},
			error: function (status, data) {
				div.ccartdata = 'Ajax error: '.data;
			}
		});
	}
	else
		ccartAjaxDisp(div);
}

function ccartClose()
{
	closeAll();
	return false;
}

function ccartDisp(action, div, index)
{
	$('.ajaxccart').stop(true, true);
	if (action == 1)
	{
		$('.ajaxccart-ajax').hide();
		$('.ajaxccart-def').show();
		$('.ajaxccart').hide();
		if ((index + 1) % 4 == 0 && parseInt(div.css('left')) > 0)
			div.css('left', (parseInt(div.css('left')) - div.width() - 160)+'px');
		div.show('slow');
	}
	else
		div.hide();
}

function ccartAjaxDisp(div)
{
	$('.ajaxccart').stop(true, true);
	var id = div.attr('id');
	$('#'+id+' .ajaxccart-def').hide();
	$('#'+id+' .ajaxccart-ajax').html(div.ccartdata);
	$('#'+id+' .ajaxccart-ajax').show();
	setTimeout('loadajaxcart("'+id+'");', 50);
}

function loadajaxcart(id_div)
{
	//$('a.available').unbind('click');
	$('#'+id_div+' a.available').click(function() {

		var pid = $(this).attr('alt');

		var pr = $(this).attr('rel').split(';');

		var id_category_main = $('#id_category_main_'+pid).attr('value');

		switch(currency_format) {
			case 1:
			case 3:
				$('#price_'+pid).html(currency_sign + parseFloat(pr[0]).toFixed(2));
				$('#old_price_'+pid).html(currency_sign + parseFloat(pr[1]).toFixed(2));
				break;
			default:
				$('#price_'+pid).html(parseFloat(pr[0]).toFixed(2) + ' ' + currency_sign);
				$('#old_price_'+pid).html(parseFloat(pr[1]).toFixed(2) + ' ' + currency_sign);
				break;
		}

		$('#ajax_ipa_'+pid).val($(this).attr('href'));
		// $(this).removeClass('available');
		// $('div.select-size a.selected_'+parseInt(id_category_main)).addClass('available');
			// $('div.select-size a').removeClass('selected_'+parseInt(id_category_main));
		// $(this).addClass('selected_'+parseInt(id_category_main));

		return false;
	});

	$('#'+id_div+' .ajax_add_cart').submit(function() {
			one_size = $('p.one-size-'+$(this.id_product).attr('value'));
			if (($(this.id_product_attribute).attr('value') == '' || $(this.id_product_attribute).attr('value') == 0) && (one_size.length == 0 || !one_size))
			{
				$('a#inline').fancybox().trigger('click');
				return false;
			}
			else
				ajax_cart($(this.id_product).attr('value'), $(this.id_product_attribute).attr('value'), $(this.quantity).attr('value'), id_div);
			return false;
		});

}

function ajax_cart(idProduct, idCombination, quantity, id_div)
{
	var addedFromProductPage = true;

	undercart = true;
	$.ajax({
		type: 'GET',
		url: baseDir + 'cart.php',
		async: true,
		cache: false,
		dataType : "json",
		data: 'add&ajax=true&qty=' + ( (quantity && quantity != null) ? quantity : '1') + '&id_product=' + idProduct + '&token=' + static_token + ( (parseInt(idCombination) && idCombination != null) ? '&ipa=' + parseInt(idCombination): ''),
		success: function(jsonData)
		{
			//apply 'transfert' effect
			$('#'+id_div+' .add-to-cart-btn').TransferTo({
						to: 'header-cart',
						className: 'transferProduct',
						duration: 1000,
						complete: function () {
							if (jsonData.hasError)
							{
								var errors = '';
								for(error in jsonData.errors)
									//IE6 bug fix
									if(error != 'indexOf')
										errors += jsonData.errors[error] + "\n";
								alert(errors);
							} else {
								var total = jsonData.total;
								var ntotal = jsonData.nbTotalProducts;
								$('#header-cart').html('<a href="'+baseDir+'order.php">'+ ntotal +' '+article_trad+(ntotal > 1 ? 's' : '')+'</a><br /><a href="'+baseDir+'order.php">'+total+'</a>');
							}

						}
			});

		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {
			alert("TECHNICAL ERROR: unable to add the product.\n\nDetails:\nError thrown: " + XMLHttpRequest + "\n" + 'Text status: ' + textStatus);
			//reactive the button when adding has finished
			if (addedFromProductPage)
				$('body#product p#add_to_cart input').removeAttr('disabled').addClass('exclusive').removeClass('exclusive_disabled');
			else
				$('.ajax_add_to_cart_button').removeAttr('disabled');
		}
	});
}

function imagebox(id_prod, index, src)
{
	$('.imagebox-div').stop(true, true);
	var div = $('#imagebox-id'+id_prod);
	imageboxClose();
	if (div.css('display') == 'block')
		return false;
	hlToProduct(id_prod);
	if ((index) % 4 >= 2 && parseInt(div.css('left')) > 0)
		div.css('left', (parseInt(div.css('left')) - div.width() - 180)+'px');
	var img = loadimage(src);
	div.show('slow');
	img.onload = function() {
			$('#imagebox-id'+id_prod+' img.prod_img').attr('src', src);
			$('#imagebox-id'+id_prod+' img.prod_img').show('slow');
		};
	return false;
}

function imageboxClose()
{
	closeAll();
	return false;
}

function closeAll()
{
	$('.imagebox-div').stop(true, true);
	$('.imagebox-div').hide('slow');
	$('.ajaxccart').stop(true, true);
	$('.ajaxccart').hide();
	$('.to-product-page').removeClass('hl');
	return false;
}

function hlToProduct(id)
{
	$('#to-product-page-id'+id).addClass('hl');
}

function loadimage(url)
{
	var img = new Image();
	img.src = url;
	return img;
}
