function photoGalleryNav(curr, dir) {
  if ( $("#thumbPhotos .item").length > 1 ) {
    curr--;
    if ( dir == 'prev' && curr > 0 ) {
      $("#thumbPhotos .item>a:eq(" + (curr-1) + ")").click();
    } else {
      $("#thumbPhotos .item>a:eq(" + (curr+1) + ")").click();
    }
  }
  return false;
}

$(document).ready(function() {
  
  
  var defaultCategory = 'Bend';
  /*
  function findCategory() {
    if (typeof(category) != 'undefined' && category.length) {
      return eval(category);
    } else {
      return 'Hood River';
    }
  }*/
  
  var targetElement = $('#thumbPhotos');
  var url = '/locations/photo-gallery/';
  
  //var cat = findCategory();
  
  if (locationCategory == '' || typeof(locationCategory) == 'undefined' || locationCategory == null) {
    locationCategory = defaultCategory;
  }
  
    filterset = [
        {field:"Location",comparison:"contains",value:locationCategory},
        {field:"Location",comparison:"eq",value:"All"}
    ]
  
  pixelsilk2.getListItems({ path: url, sorts: [{field:"SortOrder", direction:"Ascending"}], filters: filterset, filterrel: 'filter1 OR filter2', pageSize: '1000', page: '1', fields:["Image","Title","BriefDescription"] }, function(json) {
    if (json) {
      var html = '';
      for (j=0;j<json.length;j++) {
        html += '<div class="item">';
        html += '<a href="'+json[j].Image+'?w=500&h=375&padded=true"><img src="'+json[j].Image+'?w=65&h=65&padded=true" alt="'+json[j].Title+'" /></a>';
        html += '<p class="desc">'+json[j].BriefDescription+'</p>';
        html += '</div>';
      }
      
      targetElement.html(html);
      var i = 0;
      var total = $("#thumbPhotos .item").length;
      
      $("#thumbPhotos .item>a").each(function() {
        i++;
        $(this).parent().attr('id', 'photoGalleryItem_' + i);
        var html = '<div style="position:relative;"><div style="padding: 0 30px;">';
        html += '<h2>' + $(this).find('img').attr('alt') + '</h2>';
        html += '<div><img src="' + $(this).attr('href') + '" alt="" /></div>';
        html += '<p>' + $(this).parent().find('.desc').html() + '</p>'; //Briefdesc
        html += '</div>';
        if ( total > 1 ) {
          if ( i == 1 ) {
          html += '<a href="#" class="modalArrow modalArrowRight" onclick="return photoGalleryNav(' + i + ', \'next\');">&nbsp;</a>';
          } else if ( i == $("#thumbPhotos .item>a").length ) {
          html += '<a href="#" class="modalArrow modalArrowLeft" onclick="return photoGalleryNav(' + i + ', \'prev\');">&nbsp;</a>';
          } else {
          html += '<a href="#" class="modalArrow modalArrowLeft" onclick="return photoGalleryNav(' + i + ', \'prev\');">&nbsp;</a><a href="#" class="modalArrow modalArrowRight" onclick="return photoGalleryNav(' + i + ', \'next\');">&nbsp;</a>';
          }
        }
        html += '</div>';
        $(this).pixelsilkModal({
          contentType: "html", content: html,
          closeButtonHTML: '',
          fadeInSpeed: 300, fadeOutSpeed: 300,
          modalWidth: 600//, modalHeight: 500
        });
      });
      $('.item').hover(function(){
        $(this).find('a').prepend('<span class="cursor"></span>');
      },
      function(){
        $(this).find('.cursor').remove();    
      });      
    }
  });


});
