var search_history={},cache_list={}; function search(){ var country=text.value; // var country="日本"; var token=country; var elm_msg=document.getElementById("message"); function message(msg){ elm_msg.innerHTML=msg; } function check_change_word(){ //if(token != text.value) throw token; } document.getElementById("items").innerHTML=""; message("loading "+country); next(function(){ // 語句を日本語へ変換 check_change_word(); if(country.match(/[^\x00-\x7f]/)){ return country; // たぶん日本語 }else{ return API.en2ja(country); } }).next(function(jcountry){ var cache_key=(document.getElementById("trans").checked) ? "1":"0"; cache_key+=jcountry; if(cache_list[cache_key]){ return cache_list[cache_key]; } return next(function(){ // if no cache start // 語句の準備 check_change_word(); return parallel({ country: API.ja2en(jcountry).next(function(c){ return c; }), people: API.country2people(jcountry).next(function(p){ return p; }) }); }).next(function(res){ // 得られた語句からフレーズ生成 check_change_word(); message("fetching country images.."); var queries=[]; var stack=function(q){ queries.push(API.suggest(q)); } stack(res.people+" should"); stack(res.people+" become"); stack(res.people+" people are"); stack("why are "+res.people); stack("what "+res.people); stack(res.country+" should"); stack(res.country+" become"); stack(res.country+" have"); stack(res.country+" is "); stack("why are "+res.country); return parallel(queries); }).next(function(items){ // 日本語訳するならここで。 check_change_word(); if(!document.getElementById("trans").checked) return items; message("日本語翻訳中..."); var q=[]; function stack(item){ q.push( next(function(){ return API.en2ja(item.quote); }).next(function(w){ item.quote=w; return item; }) ); } for(var i=0,len=items.length; i Math.pow(10,7)) ? 1 : (cnt > Math.pow(10,4))? 2 : 3 ; while(cnt.length > 3){ cnt=cnt.replace(/[0-9]{3}$/,function(m){ formatted.push(m); return ""; }); } formatted.push(cnt); cnt=formatted.reverse().join(","); var li=document.createElement("li"); li.className="rank"+rank; li.innerHTML=[ '',ret[i].quote,'', '',cnt,'' ].join(""); newul.appendChild(li); } cache_list[cache_key]=newul; return newul; }); // if no cache end }).next(function(newul){ message(""); var ul=document.getElementById("items"); ul.parentNode.replaceChild(newul.cloneNode(true),ul); }).next(function(){ // history var w=text.value; if(search_history[w]) return; search_history[w]=true; link=permalink.cloneNode(false); link.href="#"+w; link.innerHTML=w; link.addEventListener("click",function(){ restore_search(w); },false); var his=document.getElementById("history"); var li=document.createElement("li"); li.appendChild(link); his.appendChild(li); }).error(function(e){ //console.log(e); message(""); }); }// def search end function restore_search(w){ text.value=w; search(); }