/** -*- Mode:php;tab-width:2;indent-tabs-mode:nil;coding:utf-8 -*- */

function MakeSiteMap()
{
  var _site_url      = '';
  var _is_processing = true;

  this.getPages = function (site_url)
  {
    if (!new XMLHttpRequest()) {
      alert("ご利用のブラウザでは、この機能を利用できません。\n 推奨：IE5.5以上, Firefox2以上");
      return
    }
    _site_url = site_url;

    url = '/webapps/sitemapxml/make_sitemap.cgi';
    url += '?url_n_u=' + encodeURIComponent(site_url);

    var xrequest = new XMLHttpRequest();
    xrequest.open("GET", url, true);
    xrequest.onreadystatechange = function () { _statusCheckThenExec(xrequest); }
    xrequest.send(null);

    setInterval(_checkProcess, 300);
  }

  function _checkProcess()
  {
    if (_is_processing == true) {
      var sitemap_form_nd = document.getElementById('sitemap_form_nd');

      var div_nd = document.createElement('div');
      div_nd.style.backgroundColor = '#ff3300';
      div_nd.style.color           = '#fff';
      div_nd.style.padding         = '15px';
      div_nd.style.width           = '380px';
      div_nd.style.margin          = '1em 0 2em 0';

      var img_nd = document.createElement('img');
      img_nd.src = '/webapps/sitemapxml/img/processing.gif';
      img_nd.style.paddingRight = '10px';

      var txt_nd = document.createTextNode('データを取得中です。サイトによっては数十分かかることもあります。そのままお待ちください。');

      div_nd.appendChild(img_nd);
      div_nd.appendChild(txt_nd);

      var ua = new UserAgent();
      if (ua.info.ua_name == 'ie') {
        div_nd.style.width = '400px';
        sitemap_form_nd.innerHTML = div_nd.outerHTML;
      }
      else {
        if (sitemap_form_nd.firstChild)
          sitemap_form_nd.replaceChild(div_nd, sitemap_form_nd.firstChild);
        else
          sitemap_form_nd.appendChild(div_nd);
      }
    }
  }

  /**
   * ステータスをチェックしてから、エンコード・デコードを実行する
   * @access private
   * @param  object xrequest   XMLHttpRequestのオブジェクト
   */
  function _statusCheckThenExec(xrequest)
  {
    if (xrequest.readyState == 4 && xrequest.status == 200)
      _createSiteMapForm(xrequest);
  }

  function _createSiteMapForm(xrequest)
  {
    var xml = xrequest.responseXML;
    var url_nds = xml.getElementsByTagName('url');

    if (! url_nds.length) {
      var sitemap_form_nd = document.getElementById('sitemap_form_nd');
      sitemap_form_nd.innerHTML = 'データが取得できませんでした。';
      _is_processing = false;
      return;
    }

    var table_nd = document.createElement("table");
    table_nd.setAttribute("border", "1");
    table_nd.setAttribute("width", "90%");

    var caption_nd = document.createElement("caption");
    var caption = document.createTextNode(_site_url + ' のページ一覧');
    caption_nd.appendChild(caption);
    table_nd.appendChild(caption_nd);

    var tr_nd = document.createElement("tr");
    var th_nd = document.createElement("th");
    th_nd.appendChild(document.createTextNode('URL'));
    tr_nd.appendChild(th_nd);

    th_nd = document.createElement("th");
    th_nd.appendChild(document.createTextNode('更新頻度'));
    tr_nd.appendChild(th_nd);

    th_nd = document.createElement("th");
    th_nd.appendChild(document.createTextNode('優先度'));
    tr_nd.appendChild(th_nd);

    table_nd.appendChild(tr_nd);

    for (var i=0; i < url_nds.length; ++i) {
      var url_nd = url_nds[i];
      var loc      = url_nd.getAttribute('loc');
      var is_index = url_nd.getAttribute('is_index');

      // URL
      var input_nd = document.createElement("input");
      input_nd.setAttribute('name', ('url_' + i));
      input_nd.setAttribute('value', loc);
      input_nd.style.width = '400px';

      var td_nd = document.createElement("td");
      td_nd.appendChild(input_nd);

      var tr_nd = document.createElement("tr");
      tr_nd.appendChild(td_nd);


      // ページの更新頻度
      var select_nd = document.createElement("select");
      select_nd.setAttribute('name', 'changefreq_' + i);

      var option_nd = document.createElement("option");
      option_nd.setAttribute('value', 'always');
      option_nd.appendChild(document.createTextNode('常時'));
      select_nd.appendChild(option_nd);

      option_nd = document.createElement("option");
      option_nd.setAttribute('value', 'hourly');
      option_nd.appendChild(document.createTextNode('毎時'));
      select_nd.appendChild(option_nd);

      option_nd = document.createElement("option");
      option_nd.setAttribute('value', 'daily');
      option_nd.appendChild(document.createTextNode('毎日'));
      if (is_index == 'yes') option_nd.selected = true;
      select_nd.appendChild(option_nd);

      option_nd = document.createElement("option");
      option_nd.setAttribute('value', 'weekly');
      option_nd.appendChild(document.createTextNode('毎週'));
      if (is_index == 'no') option_nd.selected = true;
      select_nd.appendChild(option_nd);

      option_nd = document.createElement("option");
      option_nd.setAttribute('value', 'monthly');
      option_nd.appendChild(document.createTextNode('毎月'));
      select_nd.appendChild(option_nd);

      option_nd = document.createElement("option");
      option_nd.setAttribute('value', 'yearly');
      option_nd.appendChild(document.createTextNode('毎年'));
      select_nd.appendChild(option_nd);

      option_nd = document.createElement("option");
      option_nd.setAttribute('value', 'never');
      option_nd.appendChild(document.createTextNode('不変'));
      select_nd.appendChild(option_nd);
      select_nd.style.width = '5em';

      td_nd = document.createElement("td");
      td_nd.appendChild(select_nd);
      tr_nd.appendChild(td_nd);


      // 優先度
      select_nd = document.createElement("select");
      select_nd.setAttribute('name', 'priority_' + i);

      option_nd = document.createElement("option");
      option_nd.setAttribute('value', '1.0');
      option_nd.appendChild(document.createTextNode('最高'));
      if (is_index == 'yes') option_nd.selected = true;
      select_nd.appendChild(option_nd);

      option_nd = document.createElement("option");
      option_nd.setAttribute('value', '0.7');
      option_nd.appendChild(document.createTextNode('高'));
      select_nd.appendChild(option_nd);

      option_nd = document.createElement("option");
      option_nd.setAttribute('value', '0.5');
      option_nd.appendChild(document.createTextNode('中'));
      if (is_index == 'no') option_nd.selected = true;
      select_nd.appendChild(option_nd);

      option_nd = document.createElement("option");
      option_nd.setAttribute('value', '0.3');
      option_nd.appendChild(document.createTextNode('低'));
      select_nd.appendChild(option_nd);

      option_nd = document.createElement("option");
      option_nd.setAttribute('value', '0.1');
      option_nd.appendChild(document.createTextNode('最低'));
      select_nd.appendChild(option_nd);
      select_nd.style.width = '5em';

      td_nd = document.createElement("td");
      td_nd.appendChild(select_nd);
      tr_nd.appendChild(td_nd);

      table_nd.appendChild(tr_nd);
    }

    var input_nd = document.createElement('input');
    input_nd.setAttribute('type', 'submit');
    input_nd.setAttribute('value', 'サイトマップXMLをダウンロードする');

    tr_nd = document.createElement("tr");
    td_nd = document.createElement("td");
    td_nd.setAttribute('colspan', '3');
    td_nd.style.textAlign = 'center';
    td_nd.appendChild(input_nd);

    tr_nd.appendChild(td_nd);
    table_nd.appendChild(tr_nd);

    var sitemap_form_nd = document.getElementById('sitemap_form_nd');

    var ua = new UserAgent();
    if (ua.info.ua_name == 'ie') {
      table_nd.style.fontSize = '0.9em';
      sitemap_form_nd.innerHTML = table_nd.outerHTML;
    }
    else {
      sitemap_form_nd.innerHTML = '';
      sitemap_form_nd.appendChild(table_nd);
    }

    _is_processing = false;
  }

}
