function tienda() {
  // Estado del carrito
  this.cart = new Object();
  // numero de productos
  this.cart.ndp = 0;
  // importe en centimos
  this.cart.imp = 0;
}

tienda.prototype.init = function() {
  this.iframe = document.getElementById('operacion');
}

tienda.prototype.userIn = function() {
  // Carga en ese marco la autorización.
  this.iframe.src = '/user/auth';
};

tienda.prototype._userIn = function(name) {
  if (name) {
    var anon = document.getElementById('anonimo');
    if (anon) {
      anon.style.display = 'none';
    }

    var iden = document.getElementById('identificado');
    if (iden) {
      var dstmode = location.hostname.substr(0,3);
      iden.innerHTML = 'Bienvenid@ '+name+' <span>//</span> <a href="/shop/user?op=get&envelope=micuenta&p1='+dstmode+'" target="escenario">Mi cuenta</a>';
      iden.style.display = '';
      
      if (top.escenario.location.pathname.indexOf('/usuario') != -1) {
        top.escenario.location.replace('/portada.html');
      }
      
      this.loggeduser = name;
      
      this.refreshCart('info',name+' identificado con éxito.',2);
    }
  } else {
    top.escenario.location.replace('/usuarioanonimo.html');
  }
};

tienda.prototype.refreshCart = function(t,m,o) {
  this.msgt = t;
  this.msgm = m;
  this.msgo = o;
  this.iframe.src="/shop/cart?op=get&envelope=status";
};

// cantidad productos, suma importe total
tienda.prototype._refreshCart = function(c, t) {
  this.cart.ndp = c;
  this.cart.imp = t;
  this.mensaje(this.msgt, this.msgm, this.msgo);
  this.msgt = null;
  this.msgm = null;
  this.msgo = null;
};

tienda.prototype.verificarUsuario = function(field) {
  if (field.value && !field.validate('out')) {
    this.iframe.src="/user/exist?name="+field.value;
  }
};

tienda.prototype._verificarUsuario = function(r) {
  if (r) {
    this.mensaje('error', 'El nombre de usuario indicado ya está en uso, por favor, escoja otro.', 5);
  } else {
    this.mensaje('info', 'El nombre de usuario indicado está libre, puede utilizarlo.', 5);
  }
  this.iframe.src="";
}

// identificador de producto, cantidad
tienda.prototype.addToCart = function(p, c) {
  if (this.loggeduser) {
    c = c? c : 1;
  
    this.iframe.src="/shop/cart?op=add&idproducto="+p+"&quantity="+c;
  } else {
    top.escenario.location = '/usuarioanonimo.html';
  }
};

// texto de error, total de productos, importe total en centimos
tienda.prototype._addToCart = function(e, t, i) {
  if (e) {
    this.mensaje('error', e, 3);
  } else {
    // Actualiza estado del carrito.
    this.cart.ndp = t;
    this.cart.imp = i;
// *** Error en IE   this.mensaje('info', 'Producto agregado a su carrito.<br><p style="text-align: center;"><a href="javascript:top.tienda.vercarrito();"><img src="/img/boton.vercarrito.veigeoscuro.gif" alt="Ver carrito" border="0" /></a> &nbsp; <img src="/img/boton.seguircomprando.veigeoscuro.gif" alt="Ver carrito" border="0" /></p>', 30);
    this.mensaje('info', 'Producto agregado a su carrito.<br><br><a href="javascript:top.tienda.vercarrito();"><img src="/img/boton.vercarrito.veigeoscuro.gif" alt="Ver carrito" border="0"></a> &nbsp; <img src="/img/boton.seguircomprando.veigeoscuro.gif" alt="Seguir comprando" border="0">', 30);
  }
};

// tipo (info, error, aviso), mensaje (html), tiempo de presencia (segundos)
tienda.prototype.mensaje = function(t,m,o) {
  t = t? t : 'info';
  o = o? o : 4;
  
  var a = document.getElementById('msgare');
  var i = document.getElementById('msgico');
  var p = document.getElementById('msgtxt');

  if (a && p && i) {
    var j = top.LWjsLib.remember('tomsg');
    if (j) {
      clearTimeout(j);
    }
    if (t && m && p) {
      i.src = '/img/icono.'+t+'.gif';
      p.innerHTML = m;
      top.LWjsLib.remind(top.setTimeout('top.tienda.mensaje()', o*1000), 'tomsg');
      a.style.display = 'inline';
    } else {
      a.style.display = 'none';
    }
  }

  // Actualiza estado de carrito.
  var s = document.getElementById('statxt');
  if (s) {
    var imp;
    if (this.cart.imp > 0) {
      imp = this.cart.imp/100;
      var ent = Math.floor(imp);
      var dec = Math.round((imp-Math.floor(imp))*100);
      // Pasa a cadena todo.
      dec = String(dec);
      if (dec.length < 2) {
        dec = "0"+dec;
      }
      if (dec.length > 2) {
        dec = dec.substr(0,2);
      }
      imp = ent+","+dec;
    } else {
      imp = "0,00";
    }

    s.innerHTML = 'Estado del carrito: <span>'+this.cart.ndp+'</span> productos, sumando un total de <span>'+imp+' &euro;</span>';
  }
};

tienda.prototype._usuarioAnonimo = function() {
  top.escenario.location = '/usuarioanonimo.html';
};

tienda.prototype.vercarrito = function() {
  if (this.loggeduser) {
    top.escenario.location = '/shop/cart?op=get&envelope=vercarrito';
  } else {
    top.escenario.location = '/usuarioanonimo.html';
  }
};

// id producto, cantidad
tienda.prototype.cambiarCantidad = function(p, c) {
  c = c? String(c) : '1';

  c = parseInt(c, 10);
  
  if (isNaN(c) || c < 1) {
    this.mensaje('error', 'Cantidad indicada no es correcta.', 3);
  } else {
    this.iframe.src="/shop/cart?op=add&idproducto="+p+"&quantity="+c+"&replace=on&envelope=cambiarcantidad";
  }
}

// texto de error, total de productos, importe total en centimos
tienda.prototype._cambiarCantidad = function(e, t, i) {
  if (e) {
    this.mensaje('error', e, 3);
  } else {
    // Actualiza estado del carrito.
    this.cart.ndp = t;
    this.cart.imp = i;
    top.escenario.location.reload();
  }
};

top.tienda = new tienda();
