$(function(){
  var scrollable_api;
  
  /* ======================================================== */
  /* ! picture text stuff */
  /* ======================================================== */

  $('.picture_text').css({'position':'absolute', 'top':'0px', 'left':'0px'}).hide();
  $('.picture').each(function(){
    $(this).css({'cursor':'pointer'});
  });
  
  $('.picture').hover(function(){
    if ($(this).data('forceflag') != true) {
      $(this).children('.picture_text').toggle();
    }
  }, function(){
    if ($(this).data('forceflag') != true) {
      $(this).children('.picture_text').toggle();
    }
  });
  
  $('.picture').click(function(){
    $(this).css({'height':$(this).height(), 'width':$(this).width()});
    $(this).children('img').toggle();
    if ($(this).data('forceflag') != true) {
      $(this).data('forceflag', true);
    } else {
      $(this).data('forceflag', false);
    }
  });

  /* ======================================================== */
  /* ! start-screen */
  /* ======================================================== */
    
  $('#start_overlay').each(function(){
    $('html, body').css('overflow', 'hidden');
  });
  
  $('#start_overlay').show().click(function() {
    $('html, body').css('overflow', 'visible');
    $(this).hide();
  });
  
  // fix for firefox img-copy issue
  $('img').each(function(){
    var image_src = $(this).attr('src');
    // detect if it is a relative path
    if (image_src.substring(0,2) == '..') {
      var path = 'http://' + document.location.host + '/' + document.location.href.split('/')[3];
      var new_image_src = path + image_src.substring(2);
      $(this).attr('src', new_image_src);
    }
  });
  
  // columnize columns ...
  $('.columnize').columnize();
  
  // initialize scrollable
  if ($('.scrollable .items').children().length > 0) {
    var scrollable_active = true;
    
    $('#exhibition_pictures').css({'position':'absolute','top':'0px','left':'0px'});
    $('#exhibition_text').hide();
    
    $(".scrollable .items div").css('float', 'left');
    $("#info_toggle").show().click(function(event){
      $('#exhibition_pictures').toggle();
      if ($(this).text() == 'Installation Views') {
        $(this).text('Text');
        $('#toplink').hide();
        $('#exhibition_text').hide();
      } else {
        $(this).text('Installation Views');
        $('#toplink').show();
        $('#exhibition_text').show();
      }
      event.preventDefault();
    });
    
    $('.scrollable').css('overflow', 'hidden').scrollable({
              circular: true
            });
    $('.scrollable .items').css('left', '-684px');
    scrollable_api = $('.scrollable').data('scrollable');
    
    // init numbers for display and description
    $('#picture_navigation .current').text(scrollable_api.getIndex()+1);
    $('#picture_navigation .max').text(scrollable_api.getItems().length);
    $('#picture_description').html($('.scrollable .items div:not(.cloned) .description').eq(scrollable_api.getIndex()).html());
    $('#bottom_container').css('display','block');
  }
  
  
  /* ======================================================== */
  /* ! toplink scrolling */
  /* ======================================================== */
  
  $('#toplink').click(function(event){
    $('html, body').stop().animate({
      scrollTop: 0
    }, 1000);
    event.preventDefault();
  });
  
  $('#picture_navigation a').click(function(event){
    if ($(this).hasClass('nextpic')) {
      scrollable_api.next();
      nextPic();
    } else {
      scrollable_api.prev();
      prevPic();
    }
    event.preventDefault();
  });
  
  $('.scrollable .next').live('click',function(){
    nextPic();
  });
  
  function nextPic(){
    $('#picture_navigation .current').text(scrollable_api.getIndex()+1);
    $('#picture_description').html($('.scrollable .items div:not(.cloned) .description').eq(scrollable_api.getIndex()).html());
    if ($('#picture_navigation .current').text() == 0) {
      $('#picture_navigation .current').text(1);
      $('#picture_description').html($('.scrollable .items div:not(.cloned) .description').eq(0).html());
    }
    
  }

  function prevPic(){
    $('#picture_navigation .current').text(scrollable_api.getIndex()+1);
    $('#picture_description').html($('.scrollable .items div:not(.cloned) .description').eq(scrollable_api.getIndex()).html());
    if ($('#picture_navigation .current').text() == 0) {
      $('#picture_navigation .current').text(scrollable_api.getItems().length);
      $('#picture_description').html($('.scrollable .items div:not(.cloned) .description').eq(scrollable_api.getItems().length-1).html());
    }
    
  }
  
  /* ======================================================== */
  /* ! show top-scroller, bottom-backlink */
  /* ======================================================== */

  if (show_toplink == true) {
    $('#toplink').show();
  }
  
  if (show_backlink == true) {
    $('#bottom_backlink').show();
  }
  
});
