$(document).ready(function() {

  /* Variables for scripts */
  var body = $("body");
  var htmlElement = $("html");
    
  /* Homepage news carousel */
  function mycarousel_initCallback(carousel) {
    $('#newscarousel-next').bind('click', function() {
      carousel.next();
      return false;
    });

    $('#newscarousel-prev').bind('click', function() {
      carousel.prev();
      return false;
    });
  };

  function mycarousel_buttonNextCallback(carousel, button, enabled) {
    if (enabled) {
      $('#newscarousel-next').removeClass("disabled");
    } else {
      $('#newscarousel-next').addClass("disabled");
    }
  };
  
  function mycarousel_buttonPrevCallback(carousel, button, enabled) {
    if (enabled) {
      $('#newscarousel-prev').removeClass("disabled");
    } else {
      $('#newscarousel-prev').addClass("disabled");
    }
  };

  // Ride the carousel...
  $("#newscarousel").jcarousel({
    scroll: 1,
    vertical: true,
    initCallback: mycarousel_initCallback,
    // This tells jCarousel NOT to autobuild prev/next buttons
    buttonNextHTML: null,
    buttonPrevHTML: null,
    buttonNextCallback:   mycarousel_buttonNextCallback,
    buttonPrevCallback:   mycarousel_buttonPrevCallback
  });
  
  /* Slideshow */ 
  function onAfter(curr, next, opts) {
    var index = opts.currSlide;
    $('#prevslide')[index == 0 ? 'hide' : 'show']();
    $('#nextslide')[index == opts.slideCount - 1 ? 'hide' : 'show']();
  }
   
  var featureslideshowwrap = $("div.slideshowwrapper");
  var featureslideshow = $("div.slideshow");
  if ($(featureslideshow).length > 0) {
    $(featureslideshowwrap)
      .after('<div id="slideshownav"><a href="#" class="prevslide" title="Previous slide">&lsaquo;</a><ul></ul><a href="#" class="nextslide" title="Next slide">&rsaquo;</a></div>')
      .append('<a href="#" class="prevslide" id="prevslide">Previous</a><a href="#" class="nextslide" id="nextslide">Next</a>');
    $(featureslideshow).cycle({
      timeout: 0,
      pause: true,
      prev: '.prevslide', 
      next: '.nextslide',
      pager: '#slideshownav ul',
      fx: 'scrollHorz',
      after: onAfter,
      // callback fn that creates a thumbnail to use as pager anchor 
    pagerAnchorBuilder: function(idx, slide) { 
        return '<li><a href="#">' + (idx+1) + '</a></li>'; 
      }
    });
       
    
    //If they click or focus on the slide nav, the slideshow stops auto-rotating
    $("div#slideshownav ul li a, a#prevslide, a#nextslide").click(function() {
      $("div#mainfeature").cycle("pause");
    });
    $("div#slideshownav ul li a, a#prevslide, a#nextslide").focus(function() {
      $("div#mainfeature").cycle("pause");
    })
    
  };
  
  /* Tabs */
  $("div.tabs").tabs();
  
  /* Inserted spans for browsers that don't understand :before */
  if (htmlElement.hasClass("no-generatedcontent")) {
    var headerTwos = $(".content h2");
    $(headerTwos).prepend('<span class="square"></span>');
		var thumbnailHeaders = $(".projectgroups .thumbnail h3");
		$(thumbnailHeaders).append('<span class="heightadjuster"></span>');
		var tabnav = $("ul.tabnav");
		$(tabnav).prepend('<span class="square"></span>');
		var introParagraphs = $(".home .intro p");
		$(introParagraphs).prepend('<span class="square"></span>');
  }
  
})

