$(document).ready(function(){	
  $('nav a')
		.css( {backgroundPosition: "0 0"} )
		.mouseover(function(){
			$(this).stop().animate({backgroundPosition:"(-340px 0)"}, {duration:300})
		})
		.mouseout(function(){
			$(this).stop().animate({backgroundPosition:"(-620px 0)"}, {duration:200, complete:function(){
				$(this).css({backgroundPosition: "0 0"})
			}})
		});

  $("#work-index").tabs("#work-display > div", {
  	effect: 'fade',
  	fadeOutSpeed: 350,
  	fadeInSpeed: 350,
  	rotate: true,
  	history: true
  }).slideshow({
    clickable: false
  });
  
  var api = $("#work-index").tabs()
  
  //set the backward and forwards buttons
  api.onClick(function(index) {
      setForwardBackward();
  });

});

function setForwardBackward(){

    var api = $("#work-index").tabs();

    var index = api.getIndex();      //active tab index
    var last = api.getTabs().length; //# of tabs
    var theTabs = api.getTabs();     //tabs

    var fIndex,bIndex = 0;

    if (index == 0)
    {   //start of tabs, wrap around
        fIndex = index + 1;
        bIndex = last - 1;
    }
    else if ((index + 1) == last)
    {   //end of tabs, wrap around
        fIndex = 0;
        bIndex = index - 1;
    }
    else
    {   //middle of tabs
        fIndex = index + 1;
        bIndex = index - 1;
    }

    //gets and sets the href values
    var forward  = theTabs.eq(fIndex).attr('href');
    var backward = theTabs.eq(bIndex).attr('href');
    $("a.forward").attr('href',forward);
    $("a.backward").attr('href',backward);
}
