// ==UserScript==
// @name IMDB Site improvements
// @author Robert Vock (Smir)
// @version 1.0
// @description Site improvements for the page www.imdb.com
// place this script in a folder and set this folder as UserJS Folder in your
// Opera Browser (http://opera.com/download)
// See http://www.opera.com/support/tutorials/userjs/index.dml for more
// information on UserJS.
//
// This script lets you mark movies as "watched" in the IMDB. The links to
// watched movies are red. You can add a movie to your watched list, by
// clicking the image infront of the image.
//
// Note: The information of watched movies is saved as a cookie. So you have
// to allow cookies for imdb.com. Also note, that this information will be send
// to imdb on every page request.
// Once in a while you can add the cookie to the script, see below, for an example
//
// Have fun and let me know, if you like it :)
//
// @include http://*.imdb.com*
// @include http://imdb.com*
// ==/UserScript==

(function() {
    var WatchedMovieCookie = "";
    // example: You have watched the Lord of the Rings Trilogy
    // url are:
    // http://www.imdb.com/title/tt0120737/ (Fellowship of the Ring)
    // http://www.imdb.com/title/tt0167261/ (The Two Towers)
    // http://www.imdb.com/title/tt0167260/ (The Return of the King)
    // var WatchedMovieCookie = "%2Ctt0120737%2Ctt0167261%2Ctt0167260%2C";
    // Note: %2C is a comma (,)
    // You can see this data, when you show the cookies for imdb.com
    // Just go to Tools -> Advanced -> Cookies, search for imdb and double click
    // the watchedMovies entry. There you can copy the Value.

    function setCookie(name, value, seconds, path, domain, secure) {
        if( seconds>0 )
        {
            var expires = new Date();
            expires.setTime(expires.getTime() + (seconds*1000));
        }

        var curCookie = name + "=" + escape(value) +
                        ((seconds>0) ? ";"+" expires=" + expires.toGMTString() : "") +
                        ((path) ? ";"+" path=" + path : "") +
                        ((domain) ? ";"+" domain=" + domain : "") +
                        ((secure) ? ";"+" secure" : "");
        document.cookie = curCookie;
    }


    function getCookie(name) {
        var prefix = name + "=";
        var begin = document.cookie.indexOf(";"+" " + prefix);
        if (begin == -1) {
            begin = document.cookie.indexOf(prefix);
            if (begin !== 0) {
                return null;
            }
        } else {
            begin += 2;
        }
        var end = document.cookie.indexOf(";", begin);
        if (end == -1) {
            end = document.cookie.length;
        }
        return unescape(document.cookie.substring(begin + prefix.length, end));
    }

    function appendCookie( name, value ) {
        var cookie = getCookie(name) || ",";
        cookie += value + ',';
        setCookie( name, cookie, 60*60*24*365, "/" );
    }

    document.addEventListener( 'load', function(e) {
        try {
            var elements = document.getElementsByTagName("a");
            var strMovies = unescape(WatchedMovieCookie) + getCookie( 'watchedMovies' );

            for( var i=0; i<elements.length; ++i ) {
                if( elements[i].href.search(/\/title\/tt[0-9]*\/$/) > -1 ) {
                    // get the id of the movie
                    var id = elements[i].href.substring( elements[i].href.indexOf('title/')+'title/'.length, elements[i].href.length-1 );
                    if( strMovies.indexOf( ',' + id + ',' ) > -1 ) {
                        // i watched this movie
                        elements[i].style.color = "rgb(240,10,10)";
                    } else {
                        // add a watch link infront of the movie
                        var img = document.createElement("img");
                        img.src = "http://en.wikipedia.org/skins-1.5/monobook/external.png";
                        img.alt = "^";
                        img.style.marginRight = "5px";
                        img.style.cursor = "pointer";
                        img.onclick = function( id, element, img) {
                            return function() {
                                appendCookie( 'watchedMovies', id );
                                element.parentNode.removeChild(img);
                                element.style.color = "rgb(240,10,10)";
                            };}(id,elements[i],img);
                        elements[i].parentNode.insertBefore( img, elements[i] );
                    }
                }
            }
        } catch( e ) {
            alert(e);
        }
    }, false );
})();