﻿//Class - CIndicesPanel
function CIndicesPanel(sUserControlID /*String*/, sObjIndicesPriceName /*String*/, sLang /*String*/, 
                                     sIndice_Day_Texts/*String[]*/, sIndice_Night_Texts/*String[]*/) {

    /* Private variables */
    var sUserControlID = sUserControlID;

    var iTimerID_IndiceDay;
    var iTimerID_IndiceNight;

    var iIndiceType; //1=Day, 2=Night

    var lnkCh_to_Day; //<a></a>
    var lnkCh_to_Night; //<a></a>

    var lblCurrentTime; //span
    
    var sLang = sLang; //Current langauge: "big5", "gb", "en"
    var _this = this; //this instance
    
    /* Constructor() */
    {
        lnkCh_to_Day = document.getElementById('lnkCh_to_Day');
        lnkCh_to_Night = document.getElementById('lnkCh_to_Night');
        lblCurrentTime = document.getElementById(sUserControlID + 'lblCurrentTime');

        lnkCh_to_Day.href = "javascript:" + sObjIndicesPriceName + ".ChangeIndiceType(1);";
        lnkCh_to_Night.href = "javascript:" + sObjIndicesPriceName + ".ChangeIndiceType(2);";
        
    } //end of constructor

    /* public void Start() */
    this.Start = function() {

        iIndiceType = 1;

        setTimeout(this.RefreshIndices_Day_Core, 5000);
    }
    
    /* public void ChangeIndiceType(int)  */
    this.ChangeIndiceType = function(iNewIndiceType) {
        if (iNewIndiceType == iIndiceType)
            return;

        iIndiceType = iNewIndiceType;

        //Clear two timer 
        clearTimeout(iTimerID_IndiceDay);
        clearTimeout(iTimerID_IndiceNight);

        if (iIndiceType == 1)
            this.RefreshIndices_Day_Core();
        else
            this.RefreshIndices_Night_Core();

    }

    /* public void RefreshIndices_Day_Core() */ //Refresh Indices (DAY) by AJAX
    this.RefreshIndices_Day_Core = function() {
        
        //1. Call AJAX for get Indices
        var xmlHttp = GetXmlHttpObject();

        xmlHttp.onreadystatechange = function() {
            if (xmlHttp.readyState == 4) {

                if (xmlHttp.status == 200) {
                    if (iIndiceType == 1) {
                        var sReturnContent = xmlHttp.responseText;
                        _this.FillIndicesResult_DAY(sReturnContent);
                    }
                }

                //5. Release object and set timeout
                xmlHttp = null;
                iTimerID_IndiceDay = setTimeout(_this.RefreshIndices_Day_Core, 5000);

            }
        }

        //2. Setup the object (Sync)
        xmlHttp.open("GET", "ajax/ajaxIndices.aspx?type=1", true);
        xmlHttp.setRequestHeader("If-Modified-Since", "0");
        xmlHttp.send(null);

    }  //end of RefreshIndices_Day_Core


    /* public void FillIndicesResult_DAY(String) */ //Fill Indices result (DAY)
    this.FillIndicesResult_DAY = function(sResult) {
        var sPriceArr = sResult.split("#"); //sResult.split("\r\n");

        for (var i = 1; i <= 11; i++) {
            FullOneIndices(sPriceArr[i - 1], sIndice_Day_Texts[i - 1],
                sUserControlID + 'labInd' + i + 'A',
                sUserControlID + 'lab' + i + 'B',
                sUserControlID + 'lab' + i + 'C',
                sUserControlID + 'imgInd' + i);
        }
        //Fill the current refresh time
        lblCurrentTime.innerHTML = sPriceArr[11];
    }


    /* public void FillIndicesResult_NIGHT(String) */ //Fill Indices result (NIGHT)
    this.FillIndicesResult_NIGHT = function (sResult) {
        var sPriceArr = sResult.split("#"); // sResult.split("\r\n");

        for (var i = 1; i <= 11; i++) {
            FullOneIndices(sPriceArr[i - 1], sIndice_Night_Texts[i - 1],
                sUserControlID + 'labInd' + i + 'A',
                sUserControlID + 'lab' + i + 'B',
                sUserControlID + 'lab' + i + 'C',
                sUserControlID + 'imgInd' + i);
        }

        //Fill the current refresh time
        lblCurrentTime.innerHTML = sPriceArr[11];
    }

    //Fill Indices result 2
    function FullOneIndices(sPriceStr, sCaption, 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 = "";
    
    }



    /* public void RefreshIndices_Night_Core() */ //Refresh Indices (NIGHT) by AJAX
    this.RefreshIndices_Night_Core = function () {

        //1. Call AJAX for get Indices
        var xmlHttp = GetXmlHttpObject();

        xmlHttp.onreadystatechange = function() {
            if (xmlHttp.readyState == 4) {

                if (xmlHttp.status == 200) {
                    if (iIndiceType == 2) {
                        var sReturnContent = xmlHttp.responseText;
                        
                        _this.FillIndicesResult_NIGHT(sReturnContent);
                    }
                }

                //5. Release object and set timeout
                xmlHttp = null;
                iTimerID_IndiceNight = setTimeout(_this.RefreshIndices_Night_Core, 3500);

            }
        }

        //2. Setup the object (Sync)
        xmlHttp.open("GET", "ajax/ajaxIndices.aspx?type=2", true);
        xmlHttp.setRequestHeader("If-Modified-Since", "0");
        xmlHttp.send(null);

    } //end of RefreshIndices_Day_Core
    
    
} //end of class