/*
Ticker.js
Hace uso de Helper.js
Basado en stock-ticker.js de Doron Rosenberg
ver http://devedge.netscape.com/toolbox/examples/2001/stock-ticker/
*/

function Ticker(name, element, shiftBy, interval, idTicker)
{
  this.name     = name;
  this.element  = element;
  this.shiftBy  = shiftBy ? shiftBy : 1;
  this.interval = interval ? interval : 100;
  this.idTicker = idTicker;
  this.runId	= null;
  this.div      = null;
}

Ticker.prototype.initialize = function () {
    // el dom debe estar completo al momento de esta llamada
    this.div = document.getElementById(this.element);

    // remove extra textnodes that may separate the child nodes
    // of the ticker div
    var node = this.div.firstChild;
    var next;

    while (node) {
        next = node.nextSibling;
        if (node.nodeType == 3)
            this.div.removeChild(node);
        node = next;
    }

    // Primer posicionamiento
    this.left = 0;
    if (this.div.childNodes.length > 0) {
        this.shiftLeftAt = this.div.firstChild.offsetWidth;
        this.div.style.height = this.div.firstChild.offsetHeight;
        this.div.style.width = 2 * screen.availWidth;
        this.div.style.visibility = 'visible';
        this.start();
    }
}

Ticker.prototype.start = function()
{
  this.stop();
  
  this.left -= this.shiftBy;

  if (this.left <= -this.shiftLeftAt)
  {
    this.left = 0;
    this.div.appendChild(this.div.firstChild);
  
    this.shiftLeftAt = this.div.firstChild.offsetWidth;
  }

  this.div.style.left = (this.left + 'px');

  this.runId = setTimeout(this.name + '.start()', this.interval);
}

Ticker.prototype.stop = function()
{
  if (this.runId)
    clearTimeout(this.runId);
    
  this.runId = null;
}

Ticker.prototype.changeInterval = function(newinterval)
{
  if (typeof(newinterval) == 'string')
    newinterval =  parseInt('0' + newinterval, 10); 
	
  if (typeof(newinterval) == 'number' && newinterval > 0)
    this.interval = newinterval;
    
    this.stop();
    this.start();
}

Ticker.prototype.changeItemData = function(idEspecie, ult, pvar)
{
    var nodo;
    
    /* Actualizar variacion porcentual */
    nodo = document.getElementById("TickerVar_" + idEspecie);
    if (nodo != null)
    {
        if (pvar != null)
        {
            Helper.setInnerHTML(nodo, "%" + Helper.formatNumber(Math.abs(pvar), "0.00"));
            if (pvar == 0)
                nodo.className = "TickerVar TickerVarEqual";
            else if (pvar > 0)
                nodo.className = "TickerVar TickerVarUp";
            else
                nodo.className = "TickerVar TickerVarDown";
        }
        else
        {
            Helper.setInnerHTML(nodo, "(sin info)");
            nodo.className = "TickerVar";
        }
    }
        
    /* Actualizar precio */
    nodo = document.getElementById("TickerPrecio_" + idEspecie);
    if (nodo != null) 
    {
        if (ult != null)
            Helper.setInnerHTML(nodo, Helper.formatNumber(ult, "0.00"));
        else
            Helper.setInnerHTML(nodo, "&nbsp;");
    }
}

Ticker.prototype.changeStatusText = function(text)
{
    var nodo = document.getElementById("TickerStatusFecha")
    if (nodo != null)
        Helper.setInnerHTML(nodo, text);
}

Ticker.prototype.popupGrafico = function(idEspecie, idTipoGrafico, tituloGrafico)
{
    var _this = this, 
        url = "/SitioNosisWeb/SitioNyMnet/PopupGrafico.aspx"
        + "?IdEspecie=" + idEspecie
        + "&IdTipoGrafico=" + idTipoGrafico
        + "&TituloGrafico=" + escape(tituloGrafico),
        popup = jQuery("<div></div>");

    popup.dialogPlugin({
        width: 590,
        height: 410,
        resizable: false,
        title: "Nosis - NyM - Gráficos",
        intTieneIframe: true,
        intIframeScrolling: "no",
        intSrcIframe: url,
        close: function (event, ui) { _this.limpiarGrafico(popup); }
    });
    popup.dialogPlugin("fn", "open"); ;
}

Ticker.prototype.limpiarGrafico = function(popup) {
    popup.find("iframe").remove();
    popup.dialogPlugin("fn", "destroy").remove();
}
