
var _blocked = true;

function addProduct ( objectId , cantidad )
{
	if ( _blocked )
	{
		return;
	}
	_blocked = true;
	if ( typeof cantidad == 'undefined' )
	{
		cantidad = 1;
	}
	$.getJSON ( '/srv/frontTienda/addProduct' , ( { object_id: objectId , cantidad: cantidad } ) , _addProductCallBack );
	
}

function _addProductCallBack ( data )
{
	if ( data.errno == 0 )
	{
		getCesta();
		if ( typeof addCestaCallback == 'function' )
		{
			addCestaCallback ( data.data.product )
		}
	}
	else
	{
		_showError ( data.errstr );
		_blocked = false;
	}
}

function removeProduct ( objectId , cantidad )
{
	if ( _blocked )
	{
		return;
	}
	_blocked = true;
	if ( typeof cantidad == 'undefined' )
	{
		cantidad = -1;
	}
	$.getJSON ( '/srv/frontTienda/addProduct' , ( { object_id: objectId , cantidad: cantidad } ) , _removeProductCallBack );
	
}

function _removeProductCallBack ( data )
{
	if ( data.errno == 0 )
	{
		getCesta();
		if ( typeof removeCestaCallback == 'function' )
		{
			removeCestaCallback ( data.data.product )
		}
	}
	else
	{
		_showError ( data.errstr );
		_blocked = false;
	}
}

function getCesta()
{
	if ( typeof tiendaId == 'undefined' )
	{
		return;
	}
	$.getJSON ( '/srv/frontTienda/getCesta' , ( { tienda_id: tiendaId } ) , _getCestaCallBack );
}

function _getCestaCallBack ( data )
{
	if ( data.errno == 0 )
	{
		_showCesta ( data.data );
	}
	else
	{
		showError ( data.errstr );
	}
	_blocked = false;
}

function _showError ( msg )
{
	alert ( msg );
}

function _showCesta ( cesta )
{
	if ( typeof $('cesta') == 'undefined' )
	{
		alert ( 'no encuentro');
		return;
	}
	var cont = 0;
	var precio = 0;
	for ( var i = 0 ; i < cesta.length ; i++)
	{
		cont += parseInt ( cesta[i].cantidadencesta , 10 );
		if ( cesta[i].impuestos_incluidos_en_precio == '0' )
		{
			cesta[i].precioFinal = parseFloat ( cesta[i].precio ) * ( 1 + ( parseFloat ( cesta[i].impuestos ) / 100 ) );
		}
		else
		{
			cesta[i].precioFinal = parseFloat ( cesta[i].precio );
		}
		precio += parseInt ( cesta[i].cantidadencesta , 10 ) * parseFloat ( cesta[i].precioFinal );
	}
	$('#cesta').empty();
	
	$('#cesta').append ( '<div id="contador"></div>' );
	$('#cesta #contador').append ( '<img src="' + _getLiteral ( 'imagen_carrito' ) + '" alt="' + _getLiteral ( 'texto_carrito' ) + '">	<strong>' + cont + "</strong> " + ( cont == 1 ? _getLiteral ( 'unidad' ) : _getLiteral ( 'unidades' ) ) );
	if ( cont > 0 )
	{
		$('#cesta').append ( '<ul class="item_listado"></ul>' );
		for ( var i = 0 ; i < cesta.length ; i++)
		{
			$('#cesta ul').append ( '<li class="item"></li>' );
			$('#cesta ul li:last').append ( "<span class=\"item_titulo\">" + cesta[i].nombre + "</span>" );
			$('#cesta ul li:last').append ( " - " );
			$('#cesta ul li:last').append ( "<span id=\"item_precio\">" + _formatNumber ( cesta[i].precioFinal , 2 ) + ' ' + ( cesta[i].precio == 1 ? _getLiteral ( 'moneda' ) : _getLiteral ( 'monedas' ) ) + "</span>" );
			$('#cesta ul li:last').append ( " - " );
			$('#cesta ul li:last').append ( "<span id=\"item_cantidad\">" + cesta[i].cantidadencesta + ' ' + ( cesta[i].cantidadencesta == 1 ? _getLiteral ( 'unidad' ) : _getLiteral ( 'unidades' ) ) + "</span>" );
			$('#cesta ul li:last').append ( ' <a href="#" onclick="removeProduct ( ' + cesta[i].object_id + ' , -' + cesta[i].cantidadencesta + ' ); return false;"><img src="' + _getLiteral ( 'imagen_eliminar' ) + '" alt="' + _getLiteral ( 'texto_eliminar' ) + '" border="0"></a>');
		}
		$('#cesta').append ( '<p id="carrito_coste">' + _getLiteral ( 'total' ) + ' - <strong>' + _formatNumber ( precio , 2 ) + ' ' + ( precio == 1 ? _getLiteral ( 'moneda' ) : _getLiteral ( 'monedas' ) ) + '</strong></p>' );
		$('#cesta').append ( '<p><a href="/srv/frontTienda/checkout?tienda_id=' + tiendaId + ( typeof tpl != 'undefined' ? '&tpl=flv' : '' ) + '">' + _getLiteral ( 'proceder') + '</a></p>');
	}
	else
	{
		$('#cesta').append ( '<p id="vacio">' + _getLiteral ( 'mensaje_vacio' ) + '</p>' );
	}
}

$(document).ready(function() {
   getCesta();
 } );

var _literales = {};
_literales['unidad'] = 'unidad';
_literales['unidades'] = 'unidades';
_literales['moneda'] = 'euro';
_literales['monedas'] = 'euros';
_literales['mensaje_vacio'] = 'Por ahora no tienes publicaciones en el carrito';
_literales['imagen_carrito'] = '/frontTienda/img/carrito.png';
_literales['texto_carrito'] = 'Carrito de publicaciones';
_literales['imagen_eliminar'] = '/frontTienda/img/ic_borrar.gif';
_literales['texto_eliminar'] = 'Eliminar del carrito este elemento';
_literales['total'] = 'Total';
_literales['proceder'] = 'Proceder a la compra';

function _getLiteral ( key )
{
	return typeof literales != 'undefined' && typeof literales[key] != 'undefined' ? literales[key] : _literales[key];
}

function _formatNumber (num, decplaces)
{
   num = parseFloat(num);
   if (!isNaN(num))
   {
      var sign = '';
      if ( num < 0 )
      {
         num = - num;
         sign = '-';
      }
      var str = "" + Math.round (eval(num) * Math.pow(10,decplaces) + 1e-10 );
      if (str.indexOf("e") != -1) {
         return "Out of Range";
      }
      while (str.length <= decplaces) {
         str = "0" + str;
      }
      str = sign + str;
      var decpoint = str.length - decplaces;
      return ( str.substring(0,decpoint) == '-' || str.substring(0,decpoint) == '' ? str.substring(0,decpoint) + '0' : str.substring(0,decpoint) ) + ( decplaces > 0 ? "." + str.substring(decpoint,str.length) : '' );
   } else {
      return "NaN";
   }
}


