$(document).ready(function(){  
  $('a.buttonred,a.buttonyellow').append('<span></span>');
  
  $('#nav div span').hide();
  $('#nav div a:eq(1)').click(function(){$('#nav div span').toggle();});
  
  $('.story-subnav a:eq(0)').addClass('active');
  $('#contact .story-subnav a:eq(0)').removeClass('active');
  $('.story-subnav a').click(function(){
	$('.story-subnav a').removeClass('active');
	$(this).addClass('active');
  });
  //$('#content div').hide().filter(':first').show();
  $('#content > div div').jScrollPane();
  $('#content > div div').addClass('contentdivs');
  $(".story-subnav ul").tabs("#content > div", {effect: 'slide'});


  (function($) {
  var cache = [];
  // Arguments are image paths relative to the current page.
  $.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }
  })(jQuery);jQuery.preLoadImages("/images/education_outreach_bg-funding.jpg");
  $('.story-subnav li a').click(function(){
	  $('body#education_outreach').css({'background-image':'url(/images/education_outreach_bg.jpg)'});
	  $('.fundinglogos').hide();
	  $('.communitypartners').show();
  });
  $('.story-subnav li a.funding').click(function(){
	  $('body#education_outreach').css({'background-image':'url(/images/education_outreach_bg-funding.jpg)'});
	  $('.fundinglogos').show();
	  $('.communitypartners').hide();
  });


  /*	Polly Trailer */
  $('li.pollytrailer_li').hover(function(){$('.pollytrailer').show();});
  
  /*	Polly Gallery	*/  
  /* Initialise */
  var imgCount =  $('#pollygallery li').length;
  $('#pollygallery li:eq(0)').addClass('center');
  $('#pollygallery li:eq(1)').addClass('r_thumb');
  $('#pollygallery li:gt(1)').addClass('hidden');
  $('#pollygallery li:last').addClass('l_thumb').removeClass('hidden');
  $('#pollygallery li').click(function(){
    if ($(this).hasClass('l_thumb')){moveLeft();$(this).find('a').attr({});}
    else if ($(this).hasClass('r_thumb')){moveRight();$(this).find('a').attr({});}
	else if ($(this).hasClass('center')){$(this).find('a').attr({});}
    // else if($(this).hasClass('center')){enlarge();}
  });
  $('#pollygallery li.center').hover(function(){$(this).addClass('smamodal')},function(){$(this).removeClass('smamodal')});
  $('#pollygallery li.l_thumb,#pollygallery li.r_thumb').click(function(){$('#pollygallery li.center').removeClass('smamodal')});
  //$('#pollygallery li.center a').attr({title:"Click for larger view"});
  $('#pollygallery li').attr({onmousedown:'return false'});
  $('#pollygallery li').bind("contextmenu",function(e){return false;});  
});

function moveRight(){
  var imgList = $('#pollygallery li');
  var imgCount =  imgList.length;
  var target=$('#pollygallery li.r_thumb');
  var t_index = imgList.index(target);
  var r_t_index = ((t_index + 1) >= imgCount) ? (t_index + 1) - imgCount : t_index + 1 ;
  var l_t_index = ((t_index - 1) < 0) ? ((t_index - 1) + imgCount) : t_index - 1 ;
  // alert('Target index = '+t_index+'\nLeft Thumb: '+l_t_index + '\nRight Thumb: ' + r_t_index + '\nTotal images: '+imgCount);
  var duration = 500;
  imgList.eq(r_t_index).addClass('r_thumb').removeClass('hidden');
  $('#pollygallery li.l_thumb').addClass('hidden').removeClass('l_thumb');
  imgList.eq(l_t_index).addClass('l_thumb').removeClass('center');
  
  target.addClass('center').removeClass('r_thumb');
  
}

function moveLeft(){
  var imgList = $('#pollygallery li');
  var imgCount =  imgList.length;
  var target=$('#pollygallery li.l_thumb');
  var t_index = imgList.index(target);
  var r_t_index = ((t_index + 1) >= imgCount) ? (t_index + 1) - imgCount : t_index + 1 ;
  var l_t_index = ((t_index - 1) < 0) ? ((t_index - 1) + imgCount) : t_index - 1 ;
  // alert('Target index = '+t_index+'\nLeft Thumb: '+l_t_index + '\nRight Thumb: ' + r_t_index + '\nTotal images: '+imgCount);
  var duration = 500;
  imgList.eq(l_t_index).addClass('l_thumb').removeClass('hidden');
  $('#pollygallery li.r_thumb').addClass('hidden').removeClass('r_thumb');
  imgList.eq(r_t_index).addClass('r_thumb').removeClass('center');
  
  target.addClass('center').removeClass('l_thumb');
  
}