Advertisement
Guest User

Untitled

a guest
Oct 24th, 2014
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $(function(){
  2.     var carregado = true,
  3.         filterLetter = "";
  4.         $modal          = $(document.getElementById("modal-content")),
  5.         $modalEscola    = $(document.getElementById('modal-escola')),
  6.         az              = true,
  7.         za              = false,
  8.         voto            = false,
  9.         busca           = false,
  10.         pathname = window.location.pathname,
  11.         nidUrl = pathname.split("/")[pathname.split("/").length-1],
  12.         $retorno = $(document.getElementById("resultado-retorno")),
  13.         $numResultados = $(document.getElementById("num-resultados")),
  14.         $textoBuscado = $(document.getElementById("texto-buscado")),
  15.         $verTodos = $(document.getElementById("verTodos")),
  16.         $shareFacebooke = $(document.getElementById("share-facebook")),
  17.         $shareTwitter = $(document.getElementById("share-twitter")),
  18.         inputBusca = document.getElementById("nome-escolas"),
  19.         $erroCaptcha =  $(document.getElementById("erro-captcha")),
  20.         textEscola = document.getElementById("textEscola"),
  21.         textCategoria = document.getElementById("textCategoria");
  22.        
  23.         pathname = "/educacao/desafios/votacao/escolas"
  24.         // pathname = pathname.split(nidUrl)[0]
  25.         // console.log(pathname);
  26.         if(nidUrl!="votacao" && nidUrl!=""){
  27.             abreModal(nidUrl);
  28.         }
  29.  
  30.     function ordenar(x,y){
  31.         if(az) return ((x.name == y.name) ? 0 : ((x.name > y.name) ? 1 : -1 ));
  32.         if(za) return ((x.name == y.name) ? 0 : ((x.name < y.name) ? 1 : -1 ));
  33.         if(voto) return ((x.votos == y.votos) ? 0 : ((x.votos < y.votos) ? 1 : -1 ));
  34.     }
  35.  
  36.     function atualizaVotos(nid){
  37.         if(nid){
  38.             $.getJSON(pathname + '?school_id=' + nid, function(data){
  39.                 voto = data[0].votos==1?" voto":" votos";
  40.                 var text = data[0].votos+voto;
  41.                 $modal.find(".votos").text(text);
  42.                 $(".list-escolas article").each(function(x){
  43.                     // console.log("data-nid: "+$(this).attr("data-nid"));
  44.                     // console.log("escola v: "+nid);
  45.                     if($(this).attr("data-nid") == nid){
  46.                         $(this).find(".cont-votos").text(text)
  47.                     }
  48.                 })
  49.             });
  50.         }
  51.     }
  52.  
  53.     function populaModal(nid){
  54.         var $antes = $("#antes").next(),
  55.             $depois = $("#depois").next(),
  56.             feedback = "";
  57.         $.getJSON(pathname + '?school_id=' + nid, function(data){
  58.             voto = data[0].votos==1?" voto":" votos";
  59.             $(".modal-header h1").text(data[0].name);
  60.             $(".foto-1").find("span").attr("data-src",data[0].images.big.img_1).next().attr("src",data[0].images.big.img_1);
  61.             $(".foto-2").find("span").attr("data-src",data[0].images.big.img_2).next().attr("src",data[0].images.big.img_2);
  62.             $(".foto-3").find("span").attr("data-src",data[0].images.big.img_3).next().attr("src",data[0].images.big.img_3);
  63.            
  64.             $modal.find(".votos").text(data[0].votos+voto);
  65.             $(document.getElementById("bt-votar")).attr("data-nid",data[0].nid_crypted);
  66.             $(document.getElementById("desfazer")).attr("data-nid",data[0].voted_id);
  67.             $shareFacebooke.attr("data-escola",data[0].nid_crypted)
  68.             $shareTwitter.attr("data-escola",data[0].nid_crypted)
  69.            
  70.             if(!data[0].votes_count)
  71.                 feedback = "só poderá votar em até 3 escolas. <b>Confirmar voto?</b>";
  72.             else if(data[0].votes_count == 1)
  73.                 feedback = "ainda pode votar em 2 escolas. <b>Confirmar voto?</b>";
  74.             else if(data[0].votes_count == 2){
  75.                 feedback = "ainda pode votar em 1 escola. <b>Confirmar voto?</b>";
  76.             }else if( data[0].votes_count>=3 ){
  77.                 $(".box-votacao").addClass("check")
  78.                 $modal.find(".inicial").find("button").addClass("disabled").attr("disabled","disabled");
  79.             }
  80.  
  81.             if(data[0].votes_count>=3){
  82.                 $(document.getElementById("noVoto")).show();
  83.             }else{
  84.                 $(document.getElementById("noVoto")).hide();
  85.             }
  86.  
  87.  
  88.  
  89.             $modal.find(document.getElementById("feedback-votos")).html(feedback);
  90.            
  91.             if(data[0].voted){
  92.                 $modal.find(document.getElementById("voted_school")).attr("data-nid",data[0].voted_id).html(data[0].voted_school);
  93.                 $modal.find(".efetuado").hide();
  94.             }else{
  95.                 $modal.find(".box-votacao").find(".inicial").show();
  96.             }
  97.             if(data[0].voted_id == nid && data[0].voted){
  98.                 $modal.find(".efetuado").show();
  99.             }
  100.             if(data[0].voted_id != nid && data[0].voted){
  101.                 $modal.find(".desativado").show();
  102.             }
  103.  
  104.  
  105.         }).error(function(jqXHR, textStatus, errorThrown){
  106.             alert("error occurred!");
  107.         });
  108.     }
  109.     $(document.getElementById("voted_school")).bind("click",function(){
  110.         nid  = $(this).attr('data-nid');
  111.         populaModal(nid);
  112.     });
  113.     $(document.getElementById("filtro-drop")).find("span").bind("click",function(){
  114.         $this = $(this);
  115.         $(document.getElementById("filtro-alfa")).find(".filtro-select").text($this.text());
  116.         $(document.getElementById("filtro-drop")).find("span").removeClass("ativo")
  117.         $this.addClass("ativo");
  118.  
  119.         az = $this.hasClass("ordAZ")?true:false;
  120.         za = $this.hasClass("ordZA")?true:false;
  121.         voto = $this.hasClass("ordVotos")?true:false;
  122.  
  123.  
  124.        
  125.         $(".list-escolas").find("article").remove();
  126.         if(inputBusca.value != "") index = 0;
  127.         loadData(index,perPage,true, filterLetter);
  128.     });
  129.     $verTodos.bind("click",function(){
  130.         busca = false;
  131.         $(".list-escolas").find("article").remove();
  132.         $retorno.hide();
  133.         loadData(index,perPage,true, filterLetter);
  134.         inputBusca.value = "";
  135.     })
  136.     $(document.getElementById("search-escolas")).submit(function(event){
  137.         if(inputBusca.value != ""){
  138.             $(".list-escolas").find("article").remove();
  139.             loadData(index,perPage,true, filterLetter);
  140.             busca = true;
  141.         }else{
  142.             busca = false;
  143.         }
  144.         event.preventDefault();
  145.     });
  146.  
  147.  
  148.     $modalEscola.on("click", "#overlay, #modal-close",function(){
  149.         $(".modalAberta").removeClass("modalAberta");
  150.         $modalEscola.stop(true,true).fadeOut('fast');
  151.         $(".foto-zoom").hide().find("img").attr("src","../sites/all/themes/rio2016_educacao/img/loader-314.gif");
  152.         $modal.find(".fotos ul").show().find("img").attr("src","../sites/all/themes/rio2016_educacao/img/loader-157.gif");
  153.         $modal.find(".desativado,.efetuado,.inicial,.confirmacao").hide();
  154.         $erroCaptcha.hide();
  155.         $modal.find(textCategoria).hide();
  156.         $modal.find(textEscola).show();
  157.     });
  158.    
  159.     $modal.find(".fotos li").hover(function(){
  160.         var index = $(this).index(),
  161.             $fotos = $(this).parents(".fotos");
  162.  
  163.         $modal.find("li").addClass("nohover");
  164.         $(this).removeClass("nohover");
  165.         $fotos.prev().find("li").eq(index).removeClass("nohover").addClass("hover");
  166.         $fotos.next().find("li").eq(index).removeClass("nohover").addClass("hover");
  167.     },function(){
  168.         $modal.find("li").removeClass("hover").removeClass("nohover");
  169.     });
  170.  
  171.     // Ao clicar na foto, dentro da modal
  172.     $modal.find(".fotos li").on("click",".mask",function(){
  173.        
  174.         $('#modal-content').hide();
  175.         // var alt = $(this).parent().find("span").attr('data-descricao');
  176.        
  177.         var src = $(this).parent().find("span").attr('data-src'),
  178.             title = $(this).parent().find("span").html();
  179.        
  180.         $('.foto-zoom-title').html(title);
  181.         $('.foto-zoom img').attr("src", src);
  182.         $('.foto-zoom').fadeIn();
  183.         // $('.foto-zoom img').attr("alt", alt);
  184.         // var $this    = $(this),
  185.         //  antes   = $this.attr('data-antes'),
  186.         //  depois  = $this.attr('data-depois'),
  187.         //  textoCat= $this.attr('data-descricao');
  188.         // $(".foto-zoom").fadeIn();
  189.         // $modal.find(textEscola).hide();
  190.         // $modal.find(textCategoria).show().find("p").text(textoCat);
  191.         // $(document.getElementById("antes")).find("img").attr({"src":antes});
  192.         // $(document.getElementById("depois")).find("img").attr({"src":depois});
  193.         // $modal.find("span.text b").text("Clique nas fotos para reduzi-las");
  194.  
  195.     });
  196.    
  197.     $(".foto-zoom").on("click",function(){
  198.         var $this   = $(this),
  199.             textoCat= $this.attr('data-descricao');
  200.         $(".foto-zoom").hide().find("img").attr("src","../sites/all/themes/rio2016_educacao/img/loader-314.gif");
  201.         $('#modal-content').fadeIn();
  202.         // $modal.find(".fotos").find("ul").fadeIn();
  203.         // $modal.find(".text p").text(textoCat);
  204.  
  205.         // $modal.find("span.text b").text("Clique nas fotos para ampliá-las");
  206.         // $modal.find(textCategoria).hide();
  207.         // $modal.find(textEscola).show();
  208.     });
  209.     $('.list-escolas').on("click",'.open', function(){
  210.         var $this = $(this),
  211.             nid  = $this.attr('data-nid');
  212.             console.log(nid);
  213.         $("body").attr("SCROLL","no");
  214.  
  215.         $this.addClass("modalAberta");
  216.         abreModal(nid)
  217.  
  218.     });
  219.     function abreModal(nid){
  220.         var hwindow = $(window).height();
  221.         if(hwindow < 618){
  222.             $(".modal-sty .modal-container").css({
  223.                 "height": hwindow,
  224.                 "top": 0,
  225.                 "margin-top": 0
  226.             })
  227.             $("#modal-scrtoll").css({
  228.                 "height": 520-(618-hwindow)
  229.             })
  230.         }
  231.         $modalEscola.stop(true,true).fadeIn('fast');
  232.         populaModal(nid);
  233.     }
  234.  
  235. // LISTA DE ESCOLAS
  236.     index = 0;
  237.     perPage = 6;
  238.     contentSelector = ".list-escolas";
  239.     loadingSelector = ".loading";
  240.  
  241.     // var str = "Escola 10",
  242.     //  regular_expression = new RegExp(str,'ig');
  243.     // function econtra(obj){
  244.     //  return regular_expression.exec(obj)!=null?true:false;
  245.     // }
  246.    
  247.     function econtra(obj){
  248.         var str = inputBusca.value;
  249.         var regular_expression = new RegExp(str,'ig');
  250.         return regular_expression.exec(obj)!=null?true:false;
  251.     }
  252.  
  253.     function concatenar(data){
  254.         voto = data.votos==1?" voto":" votos";
  255.         return "<article class='open' data-nid='"+data.nid_crypted+"'>"+
  256.                         "<div class='border-sty'>"+
  257.                             "<div class='content-escola'>"+
  258.                                 "<div class='clearfix box-fotos'>"+
  259.                                     "<div class='fotos'>"+
  260.                                         "<img src='"+data.images.medium.img_1+"' width='133' height='133'>"+
  261.                                     "</div>"+
  262.                                     "<div class='fotos'>"+
  263.                                         "<img src='"+data.images.medium.img_2+"' width='133' height='133'>"+
  264.                                     "</div>"+
  265.                                     "<div class='fotos'>"+
  266.                                         "<img src='"+data.images.medium.img_3+"' width='133' height='133'>"+
  267.                                     "</div>"+
  268.                                 "</div>"+
  269.                                 "<div class='nome-votos'>"+
  270.                                     "<strong class='nome-escola'>"+data.name+"</strong>"+
  271.                                     "<span class='cont-votos'>"+data.votos+voto+"</span>"+
  272.                                 "</div>"+
  273.                             "</div>"+
  274.                         "</div>"+
  275.                         "<div class='btn-conheca'>"+
  276.                             "<div class='ctt-center'>"+
  277.                                 "<span class='btn-mais'>Conheça</span>"+
  278.                                 "<span class='btn-text'>Conheça o projeto e vote</span>"+
  279.                             "</div>"+
  280.                         "</div>"+
  281.                     "</article>";
  282.     }
  283.  
  284.     function loadData(i,p,clear, filter){
  285.         carregado = false;
  286.         $(loadingSelector).show();
  287.         $.getJSON(pathname,function(data){
  288.             var html = "";
  289.             var postId = clear?0:index;
  290.             var contador = 0;
  291.             if(clear) index = 0;
  292.             data.sort(ordenar);
  293.            
  294.             if(busca){
  295.                 while(postId < data.length){
  296.                     if(econtra(data[postId].name)){
  297.                         html += concatenar(data[postId]);
  298.                         contador++;
  299.                     }
  300.                     postId++;
  301.                 }
  302.                 $retorno.show();
  303.                 $textoBuscado.text(inputBusca.value);
  304.                 $numResultados.text(contador);
  305.                 contador = 0;
  306.             }else{
  307.                 while(postId < data.length && postId < index+perPage){
  308.                     if (filterLetter != "") {
  309.                         if (data[postId].name.charAt(0).toLowerCase() == filter.toLowerCase()) {
  310.                             html += concatenar(data[postId]);
  311.                         }
  312.                     }else{
  313.                         html += concatenar(data[postId]);
  314.                     }
  315.                     postId++;
  316.                 }
  317.             }
  318.  
  319.  
  320.  
  321.             $(loadingSelector).before(html);
  322.             index = index + perPage;
  323.            
  324.             $(loadingSelector).hide();
  325.         }).done(function( json ) {
  326.             carregado = true;
  327.         });
  328.     }
  329.    
  330.     loadData(index,perPage,false, filterLetter);
  331.    
  332.     var w = $(window);
  333.     w.scroll(function(){
  334.         if(carregado)
  335.             if(w.scrollTop() + w.height() == $(document).height()){
  336.                 if(!busca){
  337.                     if (filterLetter != "") {
  338.                         loadData(index,perPage,false, filterLetter);
  339.                     }else{
  340.                         loadData(index,perPage,false, filterLetter);
  341.                     }
  342.                     $retorno.hide()
  343.                 }
  344.             }
  345.     });
  346.  
  347.     $(".box-votacao .inicial").find("button").bind("click",function(){
  348.         $(this).parent().hide().next().fadeIn().parent().addClass("check");
  349.     });
  350.     $(".box-votacao .confirmacao").find("button").bind("click",function(){
  351.         $this = $(this);
  352.         $erroCaptcha.hide();
  353.         if($this.hasClass("sim")){
  354.             $this.addClass("disabled").attr("disabled","disabled");
  355.             var $id = $this.attr("data-nid");
  356.            
  357.             challengeField = $("input#recaptcha_challenge_field").val();
  358.             responseField = $("input#recaptcha_response_field").val();
  359.  
  360.             $.ajax({
  361.                 type: "POST",
  362.                 url: pathname + '/votar',
  363.                 data: "recaptcha_challenge_field=" + challengeField +
  364.                       "&recaptcha_response_field=" + responseField +
  365.                       "&school_id=" + $id,             
  366.                 async: true,
  367.                
  368.                 success: function(data) {
  369.                     if(data.success){
  370.                         $this.parent().hide().next().fadeIn();
  371.                         var nid = $(".modalAberta").attr("data-nid");
  372.                         atualizaVotos(nid);
  373.                     }
  374.                     if(!data.captcha){
  375.                         $erroCaptcha.fadeIn();
  376.                     }
  377.                     Recaptcha.reload();
  378.                     $this.removeClass("disabled").removeAttr("disabled");
  379.                 }
  380.             });
  381.         }else{
  382.             $(this).parent().hide().prev().fadeIn().parent().removeClass("check");
  383.         }
  384.     })
  385.     $(document.getElementById("desfazer")).bind("click",function(){
  386.         $(document.getElementById("noVoto")).hide();
  387.         $this = $(this);
  388.         $id = $("#bt-votar").attr("data-nid");
  389.             $.getJSON(pathname + '/cancelar?school_id='+$id, function(data) {
  390.                 if (data.success) {
  391.                     atualizaVotos($id);
  392.                     $(document.getElementById("bt-votar")).removeAttr('disabled').removeClass("disabled");
  393.                     $modal.find(".inicial").find("button").removeClass("disabled").removeAttr("disabled");
  394.                     $this.parent().hide().parent().removeClass("check").find(".inicial").fadeIn();
  395.                 }
  396.             });
  397.     });
  398.  
  399.     // <meta property="og:title" content="Topa o Desafio?" />
  400.     // <meta property="og:summary" content="Veja como educação e esporte no #rio2016transforma podem mudar a sua escola e vote na sua preferida: http://www.rio2016.com/RVZ" />
  401.     // <meta property="og:image" content="http://ia.media-imdb.com/images/rock.jpg" />
  402.    
  403.     $(".bar-filtro .search").click(function(){
  404.         if ($("#nome-escolas").hasClass("active")) {
  405.             $("#nome-escolas").animate({width : "0px"}, 400, function(){
  406.                 $(this).removeClass("active")
  407.             });
  408.         }else{
  409.             $(".bar-filtro .search").hide();
  410.             $("#nome-escolas").animate({width : "164px"}, 400, function(){
  411.                 $(this).addClass("active")
  412.             });
  413.         }
  414.     });
  415.  
  416.  
  417.     // FILTRO AZ
  418.     $(".filtro-az a").click(function(){
  419.         filterLetter = $(this).text();
  420.         $("article").remove();
  421.         loadData(index,perPage,true, filterLetter, filterLetter);
  422.     });
  423.  
  424.     $("#nome-escolas").blur(function(){
  425.         $(this).animate({width : "0px"}, 400, function(){
  426.             $(this).removeClass("active");
  427.             $(".bar-filtro .search").show();
  428.         });
  429.     });
  430.  
  431.  
  432.     $(".button-share-facebook,#share-voto").click(function(event){
  433.         var $this   = $(this),
  434.             url     = "http://www.facebook.com/sharer/sharer.php?s=100",
  435.             title   = $("title").text().split("|"),
  436.             href    = $this.hasClass("btn-modal")?location.hostname+pathname+"/"+$shareFacebooke.attr("data-escola"):location.hostname+pathname;
  437.  
  438.         title = (title && title[0] && $.trim(title[0])) || "Topa o Desafio?";
  439.         url += '&p[title]=' + title;
  440.         url += '&p[url]=' + encodeURIComponent(href);
  441.         url += '&p[summary]=' + encodeURIComponent("Veja como educação e esporte no #rio2016transforma podem mudar a sua escola e vote na sua preferida: http://www.rio2016.com/RVZ");
  442.         url += '&p[image]=';
  443.  
  444.         event.preventDefault();
  445.  
  446.         window.open(url, 'sharer', 'toolbar=0,status=0,width=626,height=436');
  447.         console.log(href);
  448.     });
  449.  
  450.     $(".button-share-twitter").click(function(event){
  451.         event.preventDefault();
  452.  
  453.         window.open("https://twitter.com/intent/tweet?text=" + encodeURIComponent("Veja como educação e esporte no #rio2016transforma podem mudar a sua escola e vote na sua preferida: http://www.rio2016.com/RVZ"), 'sharer', 'toolbar=0,status=0,width=626,height=436');
  454.     });
  455.    
  456.  
  457.  
  458. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement