//---------------------------------------------------------------------------------------------------------------------
//----------------------------------------- Megacine - WebSite --------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------
//Scripts e Eventos JQuery - Dependem de jquery.js e history.js
//Desenvolvido por: Gabriel Vasconcellos Guilhem
//Data de Criação: 18/03/2011
//---------------------------------------------------------------------------------------------------------------------
//----------------------------------------- Megacine - WebSite --------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------
var IntervalTitle, slideshowprincipal, IntervalKeyUp;
var conteudoRefresh = {	container : "", 
						titulo : "", 
						html : "",
						proxima : "" };

//Incluído em 01/08/2011 - Permitir passar e voltar itens com teclado ou clicando nas setas correspondentes.
var IndiceAtualTrocaItem = -1;
//FIM - Variável que controla índice.
$(document).ready(function(){

	//Centraliza no caso de monitor menor que limites do site
	if($(document).width() > $(window).width()){
		$(document).scrollLeft(($(document).width() - $(window).width())/2);
	}
	
	$("#clima").height($(document).height());
	$("#clima").width($(document).width());
	
	//Inicializa plugin Jquery History
	$.history.init(AbrePagina);
	
	/*Acessar Links*/
	$("a").die("click");
	$("a").live("click",function(e){
		if($(this).attr('name').indexOf("#") != -1){
			e.preventDefault();
			var url = $(this)[0].href;
			var destino = $(this)[0].name != "" ? $(this)[0].name : "#conteudo";

			AbrePagina2(url,destino);
			return false;
		}
	});
	
	$("#tablist li a").bind("click", function(){
		$(".current").removeClass("current");
		$(this).addClass("current");
	});
	
	$("input, textarea").live("blur", function(){
		if($(this)[0].name.length > 0){
			var validationIcon;
			
			if($("img[name="+$(this)[0].name+"]").length > 0){
				validationIcon = $("img[name="+$(this)[0].name+"]").clone();
				$("img[name="+$(this)[0].name+"]").remove();
			}
			else{
				validationIcon = "<img src='img/cross.png' style='position:absolute;' name='"+$(this)[0].name+"' />";
			}		
			
			var msg = {"value" : ""};
			$(this).removeClass("invalido");
			if(validaCampo($(this), msg)){
				$(this).after(validationIcon);
			}
		}
	});
	
	//Efeito Hover - Menu
	/*
	$("#menu").append("<div class='hover'></div>");
	
	$(".hover").bind("click",function(){
		var Link = $("#menu .menu-item")[$("#ordem").val()];
		Link = $(Link).children("A")[0];
		Link.click();
	});
	
	$(".menu-item").hover(function(){
		if($("#ordem").val() != $(this)[0].name)
			MoveMenu($(this)[0].name);
	});	
	*/
	/*Mostrar/Esconder Player*/
	$(".abaElemento").bind("click",function(){
		ToggleDiv($(this).attr("name"));
	});
	$(':text').live('keydown',function(e){ //on keydown for all textboxes  
		if(e.keyCode==13) //if this is enter key  
			e.preventDefault();                  
	});
	$(".botaologin").bind("click",function(){
		if($(".botaologin strong").html() == "Sign in")
			AbrePagina2("l.php","#dialog");
		else
			AbrePagina2("lt.php","#dialog");
	});
	
	$(".abaElemento, .caixa ").hover(
	function(){
		var container = $(this).attr("name");
		$(container).stop().css({"right" : 0});
	},
	function(){
		var container = $(this).attr("name");
		$(container).stop().animate({right : -$(container +" .caixa").width()},{duration:200});
	});
	
	/*Submit de FORMS com Ajax*/
	$("form").live("submit",
	function(){
		try{
			if(validaPost($(this))){
			
				var Form = $(this);	
				
				var button = $(Form).find("input[type=submit]");

				var textobotao = "";
				if($(button).length > 0){
					textobotao = $(button).val();
					$(button).val("enviando...");
					$(button)[0].disabled = true;
				}
				
				$.post(
				$(Form)[0].action,
				$(Form).serialize(),
				   function(data){
						if($(button).length > 0){
							$(button).val(textobotao);
							$(button)[0].disabled = false;
						}
						
						if($(Form).attr("className") != "normal")
						{
							if(!isNullOrEmpty(data)){
								eval(data);
								AtualizaConteudo();
							}
						}
						else
						{
							$("#conteudo").html(data);
						}
				  });
			}
		}
		catch(e){
			alert(e.message);
			return false;
		}
		
		return false;
	});
	
	$(window).bind({
		scroll : function(){	
			//CentralizaDialog("#dialog");
			if($(".mask:not(:hidden), #mask:not(:hidden)").length > 0)
				CentralizaMask();
			
			AjustaClima();
			
			//Carrega paginas que tenham scroll abilitado.
			if($("#habilitascroll").val() == 1){
				var ultimoElement = $("#loadblogs"+$("#NrPagina").val()+" .postagemPesquisa").last();
				if($(ultimoElement).children(".acabou").length == 0){
					if(isScrolledIntoView(ultimoElement)){
						var pagina = parseInt($("#NrPagina").val()) + 1;
						CarregaPesquisa(pagina);
					}
				}
			}
		},
		resize : function(){
			CentralizaDialog("#dialog");
			CentralizaMask();
			AjustaClima();
		}
	});
	
	$("body").bind({
		keydown : function(event){
			if($("#dialog:hidden").length == 0){
				if(event.keyCode == 39)
				{// SETA p/ DIREITA
					AtualizaItemCorrente(1);
				}
				else if(event.keyCode == 37)
				{// SETA p/ ESQUERDA
					AtualizaItemCorrente(-1);
				}
				else if(event.keyCode == 27)
				{// ESC - Fecha janelas
					$($("#dialog .close")[0]).click();
				}
			}
		}
	});
	
	$("#locomotrom").resize(function(){
		AjustaClima();
	});
	
	$("#dialog").resize(function(){
		ajustaDialog();
	});
	
});
