/**
 * (c) COPYRIGHT AVIATION SOFTWARE INC. 2003-2011
 */

top.ProgressBar = ProgressBar = function ProgressBar() {
    this.percentage = 0;
    this.width = 120;
    this.layer = null;
    this.bar = null;
    this.status = null;
    this.debugging = ( top.debug && top.debug.log ) ? true : false;
}

ProgressBar.prototype.getBarPosition = function() {
    return ( this.width * ( this.percentage / 100 ) ) - this.width;
}

ProgressBar.prototype.setStatus = function ( status ) {
    if ( this.status == null ) return;
    this.status.update(status);
}

ProgressBar.prototype.setPercentage = function( percentage ) {
    var pct = toNumeric(percentage);
    if ( pct > 100 ) pct = 100;
    if ( pct < 0 ) pct = 0;
    this.percentage = pct;
    var position = this.getBarPosition();
    if ( this.bar == null ) return;
    /*
    var level = percentage/100 * 3;
    var imgIdx = 3;
    if ( level >= 1 && level < 2 ) {
        imgIdx = 2;
    }
    else if ( level >= 2 ) {
        imgIdx = 1;
    }
    */
    var imgIdx = this.percentage >= 100 ? '1' : '';
    var e = new Effect.Morph( this.bar,
        {
            style: {
                backgroundPosition: '' + position + 'px 0px'
            },
            duration: 0.8,
            transition: Effect.Transitions.linear
        }
    );
    var roundedPercentage = this.getRoundedPercentage();
    this.bar.setStyle(
        {
            backgroundImage: 'url(../images/progressbar/percentImage_back' + imgIdx + '.png)'
            /*backgroundImage: 'url(../images/progress_bar_wide_trans' + imgIdx + '.gif)'*/
        }
    );
    Element.writeAttribute( this.bar,
        {
            title: '' + roundedPercentage + '%',
            alt  : '' + roundedPercentage + '%'
        }
    );
}

ProgressBar.prototype.getRoundedPercentage = function () {
    return Math.round(this.percentage * 10) / 10;
}

ProgressBar.prototype.init = function( id ) {
    var elem = $(id);
    if ( elem.hasClassName('progressbar')) {
        if ( this.debugging ) {
            top.debug.log('valid progressbar', this);
        }
        this.layer = elem;
        var found = this.layer.select('img#pimg_' + this.layer.identify());
        this.bar = found.first() || null;
        found = this.layer.select('span#pstatus_' + this.layer.identify());
        this.status = found.first() || null;
    } else {
        if ( this.debugging ) {
            top.debug.log('Need to create a progressbar', this);
        }
        var containerElem = new Element('span',
            {
                'class': 'progressbar',
                id: id,
                width: this.width + 'px'
            }
        );
        containerElem.setStyle(
            {
                width: this.width + 'px'
            }
        );
        var roundedPercentage = this.getRoundedPercentage();
        var imgElem = new Element('img',
            {
                title: roundedPercentage + '%',
                id: 'pimg_' + id,
                src: '../images/progressbar/percentImage.120.png',
                alt: roundedPercentage + '%',
                'class': 'progressbarImg'
            }
        );
        imgElem.setStyle(
            {
                width: this.width + 'px',
                backgroundPosition: '' + this.getBarPosition() + 'px 0px',
                /*backgroundImage: 'url(../images/progress_bar_wide_trans.gif)',*/
                backgroundImage: 'url(../images/progressbar/percentImage_back.png)'
            }
        );
        var statusElem = new Element('span',
            {
                'class': 'embeddedstatus',
                id: 'pstatus_' + id
            }
        );
        this.status = statusElem;
        containerElem.insert(imgElem);
        containerElem.insert(statusElem);

        this.layer = Element.replace( elem, containerElem );
        this.bar = imgElem;
    }
}

