/**
 * @author Jesse Beach, Splendid Noise
 */

$(document).ready(function(){
	
  if ($('.nav li').filter('.current_page_item, .current_page_parent, .current_page_ancestor').hasClass('page-item-6')){
    $('.nav li').filter('.page-item-6').children('a:first').css({
      backgroundPosition: 'center -120px'
    });
  }else if($('.nav li').filter('.current_page_item, .current_page_parent, .current_page_ancestor').hasClass('page-item-7')){
    $('.nav li').filter('.page-item-7').children('a:first').css({
      backgroundPosition: 'center -150px'
    });
  }else if($('.nav li').filter('.current_page_item, .current_page_parent, .current_page_ancestor').hasClass('page-item-8')){
    $('.nav li').filter('.page-item-8').children('a:first').css({
      backgroundPosition: 'center -210px'
    });
  }else if ($('.nav li').filter('.current_page_item, .current_page_parent, .current_page_ancestor').hasClass('page-item-9')){
    $('.nav li').filter('.page-item-9').children('a:first').css({
      backgroundPosition: 'center -180px'
    });
  }else{}
	
  for (var i = 0; i < 3; i++) {
    if (i == 0) {
      $('.entry a img:last,.entry img:last').appendTo('#rc_img_bottom');
      $('#rc_img_bottom img').removeAttr('class');
      $('#rc_img_bottom img').removeAttr('height');
      $('#rc_img_bottom img').attr({
        width: "180",
        onerror: "brokenImg(this);"
      });
    }
    if (i == 1) {
      $('.entry a img:last,.entry img:last').appendTo('#rc_img_top');
      $('#rc_img_top img').removeAttr('class');
      $('#rc_img_top img').removeAttr('height');
      $('#rc_img_top img').attr({
        width: "180",
        onerror: "brokenImg(this);"
      });
    }
    if (i == 2) {
      $('.entry a img:last,.entry img:last').appendTo('#lc_img');
      $('#lc_img img').removeAttr('class');
      $('#lc_img img').removeAttr('height');
      $('#lc_img img').attr({
        width: "180",
        onerror: "brokenImg(this);"
      });
    }
  }
  $('.entry div.wp-caption').remove();
  //$('.entry a:contains("")').remove();
	
  $('.scroll-pane').jScrollPane({
    scrollbarOnLeft:false,
    showArrows:true,
    scrollbarWidth:16
  });
	
  /* [BEGIN] Image Gallery Code */
	
  doScrollPane = function() {
    var len = IMAGES.ar.length;
    for( var i = 0; i < len; i++){
      $('#album-'+i).before('<p id="album-'+i+'-title">'+IMAGES.ar[i].excerpt+'</p>');
      //Move the albums outside the unnecessary album div
      $('#album-'+i).children().contents().insertAfter('#album-'+i+'-title');
      $('#album-'+i).remove();
    }
    $('#galleries').jScrollPane({
      scrollbarOnLeft:false,
      showArrows:true,
      scrollbarWidth:16
    });
    clearTimeout(timer);
  }
  reinitialiseScrollPane = function() {
    timer = setTimeout(doScrollPane,350);
  // Timer is needed because sometimes the AJAX call happens
  // after the reinitialization of the scroll pane has been call if the network is slow.
  }
  var gal = $('#galleries');
  var albums = IMAGES.ar;
  var len = albums.length;
  // Remove the Slideshow album
  for (var k = 0; k < len; k++ ) {
    if (albums[k].title == "Slideshow") {
      albums.splice(k, 1);
      k = len;
    }
  }
  len = albums.length;
  for (var j = 0; j < len; j++){
    gal.append('<div id="album-'+j+'"></div>')
    if(j < len - 1){
      $('#album-'+j).load(albums[j].url+' .ngg-albumoverview');
    } else {
      $('#album-'+j).load(albums[j].url+' .ngg-albumoverview','',reinitialiseScrollPane);
    }
  }
  //Add a link back to the Gallery Listing on a single gallery page
  $('#gallery').parent().before('<a id="gallery-list-return" href="/grants/photo-gallery/">Back to the Gallery Listing</a>');
  $('#galleries').before('<a id="slideshow-link" href="/grants/photo-gallery/slideshow/">[Play slideshow of all projects]</a>');
  /* [END] Image Gallery Code */
	
  $('.sub-nav li:last').css({
    border:'none'
  });
  $('.footer .pages li a').not(':last').after(' : ');
  $('.footer .separator li a').not(':last').after(' : ');
	
});

function brokenImg(target){
  $(target).css({
    display:'none'
  });
}
