var MAG = Object.extend({}, MAG || {});
MAG.Preview = Class.create();
MAG.Preview.prototype = {
	initialize: function(options)
	{
		MAG.MagPreview = this;

		this.options = {
			preview_element: $('preview_image_container') ? $('preview_image_container').select('img').first() : null,
			objects: $$('#image_preview a')
		};
		
		//set the alt title		
		if(this.options.preview_element && this.options.preview_element.parentNode.title != '') {
			var img_title = $('preview_image_container').select('img').first().parentNode.title;
			
			$('alt_txt').innerHTML = '';
			$('alt_txt').innerHTML = '<br />' + img_title;
		}
		
		Object.extend(this.options, options || {});
		
		if(this.options.objects.length) this.attach_event();

		return true;
	},

	attach_event: function()
	{
		this.options.objects.each(function(el){
			el.observe('click', this.click_object.bind(this));
		}.bind(this));
	},

	click_object: function(e)
	{
		var el = Event.element(e);
		this.options.preview_element.src = el.src.toString().replace('thumb84x76_', 'thumb292x173_');
		this.options.preview_element.parentNode.href = el.src.toString().replace('thumb84x76_', '');
		if(el.parentNode.title != '') {
			$('alt_txt').innerHTML = '';
			$('alt_txt').innerHTML = '<br />' + el.parentNode.title;
		}
		Event.stop(e);
	}
};

document.observe('dom:loaded', function(){new MAG.Preview;});
