// ==UserScript==
// @name           tiwtx
// @namespace      http://www.sakito.com/
// @description    twitter poster
// @include        *
// ==/UserScript==
//
// ***** BEGIN LICENSE BLOCK *****
// Licensed under Version: GPL v3
// Full Terms at http://www.gnu.org/copyleft/gpl.html
// ***** END LICENSE BLOCK *****
//
// author: sakito
//
// this script based on
// tiwt.user.js(http://jugyo.org/blog/2695)
// original author: jugyo http://jugyo.org/blog/
//

(function() {

  var URL = "http://sakito.jp/products/tiwtx.user.js.html";
  var VERSION = "20080716";

  if (self.location.href != top.location.href) {
    return;
  }
  if (window != window.parent) {
    return;
  }

  function init() {
    init_tiwtx_content();
    init_tiwtx_poster();
  }

  function init_tiwtx_content() {
    var element = c("div", "tiwtx_content");
    with (element) {
      with (style) {
        zIndex = 1;
        backgroundColor = "#FF6600";
        position = "fixed";
        top = "80px";
        right = "0px";
        overflow = "auto";
        MozOpacity = 0.80;
        minHeight = "16px";
        minWidth = "16px";
        padding = "2px";
      }
      addEventListener("mouseover", show_tiwtx, false);
      addEventListener("mouseout", hide_tiwtx, false);
    }
    a(document.body, element);
  }

  function init_tiwtx_poster() {
    var element = c("div", "tiwtx_poster");
    with (element) {
      with (style) {
        padding = "2px";
        display = "none";
        textAlign = "right";
      }
    }

    var textarea = c("textarea", "tiwtx_textarea");
    with (textarea) {
      with (style) {
        value = "";
        display = "block";
        width = "400px";
        height = "60px";
        marginBottom = "2px";
        border = "solid 1px gray";
        borderBottomColor = "silver";
        borderRightColor = "silver";
      }
    }
    a(element, textarea);

    var img = c("img", "tiwtx_indicator");
    with (img) {
      with (style) {
        display = "none";
        margin = "0 2px";
      }
      src = 'data:image/gif;base64,'+
        'R0lGODlhEAAQAPQAAP///3d3d/r6+pSUlL29vXp6eouLi+jo6NDQ0IKCgrW1ta2trfDw8MfHx+Dg4J2dnaSkpAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH+GkNyZWF0ZWQgd2l0aCBhamF4bG9hZC5pbmZvACH5BAAKAAAAIf8LTkVUU0NBUEUyLjADAQAAACwAAAAAEAAQAAAFUCAgjmRpnqUwFGwhKoRgqq2YFMaRGjWA8AbZiIBbjQQ8AmmFUJEQhQGJhaKOrCksgEla+KIkYvC6SJKQOISoNSYdeIk1ayA8ExTyeR3F749CACH5BAAKAAEALAAAAAAQABAAAAVoICCKR9KMaCoaxeCoqEAkRX3AwMHWxQIIjJSAZWgUEgzBwCBAEQpMwIDwY1FHgwJCtOW2UDWYIDyqNVVkUbYr6CK+o2eUMKgWrqKhj0FrEM8jQQALPFA3MAc8CQSAMA5ZBjgqDQmHIyEAIfkEAAoAAgAsAAAAABAAEAAABWAgII4j85Ao2hRIKgrEUBQJLaSHMe8zgQo6Q8sxS7RIhILhBkgumCTZsXkACBC+0cwF2GoLLoFXREDcDlkAojBICRaFLDCOQtQKjmsQSubtDFU/NXcDBHwkaw1cKQ8MiyEAIfkEAAoAAwAsAAAAABAAEAAABVIgII5kaZ6AIJQCMRTFQKiDQx4GrBfGa4uCnAEhQuRgPwCBtwK+kCNFgjh6QlFYgGO7baJ2CxIioSDpwqNggWCGDVVGphly3BkOpXDrKfNm/4AhACH5BAAKAAQALAAAAAAQABAAAAVgICCOZGmeqEAMRTEQwskYbV0Yx7kYSIzQhtgoBxCKBDQCIOcoLBimRiFhSABYU5gIgW01pLUBYkRItAYAqrlhYiwKjiWAcDMWY8QjsCf4DewiBzQ2N1AmKlgvgCiMjSQhACH5BAAKAAUALAAAAAAQABAAAAVfICCOZGmeqEgUxUAIpkA0AMKyxkEiSZEIsJqhYAg+boUFSTAkiBiNHks3sg1ILAfBiS10gyqCg0UaFBCkwy3RYKiIYMAC+RAxiQgYsJdAjw5DN2gILzEEZgVcKYuMJiEAOwAAAAAAAAAAAA==';
    }
    a(element, img);

    var button = c("button", "tiwtx_button");
    with (button) {
      innerHTML = "Post";
      with (style) {
        backgroundColor = "silver";
        border = "solid 1px gray";
      }
      //addEventListener("click", post, false);
      addEventListener("click", verifi, false);
    }
    a(element, button);

    var version = c("div","tiwtx_version");
    with(version) {
      innerHTML = '<a href="' + URL + '">tiwtx</a> ver ' + VERSION;
      with(style) {
        float = "left";
      }
    }
    a(element, version);

    a(e("tiwtx_content"), element);
  }

  function verifi() {
    var script = c("script","tiwtx_verifi");
    with(script) {
      src = 'https://twitter.com/account/verify_credentials.json?callback=void';
      charset = 'utf-8';
      type = 'application/javascript';
      addEventListener('load', post, false);
    }
    a(e("tiwtx_content"), script);
    r(e("tiwtx_content"), script);
  }

  function post() {
    e("tiwtx_button").style.display = "none";
    e("tiwtx_indicator").style.display = "inline";

    GM_xmlhttpRequest({
      method: 'POST',
          url: 'https://twitter.com/statuses/update.json',
          data: 'status=' + encodeURIComponent(e("tiwtx_textarea").value),
          headers: {
          'Content-Type' : 'application/x-www-form-urlencoded'
            },
          onload : function (req) {
          e("tiwtx_button").style.display = "inline";
          e("tiwtx_indicator").style.display = "none";
          e("tiwtx_textarea").value = "";
          e("tiwtx_textarea").focus();
        },
          });
  }

  function show_tiwtx() {
    d("tiwtx_poster", "block");
    e("tiwtx_textarea").focus();
  }

  function hide_tiwtx() {
    d("tiwtx_poster", "none");
  }

  function init_style(style) {
    with(style) {
      fontSize = "12px";
      textAlign = "left";
      padding = 0;
      margin = 0;
      lineHeight = "15px";
      color = "black";
      backgroundColor = "none";
      border = "none";
    }
  }

  function a(parent, child) {
    parent.appendChild(child);
  }

  function r(parent, child) {
    parent.removeChild(child);
  }

  function c(tag_name, element_id) {
    element = document.createElement(tag_name);
    with (element) {
      id = element_id;
      init_style(style);
    }
    return element;
  }

  function u(id, text) {
    e(id).innerHTML = text;
  }

  function e(id) {
    return document.getElementById(id);
  }

  function d(id, value) {
    e(id).style.display = value;
  }

  init();
 })();

