
var _effects = new Object;
var _photoLayer = null;
var _frameFx = null;
var _thePhoto = null;
var _theLink = null;
var _clicked = new Object;
var _outTimeouts = new Object;

function loaded() {
	if (_tm) {
		clearTimeout(_tm);
		_tm = null;
	}
	_frameFx.clearTimer();
	_frameFx.goTo(1.0);
	loadNext();
}


var _src = new Array();

function downloadMessage() {
	alert('To download, please right-click this link and select "Save Target as..." / "Save Link as...". Enjoy!');
	return false;
}

function init() {
	_photoLayer = new Glider('frame',370,40,null,null,600,-1);
	_photoLayer.show();
	_frameFx = new Fx.Opacity('photoframe', {duration: 800});
	toggle('frame', false);
	_thePhoto = document.getElementById('largephoto');
	_theLink = document.getElementById('downloadlink');
	_thePhoto.onload = loaded; 
	
	var num = 204;
	for (var i = 0; i < num; i++) {
		var nid = 'p' + i;
		var fx = _effects[i] = new Fx.Style(nid, 'opacity', {duration: 400});
		_src[i] = getLargeImage(i);
		fx.set(0.3);
	}
}

var _lastLoaded = -1;
function loadNext() {
	var index = ++_lastLoaded;
	if (index < _src.length) {
		var ig = document.getElementById('preloader');
		// skip: ig.onload = loadNext;
		ig.src = _src[index];
	}
}

function loadById(id) {
	if (id < _src.length) {
		var ig = document.getElementById('preloader');
		ig.src = _src[id];
	}
}

function preload() {
	loadNext();
}

function getLargeImage(id) {
	var img = document.getElementById('p' + id);
	return img.src.replace('square/', 'large/');
}


function slideToView() {
	if (_photoLayer)
		_photoLayer.moving = true;
}

var _tm = null;
function fadeOutWhileLoading() {
	_frameFx.goTo(0);
}


var _lastClicked = -1;
function largePhotoClicked() {
	var nextId = _lastClicked + 1;
	if (_lastClicked >= _src.length) {
		nextId = 0;
	}
	gotoPhoto(nextId);
}

function gotoPhoto(id) {
	if (_lastClicked >= 0) {
		out(_lastClicked);
	}
	over(id);
	clicked(id);
	loadById(id+1);
}

function getValue(id) {
	var obj = document.getElementById(id);
	if (obj && obj.value) return obj.value;
	return '';
}

function sendAnother() {
	toggle('frm', true);
	toggle('sent', false);
}

function submitMessage() {
	var name = encodeURIComponent(getValue('name'));
	if (name == '') return false;
	var postBody = 'name=' + name + '&wish=' + encodeURIComponent(getValue('wish')) + '&message=' + encodeURIComponent(getValue('msg'));
	toggle('sending', true);
	var _msgAjax = new Ajax('message.php', {postBody: postBody, onComplete: messageSent});
	_msgAjax.request();
	setTimeout("messageSent(null)", 3000);
	return false;
	
}

function toggle(id, flag, useInline) {
	var obj = document.getElementById(id);
	if (!obj) return;
	if (flag) {
		obj.style.display = useInline ? 'inline' : 'block';
	}
	else {
		obj.style.display = 'none';
	}
}

function messageSent(response) {
	toggle('sending', false);
	toggle('frm', false);
	toggle('sent', true);
}

messageSent

var _messageFx = null;
function hideMessage() {
	if (!_messageFx) {
		toggle('frame', true);
		_messageFx = new Fx.Opacity('message', {duration: 1500});
		_messageFx.goTo(0);
	}
}

var _ajax = null;
function clicked(id) {
	slideToView();
	hideMessage();
	_clicked[id] = true;
	_lastClicked = id;
	var new_src = _thePhoto.src = getLargeImage(id);
	if (!_tm)
		_tm = setTimeout('fadeOutWhileLoading()', 200);
	t(id,new_src);
}
function t(id,new_src) {
	var photo_filename = new_src.substring(new_src.lastIndexOf('/')+1, new_src.lastIndexOf('.'));
	_theLink.href = 'download.php?f=' + encodeURIComponent(photo_filename) + '&id=' + encodeURIComponent(id);
	var urchin_url = '/2006+1/photos/' + id + '/' + photo_filename;;
	if (urchinTracker) urchinTracker(urchin_url);
	if (_ajax == null) _ajax = new Ajax('photo.php');
	_ajax.options.postBody = 'id=' + encodeURIComponent(id) + '&f=' + encodeURIComponent(photo_filename);
	_ajax.request();
}

function over(id) {
	clearTimeout(_outTimeouts[id]);
	fade(id, true);
}

function fade(id, show) {
	var fx = _effects[id];
	if (!fx) return;
	if (show) {
		fx.clearTimer();
		fx.goTo(1.0);
	}
	else {	
		fx.clearTimer();
		if (_clicked[id]) {
			fx.options.duration = 400;
			fx.goTo(0.85);
		}
		else {
			fx.options.duration = 800;
			fx.goTo(0.3);
		}
	}
}

function out(id) {
	fade(id, false);
	_outTimeouts[id] = setTimeout('fade(' + id + ', false)', 1200);
}
