/**
 * LoadFile.js
 * 
 * @author Kevin Lindsey
 * @version 1.0
 * @copyright 2001-2002, Kevin Lindsey
 * @license http://www.kevlindev.com/license.txt
 */

/*
 * class variables 
 */
LoadFile.VERSION = 1.1;

/**
 * LoadFile
 * 
 * @constructor
 * @param {String} url
 * @param {String} file
 * @param {Number} increment
 * @param {SVGElement} oldNode
 */
function LoadFile(url, file, increment, oldNode) {
    this.url             = url;
    this.file            = file;
    this.increment       = ( increment < 2 ) ? 2 : increment;
    this.oldNode         = oldNode;
    this.currentPosition = 0;
    this.totalBytes      = 0;
    this.buffer          = "";
    this.progress        = null;
    this.status          = null;
    this.state           = "";

    this.makeProgressBar();
}

/**
 * makeProgressBar
 */
LoadFile.prototype.makeProgressBar = function() {
    var svgRoot = svgDocument.documentElement;
    var xml =
        '<g>' +
            '<rect width="100%" height="100%" fill="black"/>' +
            '<g transform="translate(100,100)">' +
                '<rect width="100" height="20" fill="none" stroke="white"/>' +
                '<rect id="progress" height="20" fill="gray"/>' +
                '<text id="status" x="50" y="1.25em" fill="white" text-anchor="middle">0%</text>' +
            '</g>' +
        '</g>';

    svgRoot.appendChild( parseXML(xml, svgDocument) );

    this.progress = svgRoot.getElementById("progress");
    alert(this.progress);
    this.status   = svgRoot.getElementById("status").firstChild;

    this.loadFile();
};

/**
 * loadFile
 */
LoadFile.prototype.loadFile = function() {
    this.state = "getFileSize";
    this.send("-1", "-1");
};

/**
 * send
 * 
 * @param {Number} start
 * @param {Number} length
 */
LoadFile.prototype.send = function(start, length) {
    start  = ( start  != null ) ? start  : this.currentPosition;
    length = ( length != null ) ? length : this.increment;
    
    var params = [
        "file="+this.file,
        "start="+start,
        "length="+length
    ];
    
    // params should have special characters converted to hex encoding
    // would be a good idea to wrap this in a try/catch block to
    // catch security violations
    getURL(this.url + "?" + params.join("&"), this);
};

/**
 * operationComplete
 * 
 * @param {Object} status
 */
LoadFile.prototype.operationComplete = function(status) {
    if ( status.success ) {
        switch (this.state) {
            case "getFileSize": this.setFileSize(status.content); break;
            case "getChunk":    this.update(status.content);      break;
            default:
                alert("Unrecognized state: " + this.state);
        }
    } else {
        this.status.data = "getURL() error";
        // should report error here
    }
}

/**
 * setFileSize
 * 
 * @param {String} content
 */
LoadFile.prototype.setFileSize = function(content) {
    if ( content.match(/^done/) ) {
        // should report error here
    } else {
        this.totalBytes = content;

        // switch to file reading state and initiate file reading
        this.state = "getChunk";
        this.send();
    }
};

/**
 * update
 * 
 * @param {String} content
 */
LoadFile.prototype.update = function(content) {
    if ( content.match(/^done/) ) {
        this.replaceNode();
    } else {
        this.buffer += content;
        this.currentPosition += content.length;
        this.updateProgress();

        // get next chunk of file
        this.send();
    }
}

/**
 * updateProgress
 */
LoadFile.prototype.updateProgress = function() {
    var percent = Math.round(this.currentPosition / this.totalBytes * 100);

    percent = ( percent <= 100 ) ? percent : "100";
    this.progress.setAttributeNS(null, "width", percent);
    this.status.data = percent + "%";
}

/**
 * replaceNode
 */
LoadFile.prototype.replaceNode = function() {
    var parent  = this.oldNode.parentNode;
    var newNode = parseXML(this.buffer, svgDocument);
    
    parent.replaceChild(newNode, this.oldNode);
}
