var STATUS_SUCCESS = 1;
var STATUS_ERROR   = 0;


function handleVideoShare(playStatus){

  if (playStatus)
    // if video is playing, we restart it after the share
    var extraFunctionCallOnClose = playStream;
  else
    var extraFunctionCallOnClose = null;

  Modalbox.show('/share_video', {title: 'Share this video with your friends!', width: 620, textInFooter: '&nbsp;', overlayOpacity: 0.85, transitions: false, extraFunctionCallOnClose: extraFunctionCallOnClose}); 
  enableAlphaImage($('MB_footer'));
}


function validate_share_form(){
  // Return bool
  // We just check is required values are set.
  
  var isValid = true;
  
  if(! $('firstname').value){
    $('firstname').style.border = "2px solid red";
    isValid = false;
  }
  
  if (! $('cerises').value){
    $('cerises').style.border = "2px solid red";
    isValid = false;
  }
  return isValid
}


function share(){

  // Initial border (If one or more emails are not valid this border will be red)
  $('cerises').style.border = "1px solid #999999";
  $('firstname').style.border = "1px solid #999999";
  
  
  // Check if all required fields are set
  if ( ! validate_share_form())
    return 

  $('share_button').disabled = true;
  
  new Ajax.Request('/recommend_share_video',
    {
      method:'post',
      parameters: {cerises: $('cerises').value, message: $('message').value, firstname: $('firstname').value},
      onSuccess: function(transport){
        try
        {
          var response = transport.responseText || "0, no response";
          var reponseSplit = response.split(',');
  
          // If return value is not a tuple
          if (reponseSplit.length != 2 )
              throw "Response is not well formed";
  
          var status = reponseSplit[0];
          var message = reponseSplit[1];
  
          if (status == STATUS_SUCCESS){
            $('cerises').value = '';
          }
          else{
            $('cerises').value = message;
            $('cerises').style.border = "2px solid red";
          }
        }
        catch(err)
        {
            alert('An error has occured: ' + err);
        }
        finally
        {
            $('share_button').value = "Share this again?";
            $('share_button').disabled = false;
        }
      },
      onFailure: function(){ 
        alert('Something went wrong...'); 
        $('share_button').disabled = false; 
       }
    });
}
