// JavaScript Document


// popup Window (w640)=====================================
function popup(url) {

    sw = window.open(url, 'sw', 'width=640,resizable=yes,scrollbars=yes');
    sw.focus();
    sw.moveTo(0,0);
    sw.resizeTo(640,850);

}

// Font Size =====================================
var defaultFontSize =  75;
var minimumFontSize =  60;
var maximumFontSize = 250;

var defaultStyle = "Fixed";

$debug_this_script = false;

var prefsLoaded = false;
var currentFontSize = defaultFontSize;

var pdfIcon ="&nbsp;<img src='/i/icon/icon_pdf.gif' width='17' height='19' alt='pdf' />";
var xlsIcon ="&nbsp;<img src='/i/icon/icon_xsl.gif' width='17' height='19' alt='xsl' />";
var docIcon ="&nbsp;<img src='/i/icon/icon_doc.gif' width='17' height='19' alt='doc' />";
var pptIcon ="&nbsp;<img src='/i/icon/icon_ppt.gif' width='17' height='19' alt='ppt' />";
var zipIcon ="&nbsp;<img src='/i/icon/icon_zip.gif' width='17' height='14' alt='zip' />";
var csvIcon ="&nbsp;<img src='/i/icon/icon_csv.gif' width='17' height='19' alt='csv' />";
var popIcon ="&nbsp;<img src='/i/icon/icon_popup.gif' width='16' height='16' alt='popup' />";


function do_template_specific_stuff(){
}


function toggleFluid(){
	var switchTo = (currentStyle == "Fixed") ? "Fluid" : "Fixed";

	document.body.className = switchTo;

}


function setWidth(width){
	
	if(width != "Fluid"){
		document.body.className = '';
		currentStyle = "Fixed";
	}else{
		document.body.className = 'bodyfluid';
		currentStyle = "Fluid";
	}
}



	function revertStyles(){
		currentFontSize = defaultFontSize;
		changeFontSize(0);
	}

	
	function changeFontSize(sizeDifference){
		currentFontSize = parseInt(currentFontSize) + parseInt(sizeDifference * 20);
	
		if(currentFontSize > maximumFontSize){
			currentFontSize = maximumFontSize;
		}else if(currentFontSize < minimumFontSize){
			currentFontSize = minimumFontSize;
		}
		setFontSize(currentFontSize);

		var title = getActiveStyleSheet();
		createCookie("style", title, 365);

		createCookie("fontSize", currentFontSize, 365);

	}

	function setFontSize(fontSize){
		if($debug_this_script){alert ('fontsize is being set: ' + fontSize);}
		var stObj = (document.getElementById) ? document.getElementById('content_area') : document.all('content_area');
		document.body.style.fontSize = fontSize + '%';
	}




	function setActiveStyleSheet(title) {
	  var i, a, main;
	  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
	    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
	      a.disabled = true;
	      if(a.getAttribute("title") == title) a.disabled = false;
	    }
	  }
	}

	function getActiveStyleSheet() {
	  var i, a;
	  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
	    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
	  }
	  return null;
	}

	function getPreferredStyleSheet() {
	  var i, a;
	  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
	    if(a.getAttribute("rel").indexOf("style") != -1
	       && a.getAttribute("rel").indexOf("alt") == -1
	       && a.getAttribute("title")
	       ) return a.getAttribute("title");
	  }
	  return null;
	}




	function createCookie(name,value,days) {
	  if (days) {
	    var date = new Date();
	    date.setTime(date.getTime()+(days*24*60*60*1000));
	    var expires = "; expires="+date.toGMTString();
	  }
	  else expires = "";
	  document.cookie = name+"="+value+expires+"; path=/";
	}

	function readCookie(name) {
	  var nameEQ = name + "=";
	  var ca = document.cookie.split(';');
	  for(var i=0;i < ca.length;i++) {
	    var c = ca[i];
	    while (c.charAt(0)==' ') c = c.substring(1,c.length);
	    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	  }
	  return null;
	}



	function js_init() {
		if($debug_this_script){  alert('window.onload firing');}

			cookie = readCookie("pageWidth");
			currentStyle = cookie ? cookie : "Fixed";
			setWidth(currentStyle);


			var cookie = readCookie("style");
			var title = (cookie) ? cookie : getPreferredStyleSheet();
			setActiveStyleSheet(title);

			cookie = readCookie("fontSize");
                        currentFontSize = (cookie) ? cookie : defaultFontSize;
			setFontSize(currentFontSize);

			do_template_specific_stuff();

		if($debug_this_script){alert('window.onload is exiting');}
	}




//	window.onunload = function(e) {
//		if($debug_this_script){ alert('onunload is firing'); }
//
////		  createCookie("pageWidth", currentStyle, 365);
//
//		var title = getActiveStyleSheet();
//		createCookie("style", title, 365);
//
//		createCookie("fontSize", currentFontSize, 365);
//
//		if($debug_this_script){ alert('onunload is firing'); }
//	};



// File Size get=====================================
var pdfIcon ="&nbsp;<img src='/i/pdficon_small.gif' width='17' height='17' alt='pdf' />";
// var xlsIcon ="&nbsp;<img src='/i/icon_xsl.gif' width='17' height='19' alt='xsl' />";
// var docIcon ="&nbsp;<img src='/i/icon_doc.gif' width='17' height='19' alt='doc' />";
// var pptIcon ="&nbsp;<img src='/i/icon_ppt.gif' width='17' height='19' alt='ppt' />";
// var zipIcon ="&nbsp;<img src='/i/icon_zip.gif' width='17' height='14' alt='zip' />";
// var csvIcon ="&nbsp;<img src='/i/icon_csv.gif' width='17' height='19' alt='csv' />";
// var popIcon ="&nbsp;<img src='/i/icon_popup.gif' width='16' height='16' alt='popup' />";

function formatByte (size) {
  var format = '';
  if (size <= 0) format = size;
  else if (size < 1024) format = size + 'B';
  else if (size < (1024*1024)) format = Math.round(Math.round(size/1024*100)/100) + ' KB';
  else if (size < (1024*1024*1024)) format = Math.round(Math.round(size/(1024*1024)*100)/100) + ' MB';
  else format = Math.round(Math.round(size/(1024*1024*1024)*100)/100) + ' GB';
  return format;
};

function get_file_size(obj) {
  $(obj).each(function(){
    var this_obj = $(this);
    var this_href = this_obj.attr("href");
    var icon      = false;
    var length    = false;
    var file_size;
    if(this_href){
      var text = this_obj.html();
      var Extension = this_href.split(".");
      var g_Extension = Extension[Extension.length-1];
      if(g_Extension == "pdf" || g_Extension == "PDF"){
        icon = pdfIcon;
      }
      else if(g_Extension == "xls" || g_Extension == "xlsx" || g_Extension == "XLS" || g_Extension == "XLSX"){
        icon = xlsIcon;
      }
      else if(g_Extension == "doc" || g_Extension == "docx" || g_Extension == "DOC" || g_Extension == "DOCX"){
        icon = docIcon;
      }
      else if(g_Extension == "ppt" || g_Extension == "pptx" || g_Extension == "PPT" || g_Extension == "PPTX"){
        icon = pptIcon;
      }
      else if(g_Extension == "zip" || g_Extension == "ZIP"){
        icon = zipIcon;
      }
      else if(g_Extension == "csv" || g_Extension == "CSV"){
        icon = csvIcon;
      }
      if($(this).attr("href").split(":")[0]=="javascript"){
        Extension = $(this).attr("href").split(":");
        if(Extension[1].substring(0,5) == "popup"){
          $(this).html(text + popIcon);
        }
        return;
      }
    }
    if(icon != false){
      try{
        file_size = $.ajax({
          type: "HEAD",
          url: this_href,
          cache: false,
          complete: function () {
            length = formatByte(file_size.getResponseHeader("Content-Length"));
            text = text + '（' + length + '）' + icon;
            this_obj.html(text);
          }
        });
      }
      catch(e){
      }
    }
  });
}

//On Load
$(document).ready(function(){

  //Gnavi
  $("#select_category_news li").hover(
  function(){
    $(this).children("div").show("fast");
  },
  function(){
    $(this).children("div").hide("fast");
  });

  //Key Image
  if($("#loopedSlider").size() > 0){
    $(function(){
      $('#loopedSlider').loopedSlider({
        autoStart: 7000,
        restart: 8000
      });
    });
    $(".slides a").click(function(){
      if($(this).attr("href").split(":")[0]=="http"||$(this).attr("href").split(":")[0]=="https"){
        window.open($(this).attr("href"), '_blank');
        return;
      }
      var link_href=location.protocol + "//" + location.hostname + $(this).attr("href");
      location.href = link_href;
    });
  }

  //Side Navi
  if($("#AreaLnavi").size() > 0){
    $("#AreaLnavi").treeview({
      animated: "fast",
      collapsed: true,
      unique: true,
      persist: "location"
    });
    $("#AreaLnavi li").each(function(){
      if(!$(this).hasClass("open")){
        $(this).children("ul").hide();
      }
    });
  }

  //New Mark
  if($(".news").size() > 0){
    var currentDate = new Date().getTime();
    var pass = 7 * 24;
    $(".news td").each(function(){
      if($(this).children("a").size() > 0){
        var release_date;
        release_date = $(this).siblings("th").html();
        var release_year = release_date.split("年");
        var release_month = release_year[1].split("月");
        var release_day = release_month[1].split("日")[0];
        var entryDate = new Date(release_year[0], release_month[0]-1, release_day);
        var now = (currentDate - entryDate.getTime())/(60*60*1000); 
        now = Math.ceil(now);
        if(now <= pass){
          $(this).append('&nbsp<img src="i/common_icon_new.png" width="43" height="14" alt="New" />');
        }
      }
    });
  }

  //Get File Size
  get_file_size("div.AreaTopicks a");

});
