//
// image-loader.js
//
//


var currentImage	= 0;
var currentShow 	= 0;
var data			= '';

var DEFAULT_FOLDER = '';
var DEFAULT_ROOT = 'http://www.circa1973.com/';
var ID_ABSTRACT_PANEL = 'abstract_panel';
var ID_ABSTRACT_TEXT = 'abstract_text';
var ID_EMAIL_SUSANNA = 'email_susanna';
var ID_IMAGE_DATA = 'image_data';
var ID_IMAGE_DISPLAY = 'image_display';
var ID_IMAGE_NUM = 'image_num';
var ID_LIGHTBOX = 'lightbox';
var ID_PRELOAD_PANEL = 'preload_panel';
var ID_SLIDESHOWS = 'slideshows';
var ID_SLIDESHOW_INFO = 'slideshow_info';
var RE_SLIDESHOW_ID = /^\s*(\d+)\s*$/;
var SLIDESHOWS = [];


function $(id) {
	return document.getElementById(id) || id;
}

function init() {
	drawSlideshowLinks();

	// Start first slideshow
	for (var i = 0, n = SLIDESHOWS.length; i < n; i++) {
		var s = SLIDESHOWS[i];
		if (!s) { continue; }
		startSlideshow(i);
		break;
	}

	// Preload slideshows
	var panel = $(ID_PRELOAD_PANEL);
	var sb = [];
	for (var i = 0, n = SLIDESHOWS.length; i < n; i++) {
		var s = SLIDESHOWS[i];
		if (!s) { continue; }
		for (var j = 0; j < s.photos.length; j++) {
			sb.push('<img src="' + s.getImagePath(j) + '" width="1" height="1" border="0" alt="" />\n');
		}
	}
	panel.innerHTML = sb.join('');
}

function initAbout() {
	// Make email address harder for robots to scrape.  Terrible UI for
	// screenreaders and the manually disabled, but half the blame lies
	// with the spammers.
	$(ID_EMAIL_SUSANNA).onmouseover = function() {
		$(ID_EMAIL_SUSANNA).href = 'mailto:' + 'susanna@' + 'circa1973.com';
	}
}

function drawSlideshowLinks() {
	var doc = $(ID_IMAGE_DATA).contentDocument || document.frames[ID_IMAGE_DATA].document;
	data = doc.body.textContent || doc.body.innerText;
	// Remove <pre> tag added by browsers
	data = data.replace(/^<pre>/i, '\n');
	data = data.replace(/<\/pre>$/i, '');
	// Remove Mac carriage returns
	data = data.replace(/\r/g, '');
	// Remove comment lines
	data = data.replace(/\n#[^\n]*/g, '');
	// Convert tabs to spaces
	data = data.replace(/\t/g, '    ');
	// Convert abstract blocks to html
	data = data.replace(/\*{3}[^*{3}]*\*{3}/g, multilineToHTML);
	items = data.split('\n');
	var idx = 0;
	var len = items.length;

	// Find slideshows and initialize objects
	for (var i = 0, n = items.length; i < n; i++) {
		var itm = items[i];
		if (itm.match(RE_SLIDESHOW_ID)) {
			var num = Number(RegExp.$1);
			if (!num) { continue; }
			new Slideshow(items.slice(i));
		}
	}

	// Draw slideshow links
	var links = $(ID_SLIDESHOWS);
	var html = '';
	for (var i = 0, n = SLIDESHOWS.length; i < n; i++) {
		var s = SLIDESHOWS[i];
		if (!s) { continue; }
		html += '<a href="#" onclick="return startSlideshow(' + s.index + ');" title="' + s.title + '">';
		html += '<span style="color: #F87431">' + s.title.toUpperCase() + '</span></a>';
		if (i < (n - 1)) {
			html += ' &nbsp; | &nbsp; ';
		}
	}
	links.innerHTML = html;
}

function startSlideshow(num) {
	var s = SLIDESHOWS[num];
	if (!s) { return false; }
	currentShow = num;
	currentImage = 0;
	drawSlide(currentShow,currentImage);

	var info = $(ID_SLIDESHOW_INFO);
	if (s.abstract) {
		info.innerHTML = '<img onclick="showAbstract(' + num + ')" ' +
						 'src="images/info.gif" width="11" height="11" border="0" ' +
						 'class="info-icon" align="absmiddle" title="About this portfolio" />';
	} else {
		info.innerHTML = '';
	}
	return false;
}

function drawSlide(sNum,iNum) {
	var s = SLIDESHOWS[sNum];
	if (!s) { return; }
	var p = s.photos[iNum];
	if (!p) { return; }

	var html = '<div class="image-placeholder"><img onclick="next()" class="gallery-image" ' +
			   'src="' + s.getImagePath(iNum) + '" border="0" ' +
			   'onerror="imageError(this)" /></div>';
	$(ID_IMAGE_DISPLAY).innerHTML = html;

	var nHTML = (iNum + 1) + '/' + s.photos.length;
	$(ID_IMAGE_NUM).innerHTML = nHTML;
}

function previous() {
	currentImage--;
	if (currentImage < 0) {
		currentImage = (SLIDESHOWS[currentShow].photos.length - 1);
	}
	drawSlide(currentShow,currentImage);
	return false;
}

function next() {
	currentImage++;
	if (currentImage >= SLIDESHOWS[currentShow].photos.length) {
		currentImage = 0;
	}
	drawSlide(currentShow,currentImage);
	return false;
}

function showAbstract(sNum) {
	var s = SLIDESHOWS[sNum];
	if (!s) { return; }

	var html = '';
	if (s.title) { html += '<h2>' + s.title + '</h2>'; }
	html += s.abstract;
	$(ID_ABSTRACT_TEXT).innerHTML = html;

	var pnl = $(ID_ABSTRACT_PANEL);
	pnl.style.display = 'block';
	var lb = $(ID_LIGHTBOX);
	lb.style.display = 'block';
}

function hideAbstract() {
	var pnl = $(ID_ABSTRACT_PANEL);
	pnl.style.display = 'none';
	var lb = $(ID_LIGHTBOX);
	lb.style.display = 'none';
}

function multilineToHTML() {
	var m = arguments[0];
	m = m.replace(/\n*\*{3}\n*/g, '');
	m = m.replace(/\n/g, '<br/>');
	m = m.replace(/ /g, '&nbsp;');
	return m;
}

function kill(event) {
	var e = event || window.event;
	if (e.stopPropagation) { e.stopPropagation(); }
	e.cancelBubble = true;
}

function imageError(img) {
	if (window['error']) {
		error('Bad image path: ' + img.src);
	}
}

var Slideshow = function(items) {

	// Set default values
	this.photos = [];
	this.index = Number(items.shift());
	this.root = DEFAULT_ROOT;
	this.folder = DEFAULT_FOLDER;

	// Initialize from data file values, stopping when next slideshow marker is found
	ITEMS:
	for (var i = 0, n = items.length; i < n; i++) {
		var itm = items[i];
		if (!itm) { continue; }
		if (itm.match(RE_SLIDESHOW_ID)) { break; }
		for (var j = 0; j < Slideshow.fields.length; j++) {
			var field = Slideshow.fields[j];
			var re = new RegExp(field + ':\\s*(.+)$');
			if (itm.match(re)) {
				this[field] = RegExp.$1;
				continue ITEMS;
			}
		}
		this.photos.push(itm);
	}

	// Add slideshow to globals
	if (SLIDESHOWS[this.index]) {
		var idx = this.index;
		while (1) {
			idx++;
			if (!SLIDESHOWS[idx]) {
				this.index = idx;
				break;
			}
		}
	}
	SLIDESHOWS[this.index] = this;

	// Correct paths by appending '/' if needed
	for (var i = 0, n = Slideshow.paths.length; i < n; i++) {
		if (this[Slideshow.paths[i]]) {
			var last = this[Slideshow.paths[i]].charAt(this[Slideshow.paths[i]].length - 1);
			if (last !== '/' && last !== '\\') { this[Slideshow.paths[i]] += '/'; }
		}
	}
	//log(this);
}
Slideshow.paths = ['folder', 'root'];
Slideshow.fields = Slideshow.paths.concat(['abstract', 'title']);
Slideshow.prototype.getImagePath = function(num) {
	var path = this.root + this.folder + this.photos[num];
	//log(path);
	return path;
}


function log(msg) {
	if (window['console'] && console.log) {
		console.log(msg);
	}
}

function error(msg) {
	msg = 'Error: ' + msg;
	if (window['console'] && console.error) {
		console.error(msg);
	} else if (window['console'] && console.log) {
		log(msg);
	}
	alert(msg);
}

