
var ImageGallery = Class.create();
ImageGallery.prototype = {

    initialize: function( image_gallery, path ) {
	this.image_gallery = image_gallery;
	this.path = path;
	this.image_width = 400;
	this.image_height = 800;
    },

    gallery_changed: function( select ) { 
	var html = '';

	var index = select.selectedIndex;

	if ( index == 0 )
	{
	    Element.update( 'image_gallery', '' );
	}

	var value = select.options[ index ].value;
	var ig = this.image_gallery[ value ];

	var colspan = 3;
	var col = 1;
	var first = null;

	html += '<table border=0 id=image_gallery_table><tr>';

	if ( ig )
	{
	    var images = ig[ 'images' ].split( ',' );
	    var thumbnails = ig[ 'thumbnails' ].split( ',' );

	    thumbnails.each( function(thumbnail) {
		    thumbnail = TrimString( thumbnail );
		    var image = TrimString( images.shift() );

		    /* we need to get the first image */
		    if ( first == null )
		    {
			first = image;
			update_image( this.path + image );
		    }

		    var link = '<A HREF=# onmouseover=update_image(\'' + this.path + image + '\')  onclick=display_image(\'' + this.path + image + '\') ><IMG SRC=' + this.path + thumbnail + '></a>';

		    html += '<td>' + link + '</td>';


		    if ( col == colspan )
		    {
			html += '</tr><tr>';
			col = 0;
		    }

		    col++;

	    });

	    html += '</tr></table>';
	}

	Element.update( 'image_gallery', html );
    }

};


    function update_image( image_path )
    {
	Element.update( 'image', '<IMG SRC=' + image_path + ' />' );
    }

    function display_image( image_path )
    {
	open_window( image_path, 'image', 'scrollbars,resizable' );
    }



    function append_value( select, option )
    {
	var old = select.options[ select.length - 1 ];

	try 
	{
	    select.add( option, null ); // standards compliants; doesn't work in IE :(
	}
	catch ( ex ) 
	{
	    select.add( option ); // IE only
	}
	return select;
    }

    function TrimString(InString) 
    {
	InString = InString.replace( /^\s+/g, "" );// strip leading
	return InString.replace( /\s+$/g, "" );// strip trailing
    }
