/*** Adjust these two variables for your installation.
    proxy: the absolute location to the get_thumb.php script.
    gallery: the base URL of your ZenPhoto installation. ***/
var proxy = 'http://james.roomfullofmirrors.com/get_thumb.php';
var gallery = 'http://gallery.roomfullofmirrors.com/';

/*** You shouldn't need to edit anything below this line. ***/
var thumbs = new Array();
function parse_thumbs() {
	var thumbs = getElementsByClass('extthumb', 'span');
	for (var i = 0; i < thumbs.length; i++) {
		var t = thumbs[i];
		t.src = t.firstChild.src;
		t.ident = t.id;
		t.width = getQueryVariable('width', t.src);
		t.height = getQueryVariable('height', t.src);
		t.img = getQueryVariable('img', t.src);
				
		var info = new ajaxObject(proxy, fin);
		info.callback = function(responseText, responseStatus) {
			var x = eval('(' + responseText + ')');
			var result = check_error(x);
			if (result == 0) {
				var y = document.getElementById(x['thumb_id']);
				y.innerHTML = '<a href="'+gallery+'/index.php?album='+x.album+'&image='+x.image_name+'">'+y.innerHTML+'</a>';
			}
		}
		info.update('width='+t.width+'&height='+t.height+'&img='+t.img+'&action=info&id='+t.ident);
	}
}

function getQueryVariable(variable, str) { 
	var pattern = new RegExp('('+variable+'=[_\.0-9a-zA-Z]*)');
	/*var query = pattern.test(str);
	if (query == true) {
		var vars = RegExp.lastParen.split("="); 
		return vars[1];
	} else {
		return '';
	}*/
	/* Since Opera doesn't support RegExp.lastParen()...*/
	var query = pattern.test(str);
	if (query == true) {
		var result = pattern.exec(str);
		var vars = result[0].split("=");
		return vars[1];
	} else {
		return '';
	}
}

function check_error(obj) {
	if (!obj.error) {
		return 0;
	}
	
	var error;
	if (window.console && window.console.log) {
		/* Safari */
		/* The double check prevents Firefox from complaining about
		   the window.console.log function not existing. */
		error = window.console.log;
	} else if (window.dump) {
		/* IE and Mozilla? */
		error = window.dump;
	} else if (opera.postError) {
		/* Opera */
		error = opera.postError;
	} else {
		/* Catchall */
		error = alert;
	}
	
	switch (obj.error) {
		case 'domain_not_allowed':
			error('This site does not have permission to access the remote thumbnail.');
			break;
		case 'no_file_name':
			error('No file was specified for the remote thumbnail.');
			break;
		case 'image_not_exist':
			error('The requested remote image does not exist.');
			break;
		case 'url_fail_open':
			error('The proxy was not able to open the remote thumbnail script.');
			break;
	}
	
	return 1;
}

/* Just a stub. */
function fin(responseText, responseStatus) {}

/** Begin third party functions. **/
/* http://tinyurl.com/4kdxdq */ 
/*Object.prototype.getElementsByClass = function (searchClass, tag) {*/
function getElementsByClass(searchClass, tag) {
	var root = document.body;
	var returnArray = [];
	tag = tag || '*';
	var els = root.getElementsByTagName(tag);
	var pattern = new RegExp('(^|\\s)'+searchClass+'(\\s|$)');
	
	for (var i = 0; i < els.length; i++) {
		if ( pattern.test(els[i].className) ) {
			returnArray.push(els[i]);
		}
	}
	
	return returnArray;
}

function ajaxObject(url, callbackFunction) {
	var that=this;
	this.updating = false;
	this.abort = function() {
		if (that.updating) {
			that.updating=false;
			that.AJAX.abort();
			that.AJAX=null;
		}
	}
	this.update = function(passData,postMethod) { 
		if (that.updating) { return false; }
		that.AJAX = null;
		if (window.XMLHttpRequest) {
			that.AJAX=new XMLHttpRequest();
		} else {
			that.AJAX=new ActiveXObject("Microsoft.XMLHTTP");
		}
		if (that.AJAX==null) {
			return false;
		} else {
			that.AJAX.onreadystatechange = function() {
				if (that.AJAX.readyState==4) {
					that.updating=false;
					that.callback(that.AJAX.responseText,that.AJAX.status,that.AJAX.responseXML);
					that.AJAX=null;
				}
			}
			that.updating = new Date();
			if (/post/i.test(postMethod)) {
				var uri=urlCall+'?'+that.updating.getTime();
				that.AJAX.open("POST", uri, true);
				that.AJAX.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
				that.AJAX.send(passData);
			} else {
				var uri=urlCall+'?'+passData+'&timestamp='+(that.updating.getTime());
				that.AJAX.open("GET", uri, true);
				that.AJAX.send(null);
			}
			return true;
		}
	}
	var urlCall = url;
	this.callback = callbackFunction || function () { };
}
