var isIE = /*@cc_on!@*/false;


/**
 *
 *  メガネラボWebサイトの基礎となる（？）クラス
 *
 */
var Meganelab = {
  createClass: function() {
    return function() {
      this.init.apply( this, arguments );
    };
  }
};



var Hitokoto = Meganelab.createClass();
Hitokoto.prototype = {
  init: function( param ) {
    var __this = this;
    __this.body = $( '#hitokoto' );
    __this.pos = { t: -250, r: 250 };
    __this.w = __this.body.width();
    __this.h = __this.body.height();
    __this.isHidden = false;
    __this.ignoreMouse = false;
    __this.body
      .mouseover( function() {
        __this.goAwayAndComeHere();
      } )
      .mousemove( function() {
        __this.goAwayAndComeHere();
      } )
      .css( {
        visibility: 'visible',
        top:        __this.pos.t + 'px',
        right:      __this.pos.r + 'px'
      } )
    ;
    __this.isHidden = true;
    setTimeout( function() {
      __this.goAwayAndComeHere();
    }, 1000 );
    return true;
  },

  /**
   *  goAwayAndComeHere
   *  
   *
   */
  goAwayAndComeHere: function() {
    var __this = this;
    if( __this.ignoreMouse ) { return; }
    __this.ignoreMouse = true;
    if( __this.isHidden ) { clearTimeout( __this.timer ); }
    __this.timer = setInterval( function() {
      __this.isHidden ? ( __this.pos.t += 25 ) : ( __this.pos.t -= 25 );
      __this.isHidden ? ( __this.pos.r -= 25 ) : ( __this.pos.r += 25 );
      __this.body.css( {
        top:   __this.pos.t + 'px',
        right: __this.pos.r + 'px'
      } );
      if( __this.isHidden ) {
        if( __this.pos.t >= 0 ) {
          clearInterval( __this.timer );
          __this.isHidden = false;
          __this.ignoreMouse = false;
        }
      }
      else {
        if( __this.pos.t + __this.h < 0 ) {
          clearInterval( __this.timer );
          __this.isHidden = true;
          __this.ignoreMouse = false;
          __this.timer = setTimeout( function() {
            __this.goAwayAndComeHere();
          }, 1000 );
        }
      }
    }, 10 );
  }
};



var MailAddressReplacer = Meganelab.createClass();
MailAddressReplacer.prototype = {
  init: function() {
    $( 'a.email' ).each( function() {
      var a = $( this );
      a
        .text( a.text().replace( /☆/, '@' ).replace( /．/, '.' ) )
        .attr( 'href', a.attr( 'href' ).replace( /☆/, '@' ).replace( /．/, '.' ) )
      ;
    } );
    return;
  }
};




$( function() {
//  if( ! isIE ) {
    new Hitokoto();
//  }
  new MailAddressReplacer();
} );

