/** photogallery **/
function Photogallery(eclass, instanceName)
{
	this.instanceName = instanceName;
	this.images = new Array();
	
	var allImages = document.getElementsByTagName( "IMG" );
		
	for( var j = 0; j < allImages.length; j ++ ) {
		var img = allImages[ j ];
		var className = "";
		
		if( !img.getAttribute ) continue;
		
		var fullSizeUrl = img.getAttribute( "fv" );
		var fullSizeW = img.getAttribute( "fw" );
		var fullSizeH = img.getAttribute( "fh" );
		var imgid = img.getAttribute( "gi" );
		var fullTitle = img.getAttribute( "title" );
		
		if( (fullSizeUrl == "") ||
			(fullSizeUrl == null) ||
			(fullSizeW == "") ||
			(fullSizeW == null) ||
			(fullSizeH == "") ||
			(fullSizeH == null) ||
			(fullTitle == "") ||
			(fullTitle == null) ||
			(imgid == "") ||
			(imgid == null) ) {
			continue;
		}
		
		if( img.className !== undefined ) {
			className = img.className;
		} else {
			className = img.getAttribute( "class" );
		}

		if( className == "" )
			continue;
			
		for( var x = 0; x < eclass.length; x ++ ) {
			if( className == eclass[ x ] ) {
				this.images[ this.images.length ] = {
					id: imgid,
					title: fullTitle,
					url: fullSizeUrl,
					width: fullSizeW,
					height: fullSizeH					
				};
			}
		}
	}
	
	this.divScreen = null;
	this.divGallery = null;
	this.currentImage = null;
}

Photogallery.prototype.onClick = function(imgid)
{
	for( var j = 0; j < this.images.length; j ++ ) {
		if( this.images[j].id == imgid ) {
			this.currentImage = this.images[j];
			this.show();
			return false;
		}
	}
	
	return true;
}

Photogallery.prototype.onClose = function()
{
	this.hide();
	this.currentImage = null;
}

Photogallery.prototype.onWindowSize = function()
{
	this.positionElements();
}

Photogallery.prototype.onNext = function()
{
	for( var j = 0; j < this.images.length; j ++ ) {
		if( this.images[ j ].id == this.currentImage.id ) {
			if( (j + 1) < this.images.length ) {
				this.currentImage = this.images[ j+1 ];
				this.loadPhoto( this.currentImage );
				break;
			}
		}
	}
}

Photogallery.prototype.onPrev = function()
{
	for( var j = 0; j < this.images.length; j ++ ) {
		if( this.images[ j ].id == this.currentImage.id ) {
			if( (j - 1) >= 0 ) {
				this.currentImage = this.images[ j-1 ];
				this.loadPhoto( this.images[ j-1 ] );
				break;
			}
		}
	}
}

Photogallery.prototype.show = function()
{
	if( (this.divSreen == null) || (this.divGallery == null) )
		this.initElements();

	this.divScreen.style.display = "block";
	this.divGallery.style.display = "block";
	this.positionElements();
	this.loadPhoto( this.currentImage );
	
	document.body.galleryHandler = this;
	var onResizeHandler = this.createResizeHandler();
	setEventHandler( window, "resize", onResizeHandler );

    var msk = /MSIE (\d+\.\d+);/;
    if( msk.test(navigator.userAgent) ) {
		if( document.documentElement ) {
			document.documentElement.style.overflow = "hidden";
		} else {
			document.body.style.overflow = "hidden";
		}
	} else {
		document.body.style.overflow = "hidden";
	}
}

Photogallery.prototype.hide = function()
{
	this.divScreen.style.display = "none";
	this.divGallery.style.display = "none";
	
	document.body.galleryHandler = null;
	document.body.style.overflow = "";

	if( document.documentElement )
		document.documentElement.style.overflow = "";

	var onResizeHandler = this.createResizeHandler();
	remEventHandler( window, "resize", onResizeHandler );
}

Photogallery.prototype.createResizeHandler = function()
{
	eval( "var func = function() { " + this.instanceName + ".onWindowSize(); }" );
	return func;
}

Photogallery.prototype.createCloseHandler = function()
{
	eval( "var func = function() { " + this.instanceName + ".onClose(); }" );
	return func;
}

Photogallery.prototype.initElements = function()
{
	this.divScreen = document.createElement( "DIV" );
	this.divGallery = document.createElement( "DIV" );
	
	this.divGallery.id = this.instanceName;
	this.divGallery.style.position = "absolute";
	this.divGallery.style.zIndex = 101;
	
	this.divScreen.style.position = "absolute";
	this.divScreen.style.background = "#000000";
    this.divScreen.style.opacity = 0.7;
    this.divScreen.style.filter = "alpha(opacity=70)";
    if( this.divScreen.style.setAttribute )
		this.divScreen.style.setAttribute( "-moz-opacity", "0.7" );
	this.divScreen.style.zIndex = 100;
	
	setEventHandler( this.divScreen, "click", this.createCloseHandler() );

	document.body.appendChild( this.divScreen );
	document.body.appendChild( this.divGallery );
}

Photogallery.prototype.positionElements = function()
{
	var mtx = getDocMetrics();
	
	this.divScreen.style.left = mtx.offX + "px";
	this.divScreen.style.top = mtx.offY + "px";
	this.divScreen.style.width = mtx.width + "px";
	this.divScreen.style.height = mtx.height + "px";
	
	this.divGallery.style.left = (mtx.offX + ((mtx.width - this.currentImage.width) / 2)) + "px";
	this.divGallery.style.top = (mtx.offY + ((mtx.height - this.currentImage.height) / 2)) + "px";
}

Photogallery.prototype.loadPhoto = function(photo)
{
	var linkPrev = "<A class=\"linkPrev\" href=\"javascript:" + this.instanceName + ".onPrev();\">&nbsp;</A>";
	var linkNext = "<A class=\"linkNext\" href=\"javascript:" + this.instanceName + ".onNext();\">&nbsp;</A>";
	
	for( var j = 0; j < this.images.length; j ++ ) {
		if( this.images[ j ].id == photo.id ) {
			if( j == 0 )
				linkPrev = "<DIV class=\"disabledLinkPrev\">&nbsp;</DIV>";
			
			if( (j+1) >= this.images.length )
				linkNext = "<DIV class=\"disabledLinkNext\">&nbsp;</DIV>";
		}
	}
	
	this.divGallery.innerHTML = "<A class=\"linkClose\" href=\"javascript:" + this.instanceName + ".onClose();\">&nbsp;</A>" +
								"<center style=\"height:" + photo.height + "px; clear: both;\"><IMG src=\"" + photo.url + 
								"\" width=\"" + photo.width + "\" height=\"" + photo.height + "\"></center>" +
								linkPrev +
								"<div class=\"title\">" + photo.title + "</div>" +
								linkNext;
}