<?xml version="1.0" encoding="UTF-8" ?>
<Module>
  <ModulePrefs 
    title="Estoy Leyendo" 
    scrolling="true"
    height="300"/> 
  <UserPref name="usuario" 
    display_name="Usuario" 
    datatype="string" 
    required = "true"/>
  <Content type="html">
  <![CDATA[ 
  
  <div id=content_div></div>

  <script type="text/javascript"> 
	function stristr (haystack, needle, bool) {
	    var pos = 0;
	 
	    haystack += '';
	    pos = haystack.toLowerCase().indexOf( (needle+'').toLowerCase() );
	    if (pos == -1){
		return false;
	    } else{
		if (bool) {
		    return haystack.substr( 0, pos );
		} else{
		    return haystack.slice( pos );
		}
	    }
	}

	function getHtml1(url) {    
	  var params = {};  
	  params[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.TEXT;  
	  gadgets.io.makeRequest(url, response, params);
	};

	function getHtml2(url) {    
	  var params = {};  
	  params[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.TEXT;  
	  gadgets.io.makeRequest(url, response1, params);
	};

	function response(obj) {  
	 var sourc = obj.text;

	 if (sourc.indexOf("No tiene libros")==-1) {
		libros = stristr(sourc,'<div id="biblioeteca">');
		linkLibro = stristr(libros,'<a href="');
		linkLibro = linkLibro.substr(9,linkLibro.indexOf('">')-9);
		linkLibro=servidor+linkLibro;
		getHtml2(linkLibro);
	 } else {
		html+="<br/>";
		html+="<p>En este momento no estoy leyendo nada</p>";
		html+="<br/>";
		html+="<a href='http://www.biblioeteca.com'>www.biblioEteca.com</a>";
		document.getElementById('content_div').innerHTML = html;
	}
	}

	function response1(obj) {  
	  var otherSource = obj.text;

	  imagenes = stristr(otherSource,'<div id="libro_portada">');
	  linkImagen = stristr(imagenes,'<img src="');
	  linkImagen = linkImagen.substr(10,linkImagen.indexOf('" class="portada"')-10);

	  tit=stristr(otherSource,"<title>");
	  tit=tit.substr(7,tit.indexOf("- BiblioEteca")-7);

	  html+="<h3>";
	  html+="<a href ='"+linkLibro+"' target='_top'>"+tit+"</a>";
	  html+="</h3>";
	  html+="<a href ='"+linkLibro+"' target='_top'><img style='border-style: none;' src='"+linkImagen+"'/></a>";
	  html+="<br/>";
	  html+="<a href='http://www.biblioeteca.com'>www.biblioEteca.com</a>";

	  document.getElementById('content_div').innerHTML = html;
	}

	function getContent () {
	      var url = servidor+"/biblioeteca.web/libros/leyendo/"+usuario;
	      getHtml1(url);
	}
    // Get userprefs
    var prefs = new gadgets.Prefs();

    // Get the array of search terms entered by the user
    var usuario = prefs.getString("usuario");
    var servidor = "http://www.biblioeteca.com";
    var titulo = unescape(prefs.getString("titulo"));

    var html = "";

    if (usuario=="")
    {
      html += "Edita las preferencias para poder usar el gadget.";
    }
    else {
	gadgets.util.registerOnLoadHandler(getContent);
    }
  </script>

  ]]> 
  </Content>
</Module>

