// ==UserScript==
// @name          StackStockBooks from Amazon
// @namespace     http://blog.riywo.com/
// @version       0.1
// @description	  Register the book to StackStockBooks from Amazon.
// @include       http://*.amazon.co.jp/*
// ==/UserScript==

(function(){
    //--Setting---------------------------------------------------
    var userid = "id"; // Your ID of SSB
    var token = "token"; // Your Token of SSB API
    var state = "wish"; // State of the book you want to regester
    //--End of Setting-------------------------------------------
    
    var asin='';
    makeLink();
    
    function makeLink(){
//        document.body.parentNode.innerHTML.match(/ISBN-13:[^9]*(978-4\d{9})/);
        document.body.parentNode.innerHTML.match(/ASIN:[^4]*(4(\d{8}|-[\d-]{9}-)[\dX])/);
        if (RegExp.$1 != ''){
            asin = RegExp.$1.replace(/-/g,'');
            var header = document.evaluate("//b[@class='sans']",
                                           document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
//            var header = document.evaluate("//div[@class='buying']//b[@class='sans']",
//                                           document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
            if(header){
                var ssb_link = document.createElement('span');
                ssb_link.setAttribute('id', 'ssb_link');
                ssb_link.innerHTML = 'Stack Stock Books!';
                ssb_link.addEventListener('click', StackStockBooks, false);
                header.parentNode.insertBefore(ssb_link, header.nextSibling);
            }
        }
    }
    
    function StackStockBooks(){
        var ssb_link = document.getElementById("ssb_link");
        var url = "http://stack.nayutaya.jp/api/" + userid + "/" + token + "/stocks/update.1";
        var nowDate = new Date();
        var year = nowDate.getFullYear();
        var month = nowDate.getMonth() + 1;
        var date = nowDate.getDate();
        if(month < 10){
            month = "0" + month;
        }
        if(date < 10){
            date = "0" + date;
        }
        var fullDate = year + "-" + month + "-" + date;
        var inputJSON = "[{asin: \""+asin+"\", date: \""+fullDate+"\", state: \""+state+"\", public: true}]";
        alert(inputJSON);
        GM_xmlhttpRequest({
            method: "POST",
            url: url,
            headers: {
                    "User-agent": "Mozilla/4.0 (compatible) Greasemonkey",
                    "Accept": "application/atom+xml,application/xml,text/xml",
                    "Content-Type": "application/x-www-form-urlencoded",
            },
            data: "request=" + encodeURIComponent(inputJSON),
            onload: function(response){
                var getJSON = eval('('+response.responseText+')');
                var message = getJSON.message;
                ssb_link.innerHTML = message;
                if(getJSON.success == true){
                    ssb_link.removeEventListener("click", StackStockBooks, false);
                }
//                alert(getJSON.version+"\n"+getJSON.success+"\n"+getJSON.response.message+"\n"+getJSON.response.title);
            }
        });
    }
})();
