
var DLTTimer = function(countDivId, year, month, day, hour, minute, second, type)
{
    this.countDiv = document.getElementById(countDivId);
    this.year     = year;
    this.month    = month;
    this.day      = day;
    this.hour     = hour;
    this.minute   = minute;
    this.second   = second;
    this.type   = type;

    this.from = new Date(year, month, day, hour, minute, second);
    //this.from = new Date(2010, 3, 18, 13, 0, 0);
}

DLTTimer.prototype = {
    format: {
      'day': '日<br />',
      'hour': '時間',
      'minute': '分<br />',
      'second': '秒'
    },

    setFormat: function(key, value)
    {
      this.format[key] = value;
    },

    stop: function(message)
    {
        this.countDiv.innerHTML = message;
        clearInterval(this.intervalId);
    },
    run : function()
    {
        var local = this;
        this.intervalId = setInterval(function()
        {
            var to = new Date();
            //var to = new Date(2010, 4, 18, 15, 0, 0);
            if (local.type == 'down' && to.getTime() > local.from.getTime()) {
                local.stop('...約束の時間は過ぎました');
                return 0;
            }
            else if (local.type == 'up' && to.getTime() < local.from.getTime()){
                local.stop('まだカウント開始していません...！');
                return 0;
            }

            var ts = Math.floor(to.getTime() / 1000);
            var ts_diff = Math.floor(local.from.getTime() / 1000) - ts;
            if (local.type == 'up') {
              ts_diff = ts - Math.floor(local.from.getTime() / 1000);
            }

            var d = Math.floor(ts_diff / (24*60*60));
            var h = Math.floor(ts_diff % (24*60*60) / (60*60));
            var m = (Math.floor(ts_diff % (24*60*60) / 60) % 60);
            var s = (Math.floor(ts_diff % (24*60*60)) % 60 % 60);
            var ms = Math.floor((1000 - to.getMilliseconds()) / 100);
            if (local.type == 'up') ms = Math.floor((to.getMilliseconds()) / 100);

            m = m.toString();
            s = s.toString();
            ms = ms.toString();

            local.countDiv.innerHTML = d.toString() + local.format['day']
                + h.toString()
                + local.format['hour']
                + ('00' + m).substr(m.length, 2)
                + local.format['minute']
                + ('00' + s).substr(s.length, 2) +'.' + ('0' + ms).substr(ms.length, 1) +''
                + local.format['second'];

        }, 100);
    }
}

$(function()
{
  $("#login-form-link").click(function()
  {
    $("#login-form").fadeIn();
    $("#login-form").css("display", "inline");
  });

  $("#timerline-list li").hover(function()
  {
    $(this).css("backgroundColor", "#eff5ee");
  },
  function()
  {
    $(this).css("backgroundColor", "#ffffff");
  });

  // extend jquery plugins
  $.extend(DateInput.DEFAULT_OPTS, {
    stringToDate: function(string) {
      var matches;
      if (matches = string.match(/^(\d{4,4})-(\d{2,2})-(\d{2,2})$/)) {
        return new Date(matches[1], matches[2] - 1, matches[3]);
      } else {
        return null;
      };
    },

    dateToString: function(date) {
      var month = (date.getMonth() + 1).toString();
      var dom = date.getDate().toString();
      if (month.length == 1) month = "0" + month;
      if (dom.length == 1) dom = "0" + dom;
      return date.getFullYear() + "-" + month + "-" + dom;
    },

    start_of_week: 0
  });

  $.extend(TimeInput.DEFAULT_OPTS, {
    build: function()
    {
      var table = '<table class="time_selector">';
      for (var hour = this.start_hour; hour <= this.end_hour; hour++)
      {
        table += '<tr>';
        for (var minute = 0; minute < 60; minute += this.minute_chunk)
        {
          table += '<td>';
          if (this.twelve_hour)
            table += (hour % 12 == 0 ? 12 : hour % 12) + ':' + this.pad(minute) + ' ' + (hour < 12 ? this.am_pm[0] : this.am_pm[1]);
          else
            table += this.pad(hour) + ':' + this.pad(minute);
          table += '</td>';
        }
        table += '</tr>';
      }
      table += '</table>';
      this.rootLayers = $(this.input).wrap(document.createElement('span'));
      this.timeSelector = $(table).insertAfter(this.input).click(this.bindToObj(function(event) {
        var time = event.target.textContent + ':00';
        if (this.input.val() != time)
          this.input.val(time).change();
        this.hide();
        return false;
      }));;

    },
    twelve_hour: false,
    start_hour: 0,
    end_hour: 23,
    minute_chunk: 10
  });
});

