/******************************************************************
 * FlashAgent.js: requires FlashApplet.js
 *
 * TG NOTES: I butchered this from the original AgentApplet,
 * deleting many methods that could not have worked due to
 * references to non-existing member variables, etc.  I
 * assumed that I was just getting rid of historical stuff
 * that was no longer supported anyway, but in the process,
 * it was evident that so much of it was broken anyway, that
 * now there is much less functionality.  Mainly, there is no
 * longer any code referring to movement and visibility, as
 * it is assumed that you must go thru this.dragBox for this
 * functionality.  Hopefully that functionality will get
 * re-added as needed, and in an manner which actually works.
 *
 *****************************************************************/

/******************************************************************
 * class FlashAgent extends FlashApplet
 *****************************************************************/
function FlashAgent (num, swfPath, proxyPath, seqPath, width, height, bgcolor) {
    this.init(num, swfPath, proxyPath, seqPath, width, height, bgcolor);
}

FlashAgent.prototype = new FlashApplet();
FlashAgent.prototype.constructor = FlashAgent;
FlashAgent.superclass = FlashApplet.prototype;

/**
 * STATIC convenience method
 */
FlashAgent.get = function (num) {
    return FlashApplet.lookupNumber(num);
}

FlashAgent.prototype.init = function (num, swfPath, proxyPath, seqPath, width, height, bgcolor) {
    if (!num) { return; }
    FlashAgent.superclass.init.call(this, num + "", swfPath, proxyPath, width, height, bgcolor);

    // Settings
    this.seqPath = seqPath;

    // Set this if you want the first sequence in the file to be played as soon as all is loaded.
    this.autoplay = false;

    // Set this to the location of the CSS for styling the speech bubble text
    this.cssPath = null;

    // For offline use, set this to the location of the directory with the stored cache files
    this.ttsPath = null;

    // Floating container (see DragBox)
    // Will be linked to DragBox object
    this.dragBox = null;

    // Status settings
    this.playing = false;
    this.played = false;

    // Callbacks
    this.loaded = false;
    this.onPlay = null;
    this.onLeftClick = null;
    this.onPlayStop = null;
    this.onReady = null;
};

/**
 * TODO: unify this with super.setInitialized()?
 */
FlashAgent.prototype.setLoaded = function (val) {
    this.loaded = val;
    this.setInitialized (val);
};

// Insert into document in a moveable div container (see DragBox)
FlashAgent.prototype.createDragBox = function () {
    var tag = this.constructTag();
    var content = tag.toString();
    // Create container item and insert agent applet
    var newItem = new DragBox('dragAgent' + this.name, content);
    // Clear when dragBox is hidden so agent will stop talking
    newItem.clearOnHide = true;
    // Specify DrabBox parent object
    this.dragBox = DragItem['dragAgent' + this.name];
    // Add agent hide handler on close tab item
    var subEls = this.dragBox.node.getElementsByTagName("A");
    for (var i = 0; i < subEls.length; i++) {
        if (subEls[i].className.indexOf("dragcloselink") >= 0) {
            var newHref = subEls[i].getAttribute("href", 0) + " hideAgent('"+ this.name + "');";
            subEls[i].setAttribute("href", newHref, 0);
        }
    }
};


FlashAgent.prototype.getAppletSwfUrl = function () {
    return this.swfPath + "agent.swf";
};

FlashAgent.prototype.getAppletVarString = function () {
    var str = "agentNum=" + this.name + "&lcId=" + this.id + "&xmlPath=" + this.seqPath;
	if (this.cssPath != null) {
		str += "&cssPath=" + this.cssPath;
	}
	if (this.ttsPath != null) {
		str += "&ttsPath=" + this.ttsPath;
	}
	if (this.autoplay) {
		str += "&autoplay=true";
	}
	return str;
};

FlashAgent.prototype.updateSettings = function () {
    var cfg = {
        "xmlPath": this.seqPath
    };
    this.proxyCall(["updateSettings", cfg]);
    this.setLoaded(false);
};

FlashAgent.prototype.playSequence = function (seqID) {
    // Show box -- currently for testing
    if (this.dragBox != null) {
        this.dragBox.show();
    }

    if (this.loaded) {
        this.playing = true;
        this.proxyCall(['playSequence', seqID]);
    }
};

// -------------------------------------- Status reporting for FlashAgent
FlashAgent.prototype.isPlaying = function () {
    return this.playing;
};

// STATUS CODES FOR AGENT CHARACTER
// 01 = ready/stopped
// 02 = loading
// 03 = playing animation
// 04 = left click
// 08 = Error with Actions XML
// 09 = Error with Sequence XML
// 10 = Error, action not found in library
// 99 = sequence complete
FlashAgent.prototype.handleEvent = function (rawCode, message) {
    if (rawCode === undefined) { return; }

//alert("Callback:" + rawCode + "\n" + message);

    // make sure it's a string
    var code = rawCode + "";
    if (code == '01') {
        this.setLoaded(true);
        this.playing = false;
        this.callback(this.onReady);
    } else if (code == '02') {
	    this.setLoaded(false);
	    this.playing = false;
	} else if (code == '03') {
	    // Hack to work aroung IE not getting ready state callback
	    if (!this.loaded) {
	        this.setLoaded(true);
            this.playing = false;
            this.callback(this.onReady);
	    }
	    // End Hack
	    this.playing = true;
	    this.callback(this.onPlay);
	} else if (code == '04') {
	    this.callback(this.onLeftClick);
	} else if (code == '99') {
        this.playing = false;
        this.callback(this.onPlayStop);
    } else {
	    this.handleError (code, message);
    }
};

// -------------------------------------- Callbacks for FlashAgent

// NONE CURRENTLY

FlashAgent.prototype.setOnReady = function (fn) {
    this.onReady = fn;
};

FlashAgent.prototype.setOnPlay = function (fn) {
    this.onPlay = fn;
};

FlashAgent.prototype.setOnLeftClick = function (fn) {
    this.onLeftClick = fn;
};

FlashAgent.prototype.setOnPlayStop = function (fn) {
    this.onPlayStop = fn;
};

/******************************************************************
 * STATIC HACKS DUE TO HARD-CODED CALLBACKS FROM FLASH
 *****************************************************************/

// Right-click/Hide event
function hideAgent (agentNum) {
    var agent = FlashApplet.lookupNumber(agentNum);
    if (agent && agent.dragBox)
        agent.dragBox.hide();
}
