Event.observe(window, 'load', function(){
                  new ChImg;
                  new Referer;
                  new ResizeWindow;
                  
              });



var ResizeWindow = Class.create();
ResizeWindow.prototype = {
  initialize: function(){
    this.resizewin();
    Event.observe(window, 'resize', this.resizewin.bindAsEventListener(this));

  },

  resizewin: function(){
      var main_h = Element.getDimensions('main').height;
      var side_h = Element.getDimensions('side').height;

      var size = main_h > side_h ? main_h : side_h;
      $('content').setStyle({height:size + "px"});
  }
};


var Glayer = Class.create();
Glayer.prototype = {
  initialize: function(opt){
    this.set = Object.extend({
      over : document.body,
      parent : document.body,
      id : 'overlap',
      z : 1,
      opa : 0.85,
      ajust : 1,
      color : '#DCDCDC',
      duration: '0.5'
    }, opt || {});

  },

  on:function(){
    var set = this.set;

    if($(set.id)) Element.remove($(set.id));
    var overlap = document.createElement('div');
    $(set.parent).appendChild(overlap);
    overlap.id = set.id;

    new Effect.Fade(overlap,{
        to:0.1, duration: 0,
        afterFinish:function(){
          overlap.setStyle({'background':set.color});
          new Effect.Fade(overlap, {to:set.opa, duration: set.duration});
        }
      }
    );

    overlap.setStyle({
                       position: 'absolute',
                       zIndex: set.z,
                       height: Element.getHeight(set.over) + 'px',
                       width:Element.getWidth(set.over) + 'px',
                       top: 0,
                       left: 0
                     });

  },

  off: function(opt){
    var set = this.set;
    var overlap = opt ? $(opt) : $(set.over);
    if(!overlap) return;
    new Effect.Fade(overlap, {to:0.0,
      afterFinish:function(){
        Element.remove(overlap);
      }
    });
  }

};


var Referer = Class.create();
Referer.prototype = {
  initialize: function(){
    var url = window.location.href;
    var c = new MyCookie({expires:60*60*24*90});

    if(url.match(/\?as=(\d+)/)){
      c.set('as', RegExp.$1);
    }
    else if(!c.get('as')){
      if(document.referrer.match(/^http:\/\/search\.yahoo\.co\.jp/) ) {
        c.set('as', 14);
      }
      else if( document.referrer.match(/^http:\/\/www\.google\.co\.jp/) ){
        c.set('as', 15);
      }
    }
  }
};


var MyCookie = Class.create();
MyCookie.prototype = {
  initialize:function(opts){
    this.opts = Object.extend({
                 expires: 60*60*24*365,
                 path:'/'
      }, opts || {});
  },

  set: function(key, value){
    var cookieDate = new Date((new Date().getTime() + (this.opts.expires * 1000)));
    document.cookie = key + "=" + encodeURIComponent(value) + "; expires=" + cookieDate.toGMTString() + "; path=" + this.opts.path;
  },

  get: function(key){
    var cookie = document.cookie;
    var first = cookie.indexOf(key + "=");

    if(first >= 0){
      var str = cookie.substring(first, cookie.length);
      var last = str.indexOf(";");

      if(last < 0){
        last = str.length;
      }

      str = str.substring(0,last).split("=");
      return decodeURIComponent(str[1]);
    }else{
      return null;
    }
  },

  erase: function(key){
    var cookieDate = new Date(new Date().getTime() - (1000*60*60*24));
    document.cookie = key + "=; expires=" + cookieDate.toGMTString() + "; path=" + this.opts.path;
  }
}



var ChImg = Class.create();
ChImg.prototype = {
  initialize: function(){

    this.ImageList = {};
    $$('.chimg').each(function(e,i){
                        e.observe('mouseover', this.over_img.bindAsEventListener(this));
                        e.observe('mouseout', this.out_img.bindAsEventListener(this));

                        //chach
                        var src = e.src;
                        src.match(/^(.+)\.(\w+)$/);
                        var chsrc = RegExp.$1 + "_on." + RegExp.$2;
                        var img = new Image();
                        img.src = chsrc;
                        this.ImageList[e.id] = [src, chsrc];
                      }.bind(this));
  },

  over_img: function(e){
    var tar = Event.element(e);
    tar.src = this.ImageList[tar.id][1];
  },

  out_img: function(e){
    var tar = Event.element(e);
    tar.src = this.ImageList[tar.id][0];
  }
};


var ChkForm = Class.create();
ChkForm.prototype = {
  initialize: function(){
    $('submit_btn').observe('click', this.chk_form);
  },


  chk_form: function(e){
    Event.stop(e);
    var mail = $('mail');
    var mail_conf = $('mail_conf');
    var passwd = $('passwd');
    var err_color = '#ffaeb9';

    //elase err mes
    var error_msg = $('error_msg');
    while(error_msg.hasChildNodes()){
      error_msg.removeChild(error_msg.lastChild);
    }

    var msg_list = [];
    if(!mail.value){
      msg_list.push('メールアドレスを入力してください');
      mail.style.backgroundColor = err_color;
    }
    else if(!mail_check(mail.value)){
      msg_list.push('正しいメールアドレスを入力してください');
      mail.style.backgroundColor = err_color;
    }
    else{
      mail.style.backgroundColor = '';
    }

    if(!mail_conf.value){
      msg_list.push('確認メールアドレスを入力してください');
      mail_conf.style.backgroundColor = err_color;
    }
    else{
      mail_conf.style.backgroundColor = '';
    }

    if(mail.value && mail_conf.value && (mail.value != mail_conf.value) ){
      msg_list.push('メールアドレスが同じでありません');
      mail.style.backgroundColor = err_color;
      mail_conf.style.backgroundColor = err_color;
    }

    if(!passwd.value || !passwd.value.match(/^[a-zA-Z0-9]{4,}$/)){
      msg_list.push('パスワードを4文字以上入力してください');
      passwd.style.backgroundColor = err_color;
    }
    else{
      passwd.style.backgroundColor = '';
    }


    var msg = document.createElement('p');
    for (var i=0;i< msg_list.length; i++){
      msg.appendChild( document.createTextNode(msg_list[i]) );
      msg.appendChild(document.createElement('br'));
    }
    error_msg.appendChild(msg);

    if (msg_list.length >= 1){
      return false;
    }

    $('member_reg').submit();
  }
};

function mail_check(mail){
  if(!mail) return;
  return mail.match(/^(?:(?:(?:(?:[a-zA-Z0-9_!#\$\%&'*+/=?\^`{}~|\-]+)(?:\.(?:[a-zA-Z0-9_!#\$\%&'*+/=?\^`{}~|\-]+))*)|(?:"(?:\\[^\r\n]|[^\\"])*")))\@(?:(?:(?:(?:[a-zA-Z0-9_!#\$\%&'*+/=?\^`{}~|\-]+)(?:\.(?:[a-zA-Z0-9_!#\$\%&'*+/=?\^`{}~|\-]+))*)|(?:\[(?:\\\S|[\x21-\x5a\x5e-\x7e])*\])))$/)
                    ? true
                    : false
                    ;
}

