(function($) {

  $(function() {
    $('body').addClass('js');
    $('a, area').linkType();
    $('.external, .pdf').openWindow();
    $('.rollover, .globalNav a img, .sectionNav img, .localNav ul li ul li img, .backToTop img').rollover();
    $('li:has(.dropdown)').dropdown();
    //$('.features').rotate();
    $('#cycle1').cycle({
      fx: 'fade',
      speed: 500,
      timeout: 0,
      next: '#next1',
      prev: '#prev1'
    });
    $('.features-list').number();
    $('.itemListArea ul').grid(3);
    $('table tr:nth-child(even)').addClass('even');
		$('..ustreamSchedule .event').blockLink();
    
    // ColorBox
    $('<script>', {
      type: 'text/javascript',
      src: '/js/colorbox/colorbox/jquery.colorbox-min.js'
    }).appendTo('head')
    $('.colorbox').colorbox({ iframe: true, innerWidth: '750px', height: '90%', close: '&times; close' });
    
  });

  $.fn.linkType = function(options) {
    var o = $.extend({
      externalClass: 'external',
      sslClass: 'ssl',
      pdfClass: 'pdf',
      zipClass: 'zip'
    }, options);
    $(this)
      .filter('[href^="http"]').each(function() {
        if (this.hostname !== window.location.hostname) {
          $(this).addClass(o.externalClass);
        }
      }).end()
      .filter('[href^="https"]').addClass(o.sslClass).end()
      .filter('[href$=".pdf"]').addClass(o.pdfClass).end()
      .filter('[href$=".zip"]').addClass(o.zipClass);
    return this;
  };

  $.fn.openWindow = function() {
    $(this).click(function() {
      window.open(this.href);
      return false;
    });
    return this;
  };

  $.fn.rollover = function (options) {
    var o = $.extend({
      currentPostfix: '-c',
      rolloverPostfix: '-h'
    }, options);
    return this.each(function () {
      this.originalSrc = $(this).attr('src');
      this.rolloverSrc = this.originalSrc.replace(new RegExp('(' + o.currentPostfix + '|' + o.rolloverPostfix + ')?(\.gif|\.jpg|\.png)$'), o.rolloverPostfix + '$2');
      this.rolloverImg = new Image;
      this.rolloverImg.src = this.rolloverSrc;
    }).hover(function () {
      $(this).attr('src', this.rolloverSrc);
    }, function() {
      $(this).attr('src', this.originalSrc);
    });
  };

  $.fn.dropdown = function (options) {
    var o = $.extend({
      hoverClass: 'hover',
      showTimeout: 100,
      showSpeed: 200,
      hideTimeout: 200,
      hideSpeed: 100
    }, options);
    return this.each(function () {
      var parent = $(this),
        child = $('ul:first', this);
      parent.hover(function () {
        $.data(child, 'cancelHide', true);
        setTimeout(function () {
          if ($.data(child, 'cancelHide')) {
            parent.addClass(o.hoverClass);
            child.slideDown(o.showSpeed);
          }
        }, o.showTimeout);
      }, function () {
        $.data(child, 'cancelHide', false);
        setTimeout(function () {
          if (!$.data(child, 'cancelHide')) {
            child.slideUp(o.hideSpeed, function () {
              parent.removeClass(o.hoverClass);
            });
          }
        }, o.hideTimeout);
      });
    });
  };

  $.fn.grid = function (c, options) {
    var o = $.extend({
      cell: '> *',
      cellClass: 'cell',
      firstInRowClass: 'firstInRow',
      lastInRowClass: 'lastInRow'
    }, options);
    return this.each(function () {
      var cell = $(this).find(o.cell).addClass(o.cellClass);
      cell.filter(':nth-child(' + c + 'n+1)').addClass(o.firstInRowClass);
      cell.filter(':nth-child(' + c + 'n)').addClass(o.lastInRowClass);
    });
  };

  $.fn.number = function () {
    return this.each(function () {
      $(this).addClass('numbered').children().each(function (i) {
        i += 1;
        $(this).addClass('n' + i);
      });
    });
  };

  $.fn.blockLink = function (options) {
		var o = $.extend({
			external: true
		}, options);
    return this.each(function () {
      var a = $(this).find('a')[0];
      if (a) {
        $(this).hover(function () {
          $(this).addClass('hover');
        }, function () {
          $(this).removeClass('hover');
        }).click(function () {
          $(this).removeClass('hover');
          if (o.external && a.hostname !== window.location.hostname) {
            window.open(a.href);
          } else {
            location.href = a.href;
          }
          return false;
        });
      }
    });
  };

  $.fn.dragScroll = function() {
    var target = this;
    $(this).mousedown(function (event) {
      $(this)
        .data('down', true)
        .data('x', event.clientX)
        .data('y', event.clientY)
        .data('scrollLeft', this.scrollLeft)
        .data('scrollTop', this.scrollTop)
        .css('cursor', 'url(/img/common/cursor-hand-closed.cur),move');
      return false;
    }).css({
      'overflow': 'hidden', // スクロールバー非表示
      'cursor': 'url(/img/common/cursor-hand-open.cur),move'
    });
    // ウィンドウから外れてもイベント実行
    $(document).mousemove(function (event) {
      if ($(target).data('down') == true) {
        // スクロール
        target.scrollLeft($(target).data('scrollLeft') + $(target).data('x') - event.clientX);
        target.scrollTop($(target).data('scrollTop') + $(target).data('y') - event.clientY);
        return false; // 文字列選択を抑止
      }
    }).mouseup(function (event) {
      $(target).data('down', false);
      $(target).css('cursor', 'url(/img/common/cursor-hand-open.cur),move');
    });
    return this;
  }

})(jQuery);

