var mxGM = false;
var mxGMinit = null;
function getGM() {
	if( !mxGMinit ) mxGMinit = window.setTimeout( "getGM().init()", 300 );
	if( !mxGM ) mxGM = new mxGalleryManager();
	return mxGM;
}

// Prototype for the gallery manager class

var mxGalleryManagerPrototype = mxGalleryManager.prototype;

function mxGalleryManager() {

	this.DELAY = 150;
	this.blocked = false;
	this.gallery_r = new Object();
	this.gallery_r_count = 0;
}

mxGalleryManagerPrototype._create = function( width, height, cols, rows, slide ) {

	var gallery_elements = $$('div.gallery');
	
	this.gallery_r_count++;

	gallery_elements[ 0 ].setAttribute( 'id', 'gallery' + this.gallery_r_count );

	this.create( this.gallery_r_count, width, height, cols, rows, slide );
}

mxGalleryManagerPrototype._showPic = function( element, pid, ready ) {
	
	var searchRegex = /id=["]{0,1}thumbs([0-9]*)["]{0,1}/;
	 
	searchRegex.exec( element.parentNode.innerHTML );
	 
	this.showPic( RegExp.$1, pid, ready );
}

mxGalleryManagerPrototype.create = function( gid, width, height, cols, rows, slide ) {

	this.gallery_r[ gid ] = new mxGallery( gid, width, height, cols, rows, slide );
}

mxGalleryManagerPrototype.init = function() {

	for( var gid in this.gallery_r ) {
	
		this.gallery_r[ gid ].init( true );
	}
}

mxGalleryManagerPrototype.hidePic = function( gid, ready ) {
	
	if( !this.gallery_r[ gid ] || this.blocked  ) return;
	
	var gallery = this.getGallery( gid );
	
	gallery.hidePic( gallery.current, ready );
}

mxGalleryManagerPrototype.showPic = function( gid, pid, ready ) {
	
	if( !this.gallery_r[ gid ] || this.blocked  ) return;
	
	var gallery = this.getGallery( gid );
	var item = this.getItem( gid, pid );
	
	if( typeof( ready ) == 'undefined' ) gallery.pause( item );
		
	gallery.showPic( item, ready );
}

mxGalleryManagerPrototype.slidePic = function( gid, pid ) {
	
	if( !this.gallery_r[ gid ] || this.blocked  ) return;

	var gallery = this.getGallery( gid );
	var item = this.getItem( gid, pid );
	
	gallery.showPic( item );
}

mxGalleryManagerPrototype.previous = function( gid ) {

	if( !this.gallery_r[ gid ] || this.blocked ) return;
	
	this.gallery_r[ gid ].previous();
}

mxGalleryManagerPrototype.next = function( gid ) {

	if( !this.gallery_r[ gid ] || this.blocked  ) return;
	
	this.gallery_r[ gid ].next();
}

mxGalleryManagerPrototype.play = function( gid, pid ) {

	if( !this.gallery_r[ gid ] || this.blocked ) return;
	
	var gallery = this.getGallery( gid );
	var item = this.getItem( gid, pid );
	
	gallery.play( item );
}

mxGalleryManagerPrototype.pause = function( gid ) {

	if( !this.gallery_r[ gid ] || this.blocked  ) return;
	
	this.getGallery( gid ).pause();
}

mxGalleryManagerPrototype.insert = function( gid, insert ) {

	if( !this.gallery_r[ gid ] ) return;
	
	this.blocked = true;
	
	insert = insert.split( ',' );
	
	var pid = insert.shift();
	
	if( pid ) {
	
		this.gallery_r[ gid ].insert( pid );
		
		if( insert.length ) window.setTimeout( "mxGM.insert('"+gid+"','"+insert.join(',')+"')", 100 );
		
		else this.blocked = false;
	}
}

mxGalleryManagerPrototype.index = function( gid, pid ) {

	if( !this.gallery_r[ gid ] ) return false;
	
	if( this.gallery_r[ gid ].single ) {
	
		var single_r = this.singleGalleries();
		
		var count = single_r.length;
		
		if( count <= 1 ) return;
	
		for( var i = 0; i < count; i++ ) {
		
			if( single_r[ i ] != gid ) continue;
			
			var next = i + 1;
			if( next == count ) next = 0;
			
			var prev = i - 1;
			if( prev < 0 ) prev = count - 1;
			
			return [ [ single_r[ prev ], 0 ], [ single_r[ next ], 0 ], [ i, count ] ]
		}
		
		return false;
	}
	
	var count = this.gallery_r[ gid ].count;
	
	if( count == 1 ) return;
	
	var next = pid + 1;
	if( next == count ) next = 0;
	
	var prev = pid - 1;
	if( prev < 0 ) prev = count - 1;
	
	return [ [ gid, prev ], [ gid, next ], [ pid, count ] ];	
}

mxGalleryManagerPrototype.singleGalleries = function() {

	if( !this.single_r ) {
	
		this.single_r = [];
		
		for( var gid in this.gallery_r ) {
		
			if( !this.gallery_r[ gid ].single ) continue;
			
			this.single_r[ this.single_r.length ] = gid;
		}
	}
	
	return this.single_r;
}

mxGalleryManagerPrototype.getGallery = function( gid ) {

	if( !this.gallery_r[ gid ].single ) return this.gallery_r[ gid ];
	
	var single_r = this.singleGalleries();
	
	return this.gallery_r[ single_r[ 0 ] ];	
}

mxGalleryManagerPrototype.getItem = function( gid, pid ) {

	return this.gallery_r[ gid ].item_r[ pid ];
}