/**************************************************************************
 * Johnvey.com global functions
 * - jvScrambler object
 *
 * All content licensed under a Creative Commons License.
 * See http://johnvey.com for details
 *
 * 2005 Johnvey Hwang
 *************************************************************************/

function changeImage(imgObject, strImage) {
	imgObject.src = eval(strImage + ".src");
}

function newImage(strFilename) {
	strFilename = "/images/" + strFilename;
	if (document.images) {
		var objTempImage = new Image();
		objTempImage.src = strFilename;
		return objTempImage;
	}
}

function jvScrambler(objectID, phraseID, basePhrase) {
	this.cs = new Array("!","~","@","#","$","%","^","&","*","(","+","=","_","J","o","h","N","v","E","Y");
	this.oid = objectID;
	this.base = basePhrase;
	this.timer = null;
	this.eid = phraseID;
}

jvScrambler.prototype.go = function(ph) {
	this.loops = 0;
	this.cnt = 0;
	this.phrase = ph;
	this.words = this.phrase.split(" ");
	for(var i=this.words.length-1; i; i--) {
	    this.loops = Math.max(this.loops, this.words[i].length);
	}
	clearTimeout(this.timer);
	this.timer = setTimeout(this.oid + ".m_scrPhrase()",45);
}

jvScrambler.prototype.reset = function() {
	this.go(this.base);
}

jvScrambler.prototype.m_scrPhrase = function() {
	var out = "";
	if(this.cnt++ >= this.loops) {
		clearTimeout(this.timer);
		out = this.phrase;
	} else {
		for(var i=0; i < this.words.length; i++) {
		    out += this.m_scrWord(this.words[i], this.cnt) + " ";
		}
		this.timer = setTimeout(this.oid + ".m_scrPhrase()",45);
	}
	document.getElementById(this.eid).innerHTML = out;
}

jvScrambler.prototype.m_scrWord = function(wd, po) {
	var ln = wd.length;
	if(po >= ln) return wd;
	var out = wd.substring(0, po) + "<span class=\"scH\">|</span><span class=\"scL\">";
	var i = ln - po;
	do { out += this.cs[Math.floor(Math.random() * 20)]; } while(--i);
	out += "</span>";
	return out;
}

function jvEngorge(jsID, domID) {
	this.jsID = jsID;
	this.obj = document.getElementById(domID);
	this.timer = null;
}

jvEngorge.prototype.rush = function(iv, endX) {
	clearInterval(this.timer);
	var dir = (endX >= this.obj.width ? 1 : -1);
	this.timer = setInterval(this.jsID + ".m_rush(" + iv + "," + endX + "," + dir + ")", 20);
}

jvEngorge.prototype.m_rush = function(iv, endX, dir) {
	var cX = this.obj.width;
	var cY = this.obj.height;
	if((dir == 1 && cX >= endX) || (dir == -1 && cX <= endX)) {
		clearInterval(this.timer);
		this.obj.height = Math.round(endX * (cY / cX));
		this.obj.width = endX;
	} else {
		this.obj.width = cX + (dir * iv);
		this.obj.height = cY + Math.round((dir * iv) * (cY / cX));
	}
}