﻿GNgui.ProgressBar = Class.create();
GNgui.ProgressBar.prototype =
{
    Value : 0,
    PrecentValue : 0,
    onComplete : null,

    TimerSpeed : 100,

    initialize: function (Id, Parent, Left, Top, Width,
                          InitialValue, BGColor, Precent)
    {
        if (Width == null || Width > 500) Width = 500;
        
        var obj = null;
        this.Id = Id;
        this.Width = Width;
        this.Precent = Precent;
        
        this.Obj = ce$('div', this.Id, 'ProgressBarFace', Parent);
        this.Obj.style.visibility = 'hidden';
        this.Obj.Class = this;
        ltwh$ (this.Obj, Left, Top, Width, 15);
        if (BGColor != null)
            this.Obj.style.background = BGColor;
        
        obj = ce$('div', this.Id + '_Face', 'ProgressBarFace', this.Obj);
        ltwh$ (obj, 0, 0, 0, 15);
        obj.style.overflow = 'hidden';
        obj.style.background = '#FF0000';

        obj = ce$('div', this.Id + '_Begin', 'ProgressBarFace', this.Obj);
        obj.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='./" + TARGET + "/Images/ProgressBar_B.png')";
        ltwh$ (obj, -1, 0, 6, 15);

        obj = ce$('div', this.Id + '_Middle', 'ProgressBarFace', this.Obj);
        obj.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='./" + TARGET + "/Images/ProgressBar_M.png', sizingMethod='crop')";
        ltwh$ (obj, 5, 0, Width - 10, 15);

        obj = ce$('div', this.Id + '_End', 'ProgressBarFace', this.Obj);
        obj.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='./" + TARGET + "/Images/ProgressBar_E.png')";
        ltwh$ (obj, Width - 5, 0, 6, 15);
 
        if (this.Precent == true)
        {
            obj = ce$('div', this.Id + '_Percent', 'ProgressBarPrecent', this.Obj);
            ltwh$ (obj, 0, 0, Width, 15);
            obj.innerText = "(0 %)";
        }
        
        if (InitialValue != null)
            this.SetValue(InitialValue);
   },

    SetValue: function (Value)
    {
        this.PrecentValue = Value;
        this.Value =  (Value * this.Width) / 100;
        w$ (this.Id + '_Face', this.Value);

        if (this.Precent == true)
            $e(this.Id + '_Percent').innerText = roundFixedDecimal(Value, 2) + '%';

        var tmpPrecent = this.PrecentValue / 100;
        var tmpRed = LeadingZero(GetHEX(255 * (1 - tmpPrecent)), 2);
        var tmpGreen = LeadingZero(GetHEX(255 * tmpPrecent), 2);
        $e(this.Id + '_Face').style.background = '#' + tmpRed + tmpGreen + '00';
        
        if (this.onComplete != null && parseInt(this.PrecentValue) == 100)
        {
            this.onComplete();
            this.Obj.style.visibility = 'hidden';
        }
    },

    AnimateValue: function (Value, Steps)
    {
        this.Obj.style.visibility = 'visible';
        if (this.Animation != null && this.Animation.Timer != null)
            clearTimeout(this.Animation.Timer);

        this.Animation = new Object();
        this.Animation.FinalValue = (Value * this.Width) / 100;
        this.Animation.Steps = (this.Animation.FinalValue - this.Value) / Steps;
        this.Animation.Timer = setTimeout("ProgressBarAnimation(" + this.Id + ")", this.TimerSpeed);

        if (this.Animation.Steps == 'NaN' || this.Animation.Steps == 0)
            clearTimeout(this.Animation.Timer);
    },
    
    CancelAnimation : function ()
    {
        if (this.Animation != null && this.Animation.Timer != null)
            clearTimeout(this.Animation.Timer);
    }
}

function ProgressBarAnimation (obj)
{
    var tmpCls = obj.Class;
    var tmpObj= $e(tmpCls.Id + '_Face');
    var tmpWidth = w$(tmpObj) + tmpCls.Animation.Steps;

    if (((tmpCls.Animation.Steps < 0) && (tmpWidth <= tmpCls.Animation.FinalValue)) ||
        ((tmpCls.Animation.Steps > 0) && (tmpWidth >= tmpCls.Animation.FinalValue)))
        tmpWidth = tmpCls.Animation.FinalValue;
    else
    {
        tmpCls.Animation.Timer = setTimeout("ProgressBarAnimation(" + tmpCls.Id + ")", tmpCls.TimerSpeed);
    }

    tmpCls.SetValue((tmpWidth * 100) / tmpCls.Width);
}