/*
 *  robagallery.js - v1.000
 *  Robarov / Journyl JavaScript
 *  (c) 2007 Robarov <info@robarov.be>
 *
 *  This file contains all scripts needed for image gallery in blog-posts
 *
/*--------------------------------------------------------------------------*/
/*
 * CHANGELOG
 * v1.000
 * - Original version
*/
/*--------------------------------------------------------------------------*/

// This script depends on script.aculo.us: 
if(typeof Effect == 'undefined')
  throw("robabrowser.js requires including script.aculo.us' effects.js library");

Element.addMethods({
	swap_img: function(element, content) {
		element = $(element);
		new Effect.Fade(element, {
			duration: 0.3, 
			afterFinish: function(effect) {
				foo = new Image();
				foo.src = content;
				effect.element.src = content;
				foo.onload = function() {
					new Effect.Appear(effect.element, {delay: 0.2, duration: 0.3, queue: 'end'});
				}.bind(this);
			}
		}
	)
	return element;
	}
})

// Robarov namespace
if(typeof Robarov == 'undefined')
  var Robarov = {}

Robarov.RobaGallery = Class.create();  

Object.extend(Robarov.RobaGallery.prototype, {
	_id: null,
	_active: null,
	_images: Array(),
	
	initialize: function(id) {
		this._id = id;
		
		var as = $('slidethumbs').getElementsByTagName('ul')[0].getElementsByTagName('a');
		for(var i = 0; i < as.length; i++) {
			this._images[i] = as[i].href;
			as[i].id = this._id + '_thumb_' + i;
			as[i].onclick = function() {
				return show_img(this);
			}
		}
		as[0].className = 'active';
		this._active = 0;
		
		$('slide_next').onclick = function() {
			return show_next_img();
		}
		$('slide_prev').onclick = function() {
			return show_prev_img();
		}
	},
	
	show_next_img: function() {
		var new_active = this._active + 1;
		if(new_active >= this._images.length) {
			new_active = 0;
		}
		var elem = $(this._id + '_thumb_' + new_active);
		return this.show_img(elem);
	},
	
	show_prev_img: function() {
		var new_active = this._active - 1;
		if(new_active < 0) {
			new_active = (this._images.length-1);
		}
		var elem = $(this._id + '_thumb_' + new_active);
		return this.show_img(elem);
	},
	
	// Display a deeper level
	show_img: function(elem) {
		if($('slide_img').src != elem.href) {
			$(this._id + '_thumb_' + this._active).className = '';
			var repl = this._id + '_thumb_';
			this._active = parseInt(elem.id.replace(repl, ''));
			elem.className = 'active';
			$('slide_img').swap_img(elem.href);
			$('slide_title').update(elem.title);
		}
		return false;
	}	
});

var my_robagallery;

function show_img(elem) {
	return my_robagallery.show_img(elem);
}

function show_next_img() {
	return my_robagallery.show_next_img();
}

function show_prev_img() {
	return my_robagallery.show_prev_img();
}

function init() {
	if($('slideshow')) {
		my_robagallery = new Robarov.RobaGallery('slideshow', 20); 
	}
}
Event.observe(window, "load", init);
