$.archi21 = {
  init: function() {
    for (module in $.archi21) {
      if ($.archi21[module].init) {
          $.archi21[module].init();
      }
    }
  }
};
$(document).ready($.archi21.init);

$.archi21.topImage = {
  init: function() {
    var images = [
      'top_main01.jpg'
      ,'top_main02.jpg'
      ,'top_main03.jpg'
      ,'top_main04.jpg'
      ,'top_main05.jpg'
      ,'top_main06.jpg'
      ,'top_main07.jpg'
      ,'top_main08.jpg'
      ,'top_main09.jpg'
      ,'top_main10.jpg'
      ,'top_main11.jpg'
      ,'top_main12.jpg'
      ,'top_main13.jpg'
      ,'top_main14.jpg'
      ];
    var n = Math.floor(Math.random() * images.length);
    $('<img></img>').attr('src', 'img/' + images[n]).appendTo('#bg');
  }
};

// Search Field
$.archi21.searchField = {
  defaultText: "ご入力ください",
  init: function() {
    if ($("#search-field #search").val() == "") {
      $("#search-field #search").val(this.defaultText);
    }
    $("#search-field #search").bind('focus', this.setDefaultText);
    $("#search-field #search").bind('blur', this.clearDefaultText);
    $("#search-field form").bind('submit', this.submit);
  },
  setDefaultText: function() {
    if (this.value == $.archi21.searchField.defaultText) {
      this.value = "";
    }
  },
  clearDefaultText: function() {
    if (this.value == "") {
      this.value = $.archi21.searchField.defaultText;
    }
  },
  submit: function() {
    if ($("#search-field #search").val() == $.archi21.searchField.defaultText) {
      $("#search-field #search").val("");
    }
  }
};

// Pngfix
$.archi21.pngfix = {
  init: function() {
    if(!($.browser.msie && $.browser.version < 7)) return true;
    if ($.browser.version < 5.5 || !document.body.filters) return true;
    $('img').each(function() {
      if(!this.src.match(/png$/i)) return true;
      var imgID = (this.id) ? "id='" + this.id + "' " : "";
      var imgClass = (this.className) ? "class='" + this.className + "' " : "";
      var imgTitle = (this.title) ? "title='" + this.title + "' " : "title='" + this.alt + "' ";
      var imgStyle = "display:inline-block;" + this.style.cssText;
      if (this.align == "left") imgStyle = "float:left;" + imgStyle;
      if (this.align == "right") imgStyle = "float:right;" + imgStyle;
      if (this.parentElement.href) imgStyle = "cursor:hand;" + imgStyle;
      var strNewHTML = "<span " + imgID + imgClass + imgTitle
          + " style=\"" + "width:" + this.width + "px; height:" + this.height + "px;" + imgStyle + ";"
              + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
                  + "(src=\'" + this.src + "\', sizingMethod='scale');\" rel=\"" + this.src + "\"></span>" ;
      this.outerHTML = strNewHTML;
      return true;
    })
  }
};

// Mouse hover
$.archi21.hover = {
  pngfix: false,
  init: function() {
    if($.browser.msie && $.browser.version < 7
       && $.browser.version >= 5.5 && document.body.filters) {
      this.pngfix = true;
    }
    if (this.pngfix) {
      selector = 'span';
    } else {
      selector = 'img';
    }
    $(selector + '.hover')
      .bind('mouseover', this.enter)
      .bind('mouseout', this.exit)
      .each(this.preload);
  },
  preload: function() {
    this.preloaded = new Image;
    var src = ($.archi21.hover.pngfix) ? this.rel : this.src;
    if (src.match(/^(.+)_white(\.[a-z]+)$/)) {
      this.preloaded.src = RegExp.$1 + '_black' + RegExp.$2;
    } else if (src.match(/^(.+)(_black)(\.[a-z]+)$/)) {
      this.preloaded.src = RegExp.$1 + '_white' + RegExp.$2;
    }
  },
  enter: function() {
    var src = ($.archi21.hover.pngfix) ? this.rel : this.src;
    if (src.match(/^(.+)_white(\.[a-z]+)$/)) {
      $.archi21.hover.setSrc(this, RegExp.$1 + '_black' + RegExp.$2);
    } else if (src.match(/^(.+)_black(\.[a-z]+)$/)) {
      $.archi21.hover.setSrc(this, RegExp.$1 + '_white' + RegExp.$2);
    }
  },
  exit: function() {
    var src = ($.archi21.hover.pngfix) ? this.rel : this.src;
    if (src.match(/^(.+)_white(\.[a-z]+)$/)) {
      $.archi21.hover.setSrc(this, RegExp.$1 + '_black' + RegExp.$2);
    } else if (src.match(/^(.+)_black(\.[a-z]+)$/)) {
      $.archi21.hover.setSrc(this, RegExp.$1 + '_white' + RegExp.$2);
    }
  },
  setSrc: function(elt, src) {
    if ($.archi21.hover.pngfix) {
      $(elt).css('filter', "progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'" + src + "\', sizingMethod='scale');");
      $(elt).attr('rel', src);
    } else {
      $(elt).attr('src', src);
    }
  }
};

