var vCoverTimer;
var vCoverId = null;
var vCoverNow;
var vBodyWidth;
var numbers = new Array();
var article = new Array();

function fn_covertimer() {
  vCoverNow = vCoverNow - 1;
  if (vCoverNow < 0) vCoverNow = numbers.length - 1;
  if (vCoverNow == vCoverId) vCoverNow = vCoverNow - 1;
  if (vCoverNow < 0) vCoverNow = numbers.length - 1;
  fn_coverchange(vCoverNow);
  vCoverTimer = setTimeout(fn_covertimer, 2000);
}

function fn_coverchange(id) {
  $("#maincover h1:first").html(numbers[id][1]);
  $("#maincover img:first").attr("src", numbers[id][2]);
}

function fn_coverset(id) {
  vCoverId = id;
  window.location = "/?number=" + numbers[id][0];
}

function fn_substr(value, len) {
  return value.substring(0, value.indexOf(" ", len - 10)) + " ...";
}

function fn_newscolumnsize() {
  var vWidthMin = 180;
  var vOtherWidth = 400;
  var parent = $("#topnewscolumn");
  if (vBodyWidth == $("#bodytable").width()) return;
  vBodyWidth = $("#bodytable").width();
  var cNews = $(parent).find("div.news").length;
  var cCols = Math.floor((vBodyWidth - vOtherWidth) / vWidthMin);
  if (((cNews / 2) < cCols) && (cNews % 2 == 0)) cCols = cNews / 2;
  if (cCols > 3) cCols = 3;
  $(parent).find("div.cleaner").remove();
  $(parent).find("div.news:gt(" + (cCols * 2 - 1) + ")").hide();
  $(parent).find("div.news:lt(" + (cCols * 2) + ")").show();
  $(parent).find("div.news:eq(" + (cCols - 1) + ")").after('<div class="cleaner"></div>');
  $(parent).find("div.news:eq(" + (cCols * 2 - 1) + ")").after('<div class="cleaner"></div>');
  $(parent).find("div.news:eq(" + (cCols * 3 - 1) + ")").after('<div class="cleaner"></div>');
  $(parent).find("div.news").css("width", Math.floor(97 / cCols) + "%");
  var cWidthNow = Math.floor((vBodyWidth * 0.9 - vOtherWidth) / cCols);
  $("#topnewscolumn td.notice").each( function() {
    var len = $(this).parents("div").find("td.date").text().length;
    $(this).text(fn_substr($(this).attr("alt"), 110 * cWidthNow / vWidthMin + 25 - len * 1))
  });
}

function fn_setwidthlimit() {
  var vLimitMax = 1240;
  var vLimitMin = 780;
  if (($(document).width() - 20) > vLimitMax) {
    $("#bodytable").width(vLimitMax + "px");
  } else if (($(document).width() - 20) < vLimitMin) {
    $("#bodytable").width(vLimitMin + "px");
  } else {
    $("#bodytable").width("100%");
  }
}

function fn_galleryshow(dev) {
  var index = $("#gallery").attr("index");
	if (index == undefined) index = 0;
	else index = parseInt(index) + dev;
	var c = $("#gallery_photos img").length - 1;
	if (index < 0) index = 0;
	else if (index > c) index = c;
  $("#gallery").attr("index", index);
  $("#gallery_photos img:eq(" + index + ")").show();
  $("#gallery_photos img:gt(" + index + ")").hide();
  $("#gallery_photos img:lt(" + index + ")").hide();
  if (index == 0) { $("#gallery_nav img.left").hide() } else { $("#gallery_nav img.left").show() }
  if (index == c) { $("#gallery_nav img.right").hide() } else { $("#gallery_nav img.right").show() }
	$("#gallery_nav .page").html("" + (index + 1) + " / " + (c + 1));
}

$(document).ready( function() {
  fn_galleryshow(0);
  fn_setwidthlimit();
  $(".newscolumn .news").unbind("hover").hover( function() { $(this).addClass("hover"); }, function() { $(this).removeClass("hover"); } );
  $(".columnized").columnize({ width: 350, buildOnce: false });
  $("#topnewscolumn").data("timeout", setTimeout(fn_newscolumnsize, 200));
//  $("a").each(function()  { if ($(this).attr("href").substring(0, 7) == "http://") $(this).attr("target", "_blank"); })
  if (vCoverId == null) vCoverId = numbers.length - 1;
  for (i = 0, l = numbers.length - 1; i < l; i++) {
    numbers[i][3] = new Image();
    numbers[i][3].src = numbers[i][2];
  }
  $("#maincover td:first")
    .unbind("hover")
    .hover( function() { vCoverNow = vCoverId; fn_covertimer() }, function() { clearTimeout(vCoverTimer); fn_coverchange(vCoverId); } )
    .click( function() { clearTimeout(vCoverTimer); fn_coverchange(vCoverNow); fn_coverset(vCoverNow); });
//  $(document).pngFix();
})

$(window).resize( function() {
  $("#topnewscolumn").data("timeout", setTimeout(fn_newscolumnsize, 200));
  fn_setwidthlimit();
})

