function strEndsWith( str, suffix)
{
  return (str.indexOf( suffix) == str.length - suffix.length);
}

/* Inicializa el lytebox, myLytebox es la variable global que apunta a la unica instancia del lyteframe 
   que existe */
function initConfigure()
{
  /* Verificamos si existe el myLytebox, si no existe es que se disparó esta función MUY RÁPIDO: lanzamos
   * un timeout, hasta que aparezca myLytebox (ver else) */ 
  if (typeof myLytebox != 'undefined') // Mierda de IE!!!!!! - No se puede hacer: "if (myLytebox)"
  {
    myLytebox.closeOnClick = false;  // Para que no se cierre al hacer clic afuera
    
	// Si tuvimos que lanzar un timeout, lo destruimos
    if (initConfigure.timer)
    {
      clearTimeout( initConfigure.timer);
      delete initConfigure.timer;
    }

	// Lanzamos el auto-register  
    checkAutoRegister();
  }
  else 
  {
	// Si tuvimos que lanzar un timeout ANTES de este, lo destruimos
    if (initConfigure.timer)
    {
      clearTimeout( initConfigure.timer);      
    }
    
    // Esperamos medio segundo hasta que se cree el myLytebox, que demora un poco
    initConfigure.timer = setTimeout( initConfigure, 500);
  }
}

function checkAutoRegister()
{
  // Suponemos que la url es de la forma <sitio>?...&registro=(distribuidor|compraventa)
  var parameterName = 'registro';
  var retailerValue = 'distribuidor';
  var buyerSellerValue = 'compraventa';    
  
  // Si en la url de la pagina esta el parametro register, invocamos al dialogo de registro apropiado
  var url = window.location.href.toLowerCase();
  var pos = url.indexOf( 'registro=');
  if (pos !== false)
  {
    var value = url.substring( pos + 9, url.length);
    
	/* Revisamos todos los anchors de la pagina, buscando el que tenga el atributo rel = 'lyteframe' y la url del 
	 * retailer_box o del buyer_seller_box, al final, si se encuentra, se abre el lytebox de ese anchor,
     * usando myLyteox.start()	 
	 */
    if (value == retailerValue || value == buyerSellerValue)
    {
      var anchors = document.getElementsByTagName( 'a');
      
      var i = 0;
      var found = false;
      var theAnchor = null;
      
      while (i < anchors.length && theAnchor === null)
      {
        var rel = anchors[i].getAttribute( 'rel');
        var href = anchors[i].getAttribute( 'href');
                  
        if (rel == 'lyteframe')
        {
          if ((value == retailerValue && strEndsWith( href, 'retailer_box.html')) || 
              (value == buyerSellerValue && strEndsWith( href, 'buyer_seller_box.html')))
          {
            // Encontramos el anchor
            theAnchor = anchors[i]; 
          }
        }
        
        i++;
      }
      
      if (theAnchor !== null)
      {
        // Encontramos el anchor apropiado: mostramos el lytebox basado en el anchor
        myLytebox.start( theAnchor, false, true);
      }
    }
  }
}

// Se ejecuta al hacer Aceptar en el formulario del retailer
function acceptRetailer()
{
  return $('lbIframe').contentWindow.RetailerBox.sendData();
}

// Se ejecuta al hacer Aceptar en el formulario del buyer_seller
function acceptBuyerSeller()
{
  $('lbIframe').contentWindow.BuyerSellerBox.sendData();
}

/*
 * Registramos el initConfigure() con el onload del documento
 */
if (window.addEventListener) 
{
  window.addEventListener( "load", initConfigure, false);
} 
else if (window.attachEvent) 
{
  window.attachEvent( "onload", initConfigure);
} 
else 
{
  window.onload = function()
  {
    initConfigure();	
	alert('prueba');
  };
}