function readableFileSize(size) {
    var units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
    var i = 0;
    while(size >= 1024) {
        size /= 1024;
        ++i;
    }
    return size.toFixed(1) + ' ' + units[i];
}

$(document).ready(function() {

  $(".box").corners();

  // $("a.remove-from-shortlist").live('click', function() { 
  //   e = this.id;
  //   $.getJSON(this.href,
  //     function(data){
  //         $("a#" + e).parent().siblings().filter(":first").html("<img src='/images/icons/16x16/cross.png' alt='cross icon' />");
  //         $("a#" + e).fadeOut(1).text('Add to shortlist');
  //         $("a#" + e).removeClass("remove-from-shortlist").addClass("add-to-shortlist").fadeIn('slow');          
  //     });
  //   return false;
  // })
  // 
  // $("a.add-to-shortlist").live('click', function() { 
  //   e = this.id
  //   $.getJSON(this.href,
  //     function(data){
  //         $("a#" + e).parent().siblings().filter(":first").html("<img src='/images/icons/16x16/tick.png' alt='tick icon' />");
  //         $("a#" + e).fadeOut(1).text('Remove from shortlist');
  //         $("a#" + e).removeClass("add-to-shortlist").addClass("remove-from-shortlist").fadeIn('slow');        
  //     });
  //   return false;
  // })
  
  $("#castings a.remove-from-shortlist").live('click', function() { 
    e = this.id
    $.getJSON(this.href,
      function(data){
          $("#" + e).fadeOut(1)
          $("#" + e).text('Add to shortlist');
          $("#" + e).removeClass("remove-from-shortlist").addClass("add-to-shortlist").fadeIn('slow');       
      });
    return false;
  })
  
  $("#castings  a.add-to-shortlist").live('click', function() { 
    e = this.id
    $.getJSON(this.href,
      function(data){
          $("#" + e).fadeOut(1);
          $("#" + e).text('Remove from shortlist');
          $("#" + e).removeClass("add-to-shortlist").addClass("remove-from-shortlist").fadeIn('slow');   
      });
    return false;
  })
  

  
  // $("a#add-to-shortlist").click(function() { 
  //   $.getJSON(this.href,
  //     function(data){
  //       if(data.video.shortlisted){
  //         $("a#add-remove").fadeOut(1);
  //         $("a#add-remove").text('Remove from shortlist');
  //         $("a#add-remove").removeClass("add-to-shortlist");
  //         $("a#add-remove").addClass("remove-from-shortlist");
  //         $("a#add-remove").fadeIn('slow');
  //         $(".add-remove").fadeIn('slow');
  //       }
  //       else{
  //         $("a#add-remove").fadeOut(1);
  //         $("a#add-remove").text('Add to shortlist');
  //         $("a#add-remove").removeClass("remove-from-shortlist");
  //         $("a#add-remove").addClass("add-to-shortlist");
  //         $("a#add-remove").fadeIn('slow');          
  //       }
  //     });
  // 
  //   return false;
  // })  
  
  
  
  $("#notify_form").hide();
  $("#email-notify a").click(function() {
    $("#download-zip-form").slideUp("slow");
    $("#notify_form").slideToggle("slow");
    return false;
  })
  
  $("#download-zip-form").hide();
  $("#download-zip a").click(function() {
    $("#notify_form").slideUp("slow");
    $("#download-zip-form").slideToggle("slow");
    return false;
  })
  
  // $(".actor-takes").hide();  
  // $("#manage-takes").click(function() {
  //   $(".actor-takes").slideToggle("slow");
  //   return false;
  // })
  // $(".downloads").hide();  
  // $("#download-takes").click(function() {
  //   $(".downloads").slideToggle("slow");
  //   return false;
  // })
  
  // $("ul#castings li.long-list a[rel]").overlay({expose: '#000', effect: 'apple'}); 
  // 
  // $("ul#castings li.long-list a").live('click', function() { 
  // 
  //   var flashvars = {
  //   };
  //   var params = {
  //     repeat: "list",
  //     allowscriptaccess: "true",
  //     width: "640",
  //     height: "480",
  //     playlist: "over",
  //     autostart: "true",
  //     wmode: "window",
  //     displayclick: "play",
  //   };
  //   var attributes = {
  //     allowfullscreen: "true"
  //   };
  //   params.file = this.href + ".xml";
  //   swfobject.embedSWF('/swf/player.swf', 'overlay-player', '640', '480', '8', flashvars, params, attributes);
  //   return false;
  // })
  
  $("ul#castings li.actor-link a[rel]").overlay({expose: '#000', effect: 'apple'}); 
  
  $("ul#castings li.actor-link a").live('click', function() { 
        
    actor = this.href.substring(this.href.lastIndexOf('/')+1)
    
    var the_length = this.href.length;
    var last_char = this.href.charAt(the_length-1);
   
    $("#overlay-wrapper").show();
    
    $.getJSON(this.href + ".json",
      function(data){
        $("#skip-to-take").hide();
        $("#skip-to-take").empty();
        $("#player-takes").hide();
        $("#player-takes").empty();
        $("#manage-takes").empty();
        $("#manage-takes").hide();
        $(data).each(function (i) {
          var x = i + 1;
          if (this.video.shortlisted){
            $("#skip-to-take").append('<li><a href="#" id="'+ i +'" class="skip-to-take">Take '+ x + '</a></li>');
          }
          $("#player-takes").append('<li><a href="'+ this.video.url + '" class="save-download">Take '+ x + ' (' + readableFileSize(this.video.video_file_size) + ')</a></li>');
          if (this.video.shortlisted){
            $("#manage-takes").append('<li><a href="/videos/'+ this.video.id + '/toggle_shortlist/" class="remove-from-shortlist" id="manage-take-'+x+'">Take '+ x + '</a></li>');
          }
          else{
            $("#manage-takes").append('<li><a href="/videos/'+ this.video.id + '/toggle_shortlist/" class="add-to-shortlist" id="manage-take-'+x+'">Take '+ x + '</a></li>');           
          }
        });
        $("#manage-takes a").live('click', function(){ 
          e = this.id;
          var e_length = e.length;
          var e_last_char = e.charAt(e_length-1);
          var remove_take = e_last_char - 1;
          eClass = this.className;
          $.getJSON(this.href,
            function(data){
                $("#" + e).fadeOut(1);
                if (eClass == "add-to-shortlist"){
                  $("#" + e).removeClass("add-to-shortlist").addClass("remove-from-shortlist").fadeIn('slow');                 
                }
                if (eClass == "remove-from-shortlist"){
                  $("#" + e).removeClass("remove-from-shortlist").addClass("add-to-shortlist").fadeIn('slow');    
                  $("#skip-to-take a#" + remove_take).hide();                 
                }
            });
          return false;
        })

    });


    var flashvars = {
    };
    var params = {
      repeat: "list",
      allowscriptaccess: "true",
      width: "640",
      height: "480",
      playlist: "over",
      autostart: "true",
      wmode: "window",
      id: "overlay-player",
      displayclick: "play",
      screencolor: "000000"
    };
    var attributes = {
      allowfullscreen: "true",
      id: "overlay-player",
      name: "overlay-player"
    };
    params.file = "/actors/"+actor+"/shortlist.xml";
    swfobject.embedSWF('/swf/player.swf', 'overlay-player', '640', '480', '8', flashvars, params, attributes);

    player = document.getElementById('overlay-player');
    
    $("#overlay-wrapper img").hide();   

    return false;
  })
  
  $("#close-player").live('click', function() { 
    $("#overlay-wrapper").hide();
    return false;
  })
 
  $("#player-takes").hide();

  $("#toggle-download-takes").live('click', function() { 
    $("#player-takes").slideToggle();
    return false;
  })  
  
  $("#skip-to-take").hide();
  
  $("#toggle-skip-takes").live('click', function() { 
    $("#skip-to-take").slideToggle();
    return false;
  })
  
  $("#skip-to-take a").live('click', function() { 
    player.sendEvent('ITEM', this.id);
    return false;
  })
  
  
  $("#toggle-manage-takes").live('click', function() { 
    $("#manage-takes").slideToggle();
    return false;
  })
  


  
});

