
// Prototype for the gallery item class

var mxGalleryItemPrototype = mxGalleryItem.prototype;

function mxGalleryItem( gallery, id, div ) {

	this.id = id;
	this.gallery = gallery;
	this.visible = false;
	this.show_empty = false;

	var img = div.getElementsByTagName( 'img' );
	
	for( var j = 0; j < img.length; j++ ) {
		if( img[ j ].className == 'full' ) this.full = img[ j ];
		else if( img[ j ].className == 'thumb' ) this.thumb = img[ j ];
	}
	
	div.removeChild( this.full );
	div.removeChild( this.thumb );
	
	this.fwidth = this.full.getAttribute( 'width' ) - 0;
	this.fheight = this.full.getAttribute( 'height' ) - 0;
	this.twidth = this.thumb.getAttribute( 'width' ) - 0;
	this.theight = this.thumb.getAttribute( 'height' ) - 0;
	
	var head = div.getElementsByTagName( 'h3' );
	
	if( head.length ) {
		this.head = head[ 0 ];
		div.removeChild( this.head );
		this.head = this.head.innerHTML;
	} else this.head = null;

	var desc = div.getElementsByTagName( 'p' );
	
	if( desc.length ) {
		this.desc = desc[ 0 ];
		div.removeChild( this.desc );
		this.desc = this.desc.innerHTML;
	} else this.desc = null;
}

mxGalleryItemPrototype.create = function( insertBefore ) {

	if( this.visible ) return;
	
	if( !this.container ) {
	
		this.container = document.createElement( 'div' );
		this.container.className = 'gthumbnail';
		this.container.id = 'g' + this.gallery.id + 'thumb' + this.id;
		
		var height = this.gallery.thumbHeight();
		var multi = 0;
		if( !this.gallery.single ) multi = -4;
		
		this.container.style.width = this.gallery.thumbWidth() + 'px';
		this.container.style.height = height + 'px';
		
		this.inner = document.createElement( 'div' );
		this.inner.className = 'ginner';

		var height = this.gallery.thumbHeight();
		var width  = this.gallery.thumbWidth();
		
		this.inner.style.width = ( width + multi ) + 'px';
		this.inner.style.height = ( height + multi ) + 'px';
		
		var vspace = Math.round( ( height - this.theight ) / 2 ) - 3;
		if( vspace > 0 ) this.thumb.style.marginTop = vspace + 'px';
		
		var hspace = Math.round( ( width - this.twidth ) / 2 ) - 3;
		if( hspace > 0 ) this.thumb.style.marginLeft = hspace + 'px'; 
		
		this.container.appendChild( this.inner );
		this.inner.appendChild( this.load( this.thumb ) );
 		this.inner.onclick = new Function( "getGM().showPic('" + this.gallery.id + "'," + this.id + ")" );
 	}
 	
	this.visible = true;
	
	if( this.show_empty ) {
	
		this.gallery.thumbs.replaceChild( this.container, this.empty )
		
	} else this.gallery.thumbs.appendChild( this.container );
	
	new Fx.Morph( this.container.id, { duration: getGM().DELAY * 2, transition: Fx.Transitions.Quad.easeIn } ).start( { 'opacity' : [ 0, 1 ] } );
	
	//this.container.style.height = 20+'px';
	//this.container.style.border="1px solid #FF0000";
	//this.inner.style.height = 20+'px';
	//this.inner.style.border="1px solid #000000";

}

mxGalleryItemPrototype.loadPicture = function() {

	this.full = this.load( this.full ); 
}

mxGalleryItemPrototype.load = function( img ) {

	var mxsrc = img.getAttribute( 'mxsrc' )

	if( mxsrc ) {
	
		if( mxIsIE() && mxsrc.substr( mxsrc.length - 3, 3 ) == 'png' ) {
		
			var tag = document.createElement( 'span' );
			
			tag.style.width = img.getAttribute( 'width' ) + 'px';
			tag.style.height = img.getAttribute( 'height' ) + 'px';
			tag.style.display = 'inline-block';
			tag.style.background = 'white';
			tag.style.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'' + mxsrc + '\',sizingMethod=\'scale\',enabled=true)';
			tag.className = img.className;
			
			if( img.parentNode ) img.parentNode.replaceChild( tag, img );
			
			img = tag;
			
		} else {

			var old = [ img.getAttribute( 'width' ), img.getAttribute( 'height' ) ];
			img.src = mxsrc;
			img.setAttribute( 'width', old[ 0 ] );
			img.setAttribute( 'height', old[ 1 ] );
		}
 		
 		img.removeAttribute( 'mxsrc' );
 	}

 	return img;
}

mxGalleryItemPrototype.index = function() {

	if( typeof( this.picindex ) == 'undefined' ) {
	
		this.picindex = getGM().index( this.gallery.id, this.id );
	}
	
	return this.picindex;
}

mxGalleryItemPrototype.fullsize = function( mainGallery ) {

	var index = this.index();
	
	if( !index ) {

		this.full.onclick = new Function( "getGM().hidePic('" + this.gallery.id + "')" );
		this.full.oncontextmenu = new Function( "getGM().hidePic('" + this.gallery.id + "');return false;" );
		
	} else {
	
		this.full.oncontextmenu = new Function( "getGM().showPic('" + index[ 0 ][ 0 ] + "'," + index[ 0 ][ 1 ] + ");return false;" );
		this.full.onclick = new Function( "getGM().showPic('" + index[ 1 ][ 0 ] + "'," + index[ 1 ][ 1 ] + ")" );
 	}
 	
 	var frame = mainGallery.picframe;
 	var pic   = mainGallery.picture;
 	
 	if( frame.style.display == 'none' ) {
 	
		var off = mxOffsetPosition( this.gallery.thumbs );
		
		frame.style.left   = off[ 0 ] + 'px';
		frame.style.top    = off[ 1 ] + 'px';
		frame.style.width  = this.gallery.thumbs.offsetWidth + 'px';
		frame.style.height = this.gallery.thumbs.offsetHeight + 'px';
 	}
 	
 	var replace = pic.firstChild;
 	if( replace ) pic.removeChild( replace );
 	
	var winsize = mxWinSize();
	
	var width = this.fwidth;
	var height = this.fheight;
	
	if( width < 250 ) {
		this.full.style.paddingLeft = Math.round( ( 250 - width ) / 2 ) + 'px';
		width = 250;
	}
	if( height < 200 ) {
		this.full.style.paddingTop = Math.round( ( 200 - height ) / 2 ) + 'px';
		height = 200;
	}
	
	if( width > ( winsize[ 0 ] - 100 ) ) width = winsize[ 0 ] - 120;
	if( height > ( winsize[ 1 ] - 100 ) ) height = winsize[ 1 ] - 120;
	
	var left = ( winsize[ 0 ] - width ) / 2;
	var top = ( ( winsize[ 1 ] - height ) / 2 ) - 30;
	
	var scroll = mxScrolled();

	var multi = 0;
	if( this.gallery.single ) multi = -10;

	var morph = new Fx.Morph( frame.id, { duration: this.gallery.DELAY, transition: Fx.Transitions.Sine.easeOut } )
	morph.onComplete = new Function( "getGM().showPic('"+this.gallery.id+"',"+this.id+",1)" );
	morph.start( {
		'top'    : top + scroll[ 1 ] + multi,
		'height' : height,
		'left'   : left + scroll[ 0 ],
		'width'  : width
	});
}

mxGalleryItemPrototype.createEmpty = function( insertBefore ) {

	this.show_empty = true; 

	if( !this.empty ) {

		this.empty = document.createElement( 'div' );
		this.empty.className = 'gthumbnail empty';
		this.empty.id = 'g' + this.gallery.id + 'empty' + this.id;
		
		this.empty.style.width = this.gallery.thumbWidth() + 'px';
		this.empty.style.height = this.gallery.thumbHeight() + 'px';
	}
	
	if( !insertBefore ) this.gallery.thumbs.appendChild( this.empty );
	
	else this.gallery.thumbs.insertBefore( this.empty, insertBefore );	
}

mxGalleryItemPrototype.remove = function() {
	
	if( !this.container || !this.visible ) return;
	
	this.visible = false;
	
	new Fx.Morph( this.container.id, { duration: 10, transition: Fx.Transitions.linear } ).start( { 'opacity' : 0 } );
	
	this.gallery.thumbs.removeChild( this.container );
}