var _queryField;
var _fields = $H({});
var _queryDimension;
var _queryLast;
var _completions = $A([]);
var _completionsList = $A([]);
var _completionsLength;
var _completionIndex = -1;

var completionURL = 'http://www.google.com/base/feeds/snippets?alt=json-in-script&callback=GDataCompletions&bq=';

Event.observe(
              window,
              'load',
              function ()
              {
                _queryField = 'entry-title';

                _fields = {
                  title: 'entry-title',
                  content: 'entry-content'
                }

                $(_queryField).autocomplete = 'off';
                $(_queryField).onfocus = function() { this.focused = true; };
                $(_queryField).onblur = function() { this.focused = false; };

                _queryDimension = Element.getDimensions($(_queryField));
                _completions_template = TrimPath.parseDOMTemplate('completions_template');

                getCompletionsOnChange();
              }
             );

// GDataCompletions({
//   "version": "1.0",
//   "encoding": "UTF-8",
//   "feed": {
//     "xmlns": "http://www.w3.org/2005/Atom",
//     "xmlns$openSearch": "http://a9.com/-/spec/opensearchrss/1.0/",
//     "xmlns$gd": "http://schemas.google.com/g/2005",
//     "xmlns$gCal": "http://schemas.google.com/gCal/2005",
//     "id": {"$t": "..."},
//     "updated": {"$t": "2006-11-12T21:25:30.000Z"},
//     "title": {
//       "type": "text",
//       "$t": "Google Developer Events"
//     },
//     "subtitle": {
//       "type": "text",
//       "$t": "The calendar contains information about upcoming developer
//        conferences at which Google will be speaking, along with other
//        developer-related events."
//     },
//     "link": [
//         {
//           "rel": "...",
//           "type": "application/atom+xml",
//           "href": "..."
//         },
//         {
//           "rel": "self",
//           "type": "application/atom+xml",
//           "href": "..."
//         }
//     ],
//     "author": [
//         {
//           "name": {"$t": "Google Developer Calendar"},
//           "email": {"$t": "developer-calendar@google.com"}
//         }
//     ],
//     "generator": {
//       "version": "1.0",
//       "uri": "http://www.google.com/calendar",
//       "$t": "Google Calendar"
//     },
//     "openSearch$startIndex": {"$t": "1"},
//     "openSearch$itemsPerPage": {"$t": "25"},
//     "gCal$timezone": {"value": "America/Los_Angeles"},
//
//     "entry": [
//         {
//           "id": {"$t": "..."},
//           "published": {"$t": "2006-11-12T21:25:30.000Z"},
//           "updated": {"$t": "2006-11-12T21:25:30.000Z"},
//           "category": [
//               {
//                 "scheme": "...",
//                 "term": "..."
//               }
//           ],
//           "title": {
//             "type": "text",
//             "$t": "WebmasterWorld PubCon 2006: Google Developer Tools in General"
//           },
//           "content": {
//             "type": "text",
//             "$t": 'Google is sponsoring at \n <a href="http://www.pubcon.com/">WebmasterWorld PubCon 2006</a>. \n Come and visit us at the booth or join us for an evening demo \n reception where we will be talking "5 ways to enhance your website \n with Google Code".\nAfter all,\nit is Vegas, baby! See you soon.'
//           },
//           "link": [
//               {
//                 "rel": "alternate",
//                 "type": "text/html",
//                 "href": "...",
//                 "title": "alternate"
//               },
//               {
//                 "rel": "self",
//                 "type": "application/atom+xml",
//                 "href": "..."
//               }
//           ],
//           "author": [
//               {
//                 "name": {"$t": "Google Developer Calendar"},
//                 "email": {"$t": "developer-calendar@google.com"}
//               }
//           ],
//           "gd$transparency": {"value": "http://schemas.google.com/g/2005#event.opaque"},
//           "gd$eventStatus": {"value": "http://schemas.google.com/g/2005#event.confirmed"},
//           "gd$comments": {"gd$feedLink": {"href": "..."}},
//           "gCal$sendEventNotifications": {"value": "true"},
//           "gd$when": [
//               {
//                 "startTime": "2006-11-15",
//                 "endTime": "2006-11-17",
//                 "gd$reminder": [{"minutes": "10"}]
//               }
//           ],
//           "gd$where": [
//               {"valueString": "3150 Paradise Road,Las Vegas,NV 89109"}
//           ]
//         },
//         {}
//     ]
//   }
// });

var GDataCompletions = function(root)
{
  var completions
    = (root.feed.entry)
    ? $A(root.feed.entry)
    : [{title: {'$t': 'no completions'}, content: {'$t': 'no completions'}}];

  clearCompletions();

  _completionsLength = completions.length;
  _completionIndex = -1;

  var _position = getPosition($(_queryField));
  var _style =
    {
      display: 'block',
      position: 'absolute',
      left: _position.x + 'px',
      top: (_position.y + _queryDimension.height) + 'px',
      margin: '0',
      padding: '0',
      width: _queryDimension.width + 'px'
    };

  Element.setStyle($('completions_floater'), _style);

  for (var i = 0; i < _completionsLength; i++)
    {
      _completions[i] = $H(completions[i]);
      _completions[i].index = i;
    }

  $('completions_place_holder').innerHTML
    = _completions_template.process({completions: _completions});

  _completionsList
    = document.getElementsByClassName('completion','completions_place_holder');
  for (var i = 0; i < _completionsList.length; i++)
    {
      _completionsList[i].onmouseover = function() { selectCompletion(this.title); };
      _completionsList[i].onclick = function() { _complete(this.title); };
    }
};

function selectCompletion(index)
{
  for (var i = 0; i < _completionsLength; i++)
  {
    _completionsList[i].className = 'completion';
  }

  _completionsList[index].className = 'completion_focused';

  for (var _key in _fields)
    {
      $(_fields[_key]).value = _completions[index][_key]['$t'];
    }

  $(_queryField).focused = false;
}

function nextCompletion()
{
  // 「(_completionsLength - 1)番目[最後]の補完候補」 = 「-1 番目[最初のさらに前]の補完候補」
  if (_completionIndex == (_completionsLength - 1))
    {
      _completionIndex = -1;
    }

  selectCompletion(++_completionIndex);
}

function previousCompletion()
{
  // 「0番目[最初]の補完候補」 = 「_completionsLength番目[最後のさらに次]の補完候補」
  if (_completionIndex == 0)
    {
      _completionIndex = _completionsLength;
    }

  selectCompletion(--_completionIndex);
}

function clearCompletions()
{
  _completions = [];

  if ($('scripts_place_holder').firstChild)
    {
      $('scripts_place_holder').removeChild($('scripts_place_holder').firstChild);
    }

  while ($('completions_place_holder').childNodes.length > 0)
    {
      $('completions_place_holder').removeChild($('completions_place_holder').childNodes[0]);
    }

  $('completions_floater').style.display = 'none';
}

function getCompletions(query)
{
  clearCompletions();

  var query;
  if (!query)
    {
      query = $(_queryField).value;
    }

  if (query)
    {
      var query_encoded = encodeURI(query);

      var _script = document.createElement('script');
      _script.src = completionURL + query_encoded;
      _script.charset = 'UTF-8';

      $('scripts_place_holder').appendChild(_script);
    }
}

function _complete(index)
{
  _completionIndex = index;

  if (_completions[index].title)
    {
      for (var _key in _fields)
        {
          $(_fields[_key]).value = _completions[index][_key]['$t'];
        }

      $(_queryField).focused = true;
      _queryLast = $(_queryField).value;

      $('entry-inspect').value = _completions[index].inspect();

      clearCompletions();
    }
}

getCompletionsOnChange = function(e)
{
  var _queryNow = $(_queryField).value;

  if($(_queryField).focused && _queryNow != _queryLast)
    {
      _queryLast = _queryNow;
      getCompletions(_queryNow);
    }

  setTimeout('getCompletionsOnChange()', 100);
}
