$(document).ready(function() {
	
	$("a.link-video").click(function(){
		$link = $(this).attr("href");
		window.open($link);
		return false;
	});
	
	$("textarea.maxlength").maxlength({
	    'feedback' : '.charsLeft span'
	});
	
	$("#btnRecomende20x20").click(function(){
		$.ajax({
			type: 'GET',
			data: 'barraOrigem='+document.getElementById('barraOrigem').value+'&id='+document.getElementById('id').value,
			dataType: "json",
			url: 'home!recomendarNovoSite.mm',
			charset: "iso-8859-1",
			success: function(json) {
				if (json.msg == "ok") {
					alert('Recomendação realizada com sucesso.');
				}else{
					alert(json.msg);
				}
			}
		});
		return false;
	});
	
	$(".btnRecomende20x20").click(function(){
		$.ajax({
			type: 'GET',
			data: 'barraOrigem='+document.getElementById('barraOrigem').value+'&id='+document.getElementById('id').value,
			dataType: "json",
			url: 'home!recomendarNovoSite.mm',
			charset: "iso-8859-1",
			success: function(json) {
				if (json.msg == "ok") {
					alert('Recomendação realizada com sucesso.');
				}else{
					alert(json.msg);
				}
			}
		});
		return false;
	});
	
	$("#twittar_video").click(function(){
		$.ajax({
			type: 'GET',
			data: 'barraOrigem='+document.getElementById('barraOrigem').value,
			dataType: "json",
			url: 'http://twitter.com/home/?status=${multimidiaVideo.tituloEncode}+http://www.mmonline.com.br<s:text name="mmonline.multimidiaVideo.player.path"/>${multimidiaVideo.idMultimidiaVideo}+(via+@memonline)',
			charset: "iso-8859-1",
			success: function(json) {
				if (json.msg == "ok") {
					alert('Recomendação realizada com sucesso.');
				}else{
					alert(json.msg);
				}
			}
		});
		return false;
	
		
	});
	
	$("#twittar_audio").click(function(){
		$.ajax({
			type: 'GET',
			data: 'barraOrigem='+document.getElementById('barraOrigem').value,
			dataType: "json",
			url: 'http://twitter.com/home/?status=${multimidiaVideo.tituloEncode}+http://www.mmonline.com.br<s:text name="mmonline.multimidiaVideo.player.path"/>${multimidiaVideo.idMultimidiaVideo}+(via+@memonline)',
			charset: "iso-8859-1",
			success: function(json) {
				if (json.msg == "ok") {
					alert('Recomendação realizada com sucesso.');
				}else{
					alert(json.msg);
				}
			}
		});
		return false;
	});
	
	$("#not_favo").click(function(){
		$.ajax({
			type: 'GET',
			data: 'barraOrigem='+document.getElementById('barraOrigem').value+'&idConteudo='+document.getElementById('idConteudo').value,
			dataType: "json",
			url: 'home!favoritarNovoSite.mm',
			charset: "iso-8859-1",
			success: function(json) {
				if (json.msg == "ok") {
					alert('Registro adicionado aos favoritos com sucesso.');
				}else{
					alert(json.msg);
				}
			}
		});
		return false;
	});
	
	$(".not_favo").click(function(){
		$.ajax({
			type: 'GET',
			data: 'barraOrigem='+document.getElementById('barraOrigem').value+'&idConteudo='+document.getElementById('idConteudo').value,
			dataType: "json",
			url: 'home!favoritarNovoSite.mm',
			charset: "iso-8859-1",
			success: function(json) {
				if (json.msg == "ok") {
					alert('Favoritado com sucesso.');
				}else{
					alert(json.msg);
				}
			}
		});
		return false;
	});
	
	$("#favo_barra").click(function(){		
		$.ajax({
			type: 'GET',
			data: 'barraOrigem='+document.getElementById('barraOrigem').value+'&idConteudo='+document.getElementById('idConteudo').value,
			dataType: "json",
			url: 'home!favoritarNovoSite.mm',
			charset: "iso-8859-1",
			success: function(json) {
				if (json.msg == "ok") {
					alert('Registro adicionado aos favoritos com sucesso.');
				}else{
					alert(json.msg);
				}
			}
		});
		return false;
	});
	
	$(".favo_barra").click(function(){
		$.ajax({
			type: 'GET',
			data: 'barraOrigem='+document.getElementById('barraOrigem').value+'&idConteudo='+document.getElementById('idConteudo').value,
			dataType: "json",
			url: 'home!favoritarNovoSite.mm',
			charset: "iso-8859-1",
			success: function(json) {
				if (json.msg == "ok") {
					alert('Favoritado com sucesso.');
				}else{
					alert(json.msg);
				}
			}
		});
		return false;
	});

 	$(".archives ul li h5").click(function(){
		$(".archives ul li h5").removeClass("hover");
		$(".archives ul li table").hide();
		if($(this).next().is(":visible")){
			$(this).removeClass("hover").next().hide();
		}else {
			$(this).addClass("hover").next().show();
		}
		return false;
	});
	$(".archives ul li h5.hover").trigger("click");
	
	
 	$("ul.indique li a").click(function(){
		$valor = $(this).attr("href").split("#")[1];
		if($valor != ''){
			$(".boxComentario, .boxEnvie, .boxCompartilhe, .boxComunicar_erro,").hide();
			$("ul.indique li a").removeClass("hover");
			if($(this).hasClass("hover")){
				$(this).removeClass("hover");
				$(this).parent().parent().parent().find("." + $valor).slideUp();
			}else{
				$(this).addClass("hover");
				$(this).parent().parent().parent().find("." + $valor).slideDown();
			}
		}
		return false;
	});
 	$("#comente1").click(function(){
 		$("a[href='#boxComentario']").trigger("click");
		return false;
	});
 	
 	$("#compartilhar1").click(function(){
 		$("a[href='#boxCompartilhe']").trigger("click");
		return false;
	});
 	
 	$("#enviaramigo1").click(function(){
 		$("a[href='#boxEnvie']").trigger("click");
		return false;
	});
 	
 	$("#Comunicar_Erro1").click(function(){
 		$("a[href='#boxComunicar_erro']").trigger("click");
		return false;
	});

 	$(".formComentario").each(function(){
		$(this).validate({ errorContainer: $(".msgErrorComments"), errorLabelContainer: $(".msgError"),
			submitHandler: function() {
				var dataString = $(".formComentario").serialize()
				$.ajax({
					type: 'POST',
					data: dataString,
					url: '/home!enviarComentario.mm',
					dataType: "json",
					success: function(json) {
						if (json.msg == "ok") {
							$("#comentario_total_"+json.origemId).html('['+json.comentarioTotal+']');
							$("#linkSiteBlog").val('');
							$("#comentarioTexto").val('');
							$(".msgError").hide();
							$(".sucessComments").fadeIn();
						}else{							
							$(".msgError").html(json.msg);
							$(".msgError").fadeIn;
						}
					}
				});
			}
		});
	});
 	
	$(".formReview").each(function(){
		$(this).validate({ errorContainer: $(".msgErrorComments"), errorLabelContainer: $(".msgError"),
			submitHandler: function() {
				var dataString = $(".formReview").serialize()
				$.ajax({
					type: 'POST',
					data: dataString,
					url: '/home!enviarComentario.mm',
					dataType: "json",
					success: function(json) {
						if (json.msg == "ok") {
							$("#comentario_total_"+json.origemId).html('['+json.comentarioTotal+']');
							$("#linkSiteBlog").val('');
							$("#comentarioTexto").val('');
							$(".msgError").hide();
							$(".sucessComments").fadeIn();
						}else{							
							$(".msgError").html(json.msg);
							$(".msgError").fadeIn;
						}
					}
				});
			}
		});
	});

 	$("#btnEnvie").click(function(){
 		$(".sucessEnvie").fadeOut();
		$(".contentEnvie").fadeIn();
	});
 	
 	$(".btnEnvie").click(function(){
 		$(".sucessEnvie").fadeOut();
		$(".contentEnvie").fadeIn();
	});
 	
 	$(".btnVoteEnquete").click(function(){
		$.ajax({
			type: 'GET',
			data: 'idEnqueteOpcao='+$('input[name=enqueteOpcao]:checked').val()+'&idEnquete='+$('input[name=idEnquete]').val(),
			dataType: "json",
			url: '/blog!votarMMDebate.mm',
			success: function(json) {
				if (json.msg == "ok") {
					var prct = json.porcentagens.split(';');
					for(i = 0 ; i <= prct.length ; i++){
						$('#porcentagem_'+json.origemId+'_'+i+'').html(prct[i] + '%');
					}
					$('#enquete_total_'+json.origemId).html(json.total);
					$(".question").hide();
					$(".result").show();
				}else{
					alert(json.msg);
				}
			}
		});		
		return false;
	});
 	
 	$(".result span").each(function() {
		$width = $(this).html();
		$(this).css("width",$width + "%").append('<strong>'+ $width +'%</strong>');
	});
 	
 	$("a.btnResultados").click(function(){
		$(".question").hide();
		$(".result").show();
		return false;
	});
	
	$("a.btnVoltar").click(function() {
		$(".question").show();
		$(".result").hide();
		return false;
	});
	
 	
 	$(".formEnvie").each(function(){
		$(this).validate({ errorContainer: $(".msgErrorEnvie"), errorLabelContainer: $(".msgError"),
			submitHandler: function() {
				$.ajax({
					type: 'GET',
					data: $(".formEnvie").serialize(),
					dataType: "json",
					url: '/home!processEnvie.mm',
					charset: "iso-8859-1",
					success: function(json) {
						if (json.msg == "ok") {
							$(".contentEnvie").fadeOut();
							$(".sucessEnvie").fadeIn();
						}else{
							$(".msgError").html(json.msg);
							$(".msgError").fadeIn;
						}
					}
				});
			}
		});

	});
 	
 	$(".formComunique").each(function(){
		$(this).validate({ errorContainer: $(".msgErrorComunique"), errorLabelContainer: $(".msgError"),
			submitHandler: function() {
				$.ajax({
					type: 'GET',
					data: $(".formComunique").serialize(),
					dataType: "json",
					url: '/home!comunicarErros.mm',
					charset: "iso-8859-1",
					success: function(json) {
						if (json.msg == "ok") {
							$("#comunicarErrosNome").val('');
							$("#comunicarErrosEmail").val('');
							$("#comunicarErrosMensagem").val('');
							$(".msgError2").hide();
							$(".sucessComments2").fadeIn();
						}else{
							$(".msgError2").html(json.msg);
							$(".msgError2").fadeIn;
						}
					}
				});
			}
		});

	});
 	
 	 $("#meioemensagem").validate({ errorContainer: $(".msgErrorEspaco"), errorLabelContainer: $(".msgError"),
         submitHandler: function() {
             $.ajax({
                 type: 'POST',
 				data: $("#meioemensagem").serialize(),
                 dataType: "json",
                 url: '#',
                 success: function(json) {
                     if (json.msg == "ok") {
 						$("ul.nav li.form").hide();
 						$("ul.nav li#login_nome span").html('Olá, ' + json.nome);
 						$("ul.nav li#login_nome").show();
 						$("ul.nav li#login_btn_logout").show();
 						$("ul.nav li a.btnCadastreSe").hide();
 						$("ul.nav li a.btnFaleConosco").hide();
 						$("ul.nav li#login_btn_login").hide();
 					}else{
 						$(".msgErrorLogin").val(json.msg);
 						$(".msgErrorLogin").show();
 						$(".msgError").show();
 					}
                 }
             });
         }
     });

 	$("#formEspaco").validate({ errorContainer: $(".msgErrorEspaco"), errorLabelContainer: $(".msgError"),
        submitHandler: function() {
            $.ajax({
                type: 'GET',
				data: $("#formEspaco").serialize(),
                dataType: "json",
                url: '#',
                success: function(json) {
                    if (json.msg == "ok") {
						$(".contentEspaco").hide();
						$(".sucessEspaco").show();
					}else{
						$(".msgErrorLogin").val(json.msg);
						$(".msgError").show();
					}
                }
            });
        }
    });


	$size = $(".itensClone .itemAdd").size();
    $(".addAmigos").live("click", function() {
        $clone = $(".itensClone .itemAdd:last").clone();
        $number = $(".itensClone .itemAdd:last input.nome").attr("name").split("[")[1].split("]")[0];
        $number++;
        if($size == 1){
			$(".itensClone .itemAdd").show();
			$size++;
		}else if ($size <= 3){
			$(".itensClone").append($clone);
			$(".itensClone .itemAdd:last input.nome").attr("name", "nomeAmigo[" + $number + "]").attr("id", "nomeAmigo[" + $number + "]").addClass("required");
			$(".itensClone .itemAdd:last input.email").attr("name", "emailAmigo[" + $number + "]").attr("id", "emailAmigo[" + $number + "]").addClass("required");
			$size++;
			if($size == 3){
				$(".addAmigos").fadeOut();
			}
		}
        return false;
    });

    $(".itemAdd .btnExcluirAmigo").live("click", function() {
        $size = $(".itensClone .itemAdd").size();
		if($size == 1){
			$(this).parent().find("input").removeClass("required").end().hide();
		}else{
			$(this).parent().remove();
			$(".addAmigos").fadeIn();
		}
        return false;
    });
    $("input + label").click(function() {
		if ($(this).prev().attr("type") == 'checkbox') {
			if ($(this).prev().is(":checked")) {
				$(this).prev().attr("checked", false);
			} else {
				$(this).prev().attr("checked", true);
			}
		} else {
			$(this).prev().attr("checked", true);
		}
    });
	
    $(".item a[href*=#]").click(function() {
		if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
			var $target = $(this.hash);
			$target = $target.length && $target	|| $('[name=' + this.hash.slice(1) +']');
			if ($target.length) {
				var targetOffset = $target.offset().top;
				$('html,body').animate({scrollTop: targetOffset}, 1000);
				return false;
			}
		}
	});
    
    /* tagCloud */
	$links = '';
	$(".tagcloud a").each(function(){
		$tamanho = $(this).css('font-size');
		$(this).attr({ style: 'font-size:' + $tamanho }).removeAttr('class');
		//$links += $(this).html();
	});
	$val = '<tags>' + $(".tagcloud").html() + '</tags>';
	// $val = $val.toLowerCase();
	$(".tagcloud").each(function(){
		$w = $(this).width();
		$h = $(this).height();
		$(this).flash({ src: '/swf/tagcloud.swf', width: $w, height: $h, wmode: 'transparent',  flashvars: { tagcloud: $val, distr: "true", mode: "both", tcolor: "0x636363", tcolor2: "0x636363", hicolor: "0x636363", tspeed: "200" } });
	});
	/* tagCloud */
	
	$("ul#abanoticias").idTabs();
	$("ul#noticias").idTabs();
	$("ul#noticias1").idTabs();
	$("ul.tabCincoMais").idTabs();
	
	
});

/* HINT */
/**
* @author Remy Sharp
* @url http://remysharp.com/2007/01/25/jquery-tutorial-text-box-hints/
*/

