(function() {
  $(document).ready(function() {
    window.imageIndex = 0;
    window.audioIndex = 0;
    $('.index_' + window.imageIndex).addClass('current');
    $('.image_caption').html( window.captions[ window.imageIndex ] );
    $('li').each( function(index, element) {
      var contents = $(element).html();
      var span = $('<span></span>').addClass('text_wrapper').html(contents);
      $(element).html('').append(span);
    });

    if (window.images) {
      var imageDir = window.imageFull ? 'full' : 'preview';
      $(window.images).each(function(){
        $('<img/>')[0].src = 'images/' + imageDir + '//' + this;
      });
    }

    $('li, .controls span').mouseenter(function(e) {
      $(this).addClass('hover');
    });
    $('li, .controls span').mouseleave(function(e) {
      $(this).removeClass('hover');
    });

    $('#large_image_placeholder, .image_caption').click(function(e) {
      if (window.imageFull) {
        if (window.targetPage) {
          window.location = "/" + window.targetPage[ window.imageIndex ];
        }
      }
    });

    if (!window.imageFull) {
      window.imageFull = false;
      $('#image_placeholder').append(
        $('<img>').attr('src', 'images/preview/' + '/' + window.images[window.imageIndex]).attr('class', 'top')
      )
    } else {
      $('#large_image_placeholder').append(
        $('<img>').attr('src', 'images/full/' + '/' + window.images[window.imageIndex]).attr('class', 'top')
      )
    }
    var animate = function(inc) {
      //$('#image_placeholder .top').remove();
      $('#image_placeholder .bottom').removeClass('bottom').addClass('top');
      if (inc) {
        window.imageIndex++;
      }
      if (!window.images[window.imageIndex]) {
        window.imageIndex = 0;
      }

      var caption = window.captions[window.imageIndex];
      if (caption) {
        $('.image_caption').html( caption );
      }
      var imageDir = window.imageFull ? 'full' : 'preview';
      var container = window.imageFull ? '#large_image_placeholder' : '#image_placeholder';

      if (window.imageFull) {
        $('.bottom', container).removeClass('bottom').addClass('top')
        $(container).append(
          $('<img>').attr('src', 'images/' + imageDir + '/'  + '/' + window.images[window.imageIndex]).attr('class', 'bottom')
        )

        $('.top', container).fadeOut('slow', function() { $(this).remove() });
        $('.bottom', container).fadeIn('slow');
      } else {
        $(container).append(
          $('<img>').attr('src', 'images/' + imageDir + '/'  + '/' + window.images[window.imageIndex]).attr('class', 'bottom')
        )

        $('.top', container).fadeOut('slow');
        $('.bottom', container).fadeIn('slow');
      }
      $('.project_links .current').removeClass('current');
      $('.index_' + window.imageIndex).addClass('current');
    }
    window.slideshowInterval = window.setInterval(function() { animate(true) }, 5000);

    $('.project_image_container .next').click(function(e) {
      if (window.slideshowInterval) {
        window.clearInterval(window.slideshowInterval);
      }
      animate(true);
    });

    $('.image_select').click(function(e) {
      window.clearInterval(window.slideshowInterval);
      if (window.slideshowInterval) {
        window.clearInterval(window.slideshowInterval);
      }
      var index = $(e.currentTarget).attr('data-index');
      window.imageIndex = index;
      animate(false);
    });

    $('.project_image_container .big').click(function(e) {
      window.imageFull = true;
      //if (window.slideshowInterval) {
      //  window.clearInterval(window.slideshowInterval);
      //}

      $('.top_section').hide();
      $('#large_image_placeholder').html('');
      $('#large_image_placeholder').append(
        $('<img>').attr('src', 'images/full/'  + '/' + window.images[window.imageIndex]).attr('class', 'top')
      )
      $('.full_image_section').show();
    });

    $('.project_image_container .small').click(function(e) {
      window.imageFull = false;
      $('.full_image_section').hide();
      $('.top_section').show();
    });

    $('.project_image_container .slideshow').click(function(e) {
      animate(true);
      window.slideshowInterval = window.setInterval(function() { animate(true) }, 5000);
    });

    var setupAudio = function(index, play) {
      // console.log('in setup audio');
      if (!window['audio']) return;
      var audioPath = "http://dawnsitestatic.s3.amazonaws.com/audio"  + '/' + window.audio[index];
      // console.log('file base', audioPath);
      var caption = window.audioCaptions[index];
      if (caption) {
        $('.audio_caption').html( caption );
      }

      $("#jquery_jplayer_1").jPlayer("stop");
      $("#jquery_jplayer_1").jPlayer("setMedia", {
        mp3: audioPath + '.mp3',
        oga: audioPath + '.ogg'
      });
      // console.log('in player', $("#jquery_jplayer_1"));
      $(".audio_container .current").removeClass("current");
      $(".audio_container li[data-index='" + index + "']").addClass('current');

      if (play) {
        $("#jquery_jplayer_1").jPlayer("play");
      }
    };

    if(!!document.createElement('audio').canPlayType) {
      // console.log('setting ip adio !!!');
      $("#jquery_jplayer_1").jPlayer({
        ready: function () {
          setupAudio(0, false);
          $(this).jPlayer("play");
        },
        ended: function (event) {
          $(this).jPlayer("play");
        },
        supplied: "mp3, oga"
      });

      $('.audio_link').click(function(e) {
        var index = $(e.currentTarget).attr('data-index');
        setupAudio(index, true);
      });
    } else {
      $('.player_container').hide();
      $('.audio_link').click(function(e) {
        var index = $(e.currentTarget).attr('data-index');
        var audioPath = "http://dawnsitestatic.s3.amazonaws.com/audio"  + '/' + window.audio[index] + '.mp3';
        // console.log('open audio', audioPath);
        window.open(audioPath);
      });
    }
  });

}).call(this)
