/* (c) 2009 by Gerald Wodni */

function GalleryControl()
{
        this.overall = document.getElementById( 'overall' );
        this.image = getChildElementsByClassname( this.overall, 'IMG', 'galleryImage' )[0];
        this.infoBox = getChildElementsByClassname( this.overall, 'DIV', 'galleryInfo' )[0];
	if( this.infoBox )
        	this.valueFields = getChildElementsByClassname( this.overall, 'TD', 'value' );
	else
		this.valueFields = null;
        this.small = getChildElementsByClassname( this.overall, 'SPAN', 'small' )[0];
        this.medium = getChildElementsByClassname( this.overall, 'SPAN', 'medium' )[0];
        this.large = getChildElementsByClassname( this.overall, 'SPAN', 'large' )[0];

        this.activeGallery = null;
        this.activeResolution = "640x480";
        this.small.style.color = "#C00";

	/* check for special resolution settings */
        this.activeResolution = this.image.src.replace( /^(.+)\/[c]*([0-9]*x[0-9]+)(.+)$/g, "$2" );
	this.resolutions = new Array();
	resolutions = this.image.getAttribute( 'kernResolutions' );
	if( resolutions )
	{
		this.image.removeAttribute( 'kernResolutions' );
		resolutions = resolutions.split( ';' );
		for( var i = 0; i < resolutions.length; i++ )
			this.resolutions.push( resolutions[i] );

		this.activeResolution = this.resolutions[0];
	}
	else
	{
		this.resolutions.push( '640x480' );
		this.resolutions.push( '800x600' );
		this.resolutions.push( '1024x768' );
	}

        /* get galleries */
        this.galleryDivs = getElementsByClassname( "DIV", "gallery" );
        this.galleries = new Array();
        for( var i = 0; i < this.galleryDivs.length; i++ )
                var gallery = new Gallery( this.galleryDivs[i], this );

        /* overall-span callbacks */
        var me = this;
        this.controlCallback = function( evt )
        {
                var target = getTarget( evt );

                switch( target.className )
                {
                        case 'small':
                                me.activeResolution = me.resolutions[0];
                                me.small.style.color = "#C00";
                                me.medium.style.color = "#000";
                                me.large.style.color = "#000";
                                me.activeGallery.setImage();
                                break;
                        case 'medium':
                                me.activeResolution = me.resolutions[1];
                                me.small.style.color = "#000";
                                me.medium.style.color = "#C00";
                                me.large.style.color = "#000";
                                me.activeGallery.setImage();
                                break;
                        case 'large':
                                me.activeResolution = me.resolutions[2];
                                me.small.style.color = "#000";
                                me.medium.style.color = "#000";
                                me.large.style.color = "#C00";
                                me.activeGallery.setImage();
                                break;
                        case 'close':
                                me.overall.style.display = 'none';
                                break;
                        case 'previous':
                                me.activeGallery.previous();
                                break;
                        case 'next':
                                me.activeGallery.next();
                                break;
                }
        }

        /* set callbacks */
        var controls = getChildElementsByTagName( this.overall, 'SPAN' );
        for( var i = 0; i < controls.length; i++ )
                if( controls[i].className )
                        controls[i].onclick = this.controlCallback;

}

function Gallery( element, control )
{
        this.element = element;
        this.galleryControl = control;
        this.currentImage = 0;
        var me = this;

        this.imageImgs = getChildElementsByTagName( this.element, 'IMG' );

        this.imageCallback = function( evt, image )
        {
                var target;

		/* allow direct call */
		if( evt == -123 )
			target = image;
		else
			target = getTarget( evt );


                me.currentImage = 0;
                for( var i = 0; i < me.imageImgs.length; i++ )
                        if( me.imageImgs[i] == target )
                        {
                                me.currentImage = i;
                                break;
                        }

                me.galleryControl.activeGallery = me;
                me.setImage();
                me.galleryControl.overall.style.display = 'block';
        }

        for( var i = 0; i < this.imageImgs.length; i++ )
        {
                var image = this.imageImgs[i];
                image.onclick = this.imageCallback;
        }
}
Gallery.prototype.previous = function()
{
        this.currentImage--;
        if( this.currentImage < 0 )
                this.currentImage =  this.imageImgs.length - 1;

        this.setImage();
}
Gallery.prototype.next = function()
{
        this.currentImage++;
        this.currentImage %=  this.imageImgs.length;

        this.setImage();

}
Gallery.prototype.setImage = function()
{
        var nextImage = this.imageImgs[ this.currentImage ];

	if( ( nextImage.hasAttribute && nextImage.hasAttribute( 'kernValue0' ) )
		|| ( ! nextImage.hasAttribute && nextImage.getAttribute( 'kernValue0' ) != null )
	       	&& this.galleryControl.infoBox )
	{
		this.galleryControl.infoBox.style.display = 'block';
		for( var i = 0; i < this.galleryControl.valueFields.length; i++ )
			this.galleryControl.valueFields[i].firstChild.nodeValue = nextImage.getAttribute( 'kernValue' + i );
	}

        this.galleryControl.image.src = this.getImageSrc( this.currentImage );
	this.loadNeighbours();
}
Gallery.prototype.loadNeighbours = function()
{
	var nextImage = new Image();
	var nextImageIndex = ( this.currentImage + 1 ) % this.imageImgs.length;
	var previousImage = new Image();
	var previousImageIndex = this.currentImage - 1;
	if( previousImageIndex < 0 )
		previousImageIndex = this.imageImgs.length - 1;

	nextImage.src = this.getImageSrc( nextImageIndex );
	previousImage.src = this.getImageSrc( previousImageIndex );
}
Gallery.prototype.getImageSrc = function( index )
{
        var nextImage = this.imageImgs[index];
	var src = nextImage.src;
        src = src.replace( /^(.+)\/[c]*[0-9]*x[0-9]+(.+)$/g, "$1/"+this.galleryControl.activeResolution+"$2" );

	return src;
}


var galleryControl = new GalleryControl();


