// JavaScript OnLoad Document for KAO Ban website
$(document).ready(function (){
  $.preLoadImages('/images/products_solids_over.jpg','/images/products_roll-ons_over.jpg','/images/logos_all_over.jpg');

  // Hide FAQ elements
  $("#faq_qa h2, #faq_qa dl").hide();
  $("#faq_qa h2.how_work, #faq_qa dl.how_work").show();

  // Form page fixes
  $(".submit").attr({value: ''});
  $(".eventPage input[type='submit']").attr({value: ''});

  // Setup lightbox for charts
  $("a[href='#charts']").click(function(e){
    e.preventDefault();
    $("#charts a").colorbox({
      open: "true",
      rel: "chartphotos",
      current:"Chart {current} of {total}",
      scrolling: "true"
    });
    $('html, body').animate({scrollTop: "100px"}, 1500);
    $('html, body').mousewheel(function(){$('html, body').stop();});
  });
  $().bind('cbox_complete', function(){
    $('html, body').animate({scrollTop: "100px"}, 1500);});
  $().bind('cbox_closed', function(){
    $('html, body').animate({scrollTop: "0px"}, 500);});

  if (document.getElementById("theaterNav")) {
    // Home page theater area
    $("#theaterNav li").featureList({
      output : '#theaterMain li',
      start_item : 0,
      stop_on_click : true,
      tabChanged_func : false,
      transition_interval : 5000,
      fade_duration : 1800
    });
  };
  
  // FAQ page events
  $("#faq_sidebar li a").click(function(e){
    e.preventDefault();
    $("#faq_sidebar li").removeClass("active");
    $(this).parent().addClass("active");
    var faqtoshow = $(this).attr("href");
    faqtoshow = "."+faqtoshow.substring(1);
    $("#faq_qa h2, #faq_qa dl").hide();
    $("#faq_qa h2"+faqtoshow).show();
    $("#faq_qa dl"+faqtoshow).show();
  });
  $("#faq_qa h2 a").click(function (e){
    e.preventDefault();
    $("#faq_sidebar li").removeClass("active");
    $("#faq_sidebar li#fn_work").addClass("active");
    $.scrollTo(0);
  });
});

(function($) { //Image preloader
  var cache = [];
  // Arguments are image paths relative to the current page.
  $.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }
})(jQuery)

