﻿
window.onload = function() {
  
  childrenClose();
  
  $$("h3.Bgenre").each(function(obj){
    Event.observe(obj, "click", clickbig, false);
  });
  
  $$("span.Child").each(function(obj){
    Event.observe(obj, "mouseout", outsmall, false);
    Event.observe(obj, "mouseover", oversmall, false);
    Event.observe(obj, "mousedown", downsmall, false);
  });
  
  if(searchflag){
    search(genre,y,x);
  }
}

function clickbig(event){
  var eID = Event.element(event);
  var childrenid = 'Sgenre_' + eID.id.replace('Bgenre_','');
  
  if($(childrenid).style.display == 'none'){
    childrenClose();
    $(childrenid).style.display = '';
    $(eID.id).style.backgroundPosition = '0 0';
  } else {
    $(childrenid).style.display = 'none';
    $(eID.id).style.backgroundPosition = '0 -20px';
  }
}
function childrenClose(){
  $$("ul.Sgenre").each(function(obj){
    $(obj.id).style.display = 'none';
  });
  $$("h3.Bgenre").each(function(obj){
    $(obj.id).style.backgroundPosition = '0 -20px';
  });
}
function outsmall(event){
  var eID = Event.element(event);
  $(eID.id).style.backgroundPosition = '0 0';
}
function oversmall(event){
  var eID = Event.element(event);
  $(eID.id).style.backgroundPosition = '0 -15px';
}
function downsmall(event){
  var eID = Event.element(event);
  $(eID.id).style.backgroundPosition = '0 -30px';
}

function SearchStation(){
  
  var stationjson = 'http://api.doko.jp/extlightv1/getLandmark.do?method=getLandmark&key=4717c7ae2d0458766258b888da8e243d&format=json&callback=nearstation&lon_jgd='+lng+'&lat_jgd='+lat+'&pn=%e9%a7%85&radius=5000&pagesize=20';
  
  var scriptTag = document.createElement("script");
  scriptTag.setAttribute("type", "text/javascript");
  scriptTag.setAttribute("src", stationjson);
  document.getElementsByTagName("head").item(0).appendChild(scriptTag);
}
function nearstation(data){
  var landmark = data.results.landmarks.landmark;
  var i = 0;
  if(data.results.totalcount > 0){
    while(landmark[i]){
      $("nearstation").innerHTML += '<a href="http://navikun.jp/station/' + encodeURI(landmark[i].name) + '/">' + landmark[i].name + '</a> ';
      i++;
    }
  }
}

function jumpURL(url){
  window.open(url, null);
}




function search(key,lat,lon){
  if(key != ''){
    keyword = encodeURI(key);
    var url = 'http://api.doko.jp/extlightv1/getShopList.do?method=getShopList&key=4717c7ae2d0458766258b888da8e243d&format=json&callback=dokolist&pagesize=20&keyword='+keyword+'&lat_jgd='+lat+'&lon_jgd='+lon+'&order=distance';
  } else {
    keyword = encodeURI(key);
    var url = 'http://api.doko.jp/extlightv1/getShopList.do?method=getShopList&key=4717c7ae2d0458766258b888da8e243d&format=json&callback=dokolist&pagesize=20&keyword='+keyword;
  }
  
  var scriptTag = document.createElement("script");
  scriptTag.setAttribute("type", "text/javascript");
  scriptTag.setAttribute("src", url);
  document.getElementsByTagName("head").item(0).appendChild(scriptTag);
}

function dokolist(data){
  
  var hits;
  if(data.results.totalcount > 20) hits = 20;
  else hits = data.results.totalcount;
  
  var table = '';
  
  var i=0;
  while(i<hits){
    
    var tel = data.results.shops.shop[i].tel;
    var addr = data.results.shops.shop[i].addr;
    var name = data.results.shops.shop[i].name;
    
    table += '<p><a href="/shop/'+encodeURI(name)+':'+encodeURI(tel)+'.html" target="_blank">'+name+'</a><br />';
    table += addr+'<br />';
    table += tel+'</p>';
    
    i++;
  }
  if(i>0){
    document.getElementById("result").innerHTML = table;
  } else {
    document.getElementById("result").innerHTML = 'データが見つかりませんでした。';
  }
}
