// ==UserScript==
// @name         kwout
// @namespace    http://kwout.com/
// @description  A brilliant way to quote
// @include      http://*twitter.com/*
// @include      https://*twitter.com/*
// @include      http://*jaiku.com/*
// @include      http://*tumblr.com/*
// @include      http://favotter.matope.com/*
// ==/UserScript==
// Version 1.0.1

function kwout() {
    if (window.location.href.match(/^https?:\/\/twitter\.com\/.+\/status\/[0-9]+$/)) {
        var elements = document.getElementsByTagName('span');
        for (var index = 0; index < elements.length; index++) {
            if (hasClassAttribute(elements[index], 'entry-meta')) {
                appendKwoutButton(elements[index], window.location.href, 'content');
                break;
            }
        }
    } else if (window.location.href.match(/^https?:\/\/twitter\.com\/#replies$/) ||
               window.location.href.match(/^https?:\/\/twitter\.com\/#favorites$/) ||
               window.location.href.match(/^https?:\/\/twitter\.com\/.*$/)) {
        var elements = document.getElementsByTagName('a');
        for (var index = 0; index < elements.length; index++) {
            if (hasClassAttribute(elements[index], 'entry-date')) {
                appendKwoutButton(elements[index].parentNode, elements[index].getAttribute('href'), 'content');
            }
        }
    } else if (window.location.href.match(/^http:\/\/search.twitter\.com\/.*$/)) {
        var elements = document.getElementsByTagName('a');
        for (var index = 0; index < elements.length; index++) {
            if (hasClassAttribute(elements[index], 'lit')) {
                appendKwoutButton(elements[index].parentNode, elements[index].getAttribute('href'), 'content');
            }
        }
    } else if (window.location.href.match(/^http:\/\/.+\.jaiku\.com\/presence\/[0-9a-z]+$/)) {
        var elements = document.getElementById('current-stream').getElementsByTagName('p');
        for (var index = 0; index < elements.length; index++) {
            if (hasClassAttribute(elements[index], 'meta')) {
                appendKwoutButton(elements[index], window.location.href, 'current-stream');
            }
        }
    } else if (window.location.href.match(/^http:\/\/.+\.jaiku\.com\/$/)) {
        var elements = document.getElementsByTagName('p');
        for (var index = 0; index < elements.length; index++) {
            if (hasClassAttribute(elements[index], 'meta')) {
                var h3 = elements[index].parentNode.getElementsByTagName('h3');
                if (h3 && h3[0]) {
                    var a = h3[0].getElementsByTagName('a');
                    if (a && a[0]) appendKwoutButton(elements[index], a[0].href, 'current-stream');
                }
            }
        }
    } else if (window.location.href.match(/^http:\/\/.+\.tumblr\.com\/.*$/)) {
        var elements = document.getElementsByTagName('div');
        var link = 0;
        var quote = 0;
        var photo = 0;
        var video = 0;
        for (var index = 0; index < elements.length; index++) {
            if (hasClassAttribute(elements[index], 'link')) {
                appendKwoutButton(elements[index], window.location.href, 'link,' + link + '&preset=0');
                link++;
            } else if (hasClassAttribute(elements[index], 'quote')) {
                appendKwoutButton(elements[index], window.location.href, 'quote,' + quote + '&preset=0');
                quote++;
            } else if (hasClassAttribute(elements[index], 'photo')) {
                appendKwoutButton(elements[index], window.location.href, 'photo,' + photo + '&preset=0');
                photo++;
            } else if (hasClassAttribute(elements[index], 'video')) {
                appendKwoutButton(elements[index], window.location.href, 'video,' + video + '&preset=0');
                video++;
            }
        }
    } else if (window.location.href.match(/^http:\/\/favotter\.matope\.com\/status\.php\?id=[0-9]+$/)) {
        var elements = document.getElementsByTagName('a');
        for (var index = 0; index < elements.length; index++) {
            if (hasClassAttribute(elements[index], 'taggedlink')) {
                appendKwoutButton(elements[index].parentNode, elements[index].getAttribute('href'), 'content');
            }
        }
    }
};

function appendKwoutButton(element, address, block) {
    var space = document.createElement('span');
    space.innerHTML = '&nbsp;';
    var button = document.createElement('a');
    button.href = 'http://kwout.com/grab?address=' + encodeURIComponent(address) + '&block=' + block;
    button.target = '_blank';
    button.style.textDecoration = 'none';
    button.innerHTML = '<img src="http://kwout.com/images/api.gif" alt="kwout this!" title="kwout this!" height="16" width="19" style="border: none; vertical-align: middle;" />';
    element.appendChild(space);
    element.appendChild(button);
};

function hasClassAttribute(element, class) {
    var attribute = element.getAttribute('class');
    if (!attribute) attribute = element.getAttribute('className');
    if (!attribute) return false;
    var values = attribute.split(' ');
    for (var index = 0; index < values.length; index++) if (values[index] == class) return true;
    return false;
};

var version = '1.0.1';
var option = {
    url : 'http://kwout.com/gm_version?t=' + (new Date()).getTime(),
    method : 'get',
    onload : function (response) {
        if (response.status == 200) {
            GM_setValue('kwout.checked', (new Date()).getTime().toString());
            var latest = response.responseText.replace(/[^\d\.]/, '');
            kwout();
            if (latest != version && GM_getValue('kwout.notified', 'false') != version) {
                GM_setValue('kwout.notified', version);
                alert('Please update "kwout.user.js" because it has a new version "' + latest +'".');
            }
        } else {
            kwout();
        }
    },
    onerror : function (response) {}
};

var checked = parseInt(GM_getValue('kwout.checked', '0'));
if (checked < (new Date()).getTime() - 3600000) {
    GM_xmlhttpRequest(option);
} else {
    kwout();
}
