var Geral = {
	__construct: function() {
		var me = this;
		$("input[type='text'], textarea").resetDefaultValue();
		$("a.aumentar").click(me.aumentar);
		$("a.diminuir").click(me.diminuir);
		me.emoticons();
		me.divopenToque98();
		me.divopenFechar();
		me.divopenGaleria();
		me.frmNewsletter();
		me.frmEnquete();
		me.autocompleteBusca();
		//me.top20();
		me.secao = $(".galeria-fotos .foto li");
		me.play();

		$(".controls #left").click(function(e) { 
			e.preventDefault();
			me.voltar();
			$(".controls .play img").attr("src", "img/bt-play-off.gif");
			me.ativado = false;
			clearInterval(me.intervalId);
			$(".controls #play").data("play", "play");
		});

		$(".controls #right").click(function(e) {	
			e.preventDefault();
			me.avancar(); 
			$(".controls #play img").attr("src", "img/bt-play-off.gif");
			me.ativado = false;
			clearInterval(me.intervalId);
			$(".controls #play").data("play", "play");
		});
	},

	aumentar: function() {
		$("#content .padding *").each(function() {
			var tamAtual = parseInt($(this).css("fontSize"));
			if(tamAtual < 15) $(this).css({fontSize:tamAtual+1});
		})
	},

	diminuir: function() {
		$("#content .padding *").each(function() {	
			var tamAtual = parseInt($(this).css("fontSize"));
			if(tamAtual > 9) $(this).css({fontSize:tamAtual-1});
		})
	},

	divopenToque98: function() {
		$("a.toque98").click(function() {
			window.divopen("divopen-toque98",324, 590);
			return false;
		});
	},

	divopenFechar: function() {
		$("a.divopenfechar").click(function() {
			parent.window.closeDivPopups();
			return false;
		})
	},

	divopenGaleria: function() {
		$(".controls #play").data("play","play");
		var botao = "";
		$("a#play").hover(
			function() {
				botao = $(this).data("play");
				$(this).find('img').attr('src',"img/bt-"+botao+"-over.gif");
			},
			function() {
				botao = $(this).data("play");
				$(this).find('img').attr('src',"img/bt-"+botao+"-off.gif");
			}
		);

		$("a#right").hover(
			function() {
				$(this).find('img').attr('src',"img/bt-arrowright-over.gif");					 
			},
			function() {
				$(this).find('img').attr('src',"img/bt-arrowright-off.gif");			 
			}
		);

		$("a#left").hover(
			function() {
				$(this).find('img').attr('src',"img/bt-arrowleft-over.gif");							 
			},
			function() {
				$(this).find('img').attr('src',"img/bt-arrowleft-off.gif");					 
			}
		);
	},

	frmNewsletter : function (){
		var $form = $('form[name="frmNewsletter"]');
		$form.submit(function(e){
			var nome = $(this).find('#newsnome').val();
			var email = $(this).find('#newsemail').val();
			$.ajax(
				{
					url: "newsletter",
					data: "nome="+nome+"&m1="+email,
					success: function(data, status){
						alert(data);
					}
				}
			);
			return false;
		});
	},

	frmEnquete: function (){
		var $form = $('form[name="frmEnquete"]');
		$form.submit(function(e){
			var opcao = $(this).find('input[type="radio"]:checked').val();
			var javotou = parseInt($(this).find('input[type="hidden"]').val());
			if(javotou == 1){
				alert('Você já votou nesse enquete');
				$form.find('input[type="radio"]').add('#frmenquetesubmit').remove();
				return false;
			}
			if(opcao == "" || typeof opcao == "undefined"){
				alert('Selecione uma opcao');
				return false;
			}
			$.ajax(
				{
					url: "enquete",
					data: "opcao="+opcao,
					success: function(data, status){
						var msg = data.split("+");
						alert(msg[1]);
						if(parseInt(msg[0]) == 1){
							$form.find('input[type="radio"]').add('#frmenquetesubmit').remove();
						}
						location.reload();
					}
				}
			);
			return false;
		});
		$("#frmenquetesubmit").click(function(e){
			$form.triggerHandler('submit');
			return false;
		});
		$("#frmenqueteparcial").click(function(e){
			$('.frmenqueteparcial').toggleClass("frmenqueteparcialshow");
			return false;
		});
	},

	top20 : function(){
		var me = this;
		me.top20.playing = "";
		$('#top20 li .indice').click(function(e){
			$('.'+$(this).parent().attr('class')).toggle();
		});
		/*$("#sound").sound({swf: "./swf/Player.swf"});
		this.top20.play = function(){
			$('#sound').play();
		}
		this.top20.stop = function(){
			$('#sound').stop();
		}
		$('.ouvir, .audio').click(function(){
			var musica = $(this).attr('name');
			if(me.top20.playing == musica){
				$('#sound').stop();
				me.top20.playing = "";
			} else {
				$('#sound').load(musica);
				$('#sound').play();
				me.top20.playing = musica;
			}
		});*/
	},

	atual:0,

	foto:'',

	ativado: false,

	secao: null,

	play: function() {
		me = this;
		
		$(".controls #play").click(function() {
			if (me.ativado == false) {
				$(this).find("img").attr("src", "img/bt-stop-over.gif");
				me.avancar();
				me.timer();
				me.ativado = true;
				$(this).data("play", "stop");
			} else {
				$(this).find("img").attr("src", "img/bt-play-over.gif");
				me.ativado = false;
				clearInterval(me.intervalId);
				$(this).data("play", "play");
			}
		});
	},

	voltar: function()
	{
		var me = this;
		var seletor = ".galeria-fotos";
		var tam = me.secao.length;
		var anterior = me.atual;
		me.atual = (((me.atual-1)%tam) + tam) % tam;
		if (anterior > 0){
			var descricao = "";
			$(seletor+" .foto li:eq("+(me.atual)+")").fadeIn("normal", function () {
				descricao = $(this).attr('valor');
				$("#divopen-galeria .legenda")/*.slideUp('fast',function(){
					$(this)*/.text(descricao)/*;
				}).slideDown("fast")*/;
			});
		} else {
			me.atual = me.secao.length - 1;
			descricao = $(seletor+" .foto li:eq("+(me.atual)+")").attr("valor");
			$("#divopen-galeria .legenda")/*.slideUp('fast',function(){
				$(this)*/.text(descricao)/*;
			}).slideDown("fast")*/;
			$(".galeria-fotos .foto li:not(:last, :first)").hide();
			$(".galeria-fotos .foto li:not(:last)").fadeOut("normal");
		}
	},

	avancar: function() 
	{
		var me = this;
		var seletor = ".galeria-fotos";
		var tam = me.secao.length;
		var anterior = me.atual;
		me.atual = ((me.atual+1)%tam);

		if (anterior < me.secao.length-1){
			$(".galeria-fotos .foto li:eq("+(me.atual-1)+")").fadeOut("normal");
			descricao = $(".galeria-fotos .foto li:eq("+(me.atual)+")").attr('valor');
			$("#divopen-galeria .legenda")/*.slideUp('fast',function(){
				$(this)*/.text(descricao)/*;
			}).slideDown("fast")*/;
		} else {
			$(".galeria-fotos .foto li:eq(0)").fadeIn("normal", function () {
				me.atual = 0;
				descricao = $(".galeria-fotos .foto li").show().attr('valor');
				$("#divopen-galeria .legenda")/*.slideUp('fast',function(){
					$(this)*/.text(descricao)/*;
				}).slideDown("fast")*/;
			});
		}
	},

	intervalId: 0,

	timer: function() {
		var me = this;
		clearInterval(me.intervalId);
		me.intervalId = setInterval(function() {
			me.avancar();
		},3000)
	},

	emoticons: function() {
		$("#divopen-aloalo .emoticons td:not('.ativo')").hover(
			function() {
				$(this).addClass('over');	
			},
			function() {
				$(this).removeClass('over');	
			} 
		)
		$(".emoticons tr *").click(function(e){
			if(e.target.tagName == "TD"){
				$o = $(e.target);
			} else {
				$o = $(e.target).parents('TD');
			}
			$(".emoticons td").removeClass('ativo');
			$o.addClass('ativo');
			var smile = $o.find('img').attr('id');
			$('#smile').val(smile);
		});
	},
	
	intervalSubmenu: 0,
	
	mostrarSubmenu:function(value){
		$(value).show();
	},
	
	esconderSubmenu:function(value){
		$(value).hide();
	},
	
	mouseOverBlog: false,
		 
	esconderSubMenuDelayed:function() {
		if (!Geral.mouseOverBlog) {
			var me = this;
			clearTimeout(me.intervalSubmenu);
			me.intervalSubmenu = setTimeout(function(){
				me.esconderSubmenu("#blogs");
			}, 500);
		}
	},

	autocompleteBusca: function() {
		/*var $obj = $("#autocomplete li");

		$obj.parent().children(':first').addClass('ativo');

		$("#menu input[type=text]").click(function() {

			$(document).unbind('click');

			$("#autocomplete").slideDown("fast", function() {
				$(document).bind("click", function(){
					$("#autocomplete").hide();
				});
			});

		});

		$obj.hover(
			function() {
				$obj.removeClass('ativo');
				$(this).addClass('ativo');
			},

			function() {
				$(this).removeClass('ativo');
			}
		)*/
		if(typeof jQuery.fn.autocomplete === "function"){
			$.getJSON('./autocompletar_dados.php',function(data){
				$("#input_busca").autocomplete(data,{
					width: 282,
					minChars: 0,
					max: 12,
					autoFill: true,
					mustMatch: false,
					matchContains: false,
					scrollHeight: 150
				});
			});
		}
	}

}

$(function() {
	Geral.__construct();
	$("#blogs").hover(function(e){
		var me = Geral;
		me.mouseOverBlog = true;
		clearTimeout(me.intervalSubmenu);
	},function(e){
		if (e.target.id == 'blogs') {
			me.mouseOverBlog = false;
			Geral.esconderSubmenu('#blogs');
		}
	});
});