var baseurl = "http://"+location.host;

  $(function()
  {    
    var pesq_common_funcs=function()
    {      
      $.extend(DateInput.DEFAULT_OPTS, 
      {
        stringToDate: function(string) 
        {
          var matches;
          if (matches = string.match(/^(\d{4,4})-(\d{2,2})-(\d{2,2})$/)) 
          {
            return new Date(matches[1], matches[2] - 1, matches[3]);
          } 
          else 
          {
            return null;
          };
        },
      
        dateToString: function(date) 
        {
          var month = (date.getMonth() + 1).toString();
          var dom = date.getDate().toString();
          if (month.length == 1) month = "0" + month;
          if (dom.length == 1) dom = "0" + dom;
          return date.getFullYear() + "-" + month + "-" + dom;
        },
        
        month_names: ["Janeiro", "Fevereiro", "Março", "Abril", "Maio", "Junho", "Julho", "Agosto", "Setembro", "Outubro", "Novembro", "Dezembro"],
        short_month_names: ["Jan", "Fev", "Mar", "Abr", "Mai", "Jun", "Jul", "Ago", "Set", "Out", "Nov", "Dez"],
        short_day_names: ["Dom", "Seg", "Ter", "Qua", "Qui", "Sex", "Sab"]
      });
      
      //$(".input_date_picker").date_input();
      $("#p_limpar_datas").click(function()
      {
        $('#input_data_ini').val('');
        $('#input_data_fim').val('');
      });          
      //---------------------
      $("#select_distrito").change(function()
      {
          var id_distrito=$(this).val();         
          if(id_distrito==0)
            $("#select_concelho").attr("disabled","disabled");
          else
          {
            $.ajax(
            {
              type:'GET',
              url: baseurl+'/?p=index&ajax=1',
              data:'function=get_concelhos&id_distrito='+id_distrito,
              success:function(data)
              {
                if(data!='')
                {
                  $("#select_concelho").removeOption(/./);
                  $("#select_concelho").addOption(0,'Concelho');
                  $("#select_concelho").removeAttr("disabled");
                  var result=data.split('|');              
                  var resgisto;
                  for(var i=0; i<result.length; i++)
                  {
                    registo=result[i].split(':');
                    $("#select_concelho").addOption(registo[0],registo[1]);
                  }
                                    
                  $("#select_concelho option:first").attr('selected','selected');                                      
                  $('#select_concelho option').each(function()
                  {
                    if(gup('select_concelho')==$(this).val() && id_distrito==gup('select_distrito'))
                      $(this).attr('selected','selected');
                    else                                  
                      $(this).removeAttr('selected');        
                  });
                }
              }
      		  });
      		}
      });
            
      $(".mais_menos").click(function()
      {                                            
        $(this).parent().next(".div_hidden_feature").toggle();
        if($(this).text()=='+')
        {
          $(this).text('-');                    
          $(this).parent().next('.div_hidden_feature').children().children('input').each(function()
          {            
            $(this).removeAttr('disabled');
          });          
        }
        else
        {
          $(this).text('+');
          $(this).parent().next('.div_hidden_feature').children().children('input').each(function()
          {            
            $(this).attr('disabled','disabled');
          });
        }        
      });
      
      $('#select_distrito option:first').removeAttr('selected','selected');
      
      $('#select_distrito option').each(function()
      {
        if(gup('select_distrito')==$(this).val())
        {
          $(this).attr('selected','selected');
          $('#select_distrito').trigger('change');
        }
      });
    }
    
    var muda_pesquisa=function(value)
    {
      $("#div_pesq_toggle").html('<img src="'+baseurl+'/images/ajax-loader.gif" alt="A Carregar..."/>');
      $.ajax(
      {
        type:'GET',
        url: baseurl+'/?p=index&ajax=1',
        data:'function=toogleSearch&id='+value,
        success:function(data)
        {
          if(data!='')
          {
            $("#div_pesq_toggle").html(data);
            pesq_common_funcs();
          }
        }
		  });
    };
        
    /*********************************/
    pesq_common_funcs();
    
    var tab_seleccionada=0;
    
    $(this).addClass("tab_selected");
                
    $('.p_radio_section_choice input:radio').each(function()
    {            
      if($(this).val()==gup('radio_section_choice') || (strpos(window.location.href,'/muito-bom') && $(this).val()==2) || (strpos(window.location.href,'/restaurantes') && $(this).val()==5) || (strpos(window.location.href,'/alojamentos') && $(this).val()==6) || (strpos(window.location.href,'/cartaz') && $(this).val()==3) || ((strpos(window.location.href,'/tarde') || strpos(window.location.href,'/noite')) && $(this).val()==20))
      {
        $(this).attr('checked','checked');
        $("#p_titulo_pesquisa").text('» '+$(this).siblings('label').children('b').text());
        $("#p_titulo_pesquisa").css('color',$(this).next('input:hidden').val());        
        tab_seleccionada=1;        
      }
    });
    
    (tab_seleccionada==0?$("#td_tab_1").addClass("tab_selected"):$("#td_personalize").addClass("tab_selected"));    
    
    $('#pesq_global_tabs_table td.tab_unselected').click(function()
    {
      var item_selected=$('#pesq_global_tabs_table td.tab_selected');
      
      item_selected.removeClass();
      item_selected.addClass('tab_unselected');
      $(this).removeClass();
      $(this).addClass("tab_selected");
      
      if($(this).attr("id")=="td_personalize")
      {
        if($(this).children('img').attr('src')== baseurl+'/images/open_arrow.jpg')
          $(this).children('img').attr('src', baseurl+'/images/closed_arrow.jpg');
        else
          $(this).children('img').attr('src', baseurl+'/images/open_arrow.jpg');                        
        $("#div_items_pesquisa_personalizada").toggle();
      }
      else
      {
        if($("#td_personalize").children('img').attr('src')== baseurl+'/images/open_arrow.jpg')
          $("#td_personalize").children('img').attr('src', baseurl+'/images/closed_arrow.jpg');
        muda_pesquisa(0);
        $("#p_titulo_pesquisa").text('');
        $("#div_items_pesquisa_personalizada input").removeAttr('checked');
        $("#div_items_pesquisa_personalizada").hide();
      }
    });
   
    $("#input_pesq_global").bind("focus",function() 
		{ 
      if($(this).val()=="pesquise por texto livre")
				$(this).val("");
		});
		$("#input_pesq_global").bind("blur",function() 
		{ 
			if($(this).val()=="")
				$(this).val("pesquise por texto livre");
		});
		
    $("#form_pesquisa_global").submit(function()
    {
      if($("#input_pesq_global").val()=="pesquise por texto livre")  
        $("#input_pesq_global").attr('disabled','disabled');      
    });
    
    $("#div_items_pesquisa_personalizada input").click(function()
    {
      var value=$(this).val();
      if($("#td_personalize").children('img').attr('src')== baseurl+'/images/open_arrow.jpg')
        $("#td_personalize").children('img').attr('src', baseurl+'/images/closed_arrow.jpg');
      $("#div_items_pesquisa_personalizada").hide();      
      $("#p_titulo_pesquisa").text('» '+$(this).siblings('label').children('b').text());
      $("#p_titulo_pesquisa").css('color',$(this).next('input:hidden').val());
      muda_pesquisa(value);
    });
  });  
