var img_ambiente_actual;
var img_ambiente_menor;
var img_ambiente_maior;

function alteraImagemAmbiente(i) {
  if (i != -1) {
    $('.imagem_ambiente#imagem_ambiente_' + (((i+1) > img_ambiente_maior) ? img_ambiente_menor : i+1)).fadeIn("slow");
    $('.imagem_ambiente#imagem_ambiente_' + i).fadeOut("slow",
      function() {
        i = ((i+1) > img_ambiente_maior) ? img_ambiente_menor : i+1;
        setTimeout("alteraImagemAmbiente(" + i + ")", 7000);
      }
    );
  }
  else {
    i = ((i+1) > img_ambiente_maior) ? img_ambiente_menor : i+1;
    setTimeout("alteraImagemAmbiente(" + i + ")", 7000);
  }
}

var selectsubfamiliaclone;
function filtersubfamilia(optionid) {
  $("#selectsubfamilia option:not([@value=''])").remove();
  $("#selectsubfamilia").append($('option.' + optionid, selectsubfamiliaclone.clone()));              
  $("#selectsubfamilia").each(
    function() {
      this.selectedIndex = 0;
    }
  );
}
$(document).ready(
  function() {  
    selectsubfamiliaclone = $("#selectsubfamilia").clone();
    $("#selectfamilia option").each(
      function() {
        if (this.selected) {
          var optionid = $(this).attr('id');
          filtersubfamilia(optionid);
          return false;
        }
      }
    );
    $("#selectfamilia").change(
      function() {
        $('option', $(this)).each(
          function() {
            if (this.selected) {
              var optionid = $(this).attr('id');
              filtersubfamilia(optionid);
              return false;
            }
          }
        );
      }
    );
    $('.btFavoritos').click(
      function() {
        if (window.sidebar) {
          window.sidebar.addPanel(document.title, location.href , "");
        } 
        else if( window.external ) {
          window.external.AddFavorite(location.href, document.title); 
        }
        return false;
      }
    );
    $('.imagem_ambiente.nothidden').each(
      function() {
        img_ambiente_actual = $(this).attr('id').replace(/imagem_ambiente_/, '');
        img_ambiente_maior = img_ambiente_actual;
        img_ambiente_menor = img_ambiente_actual;
      }
    );
    
    $('.imagem_ambiente').each(
      function() {
        var i = $(this).attr('id').replace(/imagem_ambiente_/, '');
        if (i > img_ambiente_maior) {
          img_ambiente_maior = i;
        }
        if (i < img_ambiente_menor) {
          img_ambiente_menor = i;
        }
      }
    );
    if (img_ambiente_maior > img_ambiente_menor)
      alteraImagemAmbiente(-1);
    $('.autohide').each(
      function() {
        $(this).show();
        $('input#' + $(this).attr('for')).focus(
          function() {
            $('label[@for=' + $(this).attr('id') + ']').hide();
          }
        );
        $('input#' + $(this).attr('for')).blur(
          function() {
            if ($(this).val() === '') {
              $('label[@for=' + $(this).attr('id') + ']').show();
            }
          }
        );
        $('textarea#' + $(this).attr('for')).focus(
          function() {
            $('label[@for=' + $(this).attr('id') + ']').hide();
          }
        );
        $('textarea#' + $(this).attr('for')).blur(
          function() {
            if ($(this).val() === '') {
              $('label[@for=' + $(this).attr('id') + ']').show();
            }
          }
        );
      }
    );
  }
);
