  var movie;
  var movieTitle;
  var id;
  var action;
  var actionBox;
  var actionBoxTitle;
  var actionMessage;
  var actionContent;
  var actionNotice;
  var sbwBox;
  var request;
  var url;
  var data;
  var boxMessageHTML;
  var boxTitleHTML;
  var boxContentHTML;
  var sbwContentHTML;
  var username;
  var comment;
  var responseJSON;
  var sbw;
  var contentHtml;
  
  function setup() {
    window.scrollTo(0, 1);
    movie = document.getElementById('movie');
    id = movie.name;
    movieTitle = document.getElementById('movieTitle');
    action = document.body.id;
    urlHost = 'http://' + window.location.host;
    actionBox = document.getElementById('actionBox');
    actionBoxTitle = document.getElementById('actionBoxTitle');
    actionMessage = document.getElementById('actionMessage');
    actionContent = document.getElementById('actionContent');
    actionNotice = document.getElementById('actionNotice');
    sbwBox = document.getElementById('sbwBox');
    movie.addEventListener('qt_play', playVideo, false);
    hideBox();
  }
  
  function ajaxSubmit() {
    request = new XMLHttpRequest();
    request.open("POST", url, false);
    request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    request.send(data);
  }
  
  function ajaxSubmitCallback() {
    request = new XMLHttpRequest();
    request.onreadystatechange = processRequestChange;
    request.open("POST", url, false);
    request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    request.send(data);
  }
  
  function processRequestChange() {
    if (request.readyState == 4) {
      if (request.status == 200) {
        if(request.responseText == 'error') {
          actionNotice.style.color = 'red';
          actionNotice.innerHTML = "Une erreur s'est produite, veuillez recommencer.";
        }
        if(request.responseText == 'ok')
          requestSuccess();
      } else {
        alert('fail');
      }
    }
  }
  
  function requestSuccess() {
    actionNotice.style.color = '#2d8330';
    actionMessage.innerHTML = '';
    if(action == 'rateVideo') {
      for(i=1;i<=5;i++)
        document.getElementById('ratingStar' + i).childNodes[0].onclick = "return void(0)";
      actionNotice.innerHTML = "Nous vous remercions pour votre vote.";
    }
    if(action == 'commentVideo') {
      actionContent.innerHTML = '';
      actionNotice.innerHTML = "Nous vous remercions pour votre commentaire.";
    }
  }
  
  function showBox() { 
    actionBox.style.display = 'block';
    return false;
  }
  
  function hideBox() { 
    actionBox.style.display = 'none';
    return false;
  }
  
  function playVideo() {
    url = "/ajax/newplay";
    data = "idcontent=" + id;
    ajaxSubmit();
    if (action=="video")
      showActionButtons();
    else
      loadBoxContent();
  }
  
  function showActionButtons() {
    videoActions = document.getElementById('videoActions');
    videoActions.style.display = 'block';
  }
  
  function loadBoxContent() {
    url = "/ajax/loadboxcontent/";
    data = "idcontent=" + id + "&action=" + action;
    ajaxSubmit();
    fillBoxContent();
  }
  
  function fillBoxContent() {
    actionNotice.innerHTML = '';
    responseJSON = request.responseText;
    eval(responseJSON);
    switch(action) {
      case 'home':
        fillRecommandBox();
      break;
      case 'theme':
        fillRecommandBox();
      break;
      case 'recommandVideo':
        fillRecommandVideoBox();
      break;
      case 'rateVideo':
        fillRateBox();
      break;
      case 'commentVideo':
        fillCommentBox();
      break;
    }
    //setSBW();
    showBox();
    window.scrollTo(0, movie.offsetTop);
  }
  
  function fillRecommandBox() {
    arrayOfString = movieTitle.title.split(';',2);
    actionBoxTitle.innerHTML = "Recommander cette vidéo";
    actionMessage.innerHTML = "Partagez cette vidéo avec vos amis.";
    contentHtml = '<a href="';
    contentHtml += 'mailto:?subject=' + arrayOfString[0];
    contentHtml += '&body=Salut retrouve cette vidéo sur VODemotion.<br><a href=' + urlHost + arrayOfString[1] + '><img src=' + movie.src + ' width=140 height=80 /></a>'
    contentHtml += '" onclick="return showVoirAussi();" id="recommandButton">Recommander</a>';
    actionContent.innerHTML = contentHtml;
  }
  
  function fillRateBox() {
    actionBoxTitle.innerHTML = "Noter cette vidéo";
    actionMessage.innerHTML = "Pour noter la vidéo, cliquez sur l'étoile : 1 pour Bof ! et 5 pour Top !";
    contentHtml = '';
    for(i=1;i<=5;i++)
      contentHtml += '<div class="ratingStar" id="ratingStar' + i + '"><a href="#" name="' + i + '" onclick="return rateVideo(this)">' + i + '</a></div>';
    contentHtml += '<div class="clear"></div>';
    actionContent.innerHTML = contentHtml;
  }
  
  function fillCommentBox() {
    actionBoxTitle.innerHTML = "Commenter cette vidéo";
    actionMessage.innerHTML = "Pour ajouter un commentaire, veuillez remplir les champs suivants.";
    contentHtml = '<form name="commentForm" method="POST" action="/ajax/newcomment/" onsubmit="return commentVideo()" class="commentForm">';
    contentHtml += '<label for="username">Votre pseudo</label>';
    contentHtml += '<input type="text" name="username" value="" id="username">';
    contentHtml += '<label for="comment">Votre Commentaire</label>';
    contentHtml += '<textarea name="comment" id="comment"></textarea>';
    contentHtml += '<input type="submit" name="submit" value="Ajouter mon commentaire" id="submit" />';
    contentHtml += '</form>';
    actionContent.innerHTML = contentHtml;
  }
  
  function setSBW() {
    sbwHtml = '<a href="' + sbw.click + '" class="sbw_ad">';
    sbwHtml += '<img src="' + sbw.bannerSrc + '" width="240" height="34" id="SBWImageBox" /></a>';
    sbwBox.innerHTML = sbwHtml;
  }
  
  function videoAction(element) {
    action = element.id;
    loadBoxContent();
    return false;
  }
  
  function fillRecommandVideoBox() {
    arrayOfString = movieTitle.title.split(';',2);
    actionBoxTitle.innerHTML = "Recommander cette vidéo";
    actionMessage.innerHTML = "Partagez cette vidéo avec vos amis.";
    contentHtml = '<a href="';
    contentHtml += 'mailto:?subject=' + arrayOfString[0];
    contentHtml += '&body=Salut retrouve cette vidéo sur VODemotion.<br><a href=' + urlHost + arrayOfString[1] + '><img src=' + movie.src + ' width=140 height=80 /></a>'
    contentHtml += '" onclick="return showVoirAussi();" id="recommandButton">Recommander</a>';
    actionContent.innerHTML = contentHtml;
  }
  
  function rateVideo(element) {
    rate = element.name;
    for(i=1;i<=rate;i++)
      document.getElementById('ratingStar' + i).style.backgroundColor = "#fca33c"
    url = "/ajax/newrating/";
    data = "id=" + id + "&rating=" + rate;
    ajaxSubmitCallback();
    showVoirAussi();
    return false;
  }
  
  function commentVideo() {
    username = document.getElementById('username').value;
    comment = document.getElementById('comment').value;
    if(checkCommentForm()) {
      url = "/ajax/newcomment/";
      data = "id=" + id + "&username=" + username + "&comment=" + comment;
      ajaxSubmitCallback();
      showVoirAussi();
    } else {
      actionNotice.style.color = 'red';
      actionNotice.innerHTML = 'Veuillez remplir les champs correctement.';
      return false;
    }
    return false;
  }
  
  function checkCommentForm() {
    return (username.length > 0 && comment.length > 0);
  }
  
  function showVoirAussi() {
    document.getElementById('voirAussi').style.display = 'block';
  }