/*
 *		 Copyright (c) Jan 19 09:29:52  MTCC
 *	Author: Michael Thomas
 *	Module: scripter.js
 *	Created: Mon Jan 19 09:29:52 2009
 *	Abstract:
 *	   scriptable interface for the maps, etc
 */

/* Edit History: 
 */

function scripter (name, access, w, h) {
    this.pane = new htmlpane (name, name+'container', 0, 0);
    this.name = name;
    this.access = access;
    this.pane.size (w, h);
    this.w = w;
    this.h = h;
    this.pane.pos (-SBWIDTH, 0);
    //this.pane.z (12);
    this.pane.setStackable (true);
    this.pane.display (0);
    this.scripturl = null;
    this.prevlink = null;
    this.nextlink = null;	
    this.demoMode = false;
    this.paused = false;
    this.stopped = false;
    this.needresume = false;
    try {
	if (smstate.lb)
	    smstate.lb.pane.display (0);
    } catch (e) {};
}
scripter.prototype.size = function (w, h) {
    this.w = w;
    this.h = h;
    this.pane.size (this.w, this.h);
};

scripter.prototype.setDemoMode = function (dm) {
    this.demoMode = dm;
};

scripter.prototype.cur = function () {
    if (this.stopped)
	return;
    if (this.paused) {
	this.needresume = true;
	return;
    }
    if (this.script.length-1 >= this.idx) {
	this.script [this.idx].func (this, 'stop');
    }
    var html = '';
    var script = null;
    if (this.idx < this.script.length) {
	var script = this.script [this.idx].func (this, 'start');
	html += this.prolog ();
	html += script;
	html += this.epilog ();
    } else {
	this.idx = this.script.length;
	script = '';
	html += this.prolog ();
	html += "<h3>Finished</h3>";
	html += this.epilog ();
    }
    if (script != null) {
	this.pane.newc (html);
	this.pane.display (1);	
	this.pane.draggable ();
    } else 
	this.pane.display (0);
};


scripter.prototype.next = function () {
    if (this.stopped)
	return;
    if (this.paused) {
	this.needresume = true;
	return;
    }
    if (this.script.length-1 >= this.idx) {
	this.script [this.idx].func (this, 'stop');
    }
    this.idx++;
    var html = '';
    var script = null;
    if (this.idx < this.script.length) {
	var script = this.script [this.idx].func (this, 'start');
	html += this.prolog ();
	html += script;
	html += this.epilog ();
    } else {
	this.idx = this.script.length;
	script = '';
	html += this.prolog ();
	html += "<h3>Finished</h3>";
	html += this.epilog ();
    }
    if (script != null) {
	this.pane.newc (html);
	this.pane.display (1);	
	this.pane.draggable ();
    } else 
	this.pane.display (0);
};

scripter.prototype.prev = function () {
    if (this.stopped)
	return;
    if (this.paused) {
	this.needresume = true;
	return;
    }
    if (this.idx < this.script.length) {
	this.script [this.idx].func (this, 'stop');
    }
    this.idx--;
    if (this.idx < 0)
	this.idx = 0;
    var html = '';
    if (this.idx >= 0) {
	var script = this.script [this.idx].func (this, 'start');
	if (script != null) {
	    html += this.prolog ();
	    html += script;
	    html += this.epilog ();
	    this.pane.newc (html);
	    this.pane.display (1);	
	    this.pane.draggable ();
	} else {
	    this.pane.display (0);	
	}
    } 
};

scripter.prototype.goto = function (label) {
    var idx;
    for (idx in this.script) {
	if (this.script [idx].label == label) {
	    this.setFrame (idx);
	    break;
	}
    }
};

scripter.prototype.jumpRelative = function (idx) {
    this.idx += idx;
};

scripter.prototype.setFrame = function (idx) {
    this.idx = idx;
    if (this.paused)
	return;
    if (this.idx >= 0) {
	var script = this.script [this.idx].func (this, 'start');
	if (script != null) {
	    var html = '';
	    html += this.prolog ();
	    html += script;
	    html += this.epilog ();
	    this.pane.newc (html);
	    this.pane.display (1);	
	    this.pane.draggable ();
	} else {
	    this.pane.display (0);	
	}
    } 
};

scripter.prototype.setPrevlink = function (prev) {
    this.prevlink = prev;
};

scripter.prototype.prolog = function () {
    if (this.demoMode)
	return '';
    return this.pane.title (this.title, this.access+".finish ();", "End Tour");
};


scripter.prototype.epilog = function () {
    var html = '';
    if (this.demoMode)
	return html;
    html += sprintf ('<div style="position:absolute; width:%dpx; height:30px; top:%dpx; left:0px">', 
		     this.w, this.h-50);
    if (this.idx > 0) {
	html += '<div style="float:left">' + imgbutton (this.name+'-prev', 'imgs/back.gif', this.access+".prev ()", null, null, null, null, true) + '</div>';
    } else if (this.prevlink) {
	html += sprintf ('<div style="float:left"><a href="%s"><img border=0 src="imgs/back.gif"></a></div>', this.prevlink);
    }
    if (this.idx < this.script.length) {
	html += '<div style="float:right">'+imgbutton (this.name+'-next', 'imgs/next.gif', this.access+".next ()", null, null, null, true)+'</div>';;
    } else if (this.nextlink) {
	html += sprintf ('<div style="float:right"><a href="%s"><img border=0 src="imgs/next.gif"></a></div>', this.nextlink);
    }
    html += '</div>';
    return html;
};


scripter.prototype.start = function (script) {
    this.script = script;
    this.stopped = false;
    this.paused = false;
    if (this.script.length) {
	this.idx = 0;
	var html = '';
	if (this.startfn)
	    this.startfn ();
	var script = this.script [this.idx].func (this, 'start');
	if (script != null) {
	    html += this.prolog ();
	    html += script;
	    html += this.epilog ();
	    this.pane.newc (html);
	    this.pane.display (1);	
	    this.pane.draggable ();
	} else {
	    this.pane.display (0);	
	}
    }
};

scripter.prototype.restart = function () {
    this.start (this.script);
};

scripter.prototype.finish = function () {
    this.pane.display (0);
    top.location = "index.php";
};

scripter.prototype.loadDone = function () {
    if (this.script == null)
	return;
    var html  = '';
    var script = this.script [this.idx].func (this, 'loaddone');
    html += this.prolog ();
    html += script;
    html += this.epilog ();
    this.pane.newc (html);
    this.pane.display (1);	
    this.pane.draggable ();
};


scripter.prototype.load = function (title, scripturl) {
    this.scripturl = scripturl;
    this.title = title;
    this.idx = 0;    
    var state = this;
    fetchServer ("GET", scripturl+".js?cb="+Math.random (), function (resp) {
		     try {
			 eval (resp);
			 state.prevlink = __scripter_prevlink;
			 state.nextlink = __scripter_nextlink;
			 state.start (__scripter_script);
		     } catch (e) {
			 Message ("can't load %s: %s", state.scripturl, e.message);
		     }
		 });
};

scripter.prototype.togglePause = function () {
    this.paused = ! this.paused;
    if (this.pausefn)
	this.pausefn (this.paused);
    if (! this.paused && this.needresume) {
	this.needresume = false;
	this.cur ();
    } 
    //Message (! this.paused ? "resuming..." : "paused...");
};

scripter.prototype.setPause = function (paused) {
    this.paused = paused;
    if (this.pausefn)
	this.pausefn (paused);
    if (! this.paused && this.needresume) {
	this.needresume = false;
	this.cur ();
    } 
};

scripter.prototype.stop = function () {
    this.stopped = true;
    if (this.stopfn)
	this.stopfn ();
};


scripter.prototype.onPause = function (fn) {
    this.pausefn = fn;
};

scripter.prototype.onStop = function (fn) {
    this.stopfn = fn;
};

scripter.prototype.onStart = function (fn) {
    this.startfn = fn;
};
