/** photogallery **/
function Photogallery(instanceName, imagesSource)
{
	this.instanceName = instanceName;
	this.mode = "passive";

	if( imagesSource != null ) {
		this.images = imagesSource;
	} else {
		this.images = null;
		this.imageSrc = 0;
		this.mode = "active";
		this.request = null;
	}
	
	this.divScreen = null;
	this.divGallery = null;
	this.currentImage = { width: 500, height: 400 };
	this.visible = false;

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

Photogallery.prototype.onResponse = function()
{
    if( (this.request.readyState == 4) &&
		((this.request.status == 200) || (window.location.href.indexOf("http")==-1)) )
    {
    	var sr = this.request.responseText;
        try {
        	this.images = sr.parseJSON(null);
			this.currentImage = this.images[0];
			this.show();
			this.positionElements();
        } catch( exception ) {
        	this.images = null;
			return;
        }
    }
}

Photogallery.prototype.onClick = function(imgid, buck)
{
	if( this.mode == "passive" ) {
		for( var j = 0; j < this.images.length; j ++ ) {
			if( this.images[j].id == imgid ) {
				this.currentImage = this.images[j];
				this.show();
				this.positionElements();
				if( buck )
					return false;
				else
					break;
			}
		}
	} else {
		if( this.imageSrc == imgid ) { // loaded
			this.currentImage = this.images[0];
			this.show();
			this.positionElements();
		} else { // not
		    if (window.XMLHttpRequest)
		        this.request = new XMLHttpRequest();
		    else if (window.ActiveXObject)
		    {   
		        try {
					this.request = new ActiveXObject("Msxml2.XMLHTTP")
		        } catch (e) { 
	                try{
	                        this.request = new ActiveXObject("Microsoft.XMLHTTP")
	                } catch (e) {
	                        return true;
	                }
		        }
		    } else {
		        return true;
		    }

			var url = "/" + imgid + ",gallery.src";

			this.request.onreadystatechange = function() { igal_index.onResponse(); };
	        this.request.open( "GET", url );
            this.request.send( null );
		}
		
		if( buck )
			return false;
	}
		
	if( buck )
		return true;
}

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

Photogallery.prototype.onWindowSize = function()
{
	if( this.visible )
		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 );
				this.positionElements();
				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 ] );
				this.positionElements();
				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 );
	
	if( document.documentElement )
		document.documentElement.style.overflow = "hidden";
	document.body.style.overflow = "hidden";
	this.visible = true;
}

Photogallery.prototype.hide = function()
{
	this.divScreen.style.display = "none";
	this.divGallery.style.display = "none";
	
	if( document.documentElement )
		document.documentElement.style.overflow = "";
	document.body.style.overflow = "";
	this.visible = false;
}

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 + 50) + "px";
	this.divScreen.style.height = (mtx.height + 50) + "px";
	
	this.divGallery.style.left = ((mtx.offX + ((mtx.width - (this.currentImage.width + 80)) / 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\" style=\"width: " + photo.width + "px;\">" + photo.title + "</div>" +
								linkNext;
	this.divGallery.style.width = (photo.width + 80) + "px";
}
