/**********************************************************************\
************************************************************************
***                                                                  ***
***   catjax 1.9.9             mike eldridge <pulsation@gmail.com>   ***
***                                                                  ***
************************************************************************
\**********************************************************************/

if (typeof(Prototype) == 'undefined')
	throw("catjax requires the prototype javascript library");

var path = null;
var tags = document.getElementsByTagName('script');
var i;

for (i = 0; i < tags.length && path == null; i++)
	if (tags[i].src && tags[i].src.match(/catjax\.js/))
		path = tags[i].src;

if (classes == null) {
	var classes = new Array(
		'bin', 'action', 'button', 'checkbox', 'collection',
		'combobox', 'cursor', 'dialog', 'edit', 'event', 'file',
		'form', 'frame', 'id', 'image', 'listbox', 'matrix',
		'meta', 'radio', 'separator', 'spinner', 'statusbar',
		'switchdesk', 'text', 'tip', 'vmatrix', 'xmldoc', 'xmlhttp'
	);
}

for (i = 0; i < classes.length; i++)
	document.write('<script type="text/javascript" src="' + path.replace(/catjax\.js/,  classes[i] + '.js') + '"></script>'); 

var catjax =
{
	version:	"1.9.9",

	collections:	new Array(),
	aryOnLoadExec:	new Array(),
	aryExec:		new Array(),
	events:			new Array(),

	images:
	{
		progress:	new Image,
		negative:	new Image,
		save:		new Image,
		arrowLeft:	new Image,
		arrowRight:	new Image
	},

	alert: function(msg)
	{
		if (catjax.onerr)
			catjax.onerr(msg);
	},

	log: function(msg)
	{
		var ta = document.getElementById('catjax_log');
		ta.value += msg;
	},

	onload: function(func)
	{
		catjax.aryOnLoadExec.push(func);
	},

	onloadExec: function()
	{
		for (var i = 0; i < catjax.aryOnLoadExec.length; i++)
			catjax.aryOnLoadExec[i]();
	},

	queue: function(fp)
	{
		catjax.aryExec.push(fp);
		setTimeout(catjax.procQueue.bind(this), 10);
	},

	procQueue: function()
	{
		var fp;

		fp = catjax.aryExec.shift();

		if (fp) {
			fp();
			setTimeout(this.procQueue.bind(this), 10);
		}
	},

	getContainer: function()
	{
		var container;

		container = this.elem;
		if (!container)
			container = this.content;
		if (!container)
			container = this.canvas;

		return container;
	},
	
	fire: function(name)
	{
		var event = 'on' + name;

		if (this.opts['fpon' + name])
			this.opts['fpon' + name](this);
	},

	showLoadMsg: function(dest)
	{
		var div;
		var img;

		if (!dest)
			dest = this.elem;

		if (this.loadMsgMask) {
			this.loadMsgSemaphore++;
			return;
		} else
			this.loadMsgSemaphore = 1;

		div = document.createElement('div');

		div.style.backgroundColor	= '#000000';
		div.style.opacity			= 0.8;
		div.style.filter			= 'alpha(opacity=80)';
		div.style.position			= 'absolute';
		div.style.left				= 0;
		div.style.top				= 0;
		div.style.fontSize			= '18px';
		div.style.fontWeight		= 'bold';
		div.style.width				= dest.offsetWidth + 'px';
		div.style.height			= dest.offsetHeight + 'px';
		div.style.zIndex = 10;

		img = document.createElement('img');
		img.src = catjax.images.progress.src;

		img.style.zIndex			= 11;
		img.style.position			= 'absolute';
		img.style.visibility		= 'hidden';

		dest.appendChild(div);
		dest.appendChild(img);

		img.style.left			= ((div.offsetWidth - catjax.images.progress.width) / 2) + 'px';
		img.style.top			= ((div.offsetHeight - catjax.images.progress.height) / 2) + 'px';
		img.style.visibility	= 'visible';

		this.loadMsgContainer	= dest;
		this.loadMsgMask		= div;
		this.loadMsgImg			= img;
	},

	clearLoadMsg: function()
	{
		this.loadMsgSemaphore--;

		if (!(this.loadMsgMask && this.loadMsgSemaphore <= 0))
			return;

		this.loadMsgContainer.removeChild(this.loadMsgMask);
		this.loadMsgContainer.removeChild(this.loadMsgImg);

		this.loadMsgContainer	= null;
		this.loadMsgMask		= null;
	},

	setPageInactive: function()
	{
		var div;

		var x;
		var y;

		var yOffset	= document.documentElement.offsetHeight;
		var yClient	= document.documentElement.clientHeight;
		var height	= yOffset > yClient ? yOffset : yClient;

		div = document.createElement('div');

		div.style.backgroundColor	= '#000000';
		div.style.opacity			= 0.8;
		div.style.filter			= 'alpha(opacity=80)';
		div.style.position			= 'absolute';
		div.style.left				= 0;
		div.style.top				= 0;
		div.style.height			= height + 'px';
		div.style.width				= document.body.offsetWidth + 'px';
		div.style.zIndex			= 10;

		document.body.appendChild(div);

		catjax.mask = div;
	},

	setPageActive: function()
	{
		if (catjax.mask)
			catjax.mask.parentNode.removeChild(catjax.mask);
	},

	onerr: function(msg)
	{
		alert("There was an error processing your request.  Please try your operation again.  For assistance, please email websupport@magazines.com.  Additional information, if any, follows:\n\n" + msg);
		throw(msg);
	},

	getEventHandler: function(name)
	{
		var evt;

		evt = catjax.events[name];

		return evt ? evt.getHandler() : null;
	},

	registerCollectionCallback: function(id, cb)
	{
		var collection;

		collection = catjax.collections[id];
		if (!collection)
			return;

		return collection.registerCallback(cb);
	},

	getCollection: function(id)
	{
		return catjax.collections[id];
	},

	getFormValue: function(id)
	{
		var form;

		elements = Form.getElements(this.getContainer());

		for (var i = 0; i < elements.length; i++)
			if (elements[i].impl && elements[i].impl.id == id)
				return elements[i].impl.getValue();

		return null;
	},

	setFormValue: function(id, value)
	{
		var form;

		elements = Form.getElements(this.getContainer());

		for (var i = 0; i < elements.length; i++)
			if (elements[i].impl && elements[i].impl.id == id)
				elements[i].impl.setValue(value);
	},

	getFormValues: function()
	{
		var container;
		var elements;
		var form;
		var obj;

		form		= {};
		elements	= Form.getElements(this.getContainer());

		for (var i = 0; i < elements.length; i++) {
			if (obj = elements[i].impl) {
				var def = {};

				if (obj.getValue)
					def.value = obj.getValue();
				if (obj.isChecked)
					def.checked = obj.isChecked();

				form[obj.id] = def;
			}
		}

		return form;
	},

	getFormSerialized: function()
	{
		return Form.serialize(this.getContainer());
	},

	setScrollBottom: function()
	{
		if (this.bin)
			this.bin.scrollTop = this.bin.scrollHeight;
		else
			this.getContainer().scrollTop = this.getContainer().scrollHeight;
	},

	nextSequence: function()
	{
		if (!this.seq)
			this.seq = 1;

		return this.seq++;
	},

	clientWidth: function()
	{
		var x;

		if (self.innerHeight)
			x = self.innerWidth;
		else if (document.documentElement && document.documentElement.clientHeight)
			x = document.documentElement.clientWidth;
		else if (document.body)
			x = document.body.clientWidth;

		return x;
	},

	clientHeight: function()
	{
		var y;

		if (self.innerHeight)
			y = self.innerHeight;
		else if (document.documentElement && document.documentElement.clientHeight)
			y = document.documentElement.clientHeight;
		else if (document.body)
			y = document.body.clientHeight;

		return y;
	}
};

catjax.path = path;

catjax.images.progress.src		= path.replace(/catjax\.js/, '/images/progress.gif');
catjax.images.negative.src		= path.replace(/catjax\.js/, '/images/negative.png');
catjax.images.arrowLeft.src		= path.replace(/catjax\.js/, '/images/arrow-left.png');
catjax.images.arrowRight.src	= path.replace(/catjax\.js/, '/images/arrow-right.png');

window.onload = catjax.onloadExec;
