__sliding = "false";
__maximageheight = 500;
__halfmaximageheight = __maximageheight/2;
__maxvideoheight = 385;
__halfmaxvideoheight = __maxvideoheight/2;
__animationtime = 500;

if (window.jQuery) 
{
  $(document).ready(function(){

    moveaction = location.hash;
    if (moveaction=="#next") {
      moveImageUp();
    }
    else {
      moveImageDown();
    }
  
    $("#previous_gallery").click( function(e) {
      slideDown();
      return false;
    });
  
    $("#next_gallery").click( function(e) {
      slideUp();
      return false;
    });
  
    $("#imagenav li").click( function(e) {
      /*
      */
      gotoimgindex = parseInt($(this).text().replace(/^[0]+/g,""));
      if (gotoimgindex>currentimageindex) {
        slideNext(gotoimgindex-1);
      }
      else {
        slidePrevious(gotoimgindex-1);
      }
      return false;
    });

    $("#next_image").click( function(e) {
      slideNext();
      return false;
    });

    $("#previous_image").click( function(e) {
      slidePrevious();
      return false;
    });

    $(document).keydown( function(e) {
      if (__sliding=="true") {
        e.preventDefault();
        return false;
      }
      handleKeyDown(e.keyCode);
      //return false;
    });

  }); // end document ready
}

function moveImageUp()
{
  cacheImages();
  imagetop = $("#imagecontainer").position().top;
  imageheight = $("#imagecontainer").height();

  checkVideo(0);

  viewportheight = $(window).height() + __halfmaximageheight;
  $("#imagecontainer").css("top", viewportheight);

  var moveimage = imagetop;
  $("#imagecontainer").animate( { "top": moveimage} , __animationtime, function() {
    $("#imagecontainer").css("top", "50%");
  } );

}

function moveImageDown()
{
  cacheImages();
  imagetop = $("#imagecontainer").position().top;
  imageheight = $("#imagecontainer").height();

  checkVideo(0);

  var moveimage = 0 - imagetop - imageheight;
  $("#imagecontainer").css("top", moveimage);
  $("#imagecontainer").animate( { "top": imagetop} , __animationtime, function() {
    $("#imagecontainer").css("top", "50%");
  } );

}

function slideNext(_gotoimg) 
{
  if (__sliding=="true") {
    return;
  }
  __sliding = "true";
  imageleft = $("#imagecontainer").position().left;
  imagewidth = $("#imagecontainer").width();
  windowwidth = $(window).width();
  var moveimage = "-=" + (imageleft + imagewidth) + "";
  nextimage = (currentimageindex+1)%imagecount;
  // quick fix to go directly to target instead of sliding one item at a time
  if (parseInt(_gotoimg)>=0) {
    nextimage = _gotoimg;
  }
  $("#imagecontainer").animate( { "left": moveimage} , __animationtime, function(){
    $("#liimg" + (currentimageindex+1)).removeClass("active");
    //$("#imagecontainer img").attr({src: galleryimages[nextimage].url + "?random=" + (new Date()).getTime()}).one('load', function() {
    $("#imagecontainer img").attr({src: galleryimages[nextimage].url});
    //alert("new image loaded, sliding:" + __sliding);
    checkVideo(nextimage);
    $("#imagecontainer").css("left", windowwidth);
    $("#imagecontainer").animate( { "left": imageleft} , __animationtime, function(){
      $("#liimg" + (nextimage+1)).addClass("active");
      currentimageindex = nextimage;
      cacheImages();
      if (parseInt(_gotoimg)>=0) {
        if (_gotoimg!=currentimageindex) {
          slideNext(_gotoimg);
        }
        else {
          __sliding="false";
        }
      }
      else {
        __sliding="false";
      }
    } );
    //} ); // image loaded, doesn't work on ie and safari because of cached images
  } );
  __sliding="false";
}

function slidePrevious(_gotoimg) 
{
  if (__sliding=="true") {
    return;
  }
  __sliding = "true";
  imageleft = $("#imagecontainer").position().left;
  imagewidth = $("#imagecontainer").width();
  windowwidth = $(window).width();
  var moveimage = "-=" + (imageleft + imagewidth) + "";
  if (currentimageindex<=0) {
    nextimage = imagecount-1;
  } else {
    nextimage = currentimageindex-1;
  }
  // quick fix to go directly to target instead of sliding one item at a time
  if (parseInt(_gotoimg)>=0) {
    nextimage = _gotoimg;
  }
  $("#imagecontainer").animate( { "left": windowwidth} , __animationtime, function(){
    $("#liimg" + (currentimageindex+1)).removeClass("active");
    //$("#imagecontainer img").attr({src: galleryimages[nextimage].url}).one('load', function() {
    $("#imagecontainer img").attr({src: galleryimages[nextimage].url});
    checkVideo(nextimage);
    $("#imagecontainer").css("left", 0-imageleft-imagewidth);
    $("#imagecontainer").animate( { "left": imageleft} , __animationtime, function(){
      $("#liimg" + (nextimage+1)).addClass("active");
      $("#imagecontainer").css("left", "");
      currentimageindex = nextimage;
      cacheImages();
      if (parseInt(_gotoimg)>=0) {
        if (_gotoimg!=currentimageindex) {
          slidePrevious(_gotoimg);
        }
        else {
          __sliding="false";
        }
      }
      else {
        __sliding="false";
      }
    } );
    //} ); // image loaded, doesn't work on ie and safari because of cached images
  } );
  __sliding="false";
}

function slideUp()
{
  if (__sliding=="true") {
    return;
  }
  __sliding = "true";
  var gotogallery = $("#next_gallery a").attr("href");
  if (! gotogallery) {
    __sliding="false";
    return false;
  }
  $("#imagecontainer").css("top", ($(window).height()/2) + "px");
  imagetop = $("#imagecontainer").position().top;
  imageheight = $("#imagecontainer").height();
  // videos and images don't have the same height, so correct for this
  if (galleryimages[currentimageindex].videourl.length>0) {
    moveimage = "-=" + (imagetop + imageheight - __halfmaxvideoheight) + "";
  }
  else {
    moveimage = "-=" + (imagetop + imageheight - __halfmaximageheight) + "";
  }
  $("#imagecontainer").animate( { "top": moveimage} , __animationtime, function(){
    location.href = gotogallery;
  } );
  __sliding="false";
  return false;
}

function slideDown()
{
  if (__sliding=="true") {
    return;
  }
  __sliding = "true";
  viewportheight = $(window).height() + __halfmaximageheight;
  $("#imagecontainer").css("top", ($(window).height()/2) + "px");
  var gotogallery = $("#previous_gallery a").attr("href");
  if (! gotogallery) {
    __sliding="false";
    return false;
  }
  $("#imagecontainer").animate( {"top": viewportheight} , __animationtime, function() {
    location.href = gotogallery;
  } );
  __sliding="false";
  return false;
}

function checkVideo(_imageindex)
{
  if (galleryimages[_imageindex].videourl.length>0) {
    // clear video-visual
    $("#videocontainer").html('<div id="video-visual"></div>');
    $("#imagecontainer img").css("display", "none");
    $("#videocontainer").css("display", "");
    var flashvars = {
      'skin': '/wordpress/wp-content/themes/oosterbeek/swf/bekle.zip'
    , 'file': escape(galleryimages[_imageindex].videourl)
    , 'image': escape(galleryimages[_imageindex].url) 
    , 'autostart': 'false'
    , 'controlbar': 'over'
    , 'plugins': '/wordpress/wp-content/themes/oosterbeek/swf/jwe.swf'
    //, 'provider': 'http'
      };
    var params = {
      'allowfullscreen': 'true'
    , 'allowscriptaccess': 'always'
    , 'bgcolor': '#CCCCCC'
    };
    var attributes = {
      'id': 'player1'
    , 'name': 'player1'
    };

    swfobject.embedSWF('/wordpress/wp-content/themes/oosterbeek/swf/player-viral.swf', 'video-visual', '640', '385', '9', 'false', flashvars, params, attributes);
    $("#imagecontainer").addClass("video");
  } else {
    $("#imagecontainer img").css("display", "");
    $("#videocontainer").css("display", "none");
    $("#videocontainer").html('<div id="video-visual"></div>');
  }
}

function handleKeyDown(_keyCode)
{
  if (__sliding=="true") {
    return false;
  }
  switch (_keyCode) {
    case 37: 
      slidePrevious();
      break;
    case 39: 
      slideNext();
      break;
    case 38: 
      slideDown();
      break;
    case 40: 
      slideUp();
      break;
    case 32: 
      var myplayer = document.getElementById("player1");
      if (myplayer != null) {
        myplayer.sendEvent("PLAY");
      }
      break;
    //default:
    //  alert(_keyCode);
  }
}

function cacheImages()
{
  cacheImage(currentimageindex-1);
  cacheImage(currentimageindex+1);
  return;
}

function cacheImage(_imgIndex)
{
  var imgindex = _imgIndex;
  if (imgindex<0) {
    imgindex = imagecount-1;
  } 
  else {
    if (imgindex>imagecount-1) {
      imgindex = 0;
    }
  }
  if (galleryimages[imgindex].cached) {
    //alert("already cached: " + imgindex);
    //return;
  }
  newimg = new Image();
  newimg.src = galleryimages[imgindex].url;
  galleryimages[imgindex].cached = true;
  return;
}


