
/*---------------*/
/*--  FILTERS  --*/
/*---------------*/

function filterHover(){
    $(this)
        .animate({color: 'rgb(0,0,0)'}, 400);
}

function filterOff(){
    $(this).stop()
        .animate({color: 'rgb(192, 193, 195)'}, 200);
}

function getSpeed(size){
    if (size > 40)
        return 20*size;
    if (size > 20)
        return 50*size;
    if (size > 5)
        return 80*size;
    else
        return 100*size;
}


/*------------*/
/*--  MENU  --*/
/*------------*/

function menuHover(){
    if (!$(this).hasClass("selected")){
        $(this).find(".japanese").fadeIn(500);
        $(this).find(".english")
            .animate({color: 'rgb(0,0,0)'}, 800);
    }
}

function menuOff(){
    if (!$(this).hasClass("selected")){
        $("#menu li:not(.selected)").each(function(){
            $(this).find(".japanese:visible").hide();
            $(this).find(".english:visible").css({color: 'rgb(192, 193, 195)'});
        });
        $(this).find(".english").stop()
            .animate({color: 'rgb(192, 193, 195)'}, 200);
    }
}

function submenuHover(){
    $("#sub_menu li:not(.separator) a").hoverIntent({
        over: filterHover,
        out: filterOff,
        interval: 20
    });
}


/*-----------------------*/
/*-    SMOOTH SCROLL    -*/
/*-----------------------*/

function smoothScroll(){

  function filterPath(string) {
      return string
        .replace(/^\//,'')
        .replace(/(index|default).[a-zA-Z]{3,4}$/,'')
        .replace(/\/$/,'');
      }
  var locationPath = filterPath(location.pathname);
  var scrollElem = scrollableElement('html', 'body');

  $('a[href*=#]').each(function() {
    var thisPath = filterPath(this.pathname) || locationPath;
    if (  locationPath == thisPath
    && (location.hostname == this.hostname || !this.hostname)
    && this.hash.replace(/#/,'') ) {
      var $target = $(this.hash), target = this.hash;
      if (target) {
        var targetOffset = $target.offset().top;
        $(this).click(function(event) {
          event.preventDefault();
          $(scrollElem).animate({scrollTop: targetOffset}, 400, function() {
            location.hash = target;
          });
        });
      }
    }
  });

  // use the first element that is "scrollable"
  function scrollableElement(els) {
    for (var i = 0, argLength = arguments.length; i <argLength; i++) {
      var el = arguments[i],
          $scrollElement = $(el);
      if ($scrollElement.scrollTop()> 0) {
        return el;
      } else {
        $scrollElement.scrollTop(1);
        var isScrollable = $scrollElement.scrollTop()> 0;
        $scrollElement.scrollTop(0);
        if (isScrollable) {
          return el;
        }
      }
    }
    return [];
  }
}


/*------------*/
/*--  INIT  --*/
/*------------*/


$(document).ready(function(){

    $("#menu li").hoverIntent({
        over: menuHover,
        out: menuOff,
        interval: 50
    });
    
    submenuHover();
    
    smoothScroll();
    
    if ($.browser.msie && $.browser.version.substr(0,1) < 7){
        $("body").prepend("<div style='color: red;position:absolute; z-index:100; font-size: 14px'><p>当ホームページを安全で快適にご利用いただくために、下記バージョンのブラウザのご利用をお勧めいたします。Windowsをお使いの場合 Microsoft Internet Explorer 8.0、Firefox 3.6</p><p>Microsoft Internet Explorer > <a href='http://www.microsoft.com/japan/windows/products/winfamily/ie/function/default.mspx'>ダウンロードサイトへ</a> Firefox ＞ <a href='http://mozilla.jp/firefox/'>ダウンロードサイトへ</a></p></div>");
    }
    
});


