// ==UserScript==
// @name           My Movies List
// @namespace      mml
// @include        http://www.imdb.com/title/*
// ==/UserScript==


// Change the url to your installation of My Movie List. Don't forget the trailing slash.

var movieListUrl = 'http://example.com/movies/';



// ===========
function init() {
  if (movieListUrl == 'http://example.com/movies/') {
    alert('Now you\'ve installed the My Movie List userscript, you need to change the URL.');
    return;
  }
  
  var h1Elm = document.getElementsByTagName('h1')[0];
  
  h1Elm.innerHTML = h1Elm.innerHTML + ' <a href="#" id="mymovieslink" style="font-size:12px;font-weight:normal">Add to my movie list...</a>';
  
  var aElm = document.getElementById('mymovieslink');
  aElm.addEventListener('click', goToMyMovieList, false);
}

function goToMyMovieList(e) {
  e.preventDefault();
  
  var ratingElm = document.getElementById('personal-voting-stars');
  var rating;
  
  if (ratingElm)
    rating = parseInt(ratingElm.style.width, 10) / 20;
  else
    rating = 1;
  
  var id = window.location.href.substring(28, 35);
  
  window.location = movieListUrl + 'add-movie.php?step=3&id=' + id + '&rating=' + rating;
}

init();
