// General slider functions
function setupSliderControls(slider, container, width) {
  var slider = $(slider);
  $(container + " div.previous").addClass("disabled");
  if (slider.width() == width) $(container + " div.next").addClass("disabled");
}
function checkSliderControls(slider, container, width) {
  var slider = $(slider);
  slider.left = parseInt(slider.css('marginLeft').replace('px', ''));
  if (slider.width() == slider.closest('div').width()) {
    $(container + " div.previous").addClass("disabled");
    $(container + " div.next").addClass("disabled");
  } else {
    (slider.left == 0) ? $(container + " div.previous").addClass("disabled") : $(container + " div.previous").removeClass("disabled");
    (Math.abs(slider.left) == slider.width() - width) ? $(container + " div.next").addClass("disabled") : $(container + " div.next").removeClass("disabled");
  }
}
function checkThumbnails(slider, thumbnails, width) {
  var thumbnails = $(thumbnails),
      slider = $(slider);
  slider.left = parseInt(slider.css('marginLeft').replace('px', ''));
  thumbnails.removeClass('current').eq((Math.abs(slider.left)/width)).addClass('current');
}

// Animate the homepage slides
function animateSlides() {
  var slider = $("#home-features .slider ul"),
      thumbnails = $("#home-features .thumbnails li");
  var currentIndex = thumbnails.index($("#home-features .thumbnails li.current"));
  var targetIndex = (currentIndex + 1 == thumbnails.length) ? 0 : thumbnails.index($("#home-features .thumbnails li.current")) + 1;
  slider.animate({marginLeft: (targetIndex * -840)}, 500, function() {
    checkSliderControls("#home-features .slider ul", "#home-features", 840)
    checkThumbnails(slider, "#home-features .thumbnails li", 840);
  });
}
var animatedSlides;

$(document).ready(function() {
  
  // Help popups for specifications
  $('table.with-help dt span').hover(
    function() {
      $(this).closest('dt').next('dd').show();
    },
    function() {
      $(this).closest('dt').next('dd').hide();
    }
  );
  
  // Controls for the colored fronts flipper
  var coloredSrc = $("#colored-fronts img").attr("src");
  $("#colored-fronts li:first").addClass("current");
  // -- preload the images
  $("#colored-fronts a").each(function() {
    $('<img src="' + $(this).attr("href") + '" alt="">').appendTo("body").hide();
  });
  $("#colored-fronts a").hover(
    function() {
      $("#colored-fronts img").attr("src", $(this).attr("href"));
    },
    function() {
      $("#colored-fronts img").attr("src", coloredSrc);
    }
  );
  $("#colored-fronts a").click(function(event) {
    event.preventDefault();
    coloredSrc = $(this).attr("href");
    $("#colored-fronts img").attr("src", $(this).attr("href"));
    $(this).closest("li").addClass("current").siblings().removeClass("current");
  });

  // Tabs for the FAQs page
  $("#page-support-faqs .tabs").each(function() {
    var container = $(this);
    var tab_controls = $('<ul class="faq-tabs"></ul>').prependTo(container);
    var tab_content = $(this).find('div');
    $(this).find("h3").each(function() {
      var title = $(this).text();
      var link = title.toLowerCase().replace(/ /g, "-");
      $('<li><a href="#' + link + '">' + title + '</a></li>').appendTo(tab_controls);
      $(this).hide();
    });
    tab_controls.find("a").click(function(event) {
      event.preventDefault();
      tab_content.hide();
      $($(this).attr("href").replace(/^.*?(#)/, '#')).show();
      $(this).closest("li").addClass("current").siblings().removeClass("current");
    });
    tab_content.not(":first").hide();
    tab_controls.find("li:first-child").addClass("current");
  });

  // Showing and hiding the detailed specifications
  $(".specifications tr.details").hide();
  $(".specifications tr.summary th").attr('rowspan', '1');
  $(".specifications tr.summary").click(function() {
    if ($(this).next('tr').hasClass('details') &! $(this).hasClass('expanded')) {
      $(this).addClass('expanded');
      $(this).find('th').attr('rowspan', '2');
      $(this).next('tr').show();
    } else if ($(this).next('tr').hasClass('details') && $(this).hasClass('expanded')) {
      $(this).removeClass('expanded');
      $(this).find('th').attr('rowspan', '1');
      $(this).next('tr').hide();
    }
  });
  
  // -- Expand the row for the product in the document hash, if defined
  if ($('.specifications').length > 0 && document.location.hash != '') {
    $('#' + document.location.hash.replace(/#/, '').toUpperCase()).addClass('expanded').find('th').attr('rowspan', '2').end().next('tr').show();
  }
  
  // Find Your Friedrich: click on a specification row and get taken to the right place
  $(".find-your-friedrich table tbody tr").click(function() {
    document.location = "http://" + document.location.hostname + $(this).find('th[scope="row"] a').attr("href");
  });
  
  // Home page sliders
  if ($("body").attr("id") == "page-homepage" && !($.browser.msie && $.browser.version == "6.0")) {
    // setup
    $("body").addClass("has-js");
    $('<div class="previous"></div><div class="next"></div>').appendTo("#home-features");
    $('<ol class="thumbnails"></ol>').appendTo("#home-features");
    $("#home-features .slider > ul > li").each(function() {
      $('<li></li>').appendTo('#home-features .thumbnails');
    });
    $("#home-features .thumbnails li:first-child").addClass('current');
    $("#home-features .slider > ul").each(function() {
      $(this).css("width", (Math.ceil($(this).children("li").length) * 840));
      setupSliderControls(this, "#home-features", 840);
    });
    // arrow control behavior
    $("#home-features .previous").click(function() {
      var slider = $("#home-features .slider ul"),
          previous = $(this),
          next = $("#home-features .next");
      slider.left = parseInt(slider.css('marginLeft').replace('px', ''));
      clearInterval(animatedSlides);
      if (!slider.is(":animated") && slider.left < 0) {
        (slider.left + 840 == 0) ? previous.addClass("disabled") : previous.removeClass("disabled");
        if (slider.left < 0) next.removeClass("disabled");
        slider.animate({marginLeft: (slider.left + 840)}, 500, function() {
          checkThumbnails(slider, "#home-features .thumbnails li", 840);
        });
      }
    });
    $("#home-features .next").click(function() {
      var slider = $("#home-features .slider ul");
      var previous = $("#home-features .previous");
      var next = $(this);
      clearInterval(animatedSlides);
      slider.left = parseInt(slider.css('marginLeft').replace('px', ''));
      if (!slider.is(":animated") && ((slider.left == 0 && slider.width() > 840) || Math.abs(slider.left) < (slider.width() - 840))) {
        if (slider.left == 0) previous.removeClass("disabled");
        (Math.abs(slider.left) + 840 == slider.width() - 840) ? next.addClass("disabled") : next.removeClass("disabled");
        slider.animate({marginLeft: (slider.left - 840)}, 500, function() {
          checkThumbnails(slider, "#home-features .thumbnails li", 840);
        });
      }
    });
    // thumbnail control behavior
    $("#home-features .thumbnails li").click(function() {
      var slider = $("#home-features .slider ul");
      var targetLeft = $("#home-features .thumbnails li").index($(this)) * -840;
      clearInterval(animatedSlides);
      slider.left = parseInt(slider.css('marginLeft').replace('px', ''));
      if (targetLeft != slider.left) {
        $(this).addClass("current").siblings().removeClass("current");
        slider.animate({marginLeft: (targetLeft)}, 500, function() {
          checkSliderControls("#home-features .slider ul", "#home-features", 840)
        });
      }
    });
    // Animate them!
    animatedSlides = setInterval('animateSlides()', 9000);
  }
  
  // Product page image gallery
  if ($("#product-shots").length == 1) {
    // setup
    $("body").addClass("has-js");
    $('<div class="previous"></div><div class="next"></div>').appendTo("#thumbnails");
    $("#thumbnails .slider").not(":first").hide();
    $("#thumbnails ul").each(function() {
      $(this).css("width", (Math.ceil($(this).children("li").length / 3) * 399));
      setupSliderControls(this, "#thumbnails", 399);
    });
    $("#product-shots > img").attr("id", "hero_1").addClass("hero");
    $("#thumbnails #images li").not(":first").each(function() {
      var target = $(this).find("a").attr("href");
      var index = $("#thumbnails #images li").index($(this)) + 1;
      $('<img src="' + target + '" width="400" height="340" alt="" id="hero_' + index + '" class="hero">').prependTo("#product-shots").hide();
    });
    // $("#thumbnails h3:first").addClass("current");
    $("#thumbnails li:first").addClass("current");
    // image thumbnail behavior
    $("#thumbnails #images li").click(function(event) {
      var target = $(this).find("a").attr("href");
      var index = $("#thumbnails #images li").index($(this)) + 1;
      var visibleHero = $("#product-shots > .hero:visible");
      var targetHero = $("#product-shots > img#hero_"  + index);
      if (!(visibleHero.attr("id") == targetHero.attr("id") && visibleHero.not(":animated"))) {
        $("#thumbnails li").removeClass("current");
        $(this).addClass("current");
        visibleHero.fadeOut(750);
        $("#product-shots > img#hero_"  + index).fadeIn(750);
      }
    });
    $("#thumbnails #images li a").click(function(event) {
      event.preventDefault();
    });
    // slider control behavior
    $("#thumbnails .previous").click(function() {
      var visibleSlider = $("#thumbnails .slider:visible ul");
      var previous = $(this);
      var next = $("#thumbnails .next");
      visibleSlider.left = visibleSlider.position().left;
      if (!visibleSlider.is(":animated") && visibleSlider.left < 0) {
        (visibleSlider.left + 399 == 0) ? previous.addClass("disabled") : previous.removeClass("disabled");
        if (visibleSlider.left < 0) next.removeClass("disabled");
        visibleSlider.animate({left: (visibleSlider.position().left + 399)}, 500);  
      }
    });
    $("#thumbnails .next").click(function() {
      var visibleSlider = $("#thumbnails .slider:visible ul");
      var previous = $("#thumbnails .previous");
      var next = $(this);
      visibleSlider.left = visibleSlider.position().left;
      if (!visibleSlider.is(":animated") && ((visibleSlider.left == 0 && visibleSlider.width() > 399) || Math.abs(visibleSlider.left) < (visibleSlider.width() - 399))) {
        if (visibleSlider.left == 0) previous.removeClass("disabled");
        (Math.abs(visibleSlider.left) + 399 == visibleSlider.width() - 399) ? next.addClass("disabled") : next.removeClass("disabled");
        visibleSlider.animate({left: (visibleSlider.position().left - 399)}, 500);
      }
    });
  }
  
  // Find Yours Switcher
  if(window.location.pathname.indexOf('find-your-friedrich') != -1) {
    $("#product-nav-tabs a.find").closest('li').addClass('current');
    $("#product-navigation > div").hide().filter(":first").show();
  } else {
    $("#product-nav-tabs a.browse").closest('li').addClass('current');
  }
  $("#product-nav-tabs a").click(function(event) {
    event.preventDefault();
    if ($(this).hasClass('find')) {
      $("#product-navigation > div").hide().filter(":first").show();
    } else {
      $("#product-navigation > div").hide().filter(":last").show();
    }
    $(this).closest('li').addClass('current').siblings().removeClass('current');
  });
  
  
  // FAQs Expanding
  $('.faq-list h2').click(function(event) {
  	$(this).closest('.faq-list').toggleClass('expanded');
  	/*$(this).siblings('ul').slideToggle(400);*/
  });
  
  // Employment Application Expanding
  $('#apply h2').click(function(event) {
  	$(this).closest('#apply').toggleClass('expanded');
  	/*$(this).siblings('ul').slideToggle(400);*/
  });
  
});
