﻿// WTM Extras
// Copyright (c) 2009-2010, Franck Quélain
// Released under the GPL license
// http://www.gnu.org/copyleft/gpl.html
//
// --------------------------------------------------------------------
// This is a Greasemonkey user script.
//
// To install, you need Greasemonkey: http://greasemonkey.mozdev.org/
// Then restart Firefox and revisit this script.
// Under Tools, there will be a new menu item to "Install User Script".
// Accept the default configuration and install.
//
// To uninstall, go to Tools/Manage User Scripts,
// select "wtm_extras", and click Uninstall.
//
// --------------------------------------------------------------------
// CHANGELOG
//
// 2009-07-06 :
//  * Creation
// 2010-03-29 :
//  * Bugfix : Title found repeated
// 2010-04-05 :
//  * Now use the AJAX showsolution call to display solution.
//  * Use WhatTheMovie CSS class to keep the same look.
//  * Add links to usefull websites 
//         to found movies by car, brand, soup, Coca-cola or Zippo
//  * Add personal keywords to a shot
//  * Search shots by personal keyword
// --------------------------------------------------------------------
//
// ==UserScript==
// @name            wtm_extras
// @namespace       http://infernal-quack.net/
// @description     WhatTheMovie Extras! - version 2010.04.05
// @include         http://*whatthemovie.com/shot/*
// ==/UserScript==

(function(){

  // === Usefull functions
  function setCookie(name, value, expiredays) {
    var exdate=new Date();
    exdate.setDate(exdate.getDate()+expiredays);
    document.cookie = name+"="+escape(value)+
      ((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
  }

  function getCookie(name) {
    if (document.cookie.length>0) {
      var start=document.cookie.indexOf(name + "=");
      if (start!=-1) {
           start=start + name.length+1;
           var end=document.cookie.indexOf(";",start);
           if (end==-1) {
             end=document.cookie.length;
           }
           return unescape(document.cookie.substring(start,end));
      }
    }
    return "";
  }
  
  function searchInCookies(text) {
    var result = new Array();
    var lastCookies = document.cookie.toLowerCase();
    while(lastCookies.length>0) {
      var start=lastCookies.indexOf(escape(text.toLowerCase()));
      if (start!=-1) {
           var startKey = 1+lastCookies.lastIndexOf(";", start);
           var endKey = lastCookies.lastIndexOf("=", start);
           result.push(unescape(lastCookies.substring(startKey,endKey).trim()));
           var newStart = start+1+lastCookies.substring(start).indexOf(";");
           if(newStart==0) {
             break;
           }
           else {
             lastCookies=lastCookies.substring(newStart);
           }
      }
      else {
           break;
      }
    }
    return result;
  }

  // ============= Show solutions for already solved movie
  
  //  Display title by double-click on the image
  document.getElementById("image_container").addEventListener("dblclick", showMovieTitle, false);

  function showMovieTitle() {
    if(unsafeWindow.amazonMovieName) {
      // AJAX request is run only if the movie has been found
      new unsafeWindow.Ajax.Request(
        '/shot/' + unsafeWindow.shot_id + '/showsolution', 
        {asynchronous:true, evalScripts:true}
      ); 

      return false;
    }
  }
  
  // =============== Extras
  
  var imageSrc = document.getElementById("shot_still").getAttribute("src");
  var extras = document.createElement("div");
  extras.setAttribute("id","extras");
  
  extras.appendChild(document.createTextNode("★ Extras ★"));

  var extraLinks = document.createElement("ul");
  extraLinks.setAttribute("class", "nav_shots");
  extras.appendChild(extraLinks);
  document.getElementById("image_container")
    .insertBefore(extras,document.getElementById("image_container").firstChild);
    
  // ===== Links
  
  // TineEye.com
  var extraLink0 = document.createElement("li");
  extraLinks.appendChild(extraLink0);
  var searchTinEye = document.createElement("a");
  searchTinEye.setAttribute("href","http://tineye.com/search?url="+imageSrc);
  searchTinEye.appendChild(document.createTextNode("⎆ Search on TinEye"));
  extraLink0.appendChild(searchTinEye);
  
  // IMDB.org
  var extraLink1 = document.createElement("li");
  extraLinks.appendChild(extraLink1);
  var goIMCDB = document.createElement("a");
  goIMCDB.setAttribute("href","http://www.imdb.com/");
  goIMCDB.appendChild(document.createTextNode("⎈ Internet Movie DB"));
  extraLink1.appendChild(goIMCDB);
  
  // IMCDB.org
  var extraLink2 = document.createElement("li");
  extraLinks.appendChild(extraLink2);
  var goIMCDB = document.createElement("a");
  goIMCDB.setAttribute("href","http://imcdb.org/");
  goIMCDB.appendChild(document.createTextNode("⎈ ... by car"));
  extraLink2.appendChild(goIMCDB);

  // Brandchannel.com
  var extraLink3 = document.createElement("li");
  extraLinks.appendChild(extraLink3);
  var goBrand = document.createElement("a");
  goBrand.setAttribute("href",
    "http://www.brandchannel.com/brandcameo_brands.asp?all_year=all_year");
  goBrand.appendChild(document.createTextNode("≣ ... by brand"));
  extraLink3.appendChild(goBrand);
  
  // Soupsong.com
  var extraLink4 = document.createElement("li");
  extraLinks.appendChild(extraLink4);
  var goSoup = document.createElement("a");
  goSoup.setAttribute("href","http://www.soupsong.com/imovies.html");
  goSoup.appendChild(document.createTextNode("≣ ... by soup"));
  extraLink4.appendChild(goSoup);

  // CokeAndMovies
  var extraLink5 = document.createElement("li");
  extraLinks.appendChild(extraLink5);
  var goCoca = document.createElement("a");
  goCoca.setAttribute("href",
    "http://cokeandmovies.canalblog.com/archives/2006/11/27/");
  goCoca.appendChild(document.createTextNode("≣ ... by Coca-Cola"));
  extraLink5.appendChild(goCoca);
  
  // Zippo.com
  var extraLink6 = document.createElement("li");
  extraLinks.appendChild(extraLink6);
  var goZippo = document.createElement("a");
  goZippo.setAttribute("href","http://www.zippo.com/Article.aspx?contentGuid={83DB3D01-1D48-4C18-B325-B1DF10D4334A}&bhcp=1");
  goZippo.appendChild(document.createTextNode("≣ ... by Zippo"));
  extraLink6.appendChild(goZippo);

  // ===== Personal keywords
  extras.appendChild(document.createTextNode("Personal keywords :"));
  var keywords=document.createElement("textarea");
  keywords.setAttribute("name","extras_keywords");
  keywords.setAttribute("spellcheck","false");
  keywords.appendChild(document.createTextNode(getCookie("extras_keywords_"+unsafeWindow.shot_id)));
  extras.appendChild(keywords);
  keywords.addEventListener("keyup", keywordsChanged, false);
  function keywordsChanged() {
    var keywords = this.value;
    if(keywords.trim().length == 0) {
      setCookie("extras_keywords_"+unsafeWindow.shot_id, keywords, -1);
    }
    else {
      setCookie("extras_keywords_"+unsafeWindow.shot_id, keywords, 365);
    }
  }
  
  // ===== Personal keywords search
  extras.appendChild(document.createTextNode("Keywords search :"));
  var keywordsSearch=document.createElement("input");
  keywordsSearch.setAttribute("name","extras_keywordsSearch");
  keywordsSearch.setAttribute("id","extras_keywordsSearch");
  keywordsSearch.setAttribute("type","text");
  extras.appendChild(keywordsSearch);
  var keywordsSearchButton=document.createElement("a");
  keywordsSearchButton.setAttribute("class","shiny_button_black");
  var keywordsSearchSpan=document.createElement("span");
  keywordsSearchSpan.appendChild(document.createTextNode("⎆"));
  keywordsSearchButton.appendChild(keywordsSearchSpan);
  extras.appendChild(keywordsSearchButton);
  var resultBox=document.createElement("ul");
  resultBox.setAttribute("style", "position:absolute;padding-left:5px");
  extras.appendChild(resultBox);
    
  keywordsSearchButton.addEventListener("click", searchMoviesByKeywords, false);
  function searchMoviesByKeywords() {
    var keyword = document.getElementById("extras_keywordsSearch").value;
    var cookieIds = searchInCookies(keyword);
    
    while(resultBox.hasChildNodes()) {
      resultBox.removeChild(resultBox.lastChild);
    }
    for(i in cookieIds) {
      var key=cookieIds[i];
      if(key.indexOf("extras_keywords_")==0) {
        var movieId=cookieIds[i].substring("extras_keywords_".length);
        var resultLi=document.createElement("li");
        resultBox.appendChild(resultLi);
        var resultLink=document.createElement("a");
        resultLink.setAttribute("href","/shot/"+movieId);
        resultLi.appendChild(resultLink);
        resultLink.appendChild(document.createTextNode(movieId));
      }
    }
  }
  
  // ==== CSS for added elements
  var styleObj=document.createElement("style");
  styleObj.setAttribute("type","text/css");
  document.getElementsByTagName("head")[0].appendChild(styleObj);
  styleObj.appendChild(document.createTextNode("\
    #extras {                   \n\
      width:120px;              \n\
      position:absolute;        \n\
      right:-120px;             \n\
      top:0;                    \n\
      z-index:10;               \n\
    }                           \n\
    #extras li a {              \n\
      float:none;               \n\
      width:120px;              \n\
      text-align:left;          \n\
    }                           \n\
    #extras ul {                \n\
      position:static;          \n\
    }                           \n\
    #extras textarea {          \n\
      width:125px;              \n\
      height:100px;             \n\
      background: #1b1b1b;      \n\
      border: 1px solid #696969;\n\
      padding: 3px;             \n\
      color: white;             \n\
    }                           \n\
    #extras_keywordsSearch {    \n\
      width:80px;               \n\
    }                           \n\
    #extras > a {               \n\
      float:right;              \n\
      width:25px;               \n\
    }                           \n\
    "
  ));  
  
}
)();

