/*
	#################################################
	#    Auto: Thiago Silva (Pedreiro Digital)      #
	#         www.pedreirodigitalweb.com.br         #       
	#################################################
*/
var IMG = new Image();
var album = new Array();
var pasta_album;
var id_galerias;
var Width;
var Height;
var Width_Metade;
var Height_Metade;

var utils = function(){}

utils.prototype.validar_campos = function(campos)
{
	var validos = 0;
	var total = campos.length;
	for(var i=0; i<campos.length; i++)
	{
		if(!document.getElementById(campos[i]).value)
		{
		}
		else
		{
			validos++;
		}
	}
	if(validos == total)
	{
		return true;
	}
	else
	{
		return false;
	}
}

utils.prototype.sair = function()
{
	class_alertas.escolha('Deseja sair do sistema agora?','utils.prototype.verificar_saida()');
}

utils.prototype.verificar_saida = function()
{
	document.getElementById('error').innerHTML = 'encerrando sessão aguarde...';
	$.ajax
	(
		{
			type: "POST",
			url: host + 'temas/usuarios/index/' + "sair.php",
			success: utils.prototype.retorno_sair
		}
	);
}

utils.prototype.retorno_sair = function()
{
	window.location = './';
}

utils.prototype.set_file = function(arq,alvo)
{
	document.getElementById(alvo).value = arq.value;
}

utils.prototype.setData = function(alvo)
{
	$(alvo).focus(
		function()
		{
			$(this).calendario
			(
				{ 
					target:this
				}
			);
		}
	);
}

utils.prototype.adicionar_item = function(valor,texto,combo)
{
	var opcao = new Option(texto, valor);
    combo.options[combo.length] = opcao;
}

utils.prototype.remover_item = function(origem) 
{
	var campo = document.getElementById(origem);
	campo.options[orig.selectedIndex] = null;
}

utils.prototype.adicionar_valor = function (texto,value,destino) 
{
	var campo = document.getElementById(destino);
	campo.options[campo.length] = new Option(texto,value);
}

utils.prototype.limpar_combo = function(combo,inicio)
{
    var tamanho = combo.options.length;
    for (i=tamanho-1;i>=inicio;i--)
	{
        combo.remove(i);
	}
}

utils.prototype.seleciona_menu = function(pagina)
{
	$('ul.itens ,.iten').each
	(
		function()
		{
			if(this.title == pagina)
			{
				$(this).addClass('selecionado');
			}
		}
	);
}

utils.prototype.preview_video = function(video)
{
	var width = 640;
	var height = 385;
	var width_metade = width / 2;
	var height_metade = height / 2;
	
	document.getElementById('conteiner').innerHTML = '';
	document.getElementById('conteiner').innerHTML += '<object width="640" height="385">';
	document.getElementById('conteiner').innerHTML += '<param name="movie" value="http://www.youtube.com/v/'+video+'&hl=pt_BR&fs=1&"></param>';
	document.getElementById('conteiner').innerHTML += '<param name="allowFullScreen" value="true"></param>';
	document.getElementById('conteiner').innerHTML += '<param name="allowscriptaccess" value="always"></param>';
	document.getElementById('conteiner').innerHTML += '<embed src="http://www.youtube.com/v/'+video+'&hl=pt_BR&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="640" height="385"></embed>';
	document.getElementById('conteiner').innerHTML += '</object>';
	document.getElementById('conteiner').innerHTML += '<div onclick="utils.prototype.cancelar()"><button class="botao_fechar_video"></button></div>';
	document.getElementById('conteiner').style.width = width+'px';
	document.getElementById('conteiner').style.height = height+'px';
	document.getElementById('conteiner').style.marginLeft = '-'+width_metade+'px';
	document.getElementById('conteiner').style.marginTop = '-'+height_metade+'px';
	document.getElementById('mascara').style.display = '';
	document.getElementById('conteiner').style.display = '';
	
	$("#mascara").show();
	$('#mascara').animate
	(
		{
			opacity: 0.5
		}
		,200,
		function()
		{
			$("#conteiner").fadeIn(600);
		}
	);
}

utils.prototype.ampliar_foto = function(pasta,id)
{
	Width = 42;
	Height = 42;
	Width_Metade = Width / 2;
	Height_Metade = Height / 2;
	
	$('.thumb').each
	(
		function()
		{
			album.push($(this));
		}
	);
	
	pasta_album = pasta;
	id_galerias = id;
	
	$('#conteiner').empty();
	$('#conteiner').append('<div class="ampliada"></div>');
	$('#conteiner').append('<div class="botoes"><button onclick="utils.prototype.cancelar()" class="fechar_foto"></button></div>');
	$('#conteiner .botoes').hide();
	
	$('#conteiner .ampliada').css
	(
		{
			background:'url('+host+'images/loader_32.gif) no-repeat center'
		}
	);
	
	$('#conteiner').animate
	(
		{
			opacity:1,
			width:Width,
			height:Height,
			marginLeft:'-'+Width_Metade+'px',
			marginTop: '-'+Height_Metade+'px'
		}
		,500,
		function()
		{
			$('#conteiner .ampliada').css
			(
				{
					width:Width,
					height:Height
				}
			);
			IMG.src = host + 'thumb_pct.php?arquivo=uploads/'+pasta_album+'/'+album[id_galerias].attr("title")+'&pw=80%';
			IMG.onload = utils.prototype.exibir_foto;
		}
	);
	
	$("#mascara").css({display:'block'});
	$("#conteiner").css({display:'block'});
	$("#mascara").css({opacity:0});
	$("#conteiner").css({opacity:0});
	
	$('#mascara').animate
	(
		{
			opacity: 0.6
		}
		,500,
		function()
		{
			$('#conteiner').animate
			(
				{
					opacity: 1
				}
			);	
		}
	);
}

utils.prototype.exibir_foto = function()
{
	Width = IMG.width;
	Height = IMG.height;
	Width_Metade = Width / 2;
	Height_Metade = Height / 2;
	
	$('#conteiner .ampliada').animate
	(
		{
			opacity:0
		}
		,500,
		function()
		{
			$('#conteiner').animate
			(
				{
					width:Width,
					height:Height,
					marginLeft:'-'+Width_Metade+'px',
					marginTop: '-'+Height_Metade+'px'
				}
				,500,
				function()
				{
					$('#conteiner .ampliada').css
					(
						{
							background:'url('+IMG.src+') no-repeat',
							width:Width,
							height:Height
						}
					);	
			
					$('#conteiner .ampliada').animate
					(
						{
							opacity:1
						}
						,500,
						function()
						{
							if(id_galerias>=(album.length-1))
							{
								$('#conteiner .pro').attr('disabled', true);
								$('#conteiner .pro').css({opacity:0.3});
							}
							else
							{
								$('#conteiner .pro').attr('disabled', false);
								$('#conteiner .pro').css({opacity:1});
							}
							
							if(id_galerias<=0)
							{
								$('#conteiner .ant').attr('disabled', true);
								$('#conteiner .ant').css({opacity:0.3});
							}
							else
							{
								$('#conteiner .ant').attr('disabled', false);
								$('#conteiner .ant').css({opacity:1});
							}
							
							$('#conteiner .botoes').slideDown('slow');
						}
					);
				}
			);
		}
	);
}

utils.prototype.passa_foto = function(tipo)
{
	Width = 42;
	Height = 42;
	Width_Metade = Width / 2;
	Height_Metade = Height / 2;
	
	switch(tipo)
	{
		case 0:
			id_galerias--;
		break
		case 1:
			id_galerias++;
		break;
	}
		
	$('#conteiner .botoes').slideUp
	(
	 	'slow',
		function()
		{
			$('#conteiner .ampliada').animate
			(
				{
					opacity:0
				}
				,500,
				function()
				{
					
					$('#conteiner').animate
					(
						{
							width:Width,
							height:Height,
							marginLeft:'-'+Width_Metade+'px',
							marginTop: '-'+Height_Metade+'px'
						}
						,500,
						function()
						{
							$('#conteiner .ampliada').css
							(
								{
									opacity:1,
									background:'url('+host+'images/loader_32.gif) no-repeat center',
									width:Width,
									height:Height
								}
							);
							utils_site.prototype.novaFoto();
						}
					);
				}
			);
		}
	);
}

utils.prototype.novaFoto = function()
{
	IMG.src = host + 'thumb_pct.php?arquivo=uploads/'+pasta_album+'/'+album[id_galerias].attr("title")+'&pw=80%';
	IMG.onload = utils.prototype.exibir_foto;
}

utils.prototype.cancelar = function()
{
	album.length = 0;
	
	Width = 42;
	Height = 42;
	Width_Metade = Width / 2;
	Height_Metade = Height / 2;
	
	$('#conteiner .botoes').slideUp
	(
	 	'slow',
		function()
		{
			$('#conteiner .ampliada').animate
			(
				{
					opacity:0
				}
				,500,
				function()
				{
					
					$('#conteiner').animate
					(
						{
							width:Width,
							height:Height,
							marginLeft:'-'+Width_Metade+'px',
							marginTop: '-'+Height_Metade+'px'
						}
						,500,
						function()
						{
							$('#conteiner').animate
							(
								{
									opacity:0
								}
								,500,
								function()
								{
									$('#mascara').animate
									(
										{
											opacity:0
										}
										,500,
										function()
										{
											$('#mascara').hide();
											$('#conteiner').hide();
										}
									);									
								}
							);
						}
					);
				}
			);
		}
	);
}
