// JavaScript Document
var galeryN;
function makeGalery(that)
{
	$("#aritcle"+artId).hide(1000);
	
	if($("#galery").length==0)
	{
		var gale = document.createElement('article');
		gale.setAttribute('id', "galery");				
		gale.setAttribute('class', "gal");
		
		$(gale).css("opacity", "0");
		
		var bgCont = document.createElement('div');
		bgCont.setAttribute('class', "bgCont");
		
		gale.appendChild(bgCont);
		
		var closs = document.createElement('img');
		closs.setAttribute('src', "imgBase/cross.png");
		closs.setAttribute('alt', "fechar Galeria");
		closs.setAttribute('class', "clos links");
		closs.setAttribute('onClick', 'closeGal()');
		gale.appendChild(closs);
		
		var galDisplay = document.createElement('div');
		galDisplay.setAttribute('class', "galDisplay");
		galDisplay.innerHTML=$("#aritcle"+artId).find(".artDisplayI div").html();
		
		
		var galContLege = document.createElement('div');
		galContLege.setAttribute('class', "galContLege");
		
		var galLegenda = document.createElement('p');
		galLegenda.setAttribute('class', "textos");
		
		galContLege.appendChild(galLegenda);
		gale.appendChild(galDisplay);
		gale.appendChild(galContLege);
		
		var galLeft = document.createElement('div');
		galLeft.setAttribute('class', "galButs galL links");
		galLeft.setAttribute('onClick', "galL()");
		
		var galRight = document.createElement('div');
		galRight.setAttribute('class', "galButs galR links");
		galRight.setAttribute('onClick', "galR()");
		
		gale.appendChild(galLeft);
		gale.appendChild(galRight);
		
		$("body").append(gale);
		$(gale).animate({"opacity": "1"}, 1000);

		if(jsonGal.length>1)
		{
			
			for( i=0; i<jsonGal.length; i++)
			{
				if(jsonGal[i]["nome"]==$(that).attr("alt"))
				{
					galeryN=i;
					
					switch(lang)
					{
						case "pt":
							if(jsonGal[i]["legendaPt"]=="")
							{
								$(".galContLege").css("opacity", "0");
								$(".galDisplay").css("top", "50%");
								$(".galDisplay").css("margin-top", $(".galDisplay").height()*-0.5);
							}
							else
							{
								$(".galContLege .textos").html(jsonGal[i]["legendaPt"]);
							}
						break;
						case "sp":
							if(jsonGal[i]["legendaSp"]=="")
							{
								$(".galContLege").css("opacity", "0");
								$(".galDisplay").css("top", "50%");
								$(".galDisplay").css("margin-top", $(".galDisplay").height()*-0.5);
							}
							else
							{
								$(".galContLege .textos").html(jsonGal[i]["legendaSp"]);
							}
						break;
						case "en":
							if(jsonGal[i]["legendaEn"]=="")
							{
								$(".galContLege").css("opacity", "0");
								$(".galDisplay").css("top", "50%");
								$(".galDisplay").css("margin-top", $(".galDisplay").height()*-0.5);
							}
							else
							{
								$(".galContLege .textos").html(jsonGal[i]["legendaEn"]);
							}
						break;
					}
					
				}
			}
		}
	}
	else
	{
		$("#galery .galDisplay").html($("#aritcle"+artId).find(".artDisplayI").html());
		$("#galery").show(1000);	
		$("#aritcle"+artId).hide(1000);
	}
}

function galL()
{
	if(jsonGal.length>1)
	{
		$("#galery .galDisplay").empty();
		
		galeryN=(galeryN-1<0)?jsonGal.length-1:galeryN-1;
		
		changeGalDisplay();
	}
}
function galR()
{
	if(jsonGal.length>1)
	{
		$("#galery .galDisplay").empty();
		
		galeryN=(galeryN+1<jsonGal.length)?galeryN+1:0;
		
		changeGalDisplay();
	}
}

function changeGalDisplay()
{
	var objN=jsonGal[galeryN]["nome"];
	var ext=objN.slice(-4);

	if(ext==".png"||ext==".gif"||ext==".jpg")
	{

		var displayImg = document.createElement('img');
		displayImg.setAttribute('src', objN);
		displayImg.setAttribute('alt', objN);
		displayImg.setAttribute('id', objN);
		displayImg.setAttribute('class', "imgArt");

		$("#galery .galDisplay").append(displayImg);

		
		$(displayImg).load(function() {

			var newL= ($("#galery .galDisplay").width() - $(displayImg).width())*0.5;

			$(displayImg).css("left", newL);

		});
	}
	else
	{
		var displayVid = document.createElement('div');
		displayVid.setAttribute('class', "imgArt");
		
		$(displayVid).html(makeFLV(objN.slice(0,-4)));

		$("#galery .galDisplay").append(displayVid);
	}
	
	switch(lang)
	{
		case "pt":
			if(jsonGal[galeryN]["legendaPt"]=="")
			{
				$(".galContLege").css("opacity", "0");
				$(".galDisplay").css("top", "50%");
				$(".galDisplay").css("margin-top", $(".galDisplay").height()*-0.5);
			}
			else
			{
				$(".galContLege").css("opacity", "1");
				$(".galDisplay").css("top", "30px");
				$(".galDisplay").css("margin-top", "0px");
				$(".galContLege .textos").html(jsonGal[galeryN]["legendaPt"]);
			}
		break;
		case "sp":
			if(jsonGal[galeryN]["legendaSp"]=="")
			{
				$(".galContLege").css("opacity", "0");
				$(".galDisplay").css("top", "50%");
				$(".galDisplay").css("margin-top", $(".galDisplay").height()*-0.5);
			}
			else
			{
				$(".galContLege").css("opacity", "1");
				$(".galDisplay").css("top", "30px");
				$(".galDisplay").css("margin-top", "0px");
				$(".galContLege .textos").html(jsonGal[galeryN]["legendaSp"]);
			}
		break;
		case "en":
			if(jsonGal[galeryN]["legendaEn"]=="")
			{
				$(".galContLege").css("opacity", "0");
				$(".galDisplay").css("top", "50%");
				$(".galDisplay").css("margin-top", $(".galDisplay").height()*-0.5);
			}
			else
			{
				$(".galContLege").css("opacity", "1");
				$(".galDisplay").css("top", "30px");
				$(".galDisplay").css("margin-top", "0px");
				$(".galContLege .textos").html(jsonGal[galeryN]["legendaEn"]);
			}
		break;
	}
}
function closeGal()
{
	$("#galery").hide(1000);	
	$("#aritcle"+artId).show(1000);
}
