﻿Type.registerNamespace('RTSJS');

RTSJS.LiveQuotes = function RTSJS$LiveQuotes(nLang, nPos, sIssueCode) 
{
    RTSJS.LiveQuotes.initializeBase(this);
    
    this._issueCode = sIssueCode;
    this._Prefix = "livequotes"+nPos;
    this._nLang = nLang;
    
    this._nStatus = 0;
    
    this.GlobalTimer = null;
    this.nMaxTry = 3;
    this.nLiveQuotesTry = 0;

    if (this.GlobalTimer == null)
    {
        var cb = $CreateCallbackDelegate(this, this.getLiveQuotes, null);
        this.GlobalTimer = new Core.Components.Timer(10000, cb);
        this.GlobalTimer.start();
        this.nLiveQuotesTry = this.nMaxTry;
    }
    
    this._onDataAvailable = $CreateCallbackDelegate(this, this.onDataAvailable, null);
    this._onGetQuotesFailed = $CreateCallbackDelegate(this, this.onGetQuotesFailed, null);
    
    this.getLiveQuotes();
}

function $CreateCallbackDelegate(instance, method, context)
{
   if (instance == null)
      throw Error("Instance must be specified");
 
   if (method == null)
       throw Error("Method must be specified");
 
   return Type.createCallback(Type.createDelegate(instance, method), context);
} 

//function RTSJS$LiveQuotes$getLiveQuotes(e, c) // this ссылается на наш текущий класс, e хранит в себе Event, c - контекст, alert (c.message);
function RTSJS$LiveQuotes$getLiveQuotes()
{
    if (this._nStatus==0)
    {
        this._nStatus=1;
        RTS.LiveQuotes.GetQuotes(this._nLang, this._issueCode, this._onDataAvailable, this._onGetQuotesFailed);
    }
}

function RTSJS$LiveQuotes$getWord(sWord1, sWord2)
{
    if (this._nLang==1) return sWord1; else return sWord2;
}

        
function RTSJS$LiveQuotes$onDataAvailable(xml)
{
    this._nStatus = 0;
    
    this.nLiveQuotesTry = this.nMaxTry;
    if (xml!=null)
    {
        if (document.getElementById(this._Prefix+"_date")!=null)
        {
        document.getElementById(this._Prefix+"_date").innerHTML=this.getWord("на", "on")+" "+xml.getElementsByTagName('rtsdata').item(0).getAttribute('moment');
        }

        var divElement = document.getElementById(this._Prefix+"_rows");
        divElement.innerHTML = "";
        
        var qt = xml.getElementsByTagName("qt");
       
        var oTable = document.createElement("TABLE");
        oTable.setAttribute("width","100%");
        
        var isIE=/*@cc_on!@*/false;
        var elStyle=isIE?"className":"class";
        oTable.setAttribute(elStyle,"table1");
        
        var oTBody = document.createElement("TBODY");
        oTable.appendChild(oTBody);
        
        var oRow = document.createElement("TR");
         
        var oCell = document.createElement("TH");
        oCell.appendChild(document.createTextNode(this.getWord("Покупка (объем)", "Bid (volume)")));
        oRow.appendChild(oCell);
        
        var oCell = document.createElement("TH");
        oCell.appendChild(document.createTextNode(this.getWord("Цена","Price")));
        oRow.appendChild(oCell);
        
        var oCell = document.createElement("TH");
        oCell.appendChild(document.createTextNode(this.getWord("Продажа (объем)", "Ask (volume)")));
        oRow.appendChild(oCell);
        
        oTBody.appendChild(oRow);
        
        if (qt.length>0)
        {
        
            for (var i=0; i<qt.length;i++)
            {
                var oRow = document.createElement("TR");
                oRow.setAttribute("align","right");

                if (qt.item(i).getAttribute("type")=="1")
                {
                    oRow.setAttribute(elStyle,"oqbid");
                    
                    var oCell = document.createElement("TD");
                    oCell.appendChild(document.createTextNode(qt.item(i).getAttribute("qty")));
                    oRow.appendChild(oCell);

                    var oCell = document.createElement("TD");
                    oCell.appendChild(document.createTextNode(qt.item(i).getAttribute("price")));
                    oRow.appendChild(oCell);

                    var oCell = document.createElement("TD");
                    oCell.appendChild(document.createTextNode(" "));
                    oRow.appendChild(oCell);
                }
                else
                {
                    oRow.setAttribute(elStyle,"oqask");
                    
                    var oCell = document.createElement("TD");
                    oCell.appendChild(document.createTextNode(" "));
                    oRow.appendChild(oCell);

                    var oCell = document.createElement("TD");
                    oCell.appendChild(document.createTextNode(qt.item(i).getAttribute("price")));
                    oRow.appendChild(oCell);

                    var oCell = document.createElement("TD");
                    oCell.appendChild(document.createTextNode(qt.item(i).getAttribute("qty")));
                    oRow.appendChild(oCell);
                }
                
                 oTBody.appendChild(oRow);
            
            }
         }
        else
        {
                  var oRow = document.createElement("TR");
                  oRow.setAttribute("align","center");
                  oRow.setAttribute("class","oqask");
                  
                  var oCell = document.createElement("TD");
                  oCell.setAttribute("colSpan","3");
                  if (isIE) oCell.style.setAttribute('padding', '14px'); else oCell.setAttribute("style","padding:14px;");
                  
                  oCell.appendChild(document.createTextNode(this.getWord("нет данных","No data")));
                  oRow.appendChild(oCell);
                  
                   oTBody.appendChild(oRow);
        }
        
        divElement.appendChild(oTable);
    }
}

function RTSJS$LiveQuotes$onGetQuotesFailed(error)
{
    this._nStatus = 0;
    
    this.nLiveQuotesTry--;
    if (this.nLiveQuotesTry==0) this.GlobalTimer.stop();

    // if(typeof userCallback == 'function'){
    //if (this._userErrorCallback !== null) this._userErrorCallback(error);
}

function RTSJS$LiveQuotes$get_code() { 
    if (arguments.length !== 0) throw Error.parameterCount();
    return this._issueCode;
}

RTSJS.LiveQuotes.prototype = 
{
    get_code:   RTSJS$LiveQuotes$get_code,
    getLiveQuotes:    RTSJS$LiveQuotes$getLiveQuotes,
    onDataAvailable:   RTSJS$LiveQuotes$onDataAvailable,
    onGetQuotesFailed: RTSJS$LiveQuotes$onGetQuotesFailed,
    getWord: RTSJS$LiveQuotes$getWord
}
        
RTSJS.LiveQuotes.registerClass('RTSJS.LiveQuotes');

