/*
 *   _____                                 _             _                    
 *  / ____|                               | |           | |                   
 * | |      ___  _ __ ___  _ __   ___  ___| |_  __ _  __| | ___  _ __ ___ ___ 
 * | |     / _ \| '_ ` _ \| '_ \ / _ \/ __| __|/ _` |/ _` |/ _ \| '__/ _ | __|
 * | |____| (_) | | | | | | |_) | (_) \__ \ |_| (_| | (_| | (_) | | |  __|__ \
 *  \_____|\___/|_| |_| |_| .__/ \___/|___/\__|\__,_|\__,_|\___/|_|  \___|___/
 *                        | |                                                 
 *                        |_|
 *                        
 * [cesta.js] iquadrat 2010
 * 
 * 		Script de gestion y control de la cesta de la compra
 *
 */

var oCesta = Array();
var cestaProductos = 0;
var debug = false;

$(document).ready(function(){
	initCesta();
});

function initCesta()
{
	if(debug) debugParamsFunction(arguments);
	var cesta = $.cookie('cesta');
	if(debug) alert("cesta: " + cesta);
	if(cesta) {
		oCesta = cesta.split(",");
		//alert('aa')
		for(var n=0; n<oCesta.length; n++) {
			var oCesta2 = oCesta[n].split(":");
			$("#cesta_" + oCesta2[0]).hide();
			$("#cesta_" + oCesta2[0] + "_off").show();
			//$("#cesta_pack_" + oCesta2[0]).hide();
			//$("#cesta_pack_" + oCesta2[0] + "_off").show();
			cestaProductos += parseInt(oCesta2[1]);
		}
	}
	
	$('.inner_desta_sumar').live('click', function() {
		if(debug) alert(".inner_desta_sumar click();"); 
		var id = this.id;
		if(id != "") {
			id = id.replace("cesta_", "");
			//id = id.replace("pack_", "");
			// TODO verificar que no esta en la cesta
			$("#cesta_"+ id).hide();
			//$("#cesta_pack_"+ id).hide();			
			$("#cesta_" + id + "_off").show();
			//$("#cesta_pack_" + id + "_off").show();
			oCesta.push(id + ":1:");
			$.cookie('cesta', oCesta.join(), { expires: 1, path:'/' });
			//$.cookie('cesta_trans', '', { expires: 1, path:'/' });
			//$.cookie('cesta_pago', '', { expires: 1, path:'/' });
			cestaProductos++;
			actualizaCesta(cestaProductos);
		}
	});
	
	$('.inner_desta_sumar2').live('click', function() {
		var id = this.id;
		if(id != "") {
			id = id.replace("cesta_", "");
			//id = id.replace("pack_", "");
			showPromo(id);
		}
	});
	
	$('.inner_desta_sumar_off').live('click', function() {
		//document.location = '/' + __("cistella-de-la-compra");
		document.location = '/' + "cistella-de-la-compra";
	});
	
	$('.inner_desta_sumar2_off').live('click', function() {
		//document.location = '/' + __("cistella-de-la-compra");
		document.location = '/' + "cistella-de-la-compra";
	});
	
	$(".cesta_barra_pass").click(function(e){
		var id = $(this).attr("id");
		//var url = "/" + __("cistella-de-la-compra");
		var url = "/" + "cistella-de-la-compra";
		if(id != "") {
			url += "/" + id.replace("paso","");
		}
		document.location = url;
	});
	
	if(cestaProductos > 0) {
		actualizaCesta(cestaProductos);
	}
}

function checkCesta()
{
	var cesta = $.cookie('cesta');
	var first = cesta.substring(0,1);
	if(first==",") {
		$.cookie('cesta', cesta.substring(1), { expires: 1, path:'/' });
	}
}

function showPromo(id)
{
	$.fancybox({
		'autoScale': false,
		'autoDimensions': false,
		'transitionIn': 'fade',
		'transitionOut': 'fade',
		'width': 500,
		'height': 300,
		'href': '/productos.php?op=promo&id=' + id,
		'type': 'ajax'
	});
	return false;
}

function cestaMark()
{
	var cesta = $.cookie('cesta');
	if(cesta) {
		oCesta = cesta.split(",");
		for(var n=0; n<oCesta.length; n++) {
			var oCesta2 = oCesta[n].split(":");
			$("#cesta_" + oCesta2[0]).hide();
			$("#cesta_" + oCesta2[0] + "_off").show();
			//$("#cesta_pack_" + oCesta2[0]).hide();
			//$("#cesta_pack_" + oCesta2[0] + "_off").show();
		}
	}
}

function actualizaCesta(nPro)
{
	if(debug) debugParamsFunction(arguments);
	if(nPro > 1) {
		//$("#submenu-cesta").html('<div id="productos_cesta"><font color=white>' + nPro + '</font></div><font color=#5c5c5c>' + __("PRODUCTOS, VER CESTA") + "</font>");
		$("#submenu-cesta").html('<span id="productos_cesta">' + nPro + '</span> productes');
	} else {
		//$("#submenu-cesta").html('<div id="productos_cesta"><font color=white>' + nPro + '</font></div><font color=#5c5c5c>' + __("PRODUCTO, VER CESTA") + "</font>");
		$("#submenu-cesta").html('<span id="productos_cesta">' + nPro + ' </span> producte');
	}
}

function calculateTrans(v, url)
{
	if(debug) debugParamsFunction(arguments);
	if(!url) url = '/' + "cistella-de-la-compra";
	$.cookie('cesta_destino', v, { expires: 1, path:'/' });
	//$.cookie('cesta_trans', '', { expires: 1, path:'/' });
	// $.cookie('cesta_pago', '', { expires: 1, path:'/' });
	document.location = url;
}

function calculateTrans2(v, url)
{
	if(debug) debugParamsFunction(arguments);
	if(!url) url = '/' + "cistella-de-la-compra";
	//$.cookie('cesta_trans', v, { expires: 1, path:'/' });
	document.location = url;
}

function calculatePago(v, url)
{
	if(debug) debugParamsFunction(arguments);
	if(!url) url = '/' + __("cistella-de-la-compra");
	$.cookie('cesta_pago', v, { expires: 1, path:'/' });
	document.location = url;
}

function updateCesta(id, url)
{
	if(debug) debugParamsFunction(arguments);
	if(!url) url = '/' + __("cistella-de-la-compra");
	if(id != "")
	{
		var cantidad = $("#cantidad_" + id).val();
		if(cantidad == "") {
			message(__("Tienes que introducir la cantidad"));
			return;
		}
		if(cantidad == "0") {
			deleteCesta(id);
			return;
		}
		$("#precio_" + id).html('<img src="/js/loading.gif" align="absmiddle" />');
		$("#precio_iva").html('<img src="/js/loading.gif" align="absmiddle" />');
		$("#precio_transporte").html('<img src="/js/loading.gif" align="absmiddle" />');
		$("#precio_subtotal").html('<img src="/js/loading.gif" align="absmiddle" />');
		$("#precio_total").html('<img src="/js/loading.gif" align="absmiddle" />');

		var oCesta = null;
		var cesta = $.cookie('cesta');
		if(cesta) {
			oCesta = cesta.split(",");
			for(var n=0; n<oCesta.length; n++) {
				var oCesta2 = oCesta[n].split(":");
				if(oCesta2[0] == id) {
					if(debug) alert(oCesta2);
					oCesta[n] = oCesta2[0] + ":" + cantidad + ":" + oCesta2[2];
					$.cookie('cesta', oCesta.join(), { expires: 1, path:'/' });
					checkCesta();
					document.location = url;
					break;
				}
			}
		}
	}
	checkCesta();
}

function deleteCesta(id, url)
{
	if(debug) debugParamsFunction(arguments);
	if(!url) url = '/' + __("cistella-de-la-compra");
	if(id != "") {
		$("#linea_" + id).hide();
		var oCesta = null;
		var cesta = $.cookie('cesta');
		if(cesta) {
			oCesta = cesta.split(",");
			for(var n=0; n<oCesta.length; n++) {
				var oCesta2 = oCesta[n].split(":");
				if(oCesta2[0] == id) {
					oCesta.splice(n, 1);
					$.cookie('cesta', oCesta.join(), { expires: 1, path:'/' });
					checkCesta();
					document.location = url;
					break;
				}
			}
		}
	}
}
