jQuery(function(){
  $('#jquery_newsticker').hide(0);

  // cache some selectors for later
  var jumbotron = $('#jumbotron');
  var slides_container = $('#jt-slides');
  var items = $('.jt-slide');
  var hfw = $('#home-footer-wrapper');
  var fob = $('#footer-toggle');

  var slide_width = 0;
  var slide_heigth = 0;
  var slide_count = items.length;
  var slides_total_width = 0;
  var now_showing = 0;
  var current = 0;
  var min_width = 960;
  var min_height = 500; 
  var slip = 0;
    
  calculateSize();
  checkOrientation();
  $("#jquery_newsticker").show();
  $("#jquery_newsticker").newsticker();

  var jumbo = jumbotron.serialScroll({
    target: slides_container,
    items: items,
    navigation: '.jt-nav a',
    axis: 'x',
    duration: 500,
    constant: false,
    force: true,
    //interval: 10000,
    onBefore: function(){
      $('.jt-nav a').removeClass('button-on');
      $('.forced-show').hide(function(){
        fob.show("slow");
      });
    },
    onAfter: function(i){
      wiggle(300);
      current = items.index(i); 
      $('.jt-nav a').eq(current).addClass('button-on');
      now_showing = current;
    }
  });


  function wiggle(delay){
    //Wiggle the slider back into place after the transition
    slip = $(window).scrollLeft();
    if(slip > 0){
      $.scrollTo(0, delay, {
        axis: 'x'
      });
    }
  }

  function calculateSize(){
    slide_width = $(window).width();
    slide_height = $(window).height() - 95; //accounts for the height of the header

    if ((slide_width > min_width) && (slide_height > min_height)) {
      $('body').addClass('no-scrollbars');
      slide_width = $(window).width();
      slide_height = $(window).height() - 95; //accounts for the height of the header
      slide_content_height = $('.slide-content').height();
      footer_diff = slide_height - slide_content_height; 
      slides_total = slide_width * slide_count; 
      jumbotron.css('width', slide_width);
      jumbotron.css('height', slide_height);
      items.find('.jt-image').css('width', slide_width);
      //items.find('.jt-image').css('height', slide_height);
      slides_container.css('width', slide_width);
      slides_container.css('height', slide_height);
      items.css('width', slide_width);
      items.css('height', slide_height);
      $('#jt-slides-wrap').css('width', slides_total);
      slides_container.scrollLeft(slide_width * now_showing);
      items.css('left', function(i){
        return slide_width * i;
      });

      if (footer_diff < 160) {
        hfw.hide(0).removeClass('solid-bg');
        fob.show("slow");
      }
      else {
        fob.hide("slow");
        hfw.show("slow");
      }
    }
    else {
      $('body').removeClass('no-scrollbars');
      fob.hide("slow");
      hfw.show("slow");
      jumbotron.attr('style', '')
      slides_container.attr('style', '')
      items.attr('style', '');
      items.find('.jt-image').attr('style', '');
      slides_container.scrollLeft(now_showing * 960);
    }
  }

  function checkOrientation(){
    if (window.orientation !== undefined){
      var orient = window.orientation;
      if ((orient === 0) || (orient === 180)){
        $('body').addClass('portrait');
        $('body').removeClass('landscape');
      }
      else if ((orient === 90) || (orient === -90)){
        $('body').addClass('landscape');
        $('body').removeClass('portrait');
      }
    }
    else {
      return false;
    }
  }

  $(window).bind('resize', function() {
    calculateSize();
  });

  $(window).bind('orientationchange' , function(){
    //if the orientation gets changed, sync up the slider positioning
    var adjustment = (current || 0) * slide_width;
    slides_container.scrollLeft(adjustment);
    checkOrientation();
  })

  fob.find('a').click(function(e){
    hfw.addClass('forced-show').show("slow"); 
    fob.hide("slow");
    e.preventDefault();
  });
});
