//jQuery.PhotoGallery						= {};

function PhotoGallery_OnLoad()
{
	jQuery.PhotoGallery.Init();
}

jQuery.PhotoGallery.PageNumber			= 0;
jQuery.PhotoGallery.ItemNumber			= -1;
jQuery.PhotoGallery.PageSize			= -1;
jQuery.PhotoGallery.ItemWidth			= 0;
jQuery.PhotoGallery.ItemHeight			= 0;
jQuery.PhotoGallery.Items				= [];

jQuery.PhotoGallery.Init				= function()
{
	// Hide the thumbnails
	$("#PhotoGallery #Album").hide();
	// Transparent info bar
	$("#PhotoGallery #InfoBar").slideUp(0);
	$("#PhotoGallery #InfoBar").fadeTo(100, 0.8);
	// Attach actions to buttons
	$("#PhotoGallery #PrevPage").click(this.PrevPage_OnClick);
	$("#PhotoGallery #NextPage").click(this.NextPage_OnClick);
	$("#PhotoGallery #PrevItem").click(this.PrevItem_OnClick);
	$("#PhotoGallery #NextItem").click(this.NextItem_OnClick);
	$("#PhotoGallery #FirstItem").click(this.FirstItem_OnClick);
	$("#PhotoGallery #LastItem").click(this.LastItem_OnClick);
	for (index = 0 ; index < this.Items.length; index++)
	{
		$("#PhotoGallery #ThumbBar").append("<div class=\"ImageContainer\" id=\"" + index + "\"><img src=\"" + this.Items[index].ThumbURL + "\" style=\"visibility : hidden\" /></div>");
		$("#PhotoGallery #ThumbBar #" + index + " img").load(this.Thumb_OnLoad);
	}
	$("#PhotoGallery #ThumbBar div").fadeTo(100, 0.6);
	$("#PhotoGallery #ThumbBar div").hover(this.Thumb_OnMouseOver, this.Thumb_OnMouseOut);
	$("#PhotoGallery #ThumbBar div").click(this.Thumb_OnClick);
	// Calculate Item Width
	this.ItemWidth	= $("#PhotoGallery #ThumbBar #0").width() +
						2 * ParseInt($("#PhotoGallery #ThumbBar #0").css("padding-left")) + 
						2 * ParseInt($("#PhotoGallery #ThumbBar #0").css("border-left-width")) + 
						ParseInt($("#PhotoGallery #ThumbBar #0").css("margin-left"));
	// Calculate Item Height
	this.ItemHeight	= $("#PhotoGallery #ThumbBar #0").height() +
						2 * ParseInt($("#PhotoGallery #ThumbBar #0").css("padding-top")) + 
						2 * ParseInt($("#PhotoGallery #ThumbBar #0").css("border-top-width")) + 
						ParseInt($("#PhotoGallery #ThumbBar #0").css("margin-top"));
	$("#PhotoGallery #ThumbContainer").width($("#PhotoGallery").width() - $("#PhotoGallery #LeftButtons").width() - $("#PhotoGallery #RightButtons").width());
	// Calculate the page size
	//Varianta veche de calculare page size
	//for (width = $("#PhotoGallery #ThumbContainer").width(); width > 0; width -= this.ItemWidth) this.PageSize++;
	this.PageSize = Math.floor($("#PhotoGallery #ThumbContainer").width() / this.ItemWidth);
	
	// Resize and position the Thumbs
	$("#PhotoGallery #ThumbMask").width(this.PageSize * this.ItemWidth);
	$("#PhotoGallery #ThumbMask").css("left", ($("#PhotoGallery #ThumbContainer").width() - this.PageSize * this.ItemWidth) / 2);
	$("#PhotoGallery #ThumbBar").css("top", ($("#PhotoGallery #ThumbMask").height() - this.ItemHeight) / 2);
	// Position the progress bar
	$("#PhotoGallery #ProgressBar").load(this.ProgressBar_OnLoad);
	// Position the buttons
	// Show first Item
	this.ThumbBar_ShowItem(0);
};
		
jQuery.PhotoGallery.ProgressBar_OnLoad	= function()
{
	$(this).css("left", ($(this).parent("div").width() - $(this).width()) / 2);
	$(this).css("top", ($(this).parent("div").height() - $(this).height()) / 2);
};


jQuery.PhotoGallery.Thumb_OnLoad		= function()
{
	FitImage($(this));
	$(this).css("visibility", "visible");
};
		
jQuery.PhotoGallery.Photo_OnLoad		= function()
{
	FitImage($(this));
	$(this).parents("#Photo").fadeTo(200, 1);
};
		
jQuery.PhotoGallery.InfoBar_Show		= function()
{
	var	_this	= jQuery.PhotoGallery;
	if (_this.Items[_this.ItemNumber].Title != "" || _this.Items[_this.ItemNumber].Description != "")
		$("#PhotoGallery #InfoBar").slideDown(400);
};
		
jQuery.PhotoGallery.InfoBar_Hide		= function()
{
	$("#PhotoGallery #InfoBar").slideUp(400);
};
		
jQuery.PhotoGallery.NextPage_OnClick	= function()
{
	var	_this	= jQuery.PhotoGallery;
	// If we have another next page
	if ((_this.PageNumber + 1) * _this.PageSize < _this.Items.length)
	{
		_this.PageNumber++;
		//PhotoGallery.ItemNumber++;
		_this.ThumbBar_ShowPage(_this.PageNumber);
	}
	// If we have don't have another page, deactivate the next page button
	//if ((PageNumber + 1) * PageSize < Items.length);
};
		
jQuery.PhotoGallery.PrevPage_OnClick	= function()
{
	var	_this	= jQuery.PhotoGallery;
	// If we have another prev page
	if (_this.PageNumber > 0)
	{
		_this.PageNumber--;
		_this.ThumbBar_ShowPage(_this.PageNumber);
	}
	// If we have don't have another page, deactivate the prev page button
};
		
jQuery.PhotoGallery.ThumbBar_ShowPage	= function(pageNumber)
	{
		var	_this	= jQuery.PhotoGallery;
		$("#PhotoGallery #ThumbBar").animate({left: (-pageNumber * _this.PageSize * _this.ItemWidth) + "px"}, 500);
	};
		
jQuery.PhotoGallery.ThumbBar_ShowItem	= function(itemNumber)
{
	var	_this	= jQuery.PhotoGallery;
	// Fade out old thumb
	_this.Thumb_FadeOut(_this.ItemNumber);
	// Unselect active thumb
	$("#PhotoGallery #ThumbBar #" + _this.ItemNumber).removeClass("Selected");
	_this.ItemNumber	= itemNumber;
	_this.Thumb_FadeIn(_this.ItemNumber);
	// Select newly active thumb
	$("#PhotoGallery #ThumbBar #" + _this.ItemNumber).addClass("Selected");
	_this.InfoBar_Hide();
	_this.ProgressBar_Show();
	if (_this.CONFIGURATION.Albums.PhotoGallery["Lightbox"]) 
	{
		$("#PhotoGallery #Buffer").html("<a href=\"" + _this.Items[_this.ItemNumber].SourceURL + "\"><img src=\"" + _this.Items[_this.ItemNumber].ImageURL + "\" id=\"" + _this.ItemNumber + "\" /></a>");
	}
	else
	{
		$("#PhotoGallery #Buffer").html("<img src=\"" + _this.Items[_this.ItemNumber].ImageURL + "\" id=\"" + _this.ItemNumber + "\" />");
	}
	$("#PhotoGallery #Buffer").show();
//	$("#PhotoGallery #Buffer img").load(_this.Buffer_OnLoad);
	_this.Buffer_OnLoad();
};
		
jQuery.PhotoGallery.NextItem_OnClick	= function()
{
	var	_this	= jQuery.PhotoGallery;
	if (_this.ItemNumber < _this.Items.length - 1)
	{
		_this.PageNumber	= Math.floor(_this.ItemNumber / _this.PageSize);
		if (_this.ItemNumber % _this.PageSize == _this.PageSize - 1)
			$("#PhotoGallery #NextPage").click();
		else
			_this.ThumbBar_ShowPage(_this.PageNumber);
		_this.Thumb_FadeOut(_this.ItemNumber);
		_this.ItemNumber++;
		_this.ThumbBar_ShowItem(_this.ItemNumber);
	}
};

jQuery.PhotoGallery.PrevItem_OnClick	= function()
{
	var	_this	= jQuery.PhotoGallery;
	if (_this.ItemNumber > 0)
	{
		_this.PageNumber	= Math.floor(_this.ItemNumber / _this.PageSize);
		if (_this.ItemNumber % _this.PageSize == 0)
			$("#PhotoGallery #PrevPage").click();
		else
			_this.ThumbBar_ShowPage(_this.PageNumber);
		_this.Thumb_FadeOut(_this.ItemNumber);
		_this.ItemNumber--;
		_this.ThumbBar_ShowItem(_this.ItemNumber);
	}
};

jQuery.PhotoGallery.FirstItem_OnClick	= function()
{
	var	_this	= jQuery.PhotoGallery;
	// If already first item
	if (_this.ItemNumber == 0) return;
	_this.Thumb_FadeOut(_this.ItemNumber);
	_this.ItemNumber	= 0;
	_this.PageNumber	= 0;
	_this.ThumbBar_ShowPage(_this.PageNumber);
	_this.ThumbBar_ShowItem(_this.ItemNumber);
};

jQuery.PhotoGallery.LastItem_OnClick	= function()
{
	var	_this	= jQuery.PhotoGallery;
	// If already last item
	if (_this.ItemNumber == _this.Items.length - 1) return;
	_this.Thumb_FadeOut(_this.ItemNumber);
	_this.ItemNumber	= _this.Items.length - 1;
	_this.PageNumber	= Math.floor(_this.ItemNumber / _this.PageSize);
	_this.ThumbBar_ShowPage(_this.PageNumber);
	_this.ThumbBar_ShowItem(_this.ItemNumber);
};

jQuery.PhotoGallery.Thumb_FadeOut		= function(itemNumber)
{
	$("#PhotoGallery #ThumbBar #" + itemNumber).fadeTo(100, 0.6);
};

jQuery.PhotoGallery.Thumb_FadeIn		= function(itemNumber)
{
	$("#PhotoGallery #ThumbBar #" + itemNumber).fadeTo(100, 1.0);
};

jQuery.PhotoGallery.Thumb_OnMouseOver	= function()
{
	$(this).fadeTo(100, 1);
};

jQuery.PhotoGallery.Thumb_OnMouseOut	= function()
{
	var	_this	= jQuery.PhotoGallery;
	if (this.id != _this.ItemNumber)
		$(this).fadeTo(100, 0.6);
};

jQuery.PhotoGallery.Thumb_OnClick		= function()
{
	var	_this	= jQuery.PhotoGallery;
	// If it is currently selected item, then do nothing
	if (_this.ItemNumber == this.id) return;
	_this.ThumbBar_ShowItem(this.id);
};

jQuery.PhotoGallery.Buffer_OnLoad		= function()
{
	var	_this	= jQuery.PhotoGallery;
	_this.ProgressBar_Hide();
	$("#PhotoGallery #Photo").fadeTo(200, 0, _this.SetPhoto);
};

jQuery.PhotoGallery.ProgressBar_Show	= function()
{
	//$("#PhotoGallery #ProgressBar").css("visibility", "visible");
};

jQuery.PhotoGallery.ProgressBar_Hide	= function()
{
	//$("#PhotoGallery #ProgressBar").css("visibility", "hidden");
};

jQuery.PhotoGallery.SetPhoto			= function()
{
	var	_this	= jQuery.PhotoGallery;
	//$("#PhotoGallery #Photo").hide();
	$("#PhotoGallery #Photo").html($("#PhotoGallery #Buffer").html());
	if (_this.CONFIGURATION.Albums.PhotoGallery["Lightbox"]) 
	{
		$("#PhotoGallery #Photo a").lightbox();
	}
	//$("#PhotoGallery #Photo").show();
	//$("#PhotoGallery #Photo img").load(_this.Photo_OnLoad).trigger("load");
	$("#PhotoGallery #Photo img").load(_this.Photo_OnLoad);
	$("#PhotoGallery #InfoBar h3").text(_this.Items[_this.ItemNumber].Title);
	$("#PhotoGallery #InfoBar h4").text(_this.Items[_this.ItemNumber].Description);
	_this.InfoBar_Show();
};
