if (typeof Uchimura == 'undefined') {
  var Uchimura = {};
}

// jQueryプラグイン
(function($) {

  // ロールオーバー
  $.fn.roll = function(before, after) {
    var before = before || '_off.';
    var after  = after  || '_on.';

    return this.each(function() {
      if (!this.src) {
        return true;
      }

      var img = $(this);
      var lock = false;

      var off_src = img.attr('src');
      var on_src  = off_src.replace(before, after);

      function off() {
        if (!lock) {
          img.attr('src', off_src);
        }
      }

      function on() {
        img.attr('src', on_src);
      }

      img.hover(on, off).addClass('roll');

      img.bind('lock', function() {
        lock = true;
      });

      img.bind('unlock', function() {
        lock = false;
      });

      img.bind('rollOn', function() {
        on();
      });

      img.bind('rollOut', function() {
        off();
      });
    }); 
  }

  // メガドロップダウン
  $.fn.submenu = function() {
    var show = function() {
      this.show();
    }

    var hide = function() {
      this.hide();
    }

    return this.each(function() {
      var li = $(this);
      var menu = li.find('.submenu');
      var img = li.find('img.roll');

      li.bind('mouseenter', function() {
        li.addClass('active');
        img.trigger('lock');
        show.apply(menu);
      });

      li.bind('mouseleave', function() {
        li.removeClass('active');
        img.trigger('unlock').trigger('rollOut');
        hide.apply(menu);
      });
    });
  }

  /**
   * 子要素の高さを揃える
   *
   * @param mixed params
   *                string target: 高さを揃える対象 (CSSセレクター)
   *                int    group : 高さを揃えるときグループ化する数
   * @return void
   *
   */
  $.fn.heightfix = function(params) {
    var target = params.target;
    var group  = (function(p) {
      if (typeof p.group != 'undefined') {
        return parseInt(p.group, 10);
      } else {
        return 0;
      }
    })(params);

    var fix = function() {
      var heights = [];
      var targets = $(this);

      targets.each(function() {
        heights.push($(this).height());
      });

      heights.sort(function(a, b){return b - a});
      targets.height(heights[0]);
    }

    return this.each(function(){
      var self = $(this);
      var elems = [];
      var count = -1;
      var targets = self.find(target);
      if (targets.length == 0) {
        targets = self.children();
      }
      targets.height('auto');

      targets.each(function(i, e) {
        if (count == -1 || i % group == 0) {
          fix(elems[count]);
          count++;
          elems[count] = [];
        }
        elems[count].push(e);
      });

      $.each(elems, fix);
    });
  }

})(jQuery);

$(function() {
  // ページ内移動
  $('a[href^="#"]').live('click', function(event){
    // Webkitのとき、スクロールの感知は body 要素で行う
    var body = (!$.support.checkOn) ? 'body' : 'html';
    var href = $(this).attr('href');
    var pos = $(href).offset().top;

    $(body).animate({
      scrollTop : pos 
    }, {
      duration: 400,
      easing : 'easeOutQuart',
      complete: function() {
        if (href != '#wrapper') {
          location.href = href;
        }
      }
    });

    event.preventDefault();
  });

  // カレンダーにクラスを付与
  $('.cal table tr').each(function() {
    var tr = $(this);
    tr.children(':first').addClass('sun');
  });

  // ロールオーバー
  $('img[src*="_off."]').roll();
  $('img[src*="_active."]').roll('_active.', '_on.');

  // メガドロップダウン
  $('.gnav li.hasMenu').submenu();

  // 高さを揃える
  $(window).load(function() {
    $('#homeBottomPanels .horizontal, .fnav').heightfix({target: '.block'});
    $('.calSettings .cals').heightfix({target: '.cal'});
    $('#homeInfoArea').heightfix({target: '.pane .inner'});
    $('.panes2').heightfix({target: '.pane:not(.full) .inner', group: 2});

    // 高さをそろえた後三角マークをつける
    $('.panes2 .pane:not(.full) .inner').each(function() {
      var img = document.createElement('img');
      img.src = '/wp/wp-content/themes/uchimura/images/catlist_pict_tri.gif';
      img.style.top = (($(this).outerHeight() / 2) + (img.height / 2)) + 'px';
      img.className = 'tri';
      this.appendChild(img);
    });
  });

  // コピーライト全体をリンク化
  $('.copyright').click(function() {
    var href = $(this).find('a').eq(0).attr('href');
    location.href = href;
  });

  $('body').addClass('mask');

  // IE6～8に適用
  if(!$.support.opacity){
    $('div.section:last-child').addClass('last');
  }
});

