var basePath = $('base').attr('href');
var languagechoicetimeout = 0;
$(document).ready(function() {
  /* Pagination Features */
  $('div.contentbox .righthalf .pagination a').click(function() {
    $('div.contentbox .righthalf .pagination a').removeClass('active');
    $(this).addClass('active');
    var indx = $('div.contentbox .righthalf .pagination a').index(this);
    $('div.contentbox .righthalf .feature').not(':eq(' + indx * 3 + ')').not(':eq(' + indx * 3 + ')').not(':eq(' + indx * 3 + ')').slideUp();
    for(var i = indx * 3; i < (indx + 1) * 3; i++) {
      $('div.contentbox .righthalf .feature').eq(i).slideDown();
    }
  });

  /* Pagination Inspiration */
  addPaginationClick();

  /* Language choice */
  $('#languagechoicebtn, #languagechoice').hover(function() {
    clearTimeout(languagechoicetimeout);
    $('#languagechoice').fadeIn(200);
  }, function() {
    clearTimeout(languagechoicetimeout);
    languagechoicetimeout = setTimeout(function(){$('#languagechoice').fadeOut(200);}, 500);
  });

  /* Inspirations filter */
  $('#filters ul li').click(function() {
    if(!$(this).hasClass('active')) {
      $(this).addClass('active');
    } else {
      $(this).removeClass('active');
    }
    updateFotoalbums();
  });
  $('#filters h2 div').click(function() {
    $('#filters ul li').removeClass('active');
    updateFotoalbums();
  });
  $('#filters #closer .btn').click(function() {
    $('#filters #filterlist').slideToggle();
    if($(this).html() == 'close filter') {
      $(this).html('open filter');
    } else {
      $(this).html('close filter');
    }
  });
  $('a.fotothumb').click(function() {
    bigpictureIndex = $('a.fotothumb').index($(this))
    getOverlay({activeid:bigpictureIndex});
  });

  /* Reposition overlay on resize */
  $(window).resize(function() {
    clearTimeout(resizeTimeout);
    resizeTimeout = setTimeout(positionOverlay, 500);
  });
});
var resizeTimeout = 0;
function addPaginationClick() {
  $('.pagination a').click(function() {
    $('.pagination a').removeClass('active');
    $(this).addClass('active');
    var indx = $('.pagination a').index(this);
    $('#albums .album').not(':eq(' + indx * 4 + ')').not(':eq(' + indx * 4 + ')').not(':eq(' + indx * 4 + ')').not(':eq(' + indx * 4 + ')').slideUp();
    for(var i = indx * 4; i < (indx + 1) * 4; i++) {
      $('#albums .album').eq(i).slideDown();
    }
  });
}
function positionOverlay(show) {
  var l = Math.round(($(window).width() - $('#overlay-content').outerWidth()) / 2);
  var t = Math.round(($(window).height() - $('#overlay-content').outerHeight()) / 2);
  if(t < 0) { t = 0; }
  $('#overlay-content').stop(true, true).animate({left:l+'px', top:t+'px'}, {duration:250, complete:function(){
    if(show != undefined) {$(this).fadeIn(400);}
  }});
}
var bigpictureIndex = 0;
function showOverlay(data) {
  $('#overlay').fadeTo(400, .9).show().click(function() {
    $(this).fadeOut(300, function() {
      $(this).attr('style', '');
    });
    $('#overlay-content').fadeOut(300, function() {
      $(this).attr('style', '');
    });
    window.location.hash = '';
  });
  $('#overlay-content').html(data);

  $('#bigpicture a').click(function(e) {
    e.preventDefault();
    $('#albumholder').fadeOut();
    $(function() {
      $.fn.superbgimage.options = {preload:1, reload:1};
      $('#fullscreen').superbgimage();
      $('#fullscreen-left, #fullscreen-right').css({height:$(window).height() + 'px', opacity:.3}).hover(function() {
        $(this).fadeTo(300, 1);
      }, function() {
        $(this).fadeTo(300, .3);
      });
      $('#fullscreen-left').click(function() {
        if(bigpictureIndex > 0) {
          bigpictureIndex--;
          $('#albumholder, #fullscreen a').attr('href', $('#thumbs-holder img:eq(' + bigpictureIndex + ')').attr('src').replace('_th', '_source'));
          $('#fullscreen').superbgimage();
        }
      });
      $('#fullscreen-right').click(function() {
        if(bigpictureIndex < $('#thumbs-holder img').length - 1) {
          bigpictureIndex++;
          $('#albumholder, #fullscreen a').attr('href', $('#thumbs-holder img:eq(' + bigpictureIndex + ')').attr('src').replace('_th', '_source'));
          $('#fullscreen').superbgimage();
        }
      });
      $('#superbgimage').click(function() {
        $(this).fadeOut(200, function() {
          $(this).html('');
          $('#fullscreen-left, #fullscreen-right').css({height:0});
          $('#albumholder, #fullscreen').fadeIn(400, function() {
            if(bigpictureIndex != $('#thumbs-holder img').index($('#thumbs-holder img.active'))) { $('#thumbs-holder img:eq(' + bigpictureIndex + ')').click(); }
          });
        } );
      });
    });
  });
  // preload images
  $('#thumbs-holder img').each(function() {
    preloadImages($(this).attr('src').replace('_th', '_lg'));
  });
  $('#thumbs-holder img').click(function() {
    $('#thumbs-holder img').removeClass('active');
    $(this).addClass('active');
    bigpictureIndex = $('#thumbs-holder img').index(this);
    var newSrc = $(this).attr('src').replace('_th', '_lg');
    var newTitle = $(this).attr('title');
    $('#bigpicture > img').fadeOut(400, function() {
      if(newTitle != '') {
        $('#bigpicture > span').html(newTitle).fadeIn();
      } else {
        $('#bigpicture > span').fadeOut();
      }
      $(this).attr('src', newSrc).attr('alt', newTitle).attr('title', newTitle).fadeIn();
      $(this).parent().find('a').attr('href', newSrc.replace('_lg', '_source'));
    });
    var bits  = $(this).attr('rel').split(';');
    var w = Number(bits[0]);
    var h = Number(bits[1]);
    $('#bigpicture-left, #bigpicture-right, #bigpicture').css('height', h + 'px');
    $('#bigpicture > span').animate({width:(w - 30) + 'px', left:((960 - w) / 2) + 'px'}, 300);
    $('#bigpicture > img').animate({left:((960 - w) / 2) + 'px'}, 300);
    $('#albumholder').css('height', (h + 120) + 'px');
    positionOverlay();
  });
  if($('#thumbs-holder img').length > 7) {
    var w2 = $('#thumbs-holder img').length * 140;
    $('#thumbs-left').click(function() {
      if(Number($('#thumbs-holder').css('left').replace('px', '')) <= -140) {
        $('#thumbs-holder').stop(true, true).animate({left:'+=140px'}, 400);
      }
    });
    $('#thumbs-right').click(function() {
      if(Number($('#thumbs-holder').css('left').replace('px', '')) > (960 + 140 - w2)) {
        $('#thumbs-holder').stop(true, true).animate({left:'-=140px'}, 400);
      }
    });
  }
  $('#bigpicture-left').click(function() {
    if(bigpictureIndex > 0) {
      bigpictureIndex--;
      $('#thumbs-holder img:eq(' + bigpictureIndex + ')').click();
    }
  });
  $('#bigpicture-right').click(function() {
    if(bigpictureIndex < $('#thumbs-holder img').length - 1) {
      bigpictureIndex++;
      $('#thumbs-holder img:eq(' + bigpictureIndex + ')').click();
    }
  });
  var bits  = $('#thumbs-holder img:eq(0)').attr('rel').split(';');
  var w = Number(bits[0]);
  var h = Number(bits[1]);
  $('#bigpicture-left, #bigpicture-right, #bigpicture').css('height', h + 'px');
  $('#bigpicture > span').css('width', (w - 30) + 'px');
  $('#albumholder').css('height', (h + 120) + 'px');
  positionOverlay(true);
}
function getOverlay(obj) {
  $.ajax({
    url:'ajax.php',
    data:obj,
    success:function(data) {
      showOverlay(data);
    },
    error:function() { }
  });
}
function updateFotoalbums() {
  var cats = '';
  var glue = '';
  $('#filters ul li.categorie.active').each(function() {
    cats += glue + $(this).val();
    glue = ',';
  });
  var locs = '';
  glue = '';
  $('#filters ul li.locatie.active').each(function() {
    locs += glue + $(this).val();
    glue = ',';
  });
  var kleur = '';
  glue = '';
  $('#filters ul li.kleur.active').each(function() {
    kleur += glue + $(this).val();
    glue = ',';
  });
  var voeg = '';
  glue = '';
  $('#filters ul li.voegen.active').each(function() {
    voeg += glue + $(this).val();
    glue = ',';
  });
  $.ajax({
    url:'ajax.php',
    data:{tags:[cats, locs, kleur, voeg]},
    success:function(data) {
      $('#albums').fadeOut(400, function() {
        $(this).html(data).fadeIn(400);
        $('a.fotothumb').click(function() {
          getOverlay({activeid:$('a.fotothumb').index($(this))});
        });
        addPaginationClick();
      });
    },
    error:function() {}
  });
}
function preloadImages() {
  for(var i = 0; i < arguments.length; i++) {
    var oImg = new Image();
    oImg.src = arguments[i];
  }
}
function toggleMultipleFocus() {
  for(var i = 0; i < arguments.length; i++) {
    toggleFocus(arguments[i]);
  }
}
function toggleFocus(id) {
  var val = $(id).val();
  $(id).focus(function() {
    if($(this).val() == val) {$(this).val('');}
  }).blur(function() {
    if($(this).val() == '') {$(this).val(val);}
  });
}
