var detect_load = function(result) {
    result = Json.evaluate(result);
    if(result["wait"] > 5) {
        var minutes = parseInt(Number(result["wait"]) / 30) + 1;
        $$("strong.wait").each(function(elem){
            elem.setHTML(minutes);
        });
        $('boxLoading').setStyle("display", "none");
        $('boxSorry').setStyle("display", "block");
    }
    else {
        if(result["sentenses"] != null) {
            setTimeout(interval_load("image", image_load), 10);
            setTimeout(interval_load("relimage", relimage_load), 50);
            var sentenses = result["sentenses"];
            var s = sentenses.shift();
            var date = result["date"];
            var target = document.title.replace(/^WEB素行調査 - (.+)について調査中$/, function() {
                return arguments[1];
            });
            document.title = 'WEB素行調査 - 調査結果 : ' + target;
            $("date").setHTML("<strong>調査日時</strong> ： " + date);
            $('boxLoading').setStyle('display', 'none');
            (function() {
                 $('lstResult').innerHTML += '<li>'+s+'</li>';
                 s = sentenses.shift();
                 if(s) {
                     setTimeout(arguments.callee, 200);
                 }
             })();
        }
        else {
            setTimeout(interval_load("detect", arguments.callee), 1000);
        }
    }
};

var image_load = function(result) {
    result = Json.evaluate(result);
    if(result["image"] != null) {
        var img = new Image();
        if(typeof(result["image"]) == 'string') {
            img.src = result["image"];
            var w = Number(img.width);
            var h = Number(img.height);
            var iW, iH, iMT = 0, iML = 0;
            var ratio_ref = 195 / 170;
            var ratio = w / h;
            if(w - h > 0 && ratio > ratio_ref) {
                iW = 195;
                iH = (195 / w) * h;
                iMT = (170 - iH) / 2;
            }
            else if(w == h) {
                iW = iH = 170;
                iML = 12.5;
            }
            else {
                iH = 170;
                iW = (170 / h) * w;
                iML = (195 - iW) / 2;
            }
            img.height = iH;
            img.width = iW;
            img.style.marginTop = iMT + 'px';
            img.style.marginLeft = iML + 'px';

            $($('boxPhResult').getElementsByTagName("p")[0]).appendChild(img);
        }
        else {
            var p_id = Math.floor(Math.random() * 20);
            p_id = (p_id < 10)? "0" + p_id : p_id;
            var p_path = "/img/rdm/rdm0" + p_id + ".jpg";
            img.src = p_path;
            $($('boxPhResult').getElementsByTagName("p")[0]).appendChild(img);
        }
    }
    else {
        setTimeout(interval_load("image", arguments.callee), 1000);
    }
};

var relimage_load = function(result) {
    result = Json.evaluate(result);
    if(result["image"] != null) {
        if(typeof(result["image"]) != 'string') {
            $('ttlResultPh').setStyle("display", "block");
            result["image"].each(function(img){
                var li = '<li><img src="'+img+'" alt="関連画像" /></li>';
                $('lstResultPh').innerHTML += li;
            });
        }
    }
    else {
        setTimeout(interval_load("relimage", arguments.callee), 1000);
    }
};

var interval_load = function(x, func) {
    return function () {
        var req_path = "/api/" + x + '?ticket=' + ticket;
        var req_conf = {
            method : 'get',
            onComplete : func
        };
        new Ajax(req_path, req_conf).request();
    };
};

var save_page = function() {
    var func = function(response) {
        response = Json.evaluate(response);
        if(response["status"] == 'ok') {
        }
        else {
        }
    };
    var sentenses = $('lstResult').innerHTML;
    var images_top = $('boxPhResult').getElementsByTagName("img")[0].src;
    var images_under = $('lstResultPh').innerHTML;
    var post_data = {
        'ticket'    : ticket,
        'sentenses' : sentenses,
        'imgtop'    : images_top,
        'imgunder'  : images_under
    };
    post_data = Object.toQueryString(post_data);
    var req_path = "/api/save";
    var req_conf = {
        method : 'post',
        onComplete : func,
        data : post_data
    };
    //new Ajax(req_path, req_conf).send();
};

window.addEvent('domready', function() {
    setTimeout(interval_load("detect", detect_load), 100);
});
