// http://d.hatena.ne.jp/secondlife/20050503/1115085179
// javascript:(function(){var s=document.createElement("script");s.charset="UTF-8";s.src="http://example.com/example.js";document.body.appendChild(s)})();

var HatenaSyntaxBadge = function(id){
    var HSBElement = document.getElementById(id);
    if( HSBElement ){
        this.deleteHatenaSyntaxBadge(HSBElement);
    }
    else{
        this.createHatenaSyntaxBadge(id);
    }
};

HatenaSyntaxBadge.prototype = {
    deleteHatenaSyntaxBadge: function(element){
        element.parentNode.removeChild(element);
    },
    createHatenaSyntaxBadge: function(id){
        var badge = this.analyzeLocation(location.href);
        if( badge != '' ){
            var body = document.getElementsByTagName('body')[0];
            var block = document.createElement('div');

            block.id = id;
            block.style.position =
                ( document.all && !window.opera )
                ? 'absolute' : 'fixed';
            block.style.top = '1em';
            block.style.right = '1em';
            block.style.border = '1px solid #666666';
            block.style.opacity = '0.8';
            block.style.padding = '0.5em 1em';
            block.style.color = 'black'
            block.style.backgroundColor = 'white';
            block.style.fontSize = 'smaller'

            block.appendChild(document.createTextNode(badge));
            body.appendChild(block);
        }
    },
    analyzeLocation: function(uri){
        return this.hatenaGroup(uri)
            || this.hatenaDiary(uri)
            || this.hatenaBookmark(uri)
            || this.hatenaFotolife(uri)
            || this.hatenaIdea(uri)
            || this.hatenaMap(uri)
            || this.hatenaGraph(uri)
            || this.hatenaQuestion(uri)
            || this.hatenaSearch(uri)
            || this.definiteNotation(uri)
            || this.urlNotationMovie(uri)
            || this.urlNotation(uri);
    },
    hatenaGroup: function(uri){
        if(uri.match(/^https?:\/\/([^\.]+)\.g\.hatena\.ne\.jp\/([^\/]+)\/([^\?\#]*)/)){
            var group = RegExp.$1;
            var user = RegExp.$2;
            var path = RegExp.$3;

            var hatena = '';
            if ( path != '' ) {
                if ( user == 'task' ) {
                    hatena = 'task:' + path.split('/').join(':');
                }
                else if ( user == 'keyword' ) {
                    hatena = '[g:' + group + ':keyword:' + decodeURIComponent( path ) + ']';
                }
                else {
                    hatena = 'g:' + group + ':id:' + user
                        + ':' + path.split('/').join(':');
                }
            }
            return hatena.replace(/:$/,'');
        }
        else {
            return;
        }
    },
    hatenaDiary: function(uri){
        if(uri.match(/^https?:\/\/d\.hatena\.ne\.jp\/([^\/]+)\/([^\?\#]*)/)){
            var user = RegExp.$1;
            var path = RegExp.$2;

            var hatena = '';
            if ( path != '' ) {
                if ( user == 'keyword' ) {
                    var word = document.getElementsByTagName('h1')[0].innerHTML;
                    if ( !word ) return;

                    hatena = '[d:keyword:' + word + ']';
                }
                else if ( user == 'asin' ) {
                    hatena = 'asin:' + path + ':detail';
                }
                else if ( user == 'ean' ) {
                    hatena = 'ean:' + path;
                }
                else {
                    hatena = 'd:id:' + user + ':' + path.replace(/\//g,':');
                }
            }
            return hatena.replace(/:$/,'');
        }
        else {
            return;
        }
    },
    hatenaBookmark: function(uri){
        if(uri.match(/^https?:\/\/b\.hatena\.ne\.jp\/([^\/]+)\/([^\?\#]*)/)){
            var user = RegExp.$1;
            var path = RegExp.$2;

            var hatena = '';
            if ( path != '' ) {
                if ( user == 'entry' ) {
                    return this.urlNotation(uri);
                }
                else if ( user == 'keyword' ) {
                    hatena = '[b:keyword:' + decodeURIComponent( path ) + ']';
                }
                else if ( user == 't' ) {
                    hatena = '[b:t:' + decodeURIComponent( path ) + ']';
                }
                else {
                    hatena = 'b:id:' + user + ':' + path.replace(/\//g,':');
                }
            }
            return hatena.replace(/:$/,'');
        }
        else {
            return;
        }
    },
    hatenaFotolife: function(uri){
        if(uri.match(/^https?:\/\/f\.hatena\.ne\.jp\/([^\/]+)\/([^\?\#]*)/)){
            var user = RegExp.$1;
            var path = RegExp.$2;

            var hatena = '';
            if ( path != '' ) {
                if ( user == 'keyword' ) {
                    hatena = '[f:keyword:' + decodeURIComponent( path ) + ']';
                }
                else if ( user == 't' ) {
                    hatena = '[f:t:' + decodeURIComponent( path ) + ']';
                }
                else {
                    hatena = 'f:id:' + user + ':' + path.replace(/\//g,':');
                }
            }
            return hatena.replace(/:$/,'');
        }
        else {
            return;
        }
    },
    hatenaIdea: function(uri){
        if(uri.match(/^https?:\/\/i\.hatena\.ne\.jp\/([^\/]+)\/([^\?\#]*)/)){
            var user = RegExp.$1;
            var path = RegExp.$2;

            var hatena = '';
            if ( path != '' ) {
                if ( user == 't' ) {
                    hatena = '[i:t:' + decodeURIComponent( path ) + ']';
                }
                else if ( user == 'idea' ) {
                    hatena = 'idea:' + path;
                }
                else {
                    hatena = 'i:id:' + user + ':' + path.replace(/\//g,':');
                }
            }
            return hatena.replace(/:$/,'');
        }
        else {
            return;
        }
    },
    hatenaMap: function(uri){
        if(uri.match(/^https?:\/\/map\.hatena\.ne\.jp\/(.*)$/)){
            var path = RegExp.$1;

            var hatena = '';
            if ( path.match(/^t\/(.*)$/) ) {
                var word = decodeURIComponent( RegExp.$1 );
                hatena = '[map:t:' + word + ']';
            }
            else if ( path.match(/^\?.*word=([^&]+).*$/) ) {
                var word = RegExp.$1;
                hatena = '[map:' + decodeURIComponent( word ) + ']';
            }
            else {
                path.match(/type=([^&]+)/);
                var type = RegExp.$1 || 'satellite';
                return document.getElementById('hatenasyntax').innerHTML + ':' + type;
            }
            return hatena;
        }
        else {
            return;
        }
    },
    hatenaGraph: function(uri){
        if(uri.match(/^https?:\/\/graph\.hatena\.ne\.jp\/([^\/]+)\/([^\?\#]*)/)){
            var user = RegExp.$1;
            var path = RegExp.$2;

            var hatena = 'graph:id:' + user;
            if ( path != '' ) {
                hatena = '[' + hatena + ':'
                    + path.replace(/\//g,':').replace(/:$/,'') + ':image]';
            }
            return hatena;
        }
        else {
            return;
        }
    },
    hatenaQuestion: function(uri){
        if(uri.match(/^https?:\/\/q\.hatena\.ne\.jp\/(\d+)\??.*$/)){
            var qid = RegExp.$1;
            return 'question:' + qid;
        }
        else {
            return;
        }
    },
    hatenaSearch: function(uri){
        if(uri.match(/^https?:\/\/search\.hatena\.ne\.jp\/([^\?]+)?.*$/)){
            var type = RegExp.$1;

            var word = '';
            var inputs = document.getElementsByTagName('input');
            for ( var i = 0; i < inputs.length; i++ ) {
                if ( inputs[i].type == 'text' && inputs[i].name == 'word' ) {
                    word = inputs[i].value;
                    break;
                }
            }

            if ( word == '' ) return;

            var hatena = '';
            if( type != 'search' ) {
                hatena = '[search:' + type + ':' + word + ']';
            }
            else {
                hatena = '[search:' + word + ']';
            }
            return hatena;
        }
        else {
            return;
        }
    },
    definiteNotation: function(uri){
        uri.match(/^https?:\/\/([a-z]+)\.hatena\.ne\.jp\/([^\/]+)\/([^\?\#]*)$/);
        var service = RegExp.$1;
        var user = RegExp.$2;
        var path = RegExp.$3;

        if ( user ) {
            var hatena = service + ':id:' + user;
            if ( path != '' ) {
                hatena += ':' + path.replace(/\//,':');
            }

            return hatena.replace(/:$/,'');
        }
        else {
            return;
        }
    },
    urlNotationMovie: function(uri) {
        var domains = [
            'youtube.com/watch',
            'www.youtube.com/watch',
            'video.google.com/videoplay',
            'vision.ameba.jp/watch.do',
            'www.flipclip.net/clips/'
        ];
        var regexp = new RegExp( '^https?:\/\/(' + domains.join('|').replace(/\./,'\\\.') + ')' );
        if ( uri.match(regexp) )
            return '[' + uri + ':movie]';
        else
            return;
    },
    urlNotation: function(uri){
        return '[' + uri + ':title]';
    }
};

new HatenaSyntaxBadge('HatenaSyntaxBadgeBlock');
