// JavaScript Document
var defaultSize = 60; var atual = 0; var total = 0;

var galerias = {
	getPageSize: function () {
		var xScroll, yScroll;
		if (window.innerHeight && window.scrollMaxY) {	
			xScroll = window.innerWidth + window.scrollMaxX;
			yScroll = window.innerHeight + window.scrollMaxY;
		}
		else if (document.body.scrollHeight > document.body.offsetHeight) {
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		}
		else {
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}
		var windowWidth, windowHeight;
		if (self.innerHeight) {	
			if (document.documentElement.clientWidth) windowWidth = document.documentElement.clientWidth; 
			else windowWidth = self.innerWidth;
			windowHeight = self.innerHeight;
		}
		else if (document.documentElement && document.documentElement.clientHeight) {
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		}
		else if (document.body) {
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}
		if (yScroll < windowHeight) pageHeight = windowHeight;
		else pageHeight = yScroll;
		if (xScroll < windowWidth) pageWidth = xScroll;
		else pageWidth = windowWidth;
		return new Array(pageWidth,pageHeight,windowWidth,windowHeight);
	},
	init: function() {
		var pageSize = this.getPageSize();
		$('.galeriaExterno').css('width', pageSize[0]);
		$('.galeriaExterno').css('height', pageSize[1]);
		$('.galeriaExterno').bind('click', function (  ) { galerias.close(); });
		$('.PhotosLeft').unbind('click').bind('click', function(e){
			e.preventDefault();
			var PhotoDiv = document.getElementById('thumbFrame');
			if (PhotoDiv.scrollLeft > defaultSize) PhotoDiv.scrollLeft -= defaultSize;
			
		});
		$('.PhotosRight').unbind('click').bind('click', function(e){
			e.preventDefault();
			var PhotoDiv = document.getElementById('thumbFrame');
			var SizeFull = parseInt(document.getElementById('thumbsShow').style.width);
			if (PhotoDiv.scrollLeft < SizeFull) PhotoDiv.scrollLeft += defaultSize;
		});
		$('.CloseGalerias').click(function(e) {
			e.preventDefault();
			galerias.close();
		});
		$('.PhotoPrev').unbind('click').bind('click', function (e) {
			e.preventDefault();
			if (atual > 0) {
				atual--;
				var PhotoLink = $('.showPhoto[rel='+atual+']');
				var arquivo = PhotoLink.attr('href');
				var alt = PhotoLink.attr('title');
				var classe = PhotoLink.attr('name');
				galerias.show(arquivo, alt, classe);
			}
		});
		$('.PhotoNext').unbind('click').bind('click', function (e) {
			e.preventDefault();
			if (atual+1 < total) {
				atual++;
				var PhotoLink = $('.showPhoto[rel='+atual+']');
				var arquivo = PhotoLink.attr('href');
				var alt = PhotoLink.attr('title');
				var classe = PhotoLink.attr('name');
				galerias.show(arquivo, alt, classe);
			}
		});
	},
	open: function (title, name, first) {
		window.scrollTo(0,120);
		$('.galeriaExterno').css('display', 'block');
		$('.galeriaInterno').css('display', 'block');
		$('.loading').show();
		$('#photoFrame').hide(); $('#thumbFrame').hide();
		//$('#galeriaTitulo').text(title);
		this.load(name, first);
	},
	load: function (name, first) {
		var file = 'galerias/'+name+'.xml';
		var path = 'galerias/'+name+'/';
		$.ajax({
			url: file,
			type: 'get',
			dataType: 'xml',
			success: function (data) {
				var html = '';
				var showed = false;
				var i = 0;
				$(data).find('foto').each(function (){
					var arquivo	= $(this).attr('arquivo');
					var alt		= $(this).attr('descricao');
					var classe	= $(this).attr('classe');
					var order	= parseInt($(this).attr('sub'));
					if (!showed && first == order) {
						galerias.show( path+arquivo, alt, classe );
						showed = true;
						atual = i;
					}
					html += '<a href="'+path+arquivo+'" title="'+alt+'" rel="'+i+'" name="'+classe+'" class="showPhoto"><img src="'+path+'peq/'+arquivo+'" class="PhotoThumb" alt="'+alt+'" /></a>';
					i++;
				});
				total = i;
				$('#thumbsShow').css('width', (i*defaultSize)+'px');
				$('#thumbsShow').append(html);
				$('.showPhoto').unbind('click').bind('click', function (e) {
					e.preventDefault();
					var arquivo = $(this).attr('href');
					var alt = $(this).attr('title');
					var classe = $(this).attr('name');
					atual = parseInt($(this).attr('rel'));
					galerias.show(arquivo, alt, classe);
				});
				$('.loading').hide();
				$('#photoFrame').show(); $('#thumbFrame').show();
			}
		});
	},
	show: function (arquivo, alt, classe) {
		$('#galeriaTitulo').text(classe);
 		$('.loading').show();
		$('#photoFrame').hide().empty();
		var html = '<p>'+alt+'</p><img src="'+arquivo+'" name="1'+classe+'" class="Photo" alt="'+alt+'" />';
		$('#photoFrame').append(html);
		$('.Photo').unbind('load').bind('load', function () {
			$('.loading').hide();
			$('#photoFrame').show();
		});
	},
	close: function () {
		atual = 0; total = 0;	
		$('.galeriaExterno').css('display', 'none');
		$('.galeriaInterno').css('display', 'none');
		$('#galeriaTitulo').empty(); $('#photoFrame').empty(); $('#thumbsShow').empty();
	}
};

$(document).ready(function () {
	galerias.init();
	$('.abrirGaleria').click(function(e) {
		
		e.preventDefault();
		var titulo = $(this).attr('title');
		var galeria = $(this).attr('href');
		var first = parseInt($(this).attr('rel'));
		carregaflash(galeria, first);
		galerias.open(titulo, galeria, first );
	});
});
