

var Client = new Object();
Client.H = Client.W = null;

var Explorer = /Explorer/.test(navigator.appName);



function load()
{
	Client.W = window.innerWidth ? window.innerWidth : document.body.offsetWidth || 800;
	Client.H = window.innerHeight ? window.innerHeight : document.body.offsetHeight || 650;

	var c = document.getElementById('canvas').style;
	c.width = Client.W + "px"; c.height = Client.H + "px";

	Large.construct();

	var q = unescape(window.location.search.substr(3).replace(/\+/g, "%20"));
	if (q) {
		Search(q);
		if (Search.size > 30) {
			var t = Math.ceil(Search.size/56.6);
			if (!confirm("Your search returned " + Search.size + " photographs.  Loading this many images may take up to " + t + " minute" + (t>1?"s":"") + " on a 56k modem.  Click OK to allow images to load."))
				return 0;
		}
		if (Search.size)	Canvas();
		else init("No photographs found for " + q + ".", 1.5);
	}
	else init("The Tarcoles Collection", 2);
}



Search = function(q) {
	q = Search.parse(q);
	if (!q)	return 0;
	Search.results = q == 'random' ? Search.rand() : Search.get(q);
	Search.size = Search.results.length;
}



Search.parse = function(q) {
	var i=-1, r = q.match(/(\".+\")|(\w|\"){3,}|(@\w)/gi);	//this matches a string in quotes, an "@" key, or a word greater than 2 chars
	while (r[++i])	r[i] = r[i].replace(/"/g,"")	//"
	return r;
}



Search.get = function(q) {
	var f = new Array();
	for (var i=0, j=s.length; i<j; i++)
	{
		var m = 0;
		//test all keywords until one has matched or until there are no more keywords left to search
		while (!(new RegExp("\\b"+ q[m], "gi").test(s[i])) && m < q.length)	++m;
		if (m < q.length)	f.push(i);
	}
	return f;
}



Search.rand = function() {
	var f = new Array(10);
	for (var i=0, j=s.length; i<10; i++)	f[i] = parseInt(Math.random() * j);
	return f;

}



Canvas = function() {

	Canvas.canvas = document.getElementById('canvas');

	if (arguments.length)	return Canvas.canvas;

	Canvas.elements = new Array();

	var i,j,k;
	for (i=0, j=Search.size; i<j; i++)
		Canvas.elements[i] = i;

	for (i=0, j=Search.size; i<j; i++)
	{
		k = parseInt(Math.random() * (j-i));
		Canvas.add(Search.results[Canvas.elements[k]], i);	//add a randomly selected picture from search results to the canvas
		Canvas.elements[k] = Canvas.elements[j-i-1];	//make sure this picture isn't selected again
	}
	
	return 0;
}



Canvas.add = function(i, count) {
	var data = s[i].split('^');
	var img = document.createElement("DIV");
	img.setAttribute("id", i);
	img.alt = data[0];
	img.title = data[0];
	
	imstyle = img.style;
	imstyle.backgroundImage = "url(images/" + data[1] + ".jpg)";
	imstyle.width = data[4] + "px";
	imstyle.height = data[5] + "px";
	
	Position.position(imstyle, parseInt(data[4]), parseInt(data[5]));

	img.ondblclick = Photo.large;
	img.onmousedown = Photo.set;
	Canvas.canvas.appendChild(img);
}



function Mark(x,y) {	this.x = x;	this.y = y;	}



Position = {
	marks : [ ], temps : [ ], pqueue : [ ],
	
	full : false,
	variance : .05, Z : 0,
	xoffset : 150, yoffset : 100, inset : 10, now : 150,
	
	vary : vary,
	enqueue : enqueue,
	position : position
};



function vary(k) {
	return parseInt(k * (Math.random() * 2*this.variance + 1 - this.variance));
}



function enqueue(i,w) {
	var m1 = this.marks[i-3], m2 = this.marks[i-2];
	if (m2.y - m1.y > 20)
		this.pqueue.push(new Mark(parseInt((m2.x+this.now) / 2), parseInt((m2.y+m1.y) / 2)));
	else if (m1.y - m2.y > 20)
		this.pqueue.push(new Mark(parseInt((this.now+w+m2.x) / 2), parseInt((m1.y+m2.y) / 2)));
}



function position(i, w, h) {
	if (!this.full) {
		i.top = Photo.putY(w, h, this);
		i.left = Photo.putX(w, h, this);
	}
	else if (this.pqueue.length) {
		var k = this.pqueue.shift();
		i.top = this.vary(k.y) + "px";
		i.left = this.vary(k.x) - parseInt(w/2) + "px";
	}
	else {
		i.top = this.yoffset + parseInt(Math.random() * (Client.H - 250)) + "px";
		i.left = this.xoffset + parseInt(Math.random() * (Client.W - 250)) + "px";
	}
}



Photo = function() {
	Photo.draggable = null;
	Photo.clickOffsetX = 0;
	Photo.clickOffsetY = 0;
}



Photo.set = function(event) {
	this.style.zIndex = ++Position.Z;
	Photo.draggable = this.style;
	Photo.clickOffsetX = window.event ? window.event.offsetX : event.layerX || 125;
	Photo.clickOffsetY = window.event ? window.event.offsetY : event.layerY || 75;
}



Photo.clear = function() {
	if (Photo.draggable) Photo.draggable = null;
}



Photo.putY = function(w, h, pos) {
	var i=0, j, k;

	while (j = pos.marks[i++])
	{
		if (j.x > pos.now+w)	break;
	}
	
	switch (i) {
		case 1: {	k = pos.marks[0]?pos.marks[0].y:0;	break;	}
		case 2: {	k = pos.marks[0].y;	break;	}
		default: {
			k = Math.max(pos.marks[i-2].y, pos.marks[i-3].y);
			pos.enqueue(i,w);
		}
	}
	
	pos.temps.push(new Mark(pos.now, k+h+pos.inset));
	return pos.yoffset + pos.vary(k) + "px";
}



Photo.putX = function(w, h, pos) {
	var k = pos.now;

	if (k+w > Client.W-pos.xoffset)	{
		if (pos.temps[pos.temps.length-1].y > Client.H)	pos.full = true;	//if the whole screen fills with photos
		pos.marks = pos.temps;
		pos.temps = [ ];
		pos.now = pos.xoffset;
	}
	else {
		pos.now += w + pos.inset;
	}

	return pos.vary(k) + "px";
}



Photo.large = function() {
	if (Canvas.canvas.style.opacity != undefined)	Canvas.canvas.style.opacity = .25;
//	else if (Canvas.canvas.style.filter != undefined)	Canvas.canvas.style.filter = "alpha(opacity=25)";
	Large.enlarge(s[this.id].split('^'));
}



Large = {
	div : null, txt : null, img : null,
	enlarge : enlarge,
	construct : enload,
	clear : enclear
};



function enload() {
	this.div = document.getElementById('enlarged').style;
	this.txt = document.getElementById('largetxt');
	this.img = document.getElementById('largeimg');

	this.div.width = Client.W + "px";	this.div.height = Client.H - 70 + "px";
	this.txt.innerText = "_";	//bizarre Explorer bug...this seems to fix it...
}



function enclear() {
	if (Canvas.canvas.style.opacity != undefined)	Canvas.canvas.style.opacity = 1;
//	else if (Canvas.canvas.style.filter != undefined)	Canvas.canvas.style.filter = "alpha(opacity=50)";
	this.div.display = "none";
	this.img.src = null;
}



function enlarge(s) {
	this.img.src = "images/enlargements/" + s[1] + ".jpg";
	this.img.onload = function() { Large.div.display = "block"; }
	this.txt.innerHTML = s[0];
}



drag = function(e) {
	if (!Photo.draggable)	return 0;	//if no picture is selected, cancel the drag
	Photo.draggable.left = (window.event ? window.event.clientX + (Explorer?(document.body.scrollLeft?document.body.scrollLeft:document.documentElement.scrollLeft):0) : e.pageX) - Photo.clickOffsetX + "px";
	Photo.draggable.top = (window.event ? window.event.clientY + (Explorer?(document.body.scrollTop?document.body.scrollTop:document.documentElement.scrollTop):0) : e.pageY) - Photo.clickOffsetY + "px";
}



init = function(et,em) {
	var f = new Array(4);	//these are four specially selected photos from s[ ] that appear on the main page
	f[0] = { id : 24, x : 180, y : 130, p : "top" };
	f[1] = { id : 54, x : 180, y : 360, p : "top" };
	f[2] = { id : 119, x : 440, y : 130, p : "top" };
	f[3] = { id : 41, x : 440, y : 360, p : "bottom" };

	var i, j = Canvas(0);

	while (i = f.pop())
	{
		var data = s[i.id].split('^');

		var img = document.createElement("DIV");
		img.setAttribute("id", i.id);
		img.alt = data[0];
		img.title = data[0];

		imstyle = img.style;
		imstyle.backgroundImage = "url(images/" + data[1] + ".jpg)";
		imstyle.backgroundPosition = i.p;
		imstyle.width = "250px";
		imstyle.height = "160px";

		imstyle.left = i.x + "px";
		imstyle.top = i.y + "px";
		imstyle.zIndex = 0;

		img.ondblclick = Photo.large;
		img.onmousedown = Photo.set;
		img.onmouseup = Photo.clear;
		j.appendChild(img);
	}

	var txt = document.createElement("SPAN");
	txt.setAttribute("id", "maintext");
	txt.style.fontSize = em+"em";
	txt.appendChild(document.createTextNode(et));
	j.appendChild(txt);
	
	if (et == "The Tarcoles Collection")	document.getElementById('infotext').style.display = "block";
}
