$(document).ready(function() {

  // Search submit
  $("#search-box .submit").click(function(e) {
    e.preventDefault();
    $(this).parent().submit();
  });
  
  // Newsletter form submit
  var $newsletter = $("#newsletter-form");
  $(".send_button", $newsletter).click(function(e) {
    e.preventDefault();
    $newsletter.parent().submit();
  });

  newSlideTabs();
  
  // Download brochure popup
  $("#download-brochures").fancybox({
    'width'        : 650,
    'height'      : 630,
    'autoScale'       : false,
    'transitionIn'    : 'elastic',
    'transitionOut'    : 'elastic',
    'type'        : 'iframe',
    'scrolling'      : 'no',
    'padding'      : 0,
    'margin'      : 0
  });
  
  // Clear textbox and return to default (focus/blur)
  $(".textbox-auto-clear").each(function(){
    var searchOrigVal = $(this).val(); // Store the original value
    $(this).focus(function(){
      if($(this).val() == searchOrigVal) {
        $(this).val('');
      }
    });
    $(this).blur(function(){
      if($(this).val() == '') {
        $(this).val(searchOrigVal);
      }
    });
  });
});

// Top navigation
$(function() {
    $("#nav li").hover(function() {
        $(this).children("a").addClass("hover");
        $(this).parent().children("a").addClass("hover");
        $("ul:first", this).css('visibility', 'visible');
    }, function() {
        $(this).children("a").removeClass("hover");
        $(this).parent().children("a").removeClass("hover");
        $("ul:first", this).css('visibility', 'hidden');
    });

    $("#right-nav li").hover(function() {
        $(".sub-nav", this).slideDown(300);
    }, function() {
        $(".sub-nav", this).slideUp(300);
    });
});

// Slide Tabs
function newSlideTabs() {
  var $slideshow = $("#slideshow");
  var $tabsWrap = $("#tabs");
  var $tabs = $tabsWrap.children("li");
  var tabCount = $tabs.size();
  var tabWidth = $tabs.css("width").replace("px","")
  var openWidth = $tabsWrap.width() - ((tabCount-1) * tabWidth);
  
  $tabs.each(function(i) {
    var $this = $(this);
    var id = $this.attr("id");

    $this.css({
      "z-index": tabCount-i
    });
    
    $this.hover(function() {
      $this.stop().animate({
        width: openWidth
      }, 200);
      $slideshow.hide();
      
      
    }, function() {
      $this.stop().animate({
        width: tabWidth
      }, 200);
      $slideshow.delay(1000).show();
    });
    
  });
}

(function($){
  $.fn.simplestSlideShow = function(settings){
    var config = {
      'timeOut': 3000,
      'speed': 'normal'
    };
    if (settings) $.extend(config, settings);
    this.each(function(){
      var $elem = $(this);
      $elem.children(':gt(0)').hide();
      setInterval(function(){
        $elem.children().eq(0).fadeOut(config['speed'])
        .next().fadeIn(config['speed'])
        .end().appendTo($elem);
      }, config['timeOut']);
    });
    return this;
  };
})(jQuery);


// Scroll Top 

$(document).ready(function(){
  
  // Scroll page to the top
  $('a.back-top').click(function(){
    
    $('html, body').animate({scrollTop:0}, 'slow');
    
    return false;
  
  });

})


