/* =======================================

 Javascript

 -----------------------------------------
 INDEX
 -----------------------------------------
 Mouse Over
 Copyright
 Print
 Smooth Scroll
 Cookie Script
 Chenge Font Size
 左袖カレント表示
 検索
 Top Information
 クイックリンク
 Icons and FileSize
 IE6で砂時計回避

========================================== */


/* Mouse Over
========================================== */
$(function() {
  var image_cache = new Object();
  $(".swap").each(function(i) {
    var imgsrc = this.src;
    var dot = this.src.lastIndexOf('.');
    var imgsrc_on = this.src.substr(0, dot) + '_on' + this.src.substr(dot, 4);
    image_cache[this.src] = new Image();
    image_cache[this.src].src = imgsrc_on;
    $(this).hover(
      function() { this.src = imgsrc_on; },
      function() { this.src = imgsrc; });
  });
});


/* Copyright
========================================== */
$(function() {
 var copyrightDate = new Date();
 var copyrightYear = copyrightDate.getFullYear();
 $('#copyright .year').text(copyrightYear+' ');
});


/* Print
========================================== */
$(function(){
 $('.printBtn').click(function(){
  print();
  return false;
 }).css('cursor','pointer');
});


/* Smooth Scroll
========================================== */
$(function(){ 
 $('a[href^=#]:not(a[href$=#]):not(#topInformation ul.navi li a)').click(function(){
  var href= this.hash;
  var $target = $(href == '#_top' ? 'body' : href);
  
  // アンカーリンクがあるページでだけ以下を実行
  if($target.size()){
   var top = $target.offset().top;
   $($.browser.safari ? 'body' : 'html').animate({scrollTop:top}, 500, 'swing');
  }
  return false;
 });
});


/* Cookie Script
========================================== */
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 var 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;
}


/* Chenge Font Size
========================================== */
$(function(){
  var currentstyle = readCookie('tufsFontStyle');
  if (currentstyle){
    switchFont(currentstyle);
  }else{
    switchFont("fontDefault");
  };
  
  $("dl#changeFontSize dd.default").click(function(){
    switchFont("fontDefault");
  }).css('cursor','pointer');
  
  $("dl#changeFontSize dd.large").click(function(){
    switchFont("fontLarge");
  }).css('cursor','pointer');
  
  $("dl#changeFontSize dd").hover(function(){
    $(this).find('img').hide();
  },function(){
    $(this).find('img').show();
  });
});

function switchFont(className){
   $('#contents').removeClass('fontDefault').removeClass('fontLarge').addClass(className);
   $('dl#changeFontSize').removeAttr('class').addClass(className);
   createCookie('tufsFontStyle', className, 365);
};



/* 左袖カレント表示
========================================== */
$(function(){
  $('#localnavi>.categoryMenu>ul>li ul').hide();
  var activeURL = window.location.pathname+window.location.hash;
  
  $('#localnavi>.categoryMenu ul li a').each(function(){
    if( ($(this).attr('href') == activeURL) || ($(this).attr('href') == window.location.href)) {
      if( $(this).attr('href') == '/examination/application/'){
        $('#catMenu_exApp > a').addClass('active');
      }else if( $(this).attr('href') == '/examination/opencampus/seminar/' ){
        $('#catMenu_ocSem > a').addClass('active');
      }else{
        $(this).addClass('active');
      }
    }
  });
  
  $('#localnavi>.categoryMenu>ul>li:has(a.active)').addClass('current');
  $('#localnavi>.categoryMenu>ul>li.current ul').show();
  $('#localnavi>.categoryMenu>ul>li.current ul ul').hide();
  $('#localnavi>.categoryMenu>ul>li.current ul li:has(a.active)').find('ul').show();
  
  // 研究者検索
  if(activeURL.match(/\/research\/people\//)){
    $('#localnavi>.categoryMenu>ul> li:eq(0)').addClass('current');
    $('#localnavi>.categoryMenu>ul> li:eq(0) ul').show();
    $('#localnavi>.categoryMenu>ul> li:eq(0) ul li:eq(8) a').addClass('active');
  }
});


/* 検索
========================================== */
$(function(){
  var defaultValue = 'サイト内検索';
  $('#headerSearchArea p.serchInput input').focus(function(){
    if($(this).attr('value') == defaultValue){
      $(this).attr('value','');
    }
  }).blur(function(){
    if($(this).attr('value') == ""){
      $(this).attr('value',defaultValue);
    }
  });
});


/* Top Information
========================================== */
$(window).load(function(){
  var currentstyle  = readCookie('tufsTopInfomation_showSectionID');
  var currentstyle2 = readCookie('tufsTopInfomation_activeNaviNum');
  
//  if (currentstyle){
//    $('#topInformation .section').hide();
//    $('#topInformation .section'+currentstyle).show();
//    $('#topInformation ul.navi li a:eq('+currentstyle2+')').addClass('active');
  if (currentstyle){
    $('#topInformation .section').hide();
    $('#topInformation .section:first').show();
    $('#topInformation ul.navi li a:first').addClass('active');
  }else{
    $('#topInformation .section').hide();
    $('#topInformation .section:first').show();
    $('#topInformation ul.navi li a:first').addClass('active');
  };
  
  $('#topInformation ul.navi li a').click(function(){
    var showSectionID = $(this).attr('href');
    var activeNaviNum = $('#topInformation ul.navi li a').index(this);
    //createCookie('tufsTopInfomation_showSectionID', showSectionID, 365);
    //createCookie('tufsTopInfomation_activeNaviNum', activeNaviNum, 365);
    
    $('#topInformation .section').hide();
    
    $('#topInformation .section'+showSectionID).show();
    $('#topInformation ul.navi li a').removeClass('active');
    $(this).addClass('active');
    return false;
  });
});

//cookieを無効化 101019_TK

/* クイックリンク
========================================== */
$(function(){
  $('body').append('<div id="modalWindow"></div><div id="modalContents"><div id="modalDisplay"></div><div id="modalClose"><img src="/tufs_common/images/parts/modalwindow_btn_01.png" width="123" height="29" alt="閉じる" /></div></div>');
  
  $('#modalDisplay').load('/quick/ #contents',function(){
    var modalMarginL  = ($('#modalContents').width()/2)+'px';
    var modalMarginT = ($('#modalContents').height()/2)+'px';
    
    $('#quicklink a').click(function(){
      $('#modalWindow').show().css('filter','alpha(opacity=50)');
      
      var modalContentsHeight = $('#modalContents').height();
      
      $('#modalContents').css({
          'position' : 'absolute',
          'marginTop' : '0',
          'marginLeft' : '-'+modalMarginL,
          'top' : '50px',
          'left' : '50%'
        }).show(200);
      
      return false;
    });
    
    $('#modalClose, #modalWindow').click(function(){
      $('#modalWindow').fadeOut('slow');
      $('#modalContents').hide(500);
    });
    $('#modalClose').css('cursor','pointer');
  });
});


/* Icons and FileSize
========================================== */
$(function(){
  $('#document').find('a[href$=".pdf"]').not('a:has(img)').wrap('<span class="pdfLink"></span>').after('<img src="/tufs_common/images/common/icon_pdf.gif" width="18" height="22" alt="PDF" class="icon" />');
  $('#document').find('a[href$=".doc"]').not('a:has(img)').wrap('<span class="wordLink"></span>').after('<img src="/tufs_common/images/common/icon_word.gif" width="18" height="22" alt="Word" class="icon" />');
  $('#document').find('a[href$=".xls"], a[href$=".xlsx"]').not('a:has(img)').wrap('<span class="excelLink"></span>').after('<img src="/tufs_common/images/common/icon_excel.gif" width="18" height="22" alt="Excel" class="icon" />');
  $('#document').find('a[href^="http"][target=_blank]:not(a[href$=".pdf"],a[href$=".doc"], a[href$=".docx"],a[href$=".rtf"],a[href$=".jtd"],a[href$=".xls"], a[href$=".xlsx"], a[href$=".ppt"], a[href$=".pptx"], a[href$=".zip"], a[href$=".lzh"])').not('a:has(img)').wrap('<span class="blankLink"></span>').after('<img src="/tufs_common/images/common/icon_blank.gif" width="22" height="19" alt="別ウインドウで開きます" class="icon" />');


  // ファイルサイズ取得
  $('#contents').find('a[href$=".pdf"], a[href$=".doc"], a[href$=".docx"], a[href$=".xls"], a[href$=".xlsx"], a[href$=".ppt"], a[href$=".pptx"], a[href$=".zip"], a[href$=".lzh"]').not('a:has(img)').each(function(){
    var tag = $(this);
    
    $.getJSON('http://json-head.appspot.com/?url='+encodeURIComponent(this.href)+'&callback=?', function(json){
      if(json.ok && json.headers['content-length']) {
        var length = json.headers['content-length'];
        
        var formatByte = function(size) {
          var format = '';
          if (size <= 0)
            format = size;
          else if (size < 1024)
            format = size + 'B';
          else if (size < (1024*1024))
            format = Math.round(size/1024*100)/100 + ' KB';
          else if (size < (1024*1024*1024))
            format = Math.round(size/(1024*1024)*100)/100 + ' MB';
          else
            format = Math.round(size/(1024*1024*1024)*100)/100 + ' GB';
          return format;
        };
  
        tag.after(' <span class="filesize">['+formatByte(length)+']</span>');
      }
    });
  });
});


/* IE6で砂時計回避
========================================== */
try {
  document.execCommand('BackgroundImageCache', false, true);
}catch(e){}

/* favicon設定
========================================== */
document.write("<link rel='shortcut icon' href='/favi.ico' />");

