$(document).ready(function()
{ 
  $(".more_imgs a").bind("click", function()
  {
    var idx = $(this).index();
    var thumbBase = $(this).parent();
    var base = thumbBase.prev("div.PhotoCollectionLargeImage");
    var curImg = $("img:visible", base);
    if (curImg.attr("idx") == idx || curImg.length > 1) return; // Zelfde thumb geklikt
    var showImg = $("img[idx=" + idx + "]", base);
    var showSrc = $("img", this).attr("src").replace(/_prod_imgs\/(small|thumb|large)/, "_prod_imgs/small");

    curImg.css("z-index", 5);
    
    if (showImg.length <= 0)
    { // Aanmaken
      var newImg = curImg.clone();
      newImg.attr("src", showSrc).attr("idx", idx);
      base.prepend(newImg);
      showImg = newImg;
    }
    showImg.css("z-index", 15).css("display", "none");
    $("a", thumbBase).removeClass("activeSlide");
    $(this).addClass("activeSlide");
    showImg.fadeIn(300, function()
    {
      curImg.hide();
    });
  });
  
  $(".PhotoCollectionInline").each(function()
  {
    var idx = 1;
    while ($("._PhotoCollectionHolder" + idx).length > 0 && idx < 20)
      idx++;
    if (idx >= 20) { alert("Te veel photocollecties"); return; }
    
    $(this).attr("id", "_PhotoCollectionHolder" + idx);
    
    var amount = $(this).attr("data-amount");
    $(this).hide();
    var container = $(this).parent().append("<div class='_PhotoCollectionContainer'></div>").find("._PhotoCollectionContainer");
    container.attr("_PhotoCollectionHolder", "_PhotoCollectionHolder" + idx);
    
    var width = $(this).css("width");
    
    var i = 0;
    var subcontainer;
    var photo;
    var page = 0;
    $("div.foto", this).each(function()
    {
      
      page = Math.floor(i/amount)+1;
      
      if (i%amount==0) // create new subcontainer
      {
        $(container).append("<div class='_PhotoCollectionSubContainer' data-id='" + page + "'></div>");
        subcontainer = $("._PhotoCollectionSubContainer[data-id=" + page + "]", container);
        subcontainer.css("width", width);
        if (page > 1) subcontainer.hide();
      }
      subcontainer.append($(this).clone());
      i++;
    });
    $(container).append("<div style='clear:both;'></div>");
    if (page >= 2)
      for(i = 1; i <= page; i++)
      {
        $(container).append("<span class='" + (i==1?"PhotoCollectionPagerActive":"") + " corner_3 click PhotoCollectionPager' data-id='" + i + "'>" + i + "</span>");
      }
  });
  $(".navigate_buttons").bind("selectstart", function() { return false; });
  $(".navigate_buttons").bind("mousedown", function() { return false; });
  $(".navigate_backward").bind("click", photoCollectionNavigateBackward);
  $(".navigate_foreward").bind("click", photoCollectionNavigateForeward);
  $(".navigate_close, #photo_fade").bind("click", function() { cancel_func(); });
  $(window).bind("keydown", function(e) { var chr = e.which || e.keyCode; if (chr==27 && cancel_func) cancel_func(); });

  hookEvent("photo_viewer", "mousewheel", 
  function(e)
  {
    var wheelData = e.detail ? e.detail * -1 : e.wheelDelta;
    if (wheelData >= 1) $(".navigate_backward").trigger("click"); else $(".navigate_foreward").trigger("click");
    return cancelEvent(e);
  });
  
  $(window).bind("resize", function() { $("#photo_viewer").centerScreen(); });
  $(window).bind("scroll", function() { $("#photo_viewer").centerScreen(); });
  
  // END document ready
}).delegate(".PhotoCollectionRemove", "click", function()
{
  new ajax_json("./modules/ajax/removeImage.php?imageID=" + $(this).attr("data-id"), function(data)
  {
    $("div#photo_" + data.json.ID).fadeOut(200);
  });
}).delegate(".PhotoCollectionPager", "click", function()
{
  var base = $(this).closest("._PhotoCollectionContainer");
  var ID = $(this).attr("data-id");
  
  $("._PhotoCollectionSubContainer", base).css("position", "absolute").fadeOut(200);
  $("._PhotoCollectionSubContainer[data-id=" + ID + "]", base).css("position", "static").fadeIn(300);
  $(".PhotoCollectionPagerActive").removeClass("PhotoCollectionPagerActive");
  $(this).addClass("PhotoCollectionPagerActive");
}).delegate("img.pasfoto, img.foto, img.item_photo, img.group_photo, img.photo_large", "click", function()
{ 
  // OPENEN VAN DE FOTOVIEWER
  var viewer = $("#photo_viewer");
  var thumbnails = $(".thumbnails", viewer);
  
  thumbnails.html("");
  
  var src = $(this).attr("src").replace(/_prod_imgs\/(small|thumb)\//, "_prod_imgs/large/");
  $("div#photo_viewer .large_image_container").html("<img src='" + src + "' class='corner_5' />");
  
  $("div#photo_viewer .large_image_container").bind("click", function() { cancel_func(); $("#photo_fade").fadeOut(300); }).bind("load", function() { $(".photo_large").centerScreen(); });
  
  var photoHolderId = $(this).attr("_photoCollectionHolder") || $(this).closest("._PhotoCollectionContainer").attr("_PhotoCollectionHolder");
  
  var is_collection = photoHolderId && $("#" + photoHolderId).length > 0;
  var collection = photoCollectionGetHolder(photoHolderId);
  
  if (is_collection && collection.find(".foto .foto").length > 1) 
  {
    $("div#photo_viewer div.navigate_foreward, div#photo_viewer div.navigate_backward").show().attr("is_collection", true);
    var orig;
    if ($(this).parent().is("div.foto"))
      orig = (collection.find("div#" + $(this).parent().attr("id") + " .foto"));
    else  
      orig = (collection.find("div.foto:first-child .foto"));
    
    $("div#photo_viewer")[0].orig_obj = orig;
    
    photoCollectionBindNavigationKeys();
    
    // THUMBNAILS
    
    collection.find("div.foto").each(function()
    {
      var src = $(".foto", this).attr("src").replace(/_prod_imgs\/(small|thumb|large)\//, "_prod_imgs/thumb/");
      var id = $(this).attr("id");
      thumbnails.append("<img id='" + id + "' class='foto_thumb corner_5' src='" + src + "' />");
    });
    photoCollectionLoaded2();   
  } else 
  {
    $("div#photo_viewer")[0].orig_obj = null;
    $("div#photo_viewer div.navigate_foreward, div#photo_viewer div.navigate_backward").hide().attr("is_collection", false);
  }
  $("div#photo_viewer").fadeIn(300).centerScreen();
  $("#photo_fade").fadeTo(300, 0.55);
  cancel_func = function() { photoCollectionUnbindNavigationKeys(); $("div#photo_viewer, div#photo_fade").fadeOut(200); }
}).delegate(".foto_thumb", "click", function()
{
  photoCollectionNavigateTo(this);
});

function photoCollectionGetHolder(photoHolderId)
{
  var collection = $("#photoCollectionHolder #" + photoHolderId); 
  if (collection.length <= 0) collection = $("#" + photoHolderId);
  return collection;
}
function photoCollectionLoaded2()
{
  var thumbnails = $("#photo_viewer .thumbnails");
  $("img", thumbnails).removeClass("PhotoCollectionThumbActive");
  var src = $("#photo_viewer .large_image_container img").attr("src").replace(/_prod_imgs\/(small|thumb|large)\//, "_prod_imgs/thumb/");
  src = jquery_selector_escape(src);
  $("img[src=" + src + "]", thumbnails).addClass("PhotoCollectionThumbActive");
}
function photoCollectionBindNavigationKeys()
{
  $(window).bind("keydown", photoCollectionKeyNavigation);
}
function photoCollectionUnbindNavigationKeys()
{
  $(window).unbind("keydown", photoCollectionKeyNavigation);
}

function photoCollectionKeyNavigation(e)
{
  var c = e.keyCode;
  switch(c)
  {
    case 37: $(".navigate_backward").trigger("click"); break;
    case 39: $(".navigate_foreward").trigger("click"); break;
  }
}
function photoCollectionNavigateBackward(e)
{
  photoCollectionNavigate(e, this, -1);
}
function photoCollectionNavigateTo(obj)
{
  var photoHolder = $($("div#photo_viewer")[0].orig_obj).parent().parent();
  var id = $(obj).attr("id");
  var new_obj = $("#" + id + " .foto", photoHolder);

  $("div#photo_viewer")[0].orig_obj = new_obj[0];
  src = $(obj).attr("src");
  src = src.replace(/_prod_imgs\/(small|thumb)\//, "_prod_imgs/large/");
  $("div#photo_viewer .large_image_container img").attr("src", src);
  photoCollectionLoaded2();
}
function photoCollectionNavigate(e, obj, dir)
{
  var orig_obj = $($(obj).parent().attr("orig_obj"));
  calert(orig_obj);
  
  var new_obj = dir == -1 ? orig_obj.parent().prev().find(".foto") : orig_obj.parent().next().find(".foto");
  
  if (new_obj.length <= 0)
    new_obj = dir == -1 ? orig_obj.parent().parent().find("div.foto:last-child .foto") : orig_obj.parent().parent().find("div.foto:first-child .foto");
  
  var src = new_obj.attr("src");
  if (!src) return;
  
  src = src.replace(/_prod_imgs\/(small|thumb)\//, "_prod_imgs/large/");

  $(obj).parent()[0].orig_obj = new_obj[0];
  
  $("div#photo_viewer .large_image_container img").attr("src", src);
  photoCollectionLoaded2();
}
function photoCollectionNavigateForeward(e)
{
  photoCollectionNavigate(e, this, 1);
}

function reloadCollection(_for, forID, element)
{
  new ajax("./modules/ajax/reloadPhotoCollection.php?for=" + _for + "&forID=" + forID, function(data)
  {
    $(element).html(data.responseText);
  });
}
