jQuery(document).ready(function($) {
  $('ul#nav').supersubs({
    minWidth: 8,
    maxWidth: 12,
    extraWidth: 1
  }).superfish({
    autoArrows: false,
    dropShadows: false
  });

  var cut_up_slide = function(index) {
    /* cut up the slide if it hasn't been cut up yet */
    var slide = $("#hero .slide").eq(index);
    var img_placeholder = slide.find('.img-placeholder');
    if (img_placeholder.length) {
      six_photos = "<div class='six_photos'>";
      for (var i=0;i<6;i++) {
        six_photos += "<div class='slide-img-wrapper index-"+i+"'><div class='slide-img' style='background-image:url("+img_placeholder.attr('data-img')+")'></div><div class='slide-img-style_hook style_hook'></div></div>";
      }
      six_photos += "</div>";
      img_placeholder.after(six_photos);
      img_placeholder.remove();
      
    }
  };
  // cycle the hero slides
  // http://malsup.com/jquery/cycle/options.html
  if ($("#hero .slide").length >= 2) {
    var initSlide = 1; // don't change logo bg if true
    $("#hero .slides").cycle({
      fx: 'fade',
      speed: 'slow',
      pause: 0, // pause on mouse hover
      pager: '.hero-slides-nav', // sidebar to main image area
      pagerAnchorBuilder: function(index, e){
        return '.hero-slides-nav .slide-nav:eq('+index+')';
      },
      before: function(currSlide, nextSlide, opts, forward) {
            if (!initSlide) {
              cut_up_slide(opts.nextSlide);
            } else {
              initSlide = 0;
            }
        },
      allowPagerClickBubble: 1,
      timeout: 20000,
      fastOnEvent: 0,
      sync: 1,
      containerResize: 0
    });
  }

  // sidekick promo front/back display
  promo_flip_delay = 300;
  // any sidekick_promo that has .front and .back as children
  var flippable_promo = $(".sidekick_promo > .front + .back").parent();
  flippable_promo.each(function(e){
    // The .back element needs to match the dimensions of the .front.
    var f = $(this).children(".front");
    $(this).children(".back").css({
      "width":f.css("width"),
      "height":f.css("height")
    });
  });
  flippable_promo.hover(function(){
    var time = new Date();
    $(this).data('last_shown', time.getTime());
    $(this).children(".front").css({"z-index":1});
  }, function(){
    var promo = $(this);
    var hide_promo = function() {
      var time = new Date();
      var t = time.getTime() - promo.data("last_shown");
      if (t > promo_flip_delay) {
        promo.children(".front").css({"z-index":5});
      }
    };
    setTimeout(hide_promo, promo_flip_delay);
  });

  // tabbed child promos
  $(".tabbed_child_promos").tabs();


  // colorbox http://colorpowered.com/colorbox/
  $("a#webcam-photo-link").galleria_colorbox({
    photo: true
  });
  $("a.mediagallery-picture-link").galleria_colorbox({
    rel: 'a.mediagallery-picture-link',
    photo: true,
    innerWidth: 660, /* photos are 600x400 and left/right flippers are 30px wide */
    innerHeight: 400,
    scalePhotos: false,
    current: "{current}/{total}",
    title:function(){
      var image_block = $(this).parents("div.mediagallery-picture");
      var out = document.createElement('div');
      out.id = 'galleria_cboxTitle-inner';
      image_block.find(".js_display").each(function(e){
        var e = $(this).clone();
        $(out).append(e);
      });
      var div = document.createElement('div');
      $(div).append($(out));
      return $(div).html();
    }
  });
  
  
  var social_widget = $('div#social_widget');
  var top = social_widget.offset().top - parseFloat(social_widget.css('margin-top').replace(/auto/, 0));
  $(window).scroll(function (event) {
    var y = $(this).scrollTop();
    if (y >= top-20) {
      social_widget.addClass('fixed');
    } else {
      social_widget.removeClass('fixed');
    }
  });  
  
  $('div#social_widget a').click(function(){
    social_widget.animate({left: '-=172'});
  });
  
  social_widget.hover(
    function(){
      left = social_widget.css('left');
      if(parseInt(left) == -172) {
        social_widget.animate({left: '+=172'},{
          duration: 250, 
          specialEasing: {
            width: 'swing',
            height: 'easeOutBounce'
          }
        });
      }
    }, 
    function(){
      left = social_widget.css('left');
      if(parseInt(left) == 0) {
        social_widget.animate({left: '-=172'},{
          duration: 250, 
          specialEasing: {
            width: 'swing',
            height: 'easeOutBounce'
          }
        });
      }
    }
  ); /* end social_widget.hover */
  
  /* Clear search form onfocus */
  $('input.header-search-input').focus(function(){
    $(this).attr('value','');
  });
  
  /* Make cams default */
  $('ul.tab-menu li').eq('0').removeClass('ui-tabs-selected ui-state-active');
  $('ul.tab-menu li').eq('1').addClass('ui-tabs-selected ui-state-active');
  $('div#pics, div#videos').addClass('ui-tabs-hide');
  $('div#webcam').removeClass('ui-tabs-hide');
  
  /* Woodward link opens in a new window */
  $('#nav-woodward a').attr('target','_blank');
  
  /* terrain park link open in new window */
  $('li#nav-terrain-parks ul li.navmenu-item2 a').eq(3).attr('target','_blank');
  
});

