$(document).ready(function(){	
	$.easy.navigation();
	$.easy.tooltip();
	$.easy.popup();
	$.easy.external();
	$.easy.rotate();
	$.easy.forms();
	$.easy.showhide();
	$.easy.jump();
});

function date_toggle(enabled) {
  if (enabled) {
    $("#to").removeAttr("disabled");
    $("#to").removeAttr("style");
  } else {
    $("#to").attr("disabled", "disabled");
    $("#to").attr("style", "color: #888; background-color: #ccc;");
  }
}

function shuffle_modal_entry() {
  $("#modal_main").empty();

  var nos = Array();
  for (i = 0 ; i < 9 ; i++){
    no = Math.floor(Math.random() * 50) + 1; 
    if (nos.indexOf(no) != -1) {
      i--;
      continue;
    }
    nos[i] = no;
  }

  var tag = ""
  for (i = 0; i < 9 ; i++) {
    no = nos[i];
    entry = entries[no];
    tag += '<div id="modal_entry_' + i + '" class="modal_entry">' +
           '<a href="' + entry.url + '" target="_blank">' + entry.title + '</a>' + 
           '</div>'
           ;
  }

  $("#modal_main").append(tag);
}

$(document).ready(function() {
  // Datepicker
  $.datepicker.setDefaults($.extend($.datepicker.regional['ja'] = {
    monthNames: ['1月','2月','3月','4月','5月','6月','7月','8月','9月','10月','11月','12月'],   
    monthNamesShort: ['1月','2月','3月','4月','5月','6月','7月','8月','9月','10月','11月','12月'],   
    yearStatus: '', 
    monthStatus: '',
    dayStatus: 'DD',
    dateStatus: 'D, M d', 
    weekHeader: 'Wk', weekStatus: '',   
    dayNames: ['日','月','火','水','木','金','土'],   
    dayNamesShort: ['日','月','火','水','木','金','土'],   
    dayNamesMin: ['日','月','火','水','木','金','土'],  
  }));
  $('#from').datepicker({
    dateFormat: 'yymmdd',
    date: $('#from').val(),
    current: $('#from').val(),
  });
  $('#to').datepicker({
    dateFormat: 'yymmdd',
    date: $('#from').val(),
    current: $('#from').val(),
  });

  // box
  $('#top_users').load('/ajax/users');
  $('#top_tags').load('/ajax/tags');
  $('#top_hours').load('/ajax/hours');

  // modal
  $("#modal_shuffle").click(function(){
    shuffle_modal_entry();
    return false;
  })

  $("#modal_close").click(function(){
    $("#glayLayer").hide();
    $("#overLayer").hide();
    return false;
  })

  $("a.modal").click(function(){
    $("#glayLayer").show();
    $("#overLayer").show();
    shuffle_modal_entry();
    return false;
  })

  if($.browser.msie && $.browser.version<7){
    $(window).scroll(function(){
      $("#glayLayer").css('top',$(document).scrollTop())
      $("#overLayer").css('top',($(document).scrollTop()+$(window).height()/2) +"px")
    })
  }

  // table
  $("#entry").tablesorter({
    sortList:[[0,0]],
    headers: {
      2: { sorter: false },
    }
  });

  // checkbox
  $(':checkbox').click(function(){
    date_toggle(this.checked);
  });
});

