/*********************************************************************************
 * cpe.js es java script que muestra los articulos de la propuesta a la nueva    *
 * Constitución Política del Estado en Bolivia.                                  *
 *                                                                               *
 *    Copyright (C) 2008 Oscar Garrido Gómez (Red Pro Bolivia)                   *
 *                                                                               *
 *   Este programa es software libre: usted puede redistribuirlo y/o modificarlo *
 *   bajo los términos de la Licencia Pública General GNU publicada              *
 *   por la Fundación para el Software Libre, ya sea la versión 3                *
 *   de la Licencia, o cualquier versión posterior.                              *
 *                                                                               *
 *   Este programa se distribuye con la esperanza de que sea útil, pero          *
 *   SIN GARANTÍA ALGUNA; ni siquiera la garantía implícita                      *
 *   MERCANTIL o de APTITUD PARA UN PROPÓSITO DETERMINADO.                       *
 *   Consulte los detalles de la Licencia Pública General GNU para obtener       *
 *   una información más detallada.                                              *
 *                                                                               *
 *   Debería haber recibido una copia de la Licencia Pública General GNU         *
 *   junto a este programa.                                                      *
 *   En caso contrario, consulte <http://www.gnu.org/licenses/>.                 *
 *                                                                               *
 *******************************************************************************'*/

addEvent(window,"load",init_disp);
addEvent(document,"click",show_init);

function addEvent(o,e,f) {
  if (o.addEventListener) {
    o.addEventListener(e,f,false);
    return true;
  }
  else if (o.attachEvent) {
    return o.attachEvent("on"+e,f);
  }
  else {
    return false;
  }
}

function rand (n) {
  return (Math.floor( Math.random ()*n));
}
// Estas variables son configurables:
var char_pause = 0; // el intervalo de tiempo por cada letra en milisegundos
var quote_pause = 8000; // el intervalo de tiempo por cada articulo (página)  en milisegundos
// No cambiar el resto que puede arruinar al programa.
var quoteindex;
var quote,attribution;
var pos;
var nxt;
var box;
var array_len;
var quote_len,attrib_len;
var interval = null;
var busy;
var cursor_span = "<span class=\"quotefont quotecursor\">";
var hide_span = "<span class=\"quotefont hidecolor\">"
var attr_div = "<p></p><div class=\"quotefont attrib\">";

function init_disp() {
  array_len = quotes.length;
  box = document.getElementById("quotebox");
  quoteindex = rand(array_len);
  show_init();
}
function show_init() {
  busy = false;
  clearInterval(interval);
  quote_array = quotes[quoteindex].split("\t");
  quote = quote_array[0];
  attribution = quote_array[1];
  quote_len = quote.length;
  attrib_len = attribution.length;
  quoteindex = (quoteindex+rand(6666)) % array_len;
  pos = 0;
  interval = setInterval('show_attr()',char_pause);
}
function show_quote() {
  pos++;
  if(!busy) {
    busy = true;
    if(pos <= quote_len) {
	nxt = pos + 1;
	if(quote.charAt(pos)=='&') {
	    while (quote.charAt(nxt) != ';') {
		nxt++;
	    }
	    nxt++;
	} else if (quote.charAt(pos)=='<') {
	    while (quote.charAt(nxt) != '>') {
		nxt++;
	    }
	    nxt++;
	}
	box.innerHTML = attr_div + attribution + "</div><br>" + quote.substring(0,pos) +
	    cursor_span +
	    quote.substring(pos,nxt) +
	    "</span>";
	pos = nxt - 1;
    }
    busy = false;
  }
  if(pos > quote_len) {
      box.innerHTML = attr_div + attribution + "</div><br>" + quote;
    pos = 0;
    clearInterval(interval);
    interval = setInterval('show_init()',quote_pause);
  }
}
function show_attr() {
  pos++;
  if(!busy) {
    busy = true;
    if(pos <= attrib_len) {
	if(attribution.charAt(pos)=='&') {
	    while (attribution.charAt(pos) != ';') {
		pos++;
	    }
	    pos++;
	} else if (attribution.charAt(pos)=='<') {
	    while (attribution.charAt(pos) != '>') {
		pos++;
	    }
	    pos++;
	}
      var attr = attribution.substring(0,pos);
      box.innerHTML = attr_div + attr + cursor_span +"</span></div><br>";
    }
    busy = false;
  }
  if(pos > attrib_len) {
      pos = 0;
      busy = false;
    clearInterval(interval);
    interval = setInterval('show_quote()',char_pause);
  }
}

