
// Prototype for the gallery class
var mxGalleryPrototype = mxGallery.prototype;

function mxGallery( id, width, height, cols, rows, slide ) {

	this.count = 0;

	this.width = parseInt( width );
	this.height = parseInt( height );
	this.slide = parseInt( slide );

	if( this.slide < 1 || this.slide > 20 ) this.slide = 3;
	this.slide *= 1000;
	
	this.single = false;
	this.id		= id;
	this.DELAY	= 650;
	this.DEFAULT_SIZE = 128;
	
	var container = $('gallery'+id);
	
	if( !container ) return;
	
	this.item_r	= [];
	var items	= container.getElementsByTagName( 'div' );  

	for( var i = 0; i < items.length; i++ ) {
	
		if( this.addItem( items[ i ] ) ) this.count++;
	}
	
	if( !this.count ) return;
		
	this.columns = parseInt( cols );
	var rows = parseInt( rows );
	if( this.count < rows * this.columns ) rows = Math.ceil( this.count / cols );

	this.limit = rows * this.columns;
	this.start = 0;
	this.head = null;
	this.current = null;
	
	this.step = this.columns;
	
	this.frame = document.createElement( 'div' );
	this.frame.className = 'gframe';
	
	if( container.className.substr( 8 ) == 'single' ) {
	
		this.single = true;
		this.frame.className = 'gframe single';
	}
	
	container.parentNode.replaceChild( this.frame, container );
	
	if( this.head ) this.frame.innerHTML = '<h4>' + this.head + '</h4>';
	
	var thumbs = document.createElement( 'div' );
	thumbs.className = 'gthumbs';
	this.frame.appendChild( thumbs );
	
	this.thumbs = document.createElement( 'div' );
	this.thumbs.id = 'thumbs' + id;
	this.thumbs.className = 'ginner';
	thumbs.appendChild( this.thumbs );

	var cfloat = document.createElement( 'div' );
	cfloat.style.clear = 'both';
	thumbs.appendChild( cfloat );
	
	this.tnav = document.createElement( 'div' );
	this.tnav.id = 'thumbsnav' + id;
	this.tnav.className = 'gthumbsnav';
	this.tnav.style.display = 'none';
	thumbs.appendChild( this.tnav );
	
	var a_nav = document.createElement( 'div' );
	a_nav.className = 'garrows';
	this.tnav.appendChild( a_nav );
	
	this.a_show = document.createElement( 'div' );
	this.tnav.appendChild( this.a_show );
	
	this.a_show.style.left = 0 + 'px';

	this.a_prev = document.createElement( 'a' );
	this.a_prev.innerHTML = '<img src="../static/img/gallery_up.gif" />';
	a_nav.appendChild( this.a_prev );
	
	this.a_next = document.createElement( 'a' );
	this.a_next.innerHTML = '<img src="../static/img/gallery_down.gif" />';
	a_nav.appendChild( this.a_next );

	this.a_prev.href = "javascript:getGM().previous('" + this.id + "')";
	this.a_next.href = "javascript:getGM().next('" + this.id + "')";

	var width  = ( this.columns * this.thumbWidth() );
	var height = ( ( this.limit / this.columns ) * this.thumbHeight() );

	if( !this.single ) height += 25;
	
	thumbs.style.width = width + 'px';
	thumbs.style.height = height + 'px';
}

mxGalleryPrototype.thumbWidth = function() {

	var width = this.width;
	
	width += 0;
	
	return width;
}

mxGalleryPrototype.thumbHeight = function() {

	var height = this.height;
	
	height += 0;
	
	return height;
}

mxGalleryPrototype.init = function( append, step ) {

	if( !this.count ) return;

	var insert = [];
	
	var new_start = this.start;
	
	if( typeof( step ) != 'undefined' ) new_start += step;
	
	else step = 0;
	
	var insertBefore = false;
	if( !append ) insertBefore = this.thumbs.firstChild;

	for( var i = 0; i < this.count; i++ ) {
	
		var item = this.item_r[ i ];
		
		if( step > 0 ) {
		
			if( i < new_start ) {
			
				item.remove();
				continue;
			}
			
			if( i < this.start + this.limit ) continue;
			
		} else if( step < 0 ) {
		
			if( i >= new_start + this.limit ) {
			
				item.remove();
				continue;
			}
			
			if( i >= this.start ) continue;

			if( i < new_start ) continue;
		}

		if( i >= new_start + this.limit ) break;
		
		insert[ insert.length ] = i;
		
		item.createEmpty( insertBefore );
	}
	
	window.setTimeout( "getGM().insert('"+this.id+"','"+insert.join(',')+"')", 100 );
	
	this.start = new_start;
	
	end = this.start + this.limit;
	if( end > this.count ) end = this.count;
	
	if( !end || this.start == end - 1 ) this.a_show.innerHTML = end;
		
	else this.a_show.innerHTML = ( this.start + 1 ) + ' - ' + end;
		
	this.a_show.innerHTML += ' / ' + this.count;
	
	if( this.start == 0 ) this.a_prev.style.visibility = 'hidden';
	else this.a_prev.style.visibility = 'visible';
	
	if( this.start + this.limit >= this.count ) this.a_next.style.visibility = 'hidden';
	else this.a_next.style.visibility = 'visible';
	
	if( this.tnav.style.display != 'none' ) return;
	
	this.tnav.style.display = '';
	new Fx.Morph( this.tnav.id, { duration: 1000, transition: Fx.Transitions.Sine.easeIn } ).start( { 'opacity' : [ 0, 0.6 ] } );
}

mxGalleryPrototype.insert = function( id, insertBefore ) {

	if( !this.item_r[ id ] ) return;

	this.item_r[ id ].create( $(insertBefore) );
}

mxGalleryPrototype.addItem = function( div ) {

	if( !div ) return;
	
	if( div.className == 'galleryItem' ) {
	
		var id = this.item_r.length;
		
		var item = new mxGalleryItem( this, id, div );
		
		this.item_r[ id ] = item;
	
		if( !this.current ) this.current = item;
	
		return true;
	}
	
	if( div.className == 'galleryHead' ) this.head = div.innerHTML;
		
}

mxGalleryPrototype.hidePic = function( item, ready ) {

	if( !this.bg ) return;
	
	this.current = item;
	
	if( this.slideshow ) this.pause();

	if( typeof( ready ) != 'undefined' ) {
	
		this.picframe.style.display = 'none';
		this.bg.style.display = 'none';
		return;
	}
	
	var reduce = item.gallery.thumbs;
	
	var off = mxOffsetPosition( reduce );
	
	var multi = 0;
	if( this.single ) multi = -10;
	
	var smorph = new Fx.Morph( this.picframe.id, { duration: this.DELAY, transition: Fx.Transitions.Sine.easeOut } );
	smorph.start( {
		'top'    : off[ 1 ] + multi,
		'height' : reduce.offsetHeight + multi,
		'left'   : off[ 0 ],
		'width'  : reduce.offsetWidth + multi
	});
	
	mxSwitchSelects( false );

	var morph = new Fx.Morph( this.picture.id, { duration: this.DELAY, transition: Fx.Transitions.linear } );
	morph.start( { 'opacity' : 0 } );
	morph.onComplete = new Function( "getGM().hidePic('"+this.id+"',1)" );
	
	new Fx.Morph( this.bg.id, { duration: this.DELAY, transition: Fx.Transitions.linear } ).start( { 'opacity' : 0 } );
	new Fx.Morph( this.label.id, { duration: this.DELAY, transition: Fx.Transitions.Sine.easeOut } ).start( { 'opacity' : 0 } );
}

mxGalleryPrototype.showPic = function( item, ready ) {

	this.current = item;

	if( this.p_slider ) {
		
		this.p_slider.style.width = '0px';
		this.p_slider.style.display = 'none';
	}
	
	var index = item.index();
	
	if( typeof( ready ) != 'undefined' ) {
	
		if( this.slideshow ) this.play( item );

		this.label.innerHTML = '';
	
		this.picture.appendChild( item.full );
		
		if( item.head ) this.label.innerHTML += '<b>' + item.head + '</b>';
		if( item.desc ) this.label.innerHTML += '<div>' + item.desc + '</div>';
		
		new Fx.Morph( this.label.id, { duration: this.DELAY, transition: Fx.Transitions.Sine.easeIn } ).start( { 'opacity' : [ 0, 0.6 ] } );
		
		return;
		
	} else item.loadPicture();
	
	if( this.label && this.label.style.opacity ) new Fx.Morph( this.label.id, { duration: this.DELAY / 2, transition: Fx.Transitions.Sine.easeIn } ).start( { 'opacity' : 0 } );

	if( !this.bg ) {
	
		this.bg = document.createElement( 'div' );
		this.bg.className = 'gbackground';
		this.bg.id = 'gbg' + this.id;
		this.bg.style.opacity = 0;
		this.bg.onclick = new Function( "getGM().hidePic('" + this.id + "')" );
		document.body.insertBefore( this.bg, document.body.firstChild );
		
		if( !mxIsIE6() ) this.bg.style.position = 'fixed';
		
		this.picframe = document.createElement( 'div' );
		this.picframe.id = 'gpicframe' + this.id;
		this.picframe.className = 'gpicture';
		if( !index ) this.picframe.className = 'gpicture single';
		this.picframe.style.display = 'none';
		document.body.insertBefore( this.picframe, document.body.firstChild );
	
		this.picture = document.createElement( 'div' );
		this.picture.id = 'gpicture' + this.id;
		this.picture.className = 'ginner';
		this.picframe.appendChild( this.picture );
		
		var close = document.createElement( 'div' );
		close.className = 'close';
		close.innerHTML = '<a href="javascript:getGM().hidePic(\'' + this.id + '\')"><img src="../static/img/gallery_close.gif" /></a>'; 
		this.picframe.appendChild( close );		
		
		this.nav = document.createElement( 'div' );
		this.nav.id = 'gnav' + this.id;
		this.nav.className = 'gpicnav';
		this.picframe.appendChild( this.nav );
		
		this.label = document.createElement( 'div' );
		this.label.id = 'glabel' + this.id;
		this.label.className = 'glabel';
		this.picframe.appendChild( this.label );
		
		this.p_show = document.createElement( 'div' );
		this.nav.appendChild( this.p_show );
		
		this.p_show.style.left = 0 + 'px';
	
		this.p_prev = document.createElement( 'a' );
		this.p_prev.innerHTML = '<img src="../static/img/gallery_left.gif" />';
		this.nav.appendChild( this.p_prev );

		this.p_pause = document.createElement( 'a' );
		this.p_pause.innerHTML = '<img src="../static/img/gallery_pause.gif" />';
		this.nav.appendChild( this.p_pause );
		
		this.p_play = document.createElement( 'a' );
		this.p_play.innerHTML = '<img src="../static/img/gallery_play.gif" />';
		this.nav.appendChild( this.p_play );		
		
		this.p_next = document.createElement( 'a' );
		this.p_next.innerHTML = '<img src="../static/img/gallery_right.gif" />';
		this.nav.appendChild( this.p_next );
		
		this.pause();
	}
		
	if( index ) {

		this.p_prev.href = "javascript:getGM().showPic('" + index[ 0 ][ 0 ] + "'," + index[ 0 ][ 1 ] + ")";
		this.p_next.href = "javascript:getGM().showPic('" + index[ 1 ][ 0 ] + "'," + index[ 1 ][ 1 ] + ")";
		
	} else this.nav.style.display = 'none';
	
	new Fx.Morph( this.picture.id, { duration: this.DELAY, transition: Fx.Transitions.Sine.easeIn } ).start( { 'opacity' : [ 0, 1 ] } );
	new Fx.Morph( this.bg.id, { duration: this.DELAY, transition: Fx.Transitions.Sine.easeIn } ).start( { 'opacity' : [ this.bg.style.opacity, 0.4 ] } );

	this.p_prev.blur();
	this.p_next.blur();
	
	if( index ) this.p_show.innerHTML = ( index[ 2 ][ 0 ] + 1 ) + ' / ' + index[ 2 ][ 1 ];

	var winsize = mxWinSize();
	var scroll  = mxScrolled();
	this.bg.style.width  = ( winsize[ 0 ] + scroll[ 0 ] ) + 'px';
	this.bg.style.height = ( winsize[ 1 ] + scroll[ 1 ] ) + 'px';
	
	mxSwitchSelects( true );
	
	item.fullsize( this );
	
	this.bg.style.display = 'block';
	this.picframe.style.display = 'block';
}

mxGalleryPrototype.pause = function( item ) {

	if( typeof( item ) != 'undefined' ) this.current = item;

	this.slideshow = false;

	if( this.p_slider ) {
	
		this.p_slider.style.display = 'none';
		this.p_slider.style.width = '0px';
	}
	
	if( this.timeout ) this.timeout.cancel();
	
	if( this.p_pause ) {
	
		this.p_pause.href = "javascript:void(0)";
		this.p_pause.className = "disabled";
		this.p_pause.blur();
	}
	
	if( this.p_play && this.current.index() ) {
	
		this.p_play.href = "javascript:getGM().play('" + this.current.gallery.id + "'," + this.current.id + ")";
		this.p_play.className = "";
	}
}

mxGalleryPrototype.play = function( item ) {

	var index = item.index();
	
	if( !index ) return;	

	if( !this.slideshow ) {

		this.slideshow = true;
	
		this.p_pause.href = "javascript:getGM().pause('" + this.id + "')";
		this.p_pause.className = "";
	
		this.p_play.href = "javascript:void(0)";
		this.p_play.className = "disabled";
		this.p_play.blur();
	}
	
	if( !this.p_slider ) {
	
		this.p_slider = document.createElement( 'a' );
		this.p_slider.id = 'gslider' + this.id;
		this.p_slider.className = 'gslider';
		this.picframe.appendChild( this.p_slider );
	}
	
	if( this.timeout ) this.timeout.cancel();

	this.p_slider.style.display = '';
	
	getGM().gallery_r[ index[ 1 ][ 0 ] ].item_r[ index[ 1 ][ 1 ] ].loadPicture();
	
	this.timeout = new Fx.Morph( this.p_slider.id, { duration: this.slide, transition: Fx.Transitions.linear } );
	this.timeout.start( { 'width'  : [ 0, this.picture.offsetWidth ] });
	this.timeout.onComplete = new Function( "getGM().slidePic('" + index[ 1 ][ 0 ] + "'," + index[ 1 ][ 1 ] + ")" );
}

mxGalleryPrototype.previous = function() {
	
	this.a_prev.blur();

	if( this.start == 0 ) return;
	
	this.init( false, 0 - this.step );
}

mxGalleryPrototype.next = function() {
	
	this.a_next.blur();
		
	if( this.start + this.limit >= this.item_r.length ) return;
	
	this.init( true, this.step );
}

