$(document).ready(function() {
//  $.Zanadoo.AssetUploader.formSetup();

  $('.homepage-play').click(function() {
    var track = $('#homepage-movie').attr('data-track-name');
    var $trackDetails = $('<p>', {
      'class' : 'track-details',
      'html'  : "Track: " + track, css: { 'display' : 'none' }
    });

    var data = {
      'projectType': 'video',
      'src':         $(this).attr('href'),
      'title':       $(this).attr('title'),
      'width':       $(this).attr('data-width'),
      'height':      $(this).attr('data-height')
    };

    var content = $.Zanadoo.buildProjectContainerHTML(data);
    $('#homepage-movie').replaceWith(content);

    if ($.Zanadoo.isMobileDevice()) {
      var video = document.getElementsByTagName('video')[0];

      // The following doesn't seem to work!
      // video.load(); // Required in certain circumstances on iPad

      video.play();

    } else {
      if ($('video').size()) {
        $('video').attr({'controls' : false, 'autoplay' : false});
        $.Zanadoo.startVideoJSForVideoElement($('video'));
      }
    }

    $trackDetails.insertAfter($('.video-js-box')).fadeIn(750);
    return false;
  });

  if ($('body.work').size()) {

    $('.project-wall li a').click(function(e) {
      e.preventDefault();
      $.Zanadoo.viewerSetup($(this));
    });

    if (!($.Zanadoo.isMobileDevice())) {
      $('.project-wall li img').mouseenter(function() {
        $('.project-wall li img').stop().animate({opacity: 100}, 250);
        $(this).stop().animate({ opacity: 0.25 }, 100);
        $('.play-icon-container').remove();

        var view = {
          'width'   : $(this).width(),
          'height'  : $(this).height(),
          'top'     : $(this).offset().top,
          'left'    : $(this).offset().left,
          'playTop' : ($(this).height() - 50) / 2
        };

        var playIconDiv = Mustache.to_html($.Zanadoo.playIconTemplate, view);

        $(this).parent().append(playIconDiv);
      });

      // http://groups.google.com/group/jquery-en/browse_thread/thread/5f32a04eb8b22bb6/58bfb824b446f5c2?show_docid=58bfb824b446f5c2
      $('.play-icon-container').live('mouseleave', function() {
        $(this).prev().stop().animate({opacity: 100}, 250);
        $(this).remove();
      });

      $('.project-wall').mouseleave(function() {
        $('.project-wall img').stop().animate({ opacity: 100 }, 250);
      });
    }
  }

  $('a.asset').click(function(e) {
      e.preventDefault();
      $.Zanadoo.viewerSetup($(this));
  });


});

