var ensenDialog = 0;
function stationSearch(){
    stationinput = document.getElementById('ensensearch');
    if(!stationinput.value){
        alert('駅名を入力してください。');
        document.getElementById('ensensearch').focus();
        return false;    
    }
    
    stationSerachRequest();
}
function stationSerachRequest(){
    stationinput = document.getElementById('ensensearch');
    stationname = encodeURI(stationinput.value);
    ensen_urlvar = base64decode(ensen_url);
      new Ajax.Request(
        ensen_urlvar,
        {
          asynchronous:true, 
          evalScripts:false, 
          method:'get', 
          onComplete: function(request, json) { SerachComplete(json); }, 
          parameters: 'stationName='+stationname+'&searchparam='+ensen_search_param+'&module_action='+ensen_module_action
        }
      );
    
    /*
    Ext.Ajax.request({
       url: ensen_urlvar+'?stationName='+stationname+'&searchparam='+ensen_search_param+'&module_action='+ensen_module_action,
       success: function(response, request){
           var jsonData = Ext.util.JSON.decode(response.responseText);
           SerachComplete(jsonData);
       }
    });
    */
}
var win;

jQuery(document).ready( function($) {  
    $('#ensenDialog').bind('click', function(){
      $.blockUI({ message: $('#ensenDialogbox'), css: { width: '260px'} });
    });
    $('#ensenClose').bind('click', $.unblockUI);
});


function DisplayStationList( stationurlhtml ) {
  ensenContentshtml = "<br />" + stationurlhtml;
  document.getElementById("ensenContents").innerHTML = ensenContentshtml;
  jqmDialog = document.getElementById("ensenDialog");
  jqmDialog.focus();
  jqmDialog.click();
  
  /*
    var nav = new Ext.Panel({
                region: 'west',
                split: false,
                width: 280,
                collapsible: true,
                autoScroll: true,
                margins:'3 0 3 3',
                cmargins:'3 3 3 3',
                html: stationurlhtml
            });
   if(win) win.destroy();
   win = new Ext.Window({
        title: '沿線検索',
        width: 350,
        height:300,
        minWidth: 330,
        minHeight: 280,
        layout: 'fit',
        closeAction:'hide',
        plain:true,
        bodyStyle:'padding:5px;',
        buttonAlign:'center',
        items: nav
   });
   win.show();
   */
}
function ReSearchStation(){
    if(win){
        $('nearsearch').value = '';
        $('nearsearch').focus();
        win.destroy();
    }
}
function CancelStationSearch(){
    if(win){
        $('nearsearch').value = '';
        $('nearsearch').focus();
        win.destroy();
    }
}
function SerachComplete(json){
    link = '<div style="padding-left: 10px" align="right">';
    if(townstationurl)
    {
        link += '<a href = '+base64decode(townstationurl)+'>'+townstation_message+'</a><br />';    
    }
    if(provincestationurl)
    {
        link += '<a href = '+base64decode(provincestationurl)+'>'+provincestation_message+'</a><br />';    
    }    
    link +='</div>';
                    
    stationinput = document.getElementById('ensensearch');
    if(!json){
      message = '<div style="padding:5px">'+station_message+'には【<font color="red"><b>'+stationinput.value+'</b></font>】駅はありません。<br />';
      message　+= 'もう一度検索してください。</div>';
      message += link;
      DisplayStationList(message);
      return true;
    }
    if(json.linelength == 1 ){
        for(lineid in json){
           if(json[lineid].stationlength == 1){
               for(key in json[lineid].station){
                   if(key && json[lineid].station[key])
                        window.document.location.href = key;
                        return true;
               }
           }
       } 
   }
   stationurlhtml = '<div style="padding-left: 10px" align="center">'+(json.linelength)+'個の路線と'+json.allstation+'個の駅が検索されました。<br />リストから選択してください。</div>';
   stationurlhtml += '<div style="padding-left: 10px">';
   for(lineid in json){
       if(json[lineid].name)
            stationurlhtml +=  '<div>'+json[lineid].name+'</div>'   
       stationurlhtml +=  '<div style="padding:5px">';
       count = 1;
       for(key in json[lineid].station){
           if(key && json[lineid].station[key])
                stationurlhtml +=  '<a href = '+key+'>'+json[lineid].station[key]+'</a>'+((count < json[lineid].stationlength)?'、&nbsp;&nbsp;':'') ;  
                count++;
       }
       stationurlhtml += '</div>';
       stationurlhtml += '<br />';
   }
   stationurlhtml += '</div>';
   DisplayStationList(stationurlhtml);
}
