//2x3 thumbnail image gallery

ywUtil.ScheduleHandler( ywGallery );
ywUtil.UnscheduleHandler( ywMarquee );
ywUtil.SetGlobal( 'deferMarquee', true );

function ywGallery() {
	var gallery = arguments.callee; //can't use 'this' here
	gallery.parseData();
}
ywGallery.setTableSize = function( rows, cols ) {
	this.tableSize = rows * cols;
	this.numCols = cols;
};
ywGallery.setHref = function( relative_url ) {
	this.href = relative_url;
};
ywGallery.setNavId = function( id ) {
	this.navId = id;
};
ywGallery.setRowIds = function( id1, id2 ) {
	this.rowElement = [id1, id2];
};
ywGallery.setLightBoxId = function( id ) {
	this.lightBoxId = id;
};
ywGallery.setData = function( responseText ) {
	this.data = responseText;
};
ywGallery.makeTemplate = function( markup ) {
	this.rowTemplate = markup.replace(/\f/g, "\n");	
};
ywGallery.setCategory = function( category ) {
	this.currentCategory = category;
};
ywGallery.parseData = function() {
//must put paren.s around response to prevent invalid label error!	
	this.gallery = eval( '(' + this.data + ')' );
	this.data = null;
	
//	now safe to display marquee
	ywMarquee( this );
	
	this.imgSrc = ywUtil.GetGlobal( 'imagesrc' ) + '/';
	this.containerSpan = document.createElement( 'span' );
	this.categories = this.initCategories();
	if (this.selectRange( window.location.hash ))
		return;
	else if (this.currentCategory) {
		this.selectCategory( this.currentCategory );
	}
};
ywGallery.dirName = function( file ) {
	return file.replace( /(^.*)(\/.*$)/, "$1" );
};
ywGallery.initCategories = function() {
	var cat = {};
	for (var i in this.gallery) {
		i = parseInt(i);
		category = this.dirName( this.gallery[i].image );
		if (!cat[category]) {
			cat[category] = {count:1, first:i};
		}
		else {
			++cat[category].count;
		}
	}
	return cat;
};
ywGallery.makeNavBar = function() {
	var firstitem = this.categories[this.currentCategory].first;
	if (firstitem < 0)
		return;

	var lastitem = firstitem +
				this.categories[this.currentCategory].count - 1;
	var callback = 'ywGallery.redisplay';
	
	ywNavigation( firstitem, lastitem, this.currentItem, this.tableSize, callback, this.href );
	document.getElementById( this.navId ).innerHTML = ywNavigation.makeNavBar();
};
ywGallery.buildRow = function( index ) {
	var image = this.gallery[index].image.replace( /(\/[^.]+)\./,
												"$1-thumb." );
	var html = this.rowTemplate.replace( '$image', this.imgSrc + image );
	html = html.replace( /\$id/g, this.gallery[index].id);
	html = html.replace( '$title', this.gallery[index].title );
	return html;
};
//IE hack to set <tr> innerHTML
ywGallery.replace_html = function( oldRow, html ) {
	if( oldRow ) {
		
//make new row (in a table context)
		this.containerSpan.innerHTML = '<table><tbody><tr>' + html + '</tr></tbody></table>';
		
		var newRow = this.containerSpan.firstChild.firstChild.firstChild;
		newRow.id = oldRow.id;
		
//replace old row with new
		if(oldRow.parentNode) {
			oldRow.parentNode.replaceChild( newRow, oldRow );
			oldRow.innerHTML = null;
		}
		this.containerSpan.innerHTML = null;
	}
};
ywGallery.redisplay = function( first, last ) {
	this.currentItem = first;
	if (this.categories[this.currentCategory].count > this.tableSize)
		this.makeNavBar();
	else
		document.getElementById( this.navId ).innerHTML = '';
	
	var rowHtml = [];
	for (var i in this.rowElement)
		rowHtml[parseInt(i)] = '';
		        
	var row = 0;
	var col = 0;
	for (i = first; i <= last; i++) {
		if (col == this.numCols) {
			col = 0;
			row++;
		}
		else
			++col;
		
		rowHtml[row] += this.buildRow( i );
	}
	for (i in rowHtml) {
		i = parseInt(i);
		var el = document.getElementById( this.rowElement[i] );
		ywGallery.replace_html(el, rowHtml[i]);
	}
	
	var query = window.location.href.split( '#' );
	var href = ( (query[1])? query[0]: window.location.href );
	window.location.replace( href  + '#I_' + first + '_' + last );
	ywUtil.ScrollHere();
};
ywGallery.selectRange = function( hash ) {
	if (hash && hash.substr( 0, 3 ) == '#I_') {
		var range = (hash.substr( 3 )).split( '_' );
		range[0] = parseInt( range[0] );
		range[1] = parseInt( range[1] );
		for (var c in this.categories ) {
			if (this.categories[c].first <= range[0]
				&& range[0] < this.categories[c].first + this.categories[c].count) {
				this.currentCategory = c;
				this.redisplay( range[0], range[1] );
				return true;
			}
		}
	}
	return false;
};
ywGallery.selectCategory = function( category ) {
	var first = this.categories[category].first;
	var last = first
		+ Math.min( this.tableSize, this.categories[category].count ) - 1;	
	this.redisplay( first, last );
};
ywGallery.changeCategory = function( category ) {
	if (this.currentCategory != category) {
		this.currentCategory = category;
		ywGallery.selectCategory( category );
	}
	return false; //to prevent page redraw
};
ywGallery.lightBox = function( id ) {
	var obj = this;
	var callback = function( responseText ) {
		obj.parseResponse( responseText );
	};

	ywUtil.SendRequest('controllers/lightbox.php?id=' + id, null, callback);
};
ywGallery.parseResponse = function( responseText ) {
	var response = eval( '(' + responseText + ')' );
	var lb = document.getElementById( ywGallery.lightBoxId );
	
	lb.parentNode.style.display='block';
	html = '<p><strong>Click anywhere to return to Gallery</strong></p>';
	html += '<p><strong>' + response['title'] + '</strong></p>';
	html += '<p><strong>Description: </strong>' + response['description'] + '</p>';
	html += '<img src="' + response['image'] + '" />';
	lb.innerHTML = html;
};