
if  (typeof slideCntr == 'undefined') { 
  var slideCntr = 0;
}

var thumbsLeft = 0;
var slideStep = 108;
var animationSpeed = 400;
var tmp = '';
var adFreq = 8;
var adCntr = 0;

$(document).ready(function(){
  if (slideCntr > 0) { 
   var tmp = slideCntr * slideStep * -1;
   $("div#belowimg div#thumbs").animate({marginLeft: tmp}, 100);
  }
  if (slideCntr == 0) { 
    $("div#belowimg a#prev_arrow_large").hide();
  }
  $("div#belowimg").show('fast');
  setupArrows();
  tmp = "img#imgThumb" + mainImgCntr;
  $(tmp).addClass('img_hilited');
});


function thumbsNext() { 
  slideCntr += 1;
  thumbsLeft = slideCntr * slideStep * -1;
  $("div#belowimg div#thumbs").animate({marginLeft: thumbsLeft}, animationSpeed);
  if (slideCntr == 1) { 
    $("div#belowimg a#prev_arrow_large").fadeIn('fast');
  }
  if (slideCntr == totalPhotos - 3) { 
    $("div#belowimg a#next_arrow_large").fadeOut('fast');
  }
}

function thumbsPrev() { 
  slideCntr += -1 ;
  if (slideCntr == 0) { 
    $("div#belowimg a#prev_arrow_large").fadeOut('fast');
  }
  thumbsLeft = slideCntr * slideStep * -1;
  $("div#belowimg div#thumbs").animate({marginLeft: thumbsLeft}, animationSpeed);

  if (slideCntr == totalPhotos - 4) { 
    $("div#belowimg a#next_arrow_large").fadeIn('fast');
  }
}


function getImg(dir) { 
  //console.log('getImg ' + dir);
  if (dir == 'next') { adCntr += 1; }

  if (!isNaN(dir)) { adCntr = 1; }

  if (adCntr % adFreq == 0) { 
    $('img#mainimg').hide();
    $('#ad').show();
    adCntr = 0;
    $('#picnavtop #picCntr').hide();
  }
  else { 
    $('#ad').hide();
    $('img#mainimg').show();
    $('#picnavtop #picCntr').show();
    var index = 1;
    if (dir == 'next') { mainImgCntr += 1; index = mainImgCntr; }
    else if (dir == 'prev') { mainImgCntr += -1; index = mainImgCntr; } 
    else { index = dir; mainImgCntr = index; }

    //console.log('img ' + index);

    var newPic = new Image(440,520);
    newPic.src = "http://www.x17online.com/gallery/galleries/" + galPath + "/full/" + galPhotos[index];
    $("img#mainimg").attr({ src: newPic.src });
    $("span#picCntr").html("Photo " + index + " of " + totalPhotos);
    if (slideCntr != index) { 
      thumbsLeft = (index -1) * slideStep * -1;
      $("div#belowimg div#thumbs").animate({marginLeft: thumbsLeft}, animationSpeed);  
      $("img.thumb").removeClass("img_hilited");

      tmp = "img#imgThumb" + mainImgCntr;
      $(tmp).addClass('img_hilited');

    }


    // preload next image as well
    if (galPhotos[index+1] !== null) { 
    
      var nextPic = new Image(440,520);
      nextPic.src = "http://www.x17online.com/gallery/galleries/" + galPath + "/full/" + galPhotos[index+1];
    }



    slideCntr = index;
    setupArrows();
  }
}

function setupArrows() {
  // top arrows
  if (mainImgCntr > 1) { $("img#prev_arrow_top").show('fast'); }
  else { $("img#prev_arrow_top").hide(); }
  if (mainImgCntr < totalPhotos) { $("img#next_arrow_top").show(); }
  else { $("img#next_arrow_top").hide(); }

  // arrows below image (thumbs)
  if (slideCntr > 1) { $("div#belowimg a#prev_arrow_large").show(); }
  else { $("div#belowimg a#prev_arrow_large").hide(); }
  if (slideCntr == totalPhotos) { $("div#belowimg a#next_arrow_large").hide(); }
  else { $("div#belowimg a#next_arrow_large").show(); }

}

