+function() {
  var tid, xhr1, xhr2;
  var interval = 500; // per 1 tweet
  $(function(){
      $('#form').submit(function(){
                          onsubmit();
                          return false;
                        });
      $('#submit').click(function(){onsubmit();});
      var onsubmit =  function(){
                           if (!$('#word').val()) return;
                           if (xhr1) xhr1.abort();
                           if (xhr2) xhr2.abort();
                           $('#view').remove(); // view only
                           clearInterval(tid);
                           $('#message').html('');
                           $('#result').slideDown().html(['<p>100人に聞いています。</p>',
                                              '<div id="loading"></div>'
                                             ].join(''));

      xhr1 = $.ajax({
               type: 'get',
               url: '/twhich/twitter/'+encodeURIComponent($('#word').val()),
               dataType: 'json',
               timecout: 60000,
               error: function(xhr, text, er) {
                   error(error_msg);
               },
               success: function(tweets) {
                 if (!tweets || !tweets.status) {
                   error(tweets.message);
                   return;
                 }
                 $('#loading').remove();
                 $('#result').append('<ul id="tweets"></ul>');

                 var i=0;
                 var li, pre, max, visible=3;
                 tid = setInterval(function() {
                                         if (i >= tweets.results.length) i=0;
                                         li = tweets.results[i].text.replace(tweets.word, '<b>'+tweets.word+'</b>');
                                         $('#tweets').append(['<li class="tweet_', i,'">',
                                                              '<img src="',
                                                              tweets.results[i].profile_image_url,
                                                              '" width="48" height="48" />',
                                                              li,
                                                              '</li>'].join('')).fadeIn('slow');
                                         max = tweets.results.length;
                                         pre = $('.tweet_'+((i-visible) >= 0 ? i-visible : i-visible+max));
                                         var pre2 = $('.tweet_'+((i-visible-1) >= 0 ? i-visible-1 : i-visible-1+max));
                                         pre.slideUp('slow', function() {
                                                       pre2.remove();
                                                     });
                                         i++;
                                       }, interval);
                 xhr2 = $.ajax({
                          type: 'get',
                          url: '/twhich/nazuki/'+tweets.token,
                          timeout: 300000,
                          dataType: 'json',
                          error: function(xhr, text, er) {
                            error(error_msg);
                          },
                          success: function(json) {
                            if (!json || !json.status) {
                              error(json.message);
                              return;
                            }

                            clearInterval(tid);
                            location.href = ['/about', encodeURIComponent(tweets.word), json.id].join('/');

                            //$('#result').html(html);
                       }});

               }});
      return;
  };  // end of onsubmit

      var error = function(message) {
        $('#result').slideUp();
        $('#message').html(message);
      };

      // share on twitter
      $('#view .navi a').click(function(){
                                 var word = $('#wordNow').val();
                                 if (typeof pageTracker !== 'undefined') {
                                   pageTracker._trackPageview("/Analytics/twitter/" + word);
			                     }
                                 var msg = ['「', word, '」の好き嫌い (via Twhich) - ', location.href].join('');
                                 var url  = 'http://twitter.com/?status='+encodeURIComponent(msg);
                                 window.open().location.href = url;
                                 return false;
                               });
  });
  var error_msg = 'ごめんなさい、うまくいきませんでした。違う言葉にするか、少し時間をおいてから試してみてください。';
}();
