﻿
//Class - CIndicesPanel
function CIndicesPanel(sUserControlID_Day /*String*/, sUserControlID_Night /*String*/) {

    /* Private variables */
    var sUserControlID_Day = sUserControlID_Day;
    var sUserControlID_Night = sUserControlID_Night;

    var lblCurrentTime_Day; //span
    var lblCurrentTime_Night; //span

    var _this = this; //this instance

    /* Constructor() */
    {

        lblCurrentTime_Day = document.getElementById(sUserControlID_Day + '_lblCurrentTime');
        lblCurrentTime_Night = document.getElementById(sUserControlID_Night + '_lblCurrentTime');

    } //end of constructor

    /* public void Start() */
    this.Start = function() {
        this.RefreshIndices_DayNight_Core();
    }




    /* public void RefreshIndices_DayNight_Core() */ //Refresh Indices (DAY+NIGHT) by AJAX
    this.RefreshIndices_DayNight_Core = function() {

        //1. Call AJAX for get Indices
        var xmlHttp = GetXmlHttpObject();

        xmlHttp.onreadystatechange = function() {
            if (xmlHttp.readyState == 4) {

                if (xmlHttp.status == 200) {
                    var sReturnContent = xmlHttp.responseText;
                    _this.FillIndicesResult(sReturnContent);
                    
                }

                //5. Release object and set timeout
                xmlHttp = null;
                setTimeout(_this.RefreshIndices_DayNight_Core, 5000);

            }
        }

        //2. Setup the object (Sync)
        xmlHttp.open("GET", "../ajax/ajaxIndicesDayNight.aspx", true);
        xmlHttp.setRequestHeader("If-Modified-Since", "0");
        xmlHttp.send(null);

    } //end of RefreshIndices_Day_Core


    /* public void FillIndicesResult(String) */ //Fill Indices result (DAY+NIGHT)
    this.FillIndicesResult = function(sResult) {

        var sTwoParts = sResult.split("@");

        //Fill day
        FillOnePart(sTwoParts[0], lblCurrentTime_Day, sUserControlID_Day);
        
        //Fill night
        FillOnePart(sTwoParts[1], lblCurrentTime_Night, sUserControlID_Night);
    }

    function FillOnePart(sPart, labTime, sUserControlID) {
        var sPriceArr = sPart.split("#");

        for (var i = 1; i <= 11; i++) {
            FullOneIndices(sPriceArr[i - 1], 
                sUserControlID + '_labInd' + i + 'A',
                sUserControlID + '_lab' + i + 'B',
                sUserControlID + '_lab' + i + 'C',
                sUserControlID + '_imgInd' + i);
        }
        //Fill the current refresh time
        labTime.innerHTML = sPriceArr[11];
    }



    //Fill Indices result 2
    function FullOneIndices(sPriceStr, sLabIndA, sLabIndB, sLabIndC, sImgInd) {

        //sPriceStr = "40,123.45|5099.99|1"
        var sArr = sPriceStr.split("|"); // {"40,123.45", "5099.99", "1"}
        var sImageFileName;
        if (sArr[2] == '2')
            sImageFileName = "arrow_down.png";
        else
            sImageFileName = "arrow_up_gr.png";

        //document.getElementById(sLabIndA).innerHTML = sCaption;
        document.getElementById(sLabIndB).innerHTML = sArr[0];
        document.getElementById(sLabIndC).innerHTML = sArr[1];


        if (sImageFileName != '')
            document.getElementById(sImgInd).src = "../images/" + sImageFileName;
        else
            document.getElementById(sImgInd).src = "";
    
    }
} //end of class


