// ==UserScript==
// @name           Amazon with Amazre
// @namespace      http://pm.11op.net/
// @include        http://www.amazon.co.jp/*
// @description  show rating of 'Amazre'  in Amazon item page
// @version 0.1.1
// @use  UserScript Update Notification fu by noriaki
//  http://blog.fulltext-search.biz/articles/2007/10/06/upgrade-automatically-checking-for-available-updates-in-greasemonkey
//
// ==/UserScript==
+function() {
    var d = document;
    var $ = function(id){
        return d.getElementById(id);
    };
//    var log = unsafeWindow.console.log;

    var $$ = function(id){
        var node = d.evaluate(id, d, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,null);

        var item = [];
        for ( var i=0 ; i < node.snapshotLength; i++ ) {
            item.push(node.snapshotItem(i));
        }
        return item;
    };
    var elm = $$('//input[@name="ASIN"]');
    if (!elm.length) {
          return;
    } else {
        var asin = elm[0].value;
    }

    var url = 'http://amazre.com/blogparts/view/asin/'+asin;
//log(url);

    GM_xmlhttpRequest( {
        method: 'GET',
        url: url,
        headers: {
        'User-agent': 'Mozilla/4.0 (compatible) Greasemonkey/0.3',
        'Accept': 'application/atom+xml,application/xml,text/xml',
        },
        onload: function(v) {
//        log(1111111);
        var div = d.createElement('div');
        div.id='fromAmazre';
        div.innerHTML = v.responseText.replace(/<script(.|\s)*?\/script>/, '');
        var pr = $('customerReviews');
        pr.insertBefore(div, pr.childNodes[3]);
        GM_addStyle('#fromAmazre{position:fixed; right:10px; top: 50px;opacity: 0.9;}');

        }
    });

    /**
      * update checker
      */
          ///////////// Begining of [Section] ////////////////

    function UpdateChecker() {};
    UpdateChecker.prototype = {
        script_name: 'your user.script name',        // required
        script_url: 'http://**/*.user.js',           // required
        current_version: '0.1.0',                    // required
        more_info_url: null,                         // optional

        remote_version: null,                        // no rewrite

        // Render update information in HTML
        render_update_info: function() {
            var newversion = document.createElement('div');
            newversion.setAttribute('id', 'gm_update_alert');
            var update_message = document.createElement('p');
            update_message.innerHTML = [
                '\u73FE\u5728\u304A\u4F7F\u3044\u306E',
                'Greasemonkey\u30B9\u30AF\u30EA\u30D7\u30C8 \u0027',
                this.script_name,
                '(var. ', this.current_version, ')',
                '\u0027 \u306F\u65B0\u3057\u3044\u30D0\u30FC\u30B8\u30E7\u30F3 ',
                this.remote_version,
                ' \u304C\u516C\u958B\u3055\u308C\u3066\u3044\u307E\u3059\uFF0E',
                '\u30A2\u30C3\u30D7\u30C7\u30FC\u30C8\u3057\u307E\u3059\u304B\uFF1F'
            ].join('');

            var update_link = document.createElement('a');
            update_link.setAttribute('id', 'gm_update_alert_link');
            update_link.setAttribute('href', this.script_url);
            update_link.addEventListener('click', function() {
                var update_alert = document.getElementById('gm_update_alert');
                update_alert.parentNode.removeChild(update_alert);
            }, false);
            update_link.innerHTML =
                '[o]\u4ECA\u3059\u3050\u30A2\u30C3\u30D7\u30C7\u30FC\u30C8\u3059\u308B';

            if(this.more_info_url) {
                var more_link = document.createElement('a');
                more_link.setAttribute('href', this.more_info_url);
                more_link.innerHTML = '\uFF08\u8A73\u7D30\u60C5\u5831\uFF09';
                update_message.appendChild(more_link);
            }

            var close_link = document.createElement('a');
            close_link.setAttribute('href', 'javascript:void(0);');
            close_link.addEventListener('click', function() {
                GM_setValue('last_check_day', new Date().days_since_start());
                var update_alert = document.getElementById('gm_update_alert');
                update_alert.parentNode.removeChild(update_alert);
            }, false);
            close_link.innerHTML = [
                '[x]\u4ECA\u306F\u30A2\u30C3\u30D7\u30C7\u30FC\u30C8\u3057\u306A\u3044',
                '\uFF08\u65E5\u4ED8\u304C\u5909\u308F\u308B\u307E\u3067\u6709\u52B9\uFF09'
            ].join('');

            newversion.appendChild(update_message);
            newversion.appendChild(update_link);
            newversion.appendChild(close_link);
            document.body.appendChild(newversion);
        },

        add_update_info_style: function() {
            GM_addStyle(<><![CDATA[
                /* style(like CSS) for update information */
                #gm_update_alert {
                    padding: 5px 0pt;
                    background-color: #FFF280;
                    color: #CC0099;
                    width: 100%;
                    position: absolute;
                    z-index: 99;
                    top: 0px;
                    left: 0px;
                    text-align: center;
                    font-size: 11px;
                    font-family: Tahoma;
                }

                #gm_update_alert p {
                    margin: 0pt;
                }

                #gm_update_alert a:link {
                    color: #333333;
                }

                #gm_update_alert > a:link {
                    margin: 0.5em 1em 0pt 1em;
                }

                #gm_update_alert p + a:link {
                    font-weight: bold;
                }
            ]]></>);
        },

        // Check script update remote
        check_update: function() {
            if(!this.has_need_for_check) return;
            var user_script = this;
            GM_xmlhttpRequest({
                method: 'GET',
                url: this.script_url,
                onload: function(res) {
                    user_script.remote_version = user_script.check_version(res.responseText);
                    if(user_script.remote_version && user_script.remote_version > user_script.current_version) {
                        user_script.add_update_info_style();
                        user_script.render_update_info();
                    } else {
                        GM_setValue('last_check_day', new Date().days_since_start());
                    }
                },
                onerror: function(res) { GM_log(res.status + ':' + res.responseText); }
            });
        },

        // Check the necessity for update: [Boolean]
        // return [true] if necessary
        has_need_for_check: function() {
            var last_check_day = GM_getValue('last_check_day');
            var current_day = new Date().days_since_start();
            if(typeof last_check_day == 'undefined' || current_day > last_check_day) {
                return true;
            } else {
                return false;
            }
        },

        // Check version in remote script file: [String]
        check_version: function(string) {
            if(/\/\/\s?@version\s+([\d.]+)/.test(string)) {
                return RegExp.$1;
            } else {
                return null;
            }
        }

    };

    Date.prototype.days_since_start = function() {
        var DAYS_IN_MONTH = [31,59,90,120,151,181,212,243,273,304,334,365];
        return(this.getYear() * 365 + DAYS_IN_MONTH[this.getMonth()] + this.getDate());
    };

    /////////// End of [Section] //////////////////

    var user_script = new UpdateChecker();

    user_script.script_name = 'Amazon with Amazre';
    user_script.script_url = 'http://pm.11op.net/js/amazon_with_amazre.user.js';
    user_script.more_info_url = '';
    user_script.current_version = '0.1.1';

    user_script.check_update();

}();
