/*************************************
 **
 ** Code by: Johan Guttormsson, 2009
 ** for http://www.poledance.se
 ** 
 ** Changelog:
 **
 ** 2010-08-13: Completely rewritten for jquery
 ** 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()
{
	if(this.Height > this.Width)
		TopOffset = 4;
	else
		TopOffset = 2;
	// Create a semi-transparent wrapper that 
	// will cover the rest of the page
	if( $('#Wrapper').length == 0 )
	{
		$('<div/>', {
			'id': 'Wrapper'
		}).css('top', $(window).scrollTop() + 'px').appendTo('body');
	}

	SetOpacity('#Wrapper',7);
	
	// Create the container for the image
	if( $('#Photo').length == 0 )
	{
		$('<div/>', {
			'id': 'Photo'
		}).css( {
			'height': this.Height + 20 + 'px', 
			'width': this.Width + 20 + 'px', 
			'top': $(window).scrollTop() + (this.Height / TopOffset)+'px',
			'left': ($(window).width() / 2) - (this.Width / 2) + 'px'
		}).appendTo('body');
	}

	if( $('#pic').length == 0 )
	{
		$('<div/>', {
			'id': 'pic'
		}).css( {
			'width': this.Width + 'px',
			'height': this.Height + 'px',
			'margin': '10px auto 0px auto',
			'background-image': 'url(/Galleries/'+this.Gallery+'/'+this.FileName+')'
		}).appendTo('#Photo');

		$('<div/>', {
			'id': 'prev'
		}).css( {
			'float': 'left',
			'line-height': this.Height+'px',
			'cursor': 'pointer'
		}).appendTo('#pic');
		$('#prev').text('«');
		$('#prev').click(function() {
			BrowseGallery('Prev');
		});

		$('<div/>', {
			'id': 'next'
		}).css( {
			'float': 'right',
			'line-height': this.Height+'px',
			'cursor': 'pointer'
		}).appendTo('#pic');
		$('#next').text('»');
		$('#next').click(function() {
			BrowseGallery('Next');
		});

		$('<div/>', {
			'id': 'close'
		}).css( {
			'float': 'right',
			'line-height': this.Height*1.95+'px',
			'font-size': '20px',
			'cursor': 'pointer'
		}).appendTo('#pic');
		$('#close').text('Close');
		$('#close').click(function() {
			CloseGallery();
		});
	}

	else
	{
		$('#pic').css( {	
			'width': this.Width + 'px',
			'height': this.Height + 'px',
			'margin': '10px auto 0px auto',
			'background-image': 'url(/Galleries/'+this.Gallery+'/'+this.FileName+')'
		});
		$('#Photo').css({
			'height': this.Height + 20 + 'px', 
			'width': this.Width + 20 + 'px', 
			'top': $(window).scrollTop() + (this.Height / TopOffset)+'px',
			'left': ($(window).width() / 2) - (this.Width / 2) + 'px'
		});
		$('#next').css({
			'float': 'right',
			'line-height': this.Height+'px',
			'cursor': 'pointer'
		});
		$('#prev').css({
			'float': 'left',
			'line-height': this.Height+'px',
			'cursor': 'pointer'
		});
		$('#close').css({
			'float': 'right',
			'line-height': this.Height*1.95+'px',
			'font-size': '20px',
			'cursor': 'pointer'
		});
	}
	
	// Make the wrapper cover everything, even
	// when scrolling
	$(window).scroll(function() {
		$('#Wrapper').css("top", $(window).scrollTop() + 'px');
	});
}

function SetOpacity(e, value)
{
	$(e).css("opacity", value/10);
	$(e).css("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--;

	$.ajax({
		cache: false,
		type: 'POST',
		url: '/functions/GetImageProperties.php',
		data: {
			g: this.Gallery,
			inr: this.ImageNumber
		},
		dataType: 'json',
		success: function(json) {
			ID = parseInt(json.id);	
			Caption = json.caption;
			Height = parseInt(json.height);
			Width = parseInt(json.width);
			FileName = json.filename;
			LoadPhoto();
		}
	});
}

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