// Used http://jscompress.com to minify JS file.

// ===========================================================================
// PAGE LOAD SETUP
// ===========================================================================

$(document).ready(function () {
  setCancelLinks();
  setEmptyInputs();
  
  // Newsletter form setup
    if ($('#subForm').length == 1) {
      if (findPageLanguage() == "fr") {
        $("#klihtk-klihtk").focus(function() {
          $('#subForm div.not-required:first:hidden').slideToggle();
        });
      } else {
        $("#ujitdy-ujitdy").focus(function() {
          $('#subForm div.not-required:first:hidden').slideToggle();
        });
      }
    }
  
  // Digestors Schema Setup
    if ($("#schema").length >= 1 && $.browser.msie && $.browser.version.substr(0,1) < 7) {
      $("#schema li").mouseover(function() {
        $(this).css("background-position","right top").find("p:first").toggle();
      });
      $("#schema li").mouseout(function() {
        $(this).css("background-position","left top").find("p:first").toggle();
      });
    }
  
  // Installations sheet setup
    if ($("#wrapper").hasClass("installations")) {
      setInstallationSheets();
    }
});

// ***************************************************************************
// SHARED FUNCTIONS
// ***************************************************************************

// ===========================================================================
// Erase input content on focus
// ===========================================================================

function setEmptyInputs() {
  var inputs = $(".newsletter input[type=\"text\"]");
  
  inputs.each(function() {
    if ($(this).attr("value") != "") {
      $(this).click(function() {
        return clearInput(this);
      });
    };
  });
};

function clearInput(input) {
  $(input).attr("value","");
};

// ===========================================================================
// Form 'Cancel' Links Setup
// ===========================================================================

function setCancelLinks() {
  if ($('.form-reset').length >= 1) {
    var triggers = $('.form-reset');

    triggers.each(function() {
      $(this).click(function() {
        return cancelForm(this);
      });
    });
  };
};

function cancelForm(trigger) {
  $(trigger).parents("form")[0].reset();
  
  if ($(trigger).parents("form:first").children("div.not-required:visible").length >= 1) {
    $(trigger).parents("form:first").children("div.not-required").slideToggle();
  }
  
  return false;
};

// ===========================================================================
// FIND PAGE LANGUAGE
// Find page language based on the body class.
// ===========================================================================

function findPageLanguage() {
  var pageLanguage = $("body:first").attr("class");
  return pageLanguage;
}

// ***************************************************************************
// SPECIFIC FUNCTIONS
// ***************************************************************************

// ===========================================================================
// INSTALLATION EXPERIENCE
// Display each plant as a separated experience
// ===========================================================================

function setInstallationSheets() {
  $("#content div.installation")
    .addClass("paginated")
    .find("p.return").hide();
  
  if (window.location.hash) {
    var hash = window.location.hash;
    hash = hash.replace(/#/, '');
    $("#content div.installation")
      .not("#" + hash + "-sheet")
        .hide();
    $("#" + hash).addClass("selected");
  } else {
    $("#sheet-switcher a:first").addClass("selected");
    $("#content div.installation")
      .not(":first")
        .hide();
  }
  
  $("#sheet-switcher a").click(function(e) {
    e.preventDefault();
    var newSheetId = $(this).attr("id");
    window.location.hash = newSheetId;
    
    $("#sheet-switcher a.selected:first").removeClass("selected");
    $(this).addClass("selected");
    
    $("#content div.installation:visible:first").toggle();
    $("#"+newSheetId+"-sheet").toggle();
  });
}
