var player_expose; 
 
$(document).ready(function(){
	
	// setting the width of the player wrapper to the width of the object
	// don't want to do this is css, since videos might be of different sizes
	$("#player_wrapper").css( "width", $("#player_wrapper").children('object').width() );
	
    // assign a click event to the exposed element
	player_expose = $("#player_wrapper").expose({
		api: true,
		color: '#000', 
    	opacity: 0.8, 
	}); 
    $("#lightbox").click(function() { 
        // perform exposing for the clicked element 
		if (player_expose.isLoaded()) {
			player_expose.close();
			$("#lightbox").html("Dim the lights")
		}
		else {
			player_expose.load();
			$("#lightbox").html("Turn on the lights");
			
		}
    });
});

function update_recent_raves(film_id){
  $.post(url_recent_raves_html, {
      film_id: film_id
  }, function(data){
      if (data.success) {
          $("#raved_by").html(data.html);
      }
      else {
          if (data.error) 
              alert(data.error);
          else 
              alert('Oops! An error has occurred!');
      }
  }, "json");
}

function update_rating(raves, added){
	 if (raves == 0) 
        $("#rating_count").html("0"); 
    else 
        $("#rating_count").html(raves);
				
    if (raves == 1) 
        $("#rave").html("Rave");
    else 
        $("#rave").html("Raves");
    $("#rave").html("Rave".pluralize)
    
		if (added) 
        $("#vote_button").val("Unrave");
    else 
        $("#vote_button").val("Rave it");
}

function vote(user_id, film_id){
    if (user_id == "None") {
        alert("You need to be logged in to Rave. You are being redirected to login page!");
        location.href = "/users/login/";
        return false;
    }
    $.post(url_toggle_vote_ajax, {
        user_id: user_id,
        film_id: film_id
    }, function(data){
        if (data.success) {
           update_rating(data.film_votes, data.vote_added); 
					 update_recent_raves(film_id); 
        }
        else {
            if (data.error) 
                alert(data.error);
            else 
                alert('Oops! An error has occurred!');
        }
    }, "json");
}