/*
 * Cervejaria Nacional 
 * by Fernando Arroyo
 * fernando.arroyo@dialeto.net
 */
 
  Interativo = {
    Leia           : {
      Mais        : (function(obj){
       // $(obj).fadeOut(600);
        //$(obj).parent().parent().find('p.oculto').fadeIn(600);
      })
      ,Menos        : (function(obj){
        $(obj).parent().parent().find('p.oculto').fadeOut(600);
        $(obj).parent().parent().find('p[class!="oculto"] > a[class="LeiaMais"]').fadeIn(600);
        $(obj).fadeIn(600);
      })
      ,Cortar       :(function(){
        /* $(".interna_post").each(function(ndex, domEle){
             qtdChar = 0;  
             $(this).children("p").each(function(i){
            	 
                 if( qtdChar > 1000) {  
                    $(this).addClass("oculto");
                 }
                 qtdChar += $(this).text().length;
             });
             if($(".interna_post p").size() > 1 && window.location.href != 'http://www.cervejarianacional.com.br/category/lendas/' && window.location.href != 'http://www.cervejarianacional.com.br/category/fotos-e-videos/')
             {
	             $('.interna_post p:last').append(' <a href="javascript:void(0);" class="LeiaMais" onclick="Interativo.Leia.Mais(this)">Leia mais...</a>');
	         	 $('.interna_post p:last').css("display","inline");
             }
          });*/
      })
    }
    ,Lendas       : {
      Exibe       : (function(i){
          a = true;
          if(!a){}
          $('.imgLenda').each(function(){
              $(this).attr("src",$(this).parent().attr("rel").toString().split('|')[0]);
              $(this).parent().parent().children(".ancLenda").css({display:"none"},(function(){
              }));
          });
          $(".imgLenda:eq("+i+")").css({display:"block"});
            $(".imgLenda:eq("+i+")").attr("src",$(".imgLenda:eq("+i+")").parent().attr("rel").toString().split('|')[1]);
          $(".ancLenda:eq("+i+")").css({display:"block"});
          a = false;
                  
        //document.getElementsByClassName('ancLenda').item(i).style.display='none';
        //document.getElementsByClassName('imgLenda').item(i).src=document.getElementsByClassName('imgLenda').item(i).parentNode.rel.toString().split('|')[1];
//        document.getElementsByClassName('ancLenda').item(i).style.display='block';
      })
      //,Oculta     : (function(i){
      //})
    }
    ,Cardapio     : {
      CriarCardp   : (function(obj){
    	  slug = $(obj).attr('class');
          with( document ){
              __Cardp = createElement("div");
              __CardpTt = [ createElement("span") , createElement("img"), createElement("a")  ];
              __CardpLs = createElement("span");
              with( $(__Cardp).get(0) ){
                  $(__Cardp).attr("id","CardpAbrir");
                  $(__Cardp).css({
                       top:      "300px"
                      ,left:    ( ( $(".meio").width() ) / 2 ) + "px"
                  });
                  with( $(__CardpTt[0]).get(0) ) {
                      setAttribute("class","Tt");
                      with( $(__CardpTt[1]).get(0) ){
                          setAttribute("src","http://www.cervejarianacional.com.br/wp-content/themes/CervejariaNacional/images/"+slug+".png");
                      }
                      with( $(__CardpTt[2]).get(0) ){
                          setAttribute("href","javascript:void(0)");
                          onclick = (function(){
                              $(__Cardp).animate({
                                   opacity:    0
                              },1500,"swing",(function(){
                                  $("#CardpAbrir").remove();
                                  $("div[id!='CardpAbrir']").animate({
                                      opacity: 1
                                  },300);
                              } ) ) } );
                      }
                      appendChild(__CardpTt[1]);
                      appendChild(__CardpTt[2]);
                  }
                  with( $(__CardpLs).get(0) ){
                	  $.get('http://www.cervejarianacional.com.br/busca-cardapio', { post: slug }, function(data){
                		  innerHTML = data;
                	  });
                  }
                  appendChild(__CardpTt[0]);
                  appendChild(__CardpLs);
              }
              with( $("body").get(0) ){
                  appendChild(__Cardp);
                  $(__Cardp).animate({
                       left:    ( ( $(".meio").offset().left ) + 5 ) + "px"
                      ,width:   "762px"
                      ,height:   "680px"
                  },1500,"swing",(function(){
                      $("div[id!='CardpAbrir']").animate({
                          opacity: .65
                      },1200,"swing",(function(){
                          $(__CardpTt).animate({
                              opacity:1
                          },900,"swing",(function(){
                              $("#CardpAbrir").children("span").children("ul").children("li").fadeIn(600);
                          }));
                      }))
                  }));
              }
          }
      })
      ,CriarLista  : (function(Parametros){
        if( (typeof(Parametros.indItens)=="undefined") ) Parametros.indItens = 0;
        $.ajax({
          type: "POST",
          url: Parametros.urlItens,
          data: "i="+Parametros.indItens,
          dataType: "script",
          complete: function(){
              Parametros.Itens = r;
              return Interativo.Cardapio.Listar(Parametros);
          }
        });
      })
      ,Listar      : (function(Parametros){
        $(this.objLista).css({height: 0});
        this.Parametros = Parametros;
        this.objLista = ( ( typeof(this.objLista)!="undefined" ) ? ( this.objLista ) : ( document.createElement("ul") ) );
        with ( this.objLista ){
          while( this.objLista.childNodes.length ) {
            this.objLista.removeChild(this.objLista.childNodes[0]);  
          } 
          innerHTML = "";
          id = "CardapioLista";
          with ( style ){
            overflow = "hidden";
            height   = "0px";
          }
        }
        this.lstItens = Parametros.Itens; 
        for( indLista = Parametros.indItens, cntLista = 0; (( indLista < this.lstItens.length &&  cntLista < Parametros.limItens )); cntLista ++, indLista ++ ){
         this.objItens = [ document.createElement("li"), document.createElement("h4"), document.createElement("a"), document.createElement("p"), document.createElement("a"), document.createElement("hr") ]; 
         with ( this.objLista ) {
           appendChild(this.objItens[0]);
           with ( this.objItens[0] ){
             appendChild(this.objItens[1]);
             appendChild(this.objItens[3]);
               if ( (( indLista < ( this.lstItens.length - 1 ) &&  cntLista < ( Parametros.limItens - 1 ) )) ) appendChild(this.objItens[5]);
             with ( this.objItens[1] ){
               appendChild(this.objItens[2]);
               with ( this.objItens[2] ){
                 setAttribute("href",this.lstItens[indLista].urlTitul);
                 innerHTML = this.lstItens[indLista].txtTitul;
               }
             }
             with ( this.objItens[3] ) {
              appendChild(this.objItens[4]);
               with ( this.objItens[4] ) {
                 setAttribute("href",this.lstItens[indLista].urlDescr);
                 innerHTML = this.lstItens[indLista].txtDescr;
               }
             }
           }
         }
        }
        with ( Parametros.objPai ){
          appendChild(this.objLista);
        }
        with ( Parametros.objBotao[0] ) {
          style.outline = 0;
          setAttribute("rel","indItens="+Parametros.indItens);
          onclick = (function(){
            if( ( parseInt(this.rel.split("=")[1]) > 0 ) ){
              Parametros.indItens = parseInt(this.rel.split("=")[1]) - 1;
            } 
            else{
              Parametros.indItens = ( Parametros.Itens.length -1 );
            }
            this.rel = "indItens=" + Parametros.indItens;
            return Interativo.Cardapio.CriarLista(Parametros);
          });
        }
        with ( Parametros.objBotao[1] ) {
          style.outline = 0;
          setAttribute("rel","indItens="+Parametros.indItens);
          onclick = (function(){
            if( ( parseInt(this.rel.split("=")[1]) < ( Parametros.Itens.length - Parametros.limItens ) ) ){
              Parametros.indItens = parseInt(this.rel.split("=")[1]) + 1;
            }
            else{
              Parametros.indItens = 0;
            } 
            this.rel = "indItens=" + Parametros.indItens;
            return Interativo.Cardapio.CriarLista(Parametros);

          });
        }
        return window.setTimeout("$('#CardapioLista').animate({height: '"+Parametros.altLista+"'},900);",50);
      })
    }
    ,Contato : {
        Radio : (function(){
            $(".inputRadio").click(function(){
                
                $('.'+$(this).attr("class")).val('');
                $(this).val('x');
                $(".inputRadio").removeClass('x');
                $(this).addClass('x');
                $('.inputRadio').parent().css({color:'',fontWeight:'',fontStyle:''})
                $('.'+$(this).attr("class")).css({color:'inherit',fontWeight:'normal',fontStyle:'normal'});
                $('.'+$(this).attr("class")).parent().css({color:'inherit',fontWeight:'normal',fontStyle:'normal'});
                $(this).css({color:'#2b5f1b',fontWeight:'bolder',fontStyle:'oblique'});
                $(this).parent().css({color:'#2b5f1b',fontWeight:'bolder',fontStyle:'oblique'});

            });
        })
        ,Valida : (function(){
        	nome = $('.nome').val();
        	email = $('.email').val();
        	assunto = $('.x').attr('title');
        	mensagem = $('.mensagem').val();
        	url = $('#fContato').attr('action');
        	erro = true;
        	if(nome == '')
        	{
        		erro = false;
        		msg = 'Preencha o nome';
        	}
        	if(assunto == undefined)
        	{
        		erro = false;
        		msg = 'Preencha o assunto';
        	}
        	var er = new RegExp(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/);
        	if(email == '')
        	{
        		erro = false;
        		msg = 'Preencha um email';
        	}
        	if(typeof(email) == "string"){
        		if(!er.test(email))
        		{ 
        			erro = false;
        			msg = 'Preencha com um email válido';
        		}	
        	}
        	if(mensagem == '')
        	{
        		erro = false;
        		msg = 'Preencha a mensagem';
        	}
        	if(erro)
        	{
        		$.post(url, { nome: nome, email: email, assunto: assunto, mensagem: mensagem },
        			   function(data) {
        				 data = data.replace(/^\s+|\s+$/g,"")
        			     if(data == "Mensagem enviada com sucesso")
    			    	 {
        			    	 $('#feedback').css({border: '1px solid #33701f', 
     							color: '#33701f',
     							left: '250px',
     							position: 'absolute',
     							top: '100px'
        			    	 });
        			    	 $('.contato').hide();
        			    	 $('#feedback').fadeIn(function(){
        			    		 $(this).empty();
        			    		 $(this).prepend(data);
        			    	 });
    			    	 } 
        			     else
    			    	 {
        			    	 $('#feedback').css({border: '1px solid #FF0000', 
     							color: '#FF0000',
     							left: '100px',
     							position: 'absolute',
     							top: '554px'
        			    	 });
        			    	 $('#feedback').fadeIn(function(){
        			    		 $(this).empty();
        			    		 $(this).prepend(data);
        			    	 });
    			    	 }
        		});
        	} 
        	else
        	{
        		$('#feedback').css({border: '1px solid #FF0000', 
        							color: '#FF0000',
        							left: '100px',
        							position: 'absolute',
        							top: '554px'
        		});
        		$('#feedback').fadeIn(function(){
		    		 $(this).empty();
		    		 $(this).prepend(msg);
		    	 });
        	}
        })
    }
  };
