$(document).ready (function() { 
	
	$(".volver_pop_right").each (function () {
		$(this).click(function () {
			var div_show = $(this).parents(".mod_encuesta_cont").children("#encuesta_show");
			var div_res = $(this).parents(".mod_encuesta_cont").children("#encuesta_res");
			var div_volver_pop = $(this).parents(".mod_encuesta_cont").children(".volver_pop_right");
			var div_boton_votar = $(this).parents(".mod_encuesta_cont").children("#boton_votar");
			var div_poll_stats = $(this).parents(".mod_encuesta_cont").children(".poll_stats");
			
			$(div_show).animate({opacity: "hide"}, 600, function () {
				$(div_res).animate({opacity: "show"}, 600);
				$(div_boton_votar).animate({opacity: "show"}, 600);
				$(div_poll_stats).animate({opacity: "show"}, 600);
			});
			$(div_volver_pop).animate({opacity: "hide"}, 600);
		});
	});

	$(".poll_stats").each(function () {
		$(this).click(function () {
			var poll_id = $(this).attr('rel');
			var div_res = $(this).parents(".mod_encuesta_cont").children("#encuesta_show");
			var div_res_2 = $(this).parents(".mod_encuesta_cont").children("#encuesta_res");
			var div_boton_votar = $(this).parents(".mod_encuesta_cont").children("#boton_votar");
			var div_poll_stats = $(this).parents(".mod_encuesta_cont").children(".poll_stats");
			var div_volver_pop = $(this).parents(".mod_encuesta_cont").children(".volver_pop_right");
			
			$.ajax({
				type: "POST",
				url: "/polls/view_votes_index/"+poll_id,
				data: "",
				beforeSend: function () {},
				success: function (data) {
					$(div_res_2).animate({opacity: "hide"}, 600, function () {
						$(div_res).html(data).animate({opacity: "show"}, 600);
						$(div_volver_pop).animate({opacity: "show"}, 600);							
					});
					$(div_boton_votar).animate({opacity: "hide"}, 600);
					$(div_poll_stats).animate({opacity: "hide"}, 600);
					$("#alert_encuesta").animate({opacity: "hide"}, 600);
				}
			});
		
		});
	});
	
	$(".votar").each(function () {
		$(this).click(function () {
			var ul = $(this).parents(".mod_encuesta_cont").children("#encuesta_res").children("ul");
			div_error = $(this).parents(".mod_encuesta_cont").children("#alert_encuesta");
			div_res = $(this).parents(".mod_encuesta_cont").children("#encuesta_res");
			div_boton_votar = $(this).parents(".mod_encuesta_cont").children("#boton_votar"); 
			div_poll_stats = $(this).parents(".mod_encuesta_cont").children(".poll_stats");
			var option = "";
			
			$(div_error).animate({opacity: "hide"}, 600);
			
			$(ul).children("li").each(function () {
				if ($(this).children().attr("checked")) {
					option = $(this).children().attr("value");
				}
			});
			
			if (option != "") {
				$.ajax({
					type: "POST",
					url: "/polls/add_vote_index/"+option,
					data: "",
					beforeSend: function () {
					
					},
					success: function (data) {
						if (data == 1) {
							var str = "Ya votaste en esta encuesta";
							$(div_error).html(str).animate({opacity: "show"}, 600);
						} else if (data == 2) {
							var str = "Debes ingresar para votar en esta encuesta";
							$(div_error).html(str).animate({opacity: "show"}, 600);	
						} else {
							$(div_res).animate({opacity: "hide"}, 600, function () {
								$(div_res).html(data).animate({opacity: "show"}, 600);
							});
							$(div_boton_votar).animate({opacity: "hide"}, 600, function () {
								var str = $(div_poll_stats).html();
								str = str.split(" ");
								var count = parseInt(str[0]) + 1;
								
								if (count == 1) {
								 	str = count+" voto";
								} else{
									str = count+" votos";
								}
								$(div_poll_stats).html(str);
							});
						}
					}
				});
			} else {					
				var str = "Debes seleccionar una opci&oacute;n";
				$(div_error).html(str).animate({opacity: "show"}, 600);
			}
		});
	});
	
	$(".tags_links").each (function () {
		$(this).click(function () {
			var str = $(this).children().html();
			var url = "/resultado/"+str;
			location.href = url;
			return;
		});
	});
	
});

function changeTitular (t) {
	var tag = $(t).attr("rel");	
	
	var class_index = $(t).parents("li").attr('class');
	var info_index = class_index.split("_");
	
	var i = 1;
	
	if (tag != "") {
		$.ajax({
			url: "/posts/findPostsByTag/"+tag,
			dataType: "json",
			beforeSend: function () {
				$("#titulares").children("ul").children("li").each (function () {
					var class_str = $(this).attr("class");
					var info = class_str.split("_");					
					$(this).attr("class", info[0]);
				});
				
				$(t).parents("li").attr('class', info_index[0]+"_activo");
			},
			cache: false,
			success: function (data) {
				var html = '';
				
				$.each(data, function (i, item) {
					var url_prefix = '';
					var canal = item.Post.canales_cana_id;
					var title = item.Post.post_title;
					var image = item.Post.post_image_th;
					var slug  = item.Post.post_slug;
					var galle = item.Post.galleries_id;
					var total_videos   = item.Info.total_videos;
					var total_images   = item.Info.total_images;
					var total_comments = item.Info.total_comments;
						
					switch(canal) {
						case '1':
							url_prefix = atv_url;
							break;
						case '2':
							url_prefix = tuteve_url;
							break;
						case '3':
							url_prefix = tuteve_url;
							break;
						case '4':
							url_prefix = tuteve_url;
							break;
						default:
							url_prefix = '';
							break;
					}
					
					html += "<div id='lista_titulares'>";
					html += "  <a href='"+ url_prefix +"/blogs/"+slug+"'><img src='"+image+"'/></a>";
					html += "  <a href='"+ url_prefix +"/blogs/"+slug+"'><h4>"+title+"</h4></a>";
					html += "  <ul>";
					
					if (total_images > 0) {
						html += "    <li class='imagenes'><a href='"+ url_prefix +"/blogs/"+slug+"'>imagenes</a></li>";
					}
					
					if (total_videos > 0) {
						html += "    <li class='videos'><a href='"+ url_prefix +"/blogs/"+slug+"'>videos</a></li>";
					}
					
					if (total_comments > 0) {
						html += "    <li class='comentarios'><a href='"+ url_prefix +"/blogs/"+slug+"'>comentarios</a></li>";
					}
					
					//html += "    <li class='documento'><a href='/blogs/"+slug+"'>documentos</a></li>";
					//html += "    <li class='mas'><a href='/blogs/"+slug+"'>mas</a></li>";
					
					html += "  </ul>";
					html += "</div>";
					
					if(i == 3) {
						i = 0;
						html += "<div class='clear'></div>";
					}
					i++;
				});
				$("#titulares_body").animate({opacity: "hide"}, 600, function () {
					$("#titulares_body").html(html).animate({opacity: "show"}, 600);
				});
			}
		});
	}
}

