var reduce_co2 = {
    head_obj    : null,
    hundler_obj : null,
    co2_data    : new Array(),
    co2_num     : 0,
    jsonp_url   : "http://lab.adingo.jp/api/leafbank/average_jsonp.php?callback=reduce_co2.hundler",
    
    load : function()
	{
        this.co2_num     = document.getElementById("reduce_co2_value");
        this.head_obj    = document.getElementsByTagName('head')[0];
        var script       = document.createElement( 'script' );
        script.type      = 'text/javascript';
        script.charset   = 'utf-8';
        script.src       = this.jsonp_url;
        this.hundler_obj = this.head_obj.appendChild(script);
    },

    hundler : function(data)
	{
        this.co2_data = data;
        setTimeout(this.reduceTimer,1000);
        this.head_obj.removeChild(this.hundler_obj);
    },

	reduceTimer : function()
	{
        reduce_co2.co2_data['reduce_co2'] = Math.round((reduce_co2.co2_data['reduce_co2'] + reduce_co2.co2_data['average_co2']) * 100) / 100;
        reduce_co2.co2_num.innerHTML      = reduce_co2.addFigure(reduce_co2.co2_data['reduce_co2'].toFixed(0));
        setTimeout(reduce_co2.reduceTimer,1000);
    },

    addFigure : function(str)
    {
        var num = new String(str).replace(/,/g, "");
        while(num != (num = num.replace(/^(-?\d+)(\d{3})/, "$1,$2")));
        return num;
    }
}

if (window.addEventListener) {
	window.addEventListener("load", function(){ reduce_co2.load(); }, false);
} else if(window.attachEvent) {
	window.attachEvent("onload", function(){ reduce_co2.load(); });
}

