/**
 * ImageSwitcher
 *  画像の名前を*_off.jpgとすれば、自動で*_on.jpgを読み込み、マウスオーバー設定を行う。a要素直下のimgに限る。
 *  require: prototype.js
 */
ImageSwitcher = {
  onImagePostfix: '_on',
  offImagePostfix: '_off',
  extension: '(jpe?g|png|gif)',
  load: function(){
    var offImagePostfix = this.offImagePostfix;
    var onImagePostfix  = this.onImagePostfix;
    var extension       = this.extension;
    
    if (typeof(Prototype) != 'undefined'){
      Event.observe(window, 'load', function(){
        var images = [];
        var reg = new RegExp('^(.*)' + offImagePostfix + '\.' + extension, 'i');
        var imgTags = $A(document.getElementsByTagName('img'));
        imgTags.each(function(img){
          if (img.parentNode.tagName == 'A' && reg.match(img.src)){
            images.push({
              base: img,
              baseSrc: img.src + "", // new string
              onImage: function(){ var n = new Image(); n.src = RegExp.$1 + onImagePostfix + '.' + RegExp.$2; return n;}()
            });
          }
        });
        $A(images).each(function(p){
          var base = p.base;
          base.onmouseover = function(){
            base.src = p.onImage.src;
          }
          base.onmouseout = function(){
            base.src = p.baseSrc;
          }
        });
      });
    }
  }
};
ImageSwitcher.load();
