(function ($) {
	$.fn.vAlign = function(container) {
		return this.each(function(i){
			if(container == null) {
				container = 'div';
			}
			$(this).html("<" + container + ">" + $(this).html() + "</" + container + ">");
			var el = $(this).children(container + ":first");
			var elh = $(el).height(); 
			var ph = $(this).height(); 
			var nh = (ph - elh) / 2; 
			$(el).css('padding-top', nh);
		});
	};
})(jQuery);

$(document).ready(function() {
	if( $('.descricaoGrupoMenuInferior').size() > 0 ){
		$('.descricaoGrupoMenuInferior').vAlign();
	}

	if( $('.descricaoParceiro').size() > 0 ){	
		$('.descricaoParceiro').vAlign();
		$('.descricaoParceiro, .descricaoParceiroTarjaPreta').hide();

		$('.imagemCliente').hover( mostrarTexto, escondeTexto );
	}
	
	var alturaUtilNavegador = $(window).height();
	var alturaDivGeral = $('.divGeral').height(); 
	
	if(alturaDivGeral > (alturaUtilNavegador - 20)){
		$('.divGeral').css('margin-top', '10px')					  
					  .css('top', '0');
	}
});

function mostrarTexto(){
	$('.descricaoParceiro, .descricaoParceiroTarjaPreta ', this).show('slow');
}

function escondeTexto(){
	$('.descricaoParceiro, .descricaoParceiroTarjaPreta ', this).hide('slow');
}

function roundGradientLayout() {
	$(".divGeral").backgroundCanvas();	
	estiloDiv("#CCCCCC", "#636363",0,1,"#FCFCFC");
	$(".divGeral").backgroundCanvasPaint(PaintFkt);	

	$(".topicoNoticias").backgroundCanvas();	
	estiloDiv("#575757", "#575757",5,0,"#575757");
	$(".topicoNoticias").backgroundCanvasPaint(PaintFkt);
	
	$(".cabecalhoNoticias").backgroundCanvas();	
	estiloDiv("#848484", "#848484",5,0,"#FCFCFC");
	$(".cabecalhoNoticias").backgroundCanvasPaint(PaintFkt);
	
}

function estiloDiv(cor1,cor2,tamanhoArredondamento,tamanhoBorda,corBorda){	
	objeto = new Object;
	objeto.cor1 = cor1;
	objeto.cor2 = cor2;
	objeto.radius = tamanhoArredondamento;
	objeto.border = tamanhoBorda;
	objeto.corBorda = corBorda;
	return objeto;
}

function PaintFkt(context, width, height)
{	
	var options = {x:0, height: height, width: width, radius:objeto.radius, border: 0 };
	// Draw border rectangle
	context.fillStyle = objeto.corBorda;
	$.canvasPaint.roundedRect(context,options);
	// Draw the gradient filled inner rectangle
	var backgroundGradient = context.createLinearGradient(0, 0,	0, height - 10);
	backgroundGradient.addColorStop(0 ,objeto.cor1);
	backgroundGradient.addColorStop(1, objeto.cor2);
	options.border = objeto.border;
	context.fillStyle = backgroundGradient;
	$.canvasPaint.roundedRect(context,options);
} 

function enviarEmail(){
	$('.mensagemProcessandoEmail').show();
	var dados = "nome="+escape(document.getElementById('nome').value)+
				"&email="+document.getElementById('email').value+
				"&assunto="+document.getElementById('assunto').value+
				"&mensagem="+escape(document.getElementById('mensagem').value);
	
	$.getJSON("EnviarEmail.jsp?"+dados, function(json){
		if(json.errosJSON != null && json.errosJSON != ''){
			$('.mensagemProcessandoEmail').hide('slow');
			alert(json.errosJSON);
		}else{		
			$('.mensagemProcessandoEmail').hide('slow');
			alert("Mensagem enviada com sucesso!");
			limparCamposContato();
		}
	});

	return false;
}

function limparCamposContato(){
	document.getElementById('nome').value = '';
	document.getElementById('email').value = '';
	document.getElementById('assunto').value = '';
	document.getElementById('mensagem').value = '';	
}

function escreveFlash(nomeFlash, largura, altura, idComponente){	
	var so = new SWFObject(nomeFlash, "sotester", largura, altura, "9");
	so.addParam("wmode", "transparent");
	so.addParam("menu","false");
	so.write(idComponente);
}

var i = 0;
function alinhaTexto(){
	$('h1').animate({
		left: '0'
	  }, 2000,function() {
		  $('.animacao,.animacaoSemFundo').each(function(i, elemento){
	    	  setTimeout(anima, 2000 * i );
	      }); 	      
	  });
}

function anima(){
	$($('.animacao,.animacaoSemFundo').get(i)).animate({left: 0}, 2000);
	i++;
}

function pesquisar(){
	document.getElementById('iframePrincipal').src = document.getElementById('path').value+'/amg/BuscaProdutoPorTexto.jsp?texto='+document.getElementById('texto').value;
}

function retiraTitle(){
	if($('#link0').attr("title") != null){
		for(var i=0; i<$('#qtdlistaParceiro').val(); i++){		
			$('#link'+i).attr("title","");								
		}		
	}
}

function preencheTitle(){	
	for(var i=0; i<$('#qtdlistaParceiro').val(); i++){		
		$('#link'+i).attr("title",$('#title'+i).attr("title"));								
	}
}

function exibeLojasPorEstado()
{
    var options = { 
        	autoOpen:false,
        	hide:'fold',
        	resizable:false,
        	draggable:false,
        	width:250,
        	height:250,
        	zIndex:999999
        }; 

    $("#listaLojas").dialog(options);	
	$("#listaLojas").dialog('open');
}
