// JavaScript Document
/*
*	Class to deal with Gallery
*	@Author	Guilherme R. Aiolfi
*	@Version: 0.1b
*/
var Gallery = function (w, h)
{
	var width = w;
	var height = h;

	var el = null;
	var creditContent = "credit";
	var loadingMask = "Carregando...";
	var descriptionContent = "description_image";
	var photoContent = "photo";
	var imageAlign = "center";

	this.setLoadingMask = function (mask)
	{
		loadingMask = mask;
	}

	this.setImageAlign = function (align)
	{
		imageAlign = align;
	}

	this.setCreditContent = function (div)
	{
		creditContent = div;
	}

	this.setDescriptionContent = function (div)
	{
		descriptionContent = div;
	}

	this.openPhoto = function (src, description, credit)
	{
		if (descriptionContent && document.getElementById (descriptionContent))
		{
			document.getElementById (descriptionContent).innerHTML = description;
		}

		if (el != null && el.parentNode != null) el.parentNode.removeChild (el);
		document.getElementById (photoContent).innerHTML = loadingMask;
		el = new Image();
		el.style.display = "block";
		el.onload = function ()
		{

			if (creditContent && credit)
			{
				document.getElementById (photoContent).innerHTML = "<div id='" + creditContent + "'></div>";
				var credit_content = document.getElementById (creditContent);
				credit_content.innerHTML = "Foto por: " + credit;
			}

			document.getElementById (photoContent).appendChild (el);
			var img_height = el.height;
			var img_width = el.width;
			var sHeight = img_height; var sWidth = img_width;
			if ((width/img_width) < (height/img_height))
			{ var sHeight = img_height * (width / img_width); var	sWidth = img_width * (width / img_width); }
			else
			{ var sHeight = img_height * (height / img_height); var sWidth = img_width * (height / img_height);	 }
			sWidth = Math.floor(sWidth); sHeight = Math.floor(sHeight);
			if (sWidth < el.width) { el.height = sHeight; el.width = sWidth; }
			if (creditContent && credit)
			{
				credit_content.style.width = el.width + "px";
				credit_content.style.display = "block";
			}
			el.style.display = "block";


			if (imageAlign == "center")
			{
				document.getElementById (photoContent).style.textAlign = "center";
				document.getElementById (photoContent).style.margin = "0 auto";
			}
			document.getElementById (photoContent).style.height = (el.height + (credit? 13: 0)) + "px";
		}
		document.getElementById (photoContent).innerHTML = "";
		el.src = src + "&width=" + w + "&height=" + h;
	}
	this.getWidth = function ()
	{
		return width;
	}

	this.getHeight = function ()
	{
		return height;
	}
}
