/*************************************
 **
 ** Code by: Johan Guttormsson, 2009
 ** for http://www.poledance.se
 ** 
 ** Changelog:
 **
 ** 2009-09-04: Created
 **
 ** http://www.jgsoft.se
 **
 ***********************************/

var ID;
var Caption;
var Gallery;
var Height;
var Width;
var ImageNumber;
var ImageTotal;
var FileName;

function ShowPhoto(ID, Caption, Gallery, Height, Width, ImageNumber, ImageTotal, FileName)
{
	this.ID = ID;
	this.Caption = Caption;
	this.Gallery = Gallery;
	this.Height = Height;
	this.Width = Width;
	this.ImageNumber = ImageNumber;
	this.ImageTotal = ImageTotal;
	this.FileName = FileName;

	LoadPhoto();
}

function LoadPhoto()
{
	// Create a semi-transparent wrapper that 
	// will cover the rest of the page
	if( !$('Wrapper') )
	{
		Wrapper = new Element('div', { id: 'Wrapper' });
		Wrapper.setStyle({
			top: document.viewport.getScrollOffsets().top+"px"
		});
		document.body.appendChild(Wrapper);
		SetOpacity('Wrapper',7);
	}
	else
		Wrapper = $('Wrapper');
	
	// Create the container for the image
	if( !$('Photo') )
	{
	   	Photo = new Element('div', { id: 'Photo' });
		Photo.setStyle({
			zIndex: '1001'
		});
	   	document.body.appendChild(Photo);
	}
	else
	{
		Photo = $('Photo');
	}

	Photo.setStyle({
		height: this.Height + 20 + "px",
		width: this.Width + 20 + "px",
		top: ((document.viewport.getHeight() / 2) - (this.Height / 2) + document.viewport.getScrollOffsets().top)+"px",
		left: ((document.viewport.getWidth() / 2) - (this.Width / 2)) +"px"
	});

	if( !$('pic') )
	{
		Pic = new Element('div', { 
			id: 'pic'
			//class: 'pic' 
			});
		$('Photo').appendChild(Pic);
	}
	else
	{
		Pic = $('pic');
	}

	Pic.setStyle({
		width: this.Width +'px',
		height: this.Height + 'px',
		margin: '10px auto 0px auto',
		background: "url(/Galleries/"+this.Gallery+"/"+this.FileName+")"
	});
	Pic.innerHTML = "<div style=\"float: left;line-height: "+this.Height+"px;\"><a href=\"javascript:BrowseGallery('Previous')\">«</a></div>";
	Pic.innerHTML += "<div style=\"float: right;line-height: "+this.Height+"px;\"><a href=\"javascript:BrowseGallery('Next');\">»</a></div>";
	Pic.innerHTML += "<div style=\";float: right;line-height: "+this.Height*1.95+"px;\"><a style=\"font-size: 20px;\" href=\"javascript:CloseGallery()\">Close</a></div>";

	// Make the wrapper cover everything, even
	// when scrolling
	window.onscroll = function() {
		Wrapper.setStyle({ top: document.viewport.getScrollOffsets().top+"px" });
	}
}

function SetOpacity(e, value)
{
	$(e).setStyle({
		opacity: value/10,
		filter: 'alpha(opacity=' + value*10 + ')'
	});
}

// Browse logic for gallery
function BrowseGallery(Direction)
{
	if(Direction == "Next")
		if(this.ImageNumber == this.ImageTotal)
			this.ImageNumber = 1;
		else
			this.ImageNumber++;
	else
		if(this.ImageNumber == 1)
			this.ImageNumber = this.ImageTotal;
		else
			this.ImageNumber--;

	new Ajax.Request('functions/GetImageProperties.php', {
		method: 'get',
		parameters: { g: this.Gallery, inr: this.ImageNumber },
		onSuccess: function(transport) 
		{
			this.ID = parseInt(transport.responseJSON["id"]);
			this.Caption = transport.responseJSON["caption"];
			this.Height = parseInt(transport.responseJSON["height"]);
			this.Width = parseInt(transport.responseJSON["width"]);
			this.FileName = transport.responseJSON["filename"];
			LoadPhoto();
		}
	});
}

// Remove the wrapper and the gallery div's
function CloseGallery()
{
	document.body.removeChild($('Photo'));
	document.body.removeChild($('Wrapper'));
}
