$(document).ready
(
	function()
	{
		cssHack();
		
		$(".botao").mouseover
		(
			function()
			{
				$(this).addClass("hover");
			}
		);	
				
		$(".botao").mouseout
		(
			function()
			{			
				$(this).removeClass("hover");
			}
		);
		
		$(".galeria.foto").click
		(		
			function()
			{			
				iniciaCarregamento($(this));
				return false;
			}
		);
		
		$(".galeria.video").click
		(		
			function()
			{			
				iniciaCarregamento($(this));
				return false;
			}
		);
		
		$(window).resize
		(
			function() 
			{			
				resizeIe6();			
				redraw();
			}
		);
				
		$(window).scroll
		(
			function() 
			{
				resizeIe6();
				redraw();
			}
		);
		
		$(".externo").click
		(
			function()
			{
				var link = $(this).attr("href");
				window.open(link,"_blank");
				return false;
			}
		);
		
		$(".modulo_botao_transmissao").click
		(
			function()
			{				
				iniciaCadastro();
				return false;
			}
		);		
		
		renderAjaxNews();
		
		renderAjaxNovidade();
		
		// imagem rotativa
		$(".modulo_imagem_rotativa li:first").fadeIn("slow");				
					
		setInterval
		(
			function() 
			{		
				$(".modulo_imagem_rotativa li:first").fadeOut
				(
					"slow", 
					function()
					{
						$(".modulo_imagem_rotativa ul").append(this);
						$(".modulo_imagem_rotativa li:first").fadeIn("slow");
					}
				);
			}, 5000
		);
	}
);

function resizeIe6()
{
	if ( $.browser.msie && parseInt($.browser.version, 10) == 6) 
	{	
		if($(window).width() <= 838)
		{
			$("body").css
			(
				{
					"padding-left":0,
					"padding-right":0
				}
			)
		}
		else
		{		
			var left = Math.floor(($(window).width()- 838)/2);
			var right = $(window).width() - 838 - left;
		
			$("body").css
			(
				{
					"padding-left":left,
					"padding-right":right
				}
			)
		}		
	}
}


function cssHack()
{
	resizeIe6();

	$('body').contents().each(function() {
        if(this.nodeType == 8) {
            $(this).remove()
        }
    });
	
	if ( $.browser.msie && parseInt($.browser.version, 10) < 8) 
	{	
			
		$("#home_novidades .tabela ul li ul li").each
		(
			function(index, element)
			{
				$(element).replaceWith("<td>" + $(element).html() + "</td>");
			}
		);	
		
		$("#home_novidades .tabela ul li .tabela ul").each
		(
			function(index, element)
			{
				$(element).replaceWith("<tr>" + $(element).html() + "</tr>");
			}
		);		
		
		$("#home_novidades .tabela ul li .tabela").each
		(
			function(index, element)
			{
				$(element).replaceWith("<table class='tabela'>" + $(element).html() + "</table>");
			}
		);

		$("#home_novidades .tabela ul li.destaque").each
		(
			function(index, element)
			{
				$(element).replaceWith("<td class='destaque'>" + $(element).html() + "</td>");
			}
		);	
	
		$("#home_novidades .tabela ul li").each
		(
			function(index, element)
			{
				$(element).replaceWith("<td>" + $(element).html() + "</td>");
			}
		);		
		
		$("#home_novidades .tabela ul").each
		(
			function(index, element)
			{
				$(element).replaceWith("<tr>" + $(element).html() + "</tr>");
			}
		);
		
		$("#home_novidades .tabela").each
		(
			function(index, element)
			{
				$(element).replaceWith("<table class='tabela'>" + $(element).html() + "</table>");
			}
		);
		
		$("#menu li:first em").each
		(
			function(index, element)
			{
				$(element).replaceWith($(element).html());
			}
		);
		
		$("#menu li").each
		(
			function(index, element)
			{
				$(element).replaceWith("<td class='botao'>" + $(element).html() + "</td>");
			}
		);	
		
		$("#menu ul").each
		(
			function(index, element)
			{
				$(element).replaceWith("<tr>" + $(element).html() + "</tr>");
			}
		);
		
		$("#menu").each
		(
			function(index, element)
			{
				$(element).replaceWith("<table id='menu'>" + $(element).html() + "</table>");
			}
		);		
	}
}

function renderAjaxNovidade()
{
	$("#home_novidades .modulo_paginacao a").click
	(
		function()
		{	
			var myUrl = $(this).attr("href");
			
			if($("#popup").size() < 1)
			{		
				$("body").append("<div id='popup'></div>");		
				$("#popup").css
				(
					{
						"position":"absolute",			
						"background-color":"#000",		
						"left":"0px",	
						"top":"0px",
						"opacity":"0.8",
						"display":"none",
						"z-index":"5"
					}	
				);	
			}	
			
			redraw();
			
			$("#popup").fadeIn
			(
				"fast",
				function()
				{					
					redraw();
					$("body").append("<div id='carregando'></div>");
							
					$("#carregando").css
					(
						{
							"position":"absolute",			
							"background":"url('img/carregando.gif') center center no-repeat #1f1f20",
							"border":"solid 1px #fff",
							"width":0,	
							"height":0,
							"display":"none",
							"z-index":"6"
						}	
					);	
					
					$("#carregando").css
					(
						{	
							"width":0,	
							"height":0,					
							"left":Math.round($(document).scrollLeft()+($(window).width())/2),	
							"top":Math.round($(document).scrollTop()+($(window).height())/2)
						}	
					);

					$("#carregando").animate
					(
						{
							"width": 100,
							"height": 50,					
							"left":Math.round($(document).scrollLeft()+($(window).width()- 100)/2),	
							"top":Math.round($(document).scrollTop()+($(window).height()- 50)/2)
						}, 
						"fast",
						function() 
						{
							$.ajax
							(
								{
									url:myUrl,
									success:
									function(data)
									{
										var d = $(data);			
										renderAjax(d);		
										$(".target_ajax_novidade").html(d);	
										renderAjaxNovidade();	
										cssHack();
										$("#carregando").remove();
										$("#popup").fadeOut("fast");
									}
								}
							);							
						}
					);				
				}
			);			
			
			return false;
		}
	);
}

function renderAjaxNews()
{
	$("#home_conteudo_cen .modulo_paginacao a").click
	(
		function()
		{	
			var myUrl = $(this).attr("href");
			
			if($("#popup").size() < 1)
			{		
				$("body").append("<div id='popup'></div>");		
				$("#popup").css
				(
					{
						"position":"absolute",			
						"background-color":"#000",		
						"left":"0px",	
						"top":"0px",
						"opacity":"0.8",
						"display":"none",
						"z-index":"5"
					}	
				);	
			}	
			
			redraw();
			
			$("#popup").fadeIn
			(
				"fast",
				function()
				{					
					redraw();
					$("body").append("<div id='carregando'></div>");
							
					$("#carregando").css
					(
						{
							"position":"absolute",			
							"background":"url('img/carregando.gif') center center no-repeat #1f1f20",
							"border":"solid 1px #fff",
							"width":0,	
							"height":0,
							"display":"none",
							"z-index":"6"
						}	
					);	
					
					$("#carregando").css
					(
						{	
							"width":0,	
							"height":0,					
							"left":Math.round($(document).scrollLeft()+($(window).width())/2),	
							"top":Math.round($(document).scrollTop()+($(window).height())/2)
						}	
					);

					$("#carregando").animate
					(
						{
							"width": 100,
							"height": 50,					
							"left":Math.round($(document).scrollLeft()+($(window).width()- 100)/2),	
							"top":Math.round($(document).scrollTop()+($(window).height()- 50)/2)
						}, 
						"fast",
						function() 
						{
							$.ajax
							(
								{
									url:myUrl,
									success:
									function(data)
									{
										var d = $(data);			
										renderAjax(d);		
										$(".target_ajax_news").html(d);	
										renderAjaxNews();	
										$("#carregando").remove();
										$("#popup").fadeOut("fast");
									}
								}
							);							
						}
					);				
				}
			);			
			
			return false;
		}
	);
}

function renderAjax(elemento)
{
	elemento.find(".botao").mouseover
	(
		function()
		{
			$(this).addClass("hover");
		}
	);	
			
	elemento.find(".botao").mouseout
	(
		function()
		{			
			$(this).removeClass("hover");
		}
	);	
}


function insereCarregamento()
{
	if($("#popup").size() < 1)
	{		
		$("body").append("<div id='popup'></div>");		
		$("#popup").css
		(
			{
				"position":"absolute",			
				"background-color":"#000",		
				"left":"0px",	
				"top":"0px",
				"opacity":"0.8",
				"display":"none",
				"z-index":"5"
			}	
		);

		$("#popup").click
		(
			function()
			{
				fechar();
			}
		);
	}
	
	
	
	
	$("body").append("<div id='carregando'></div>");
			
	$("#carregando").css
	(
		{
			"position":"absolute",			
			"background":"url('img/carregando.gif') center center no-repeat #1f1f20",
			"border":"solid 1px #fff",
			"width":0,	
			"height":0,
			"display":"none",
			"z-index":"6"
		}	
	);		
	
	$("#carregando").css
	(
		{	
			"width":0,	
			"height":0,					
			"left":Math.round($(document).scrollLeft()+($(window).width())/2),	
			"top":Math.round($(document).scrollTop()+($(window).height())/2)
		}	
	);	
	
	$("body").append("<h5 class='titulo_popup'></h5><span class='numero_popup'></span><a class='cancelar_popup' href='javascript:;'>Cancelar</a><a class='fechar_popup' href='javascript:;'>Fechar</a><a class='proximo_popup' href='javascript:;'>Próximo</a><a class='anterior_popup' href='javascript:;'>Anterior</a>");
	
	$("[class$='_popup']").css
	(
		{
			"color":"#fff",						
			"text-align":"center",	
			"font-size":"12px",			
			"float":"left",				
			"clear":"left",	
			"display":"none",
			"position":"absolute",
			"z-index":8
		}	
	);
	
	$(".titulo_popup").css
	(
		{
			"font-size":"17px",
			"margin-top":"-20px",
			"text-align":"left"
		}	
	);
	
	$(".numero_popup").css
	(
		{
			"font-size":"15px"
		}	
	);
	
		
	$(".cancelar_popup").click
	(
		function()
		{
			fechar();
		}
	);
	
	redraw();	
}



function iniciaCadastro()
{	
	insereCarregamento();	

	var elemento = $(".cadastro:first");
	
	$("#popup").fadeIn
	(
		"fast",
		function()
		{
			$("#carregando").animate
			(
				{
					"width": 100,
					"height": 50,					
					"left":Math.round($(document).scrollLeft()+($(window).width()- 100)/2),	
					"top":Math.round($(document).scrollTop()+($(window).height()- 50)/2)
				}, 
				"fast",
				function() 
				{													
					$(".cancelar_popup").fadeOut
					(
						"fast",
						function()
						{
							$("#carregando").animate
							(
								{
									"width": elemento.innerWidth(),
									"left":Math.round($(document).scrollLeft()+($(window).width()- elemento.innerHeight())/2)
								}, 
								"fast",
								function() 
								{										
									$("#carregando").animate
									(
										{	
											"height": elemento.height(),					
											"top":Math.round($(document).scrollTop()+($(window).height()- elemento.height())/2)
										}, 
										"fast",
										function() 
										{
											redraw();							
											elemento.fadeIn
											(
												"fast",
												function()
												{	
													$(".fechar_popup").show();		
												}
											);
										}
									);
								}
							);
						}								
					);	
				}
			);
		}
	);
}




function iniciaCarregamento(elemento)
{	
	insereCarregamento();	

	$("#popup").fadeIn
	(
		"fast",
		function()
		{
			$("#carregando").animate
			(
				{
					"width": 100,
					"height": 50,					
					"left":Math.round($(document).scrollLeft()+($(window).width()- 100)/2),	
					"top":Math.round($(document).scrollTop()+($(window).height()- 50)/2)
				}, 
				"fast",
				function() 
				{		
					if(elemento.hasClass("foto"))
					{					
						$(".cancelar_popup").fadeIn();
						
						var url = elemento.attr("href");
						novaImagem = $("<img>");
						novaImagem.hide();
						novaImagem.bind
						(
							"load",
							function()
							{				
								$(".cancelar_popup").fadeOut
								(
									"fast",
									function()
									{
										$("#carregando").animate
										(
											{
												"width": novaImagem.width(),
												"left":Math.round($(document).scrollLeft()+($(window).width()- novaImagem.width())/2)
											}, 
											"fast",
											function() 
											{										
												$("#carregando").animate
												(
													{	
														"height": novaImagem.height(),					
														"top":Math.round($(document).scrollTop()+($(window).height()- novaImagem.height())/2)
													}, 
													"fast",
													function() 
													{
														redraw();												
														
														novaImagem.fadeIn
														(
															"fast",
															function()
															{	
																$(".titulo_popup").text(elemento.find("img").attr("alt"));
																
																var total = $("[rel='"+elemento.attr("rel")+"']").size();
																var indice = ($("[rel='"+elemento.attr("rel")+"']").index(elemento)+1);
																$(".numero_popup").text(indice+" de "+total);
																
																if(indice > 1)
																{															
																	$(".anterior_popup").click
																	(
																		function()
																		{		
																			$("#carregando img").remove();	
																			$("#carregando").remove();																		
																			$(".titulo_popup").remove();													
																			$(".numero_popup").remove();	
																			$(".fechar_popup").remove();
																			$(".proximo_popup").remove();
																			$(".anterior_popup").remove();									
																			$(".cancelar_popup").remove();
																			
																			iniciaCarregamento($($("[rel='"+elemento.attr("rel")+"']").get((indice-2))));
																		}
																	);	
																	
																	$(".anterior_popup").show();											
																}
																
																if(indice < total)
																{
																	$(".proximo_popup").click
																	(
																		function()
																		{								
																			$("#carregando img").remove();	
																			$("#carregando").remove();																		
																			$(".titulo_popup").remove();													
																			$(".numero_popup").remove();	
																			$(".fechar_popup").remove();
																			$(".anterior_popup").remove();											
																			$(".proximo_popup").remove();										
																			$(".cancelar_popup").remove();
																			
																			iniciaCarregamento($($("[rel='"+elemento.attr("rel")+"']").get(indice)));
																		}
																	);	
																	
																	$(".proximo_popup").show();
																}													
																
																if(total > 1)
																{	
																	$(".titulo_popup").show();	
																}
																
																$(".numero_popup").show();
																$(".fechar_popup").show();															
															}
														);
													}
												);
											}
										);
									}								
								);	
							}
						);
						
						$('#carregando').append(novaImagem);
						novaImagem.attr('src', url);
					}
					else if(elemento.hasClass("video"))
					{
						
						var videoWidth = 560;
						var videoHeight = 340;					
						
						
						$(".cancelar_popup").fadeOut
						(
							"fast",
							function()
							{
								$("#carregando").animate
								(
									{
										"width": videoWidth,
										"left":Math.round($(document).scrollLeft()+($(window).width()- videoWidth)/2)
									}, 
									"fast",
									function() 
									{										
										$("#carregando").animate
										(
											{	
												"height": videoHeight,					
												"top":Math.round($(document).scrollTop()+($(window).height()- videoHeight)/2)
											}, 
											"fast",
											function() 
											{	
												redraw();	
												
												$("#carregando").html("<iframe src='"+elemento.attr("href")+"' width='"+videoWidth+"' height='"+videoHeight+"' ></iframe>");										
												
												$(".titulo_popup").text(elemento.attr("title"));
												
												var total = $("[rel='"+elemento.attr("rel")+"']").size();
												var indice = ($("[rel='"+elemento.attr("rel")+"']").index(elemento)+1);
												$(".numero_popup").text(indice+" de "+total);
												
												if(indice > 1)
												{															
													$(".anterior_popup").click
													(
														function()
														{		
															$("#carregando img").remove();	
															$("#carregando").remove();																		
															$(".titulo_popup").remove();													
															$(".numero_popup").remove();	
															$(".fechar_popup").remove();
															$(".proximo_popup").remove();
															$(".anterior_popup").remove();									
															$(".cancelar_popup").remove();
															
															iniciaCarregamento($($("[rel='"+elemento.attr("rel")+"']").get((indice-2))));
														}
													);	
													
													$(".anterior_popup").show();											
												}
												
												if(indice < total)
												{
													$(".proximo_popup").click
													(
														function()
														{								
															$("#carregando img").remove();	
															$("#carregando").remove();																		
															$(".titulo_popup").remove();													
															$(".numero_popup").remove();	
															$(".fechar_popup").remove();
															$(".anterior_popup").remove();											
															$(".proximo_popup").remove();										
															$(".cancelar_popup").remove();
															
															iniciaCarregamento($($("[rel='"+elemento.attr("rel")+"']").get(indice)));
														}
													);	
													
													$(".proximo_popup").show();
												}													
												
												if(total > 1)
												{															
													$(".numero_popup").show();
												}
												
												$(".titulo_popup").show();																
												$(".fechar_popup").show();	
												
											}											
										);
									}
								);
							}								
						);
						
						
						
						
						
					}
				}
			);
		}
	);
}

function redraw()
{
	var left = Math.round($(document).scrollLeft()+($(window).width()-$("#carregando").width())/2);
	var top = Math.round($(document).scrollTop()+($(window).height()-$("#carregando").height())/2);

	$("#popup").css
	(
		{			
			"width":$(window).width(),	
			"height":$(window).height(),
			"left":$(document).scrollLeft(),
			"top": $(document).scrollTop()
		}	
	);	
	
	$("#carregando,.cadastro").css
	(
		{		
			"left":Math.round($(document).scrollLeft()+($(window).width()-$("#carregando").width())/2),	
			"top":Math.round($(document).scrollTop()+($(window).height()-$("#carregando").height())/2)
		}	
	);	
	
	var topBaixo = $("#carregando").innerHeight() + top + 5;
	var leftFechar = $("#carregando").innerWidth() + left - $(".fechar_popup").innerWidth() - 10;
	
	$(".fechar_popup").css
	(
		{
			"top": topBaixo,
			"left": leftFechar
		}
	);	

	$(".fechar_popup").click
	(
		function()		
		{
			fechar();
		}
	);		
	
	var leftProximo = leftFechar - $(".proximo_popup").innerWidth() - 20;
	$(".proximo_popup").css
	(
		{
			"top": topBaixo,
			"left": leftProximo
		}
	);
	
	var leftAnterior = leftProximo - $(".anterior_popup").innerWidth() - 20;
	$(".anterior_popup").css
	(
		{
			"top": topBaixo,
			"left": leftAnterior
		}
	);
	
	var topAlto = top - 10;
	$(".titulo_popup").css
	(
		{
			"top": topAlto,
			"left": left,
			"width": $("#carregando").innerWidth()
		}
	);
	
	
	$(".numero_popup").css
	(
		{
			"top": topBaixo,
			"left": left
		}
	);
}

function fechar()
{
	$("[class$='_popup']").fadeOut
	(
		"fast",
		function()
		{
			$("#carregando").fadeOut
			(
				"fast",
				function()
				{
					$("#popup").fadeOut
					(
						"fast",
						function()
						{
							$("#carregando img").remove();																				
							$(".titulo_popup").remove();													
							$(".numero_popup").remove();	
							$(".fechar_popup").remove();
							$(".anterior_popup").remove();
							$(".proximo_popup").remove();						
							$(".cancelar_popup").remove();
							$("#carregando").remove();			
							$("#popup").remove();								
							$(".cadastro").hide();
						}
					);
				}
			);
		}
	);
}
