(function(w, d, na){

var ANIME, FADE;



//ANIMATION FUNCTION
ANIME = function(delay, time){
  this._delay    = delay;
  this._time     = time;
  this._ie       = w.attachEvent && na.toLowerCase().indexOf('msie') != -1;
  this._start    = 0;
  this._end      = 10;
  this._show     = null
  this._hidden   = null
  this._timer    = null;
  this._callback = null;
  this._running  = false;
};

ANIME.prototype = {
  _init: function(show, hidden, callback){
    if(this._running) return;
    this._running  = true;
    this._show     = show;
    this._hidden   = hidden;
    this._start    = 0;
    this._end      = 10;
    this._callback = typeof callback == "function" ? callback : null;
    var delta1     = this._end - this._start,
        delta2     = this._start - this._end,
        time       = this._now();
    this._timer    = setInterval(this._bind(this._increase, this, [time, delta1, delta2]), this._time);
  },

  _increase: function(t, d1, d2){
    var p  = Math.min(1, (this._now() - t) / this._delay),
        vs = (p == 1) ? this._end : this._start + this._linear(d1, p),
        ve = (p == 1) ? this._start : this._end + this._linear(d2, p);
    this._opacity(this._show, vs);
    this._opacity(this._hidden, ve);
    if(p == 1){ this._pause(); }
  },

  _now: function(){
    return new Date().getTime();
  },

  _linear: function(d, p){
    return (d * Math.sin((p * 90) * Math.PI / 180));
  },

  _pause: function(){
    clearInterval(this._timer);
    this._timer = null;
    if(this._callback) this._callback();
    this._running = false;
  },

  _$: function(i){
    return d.getElementById(i);
  },

  _bind: function(f, b, a){
    return function(e) { f.apply(b, a || [e]); }
  },

  _opacity: function(e, v){
    if(this._ie) e.style.filter = 'alpha(opacity=' + (v * 10) + ')';
    else e.style.MozOpacity = v / 10;
    e.style.opacity = v / 10;
  }
};



//FADE FUNCTION
FADE = function(){
  this._anime = new ANIME(1000, 10);
  this._timer = 6000;
  this._num   = 0;
  this._elems = {"img": [], "nav": []};
  this._stay  = {"img": null, "nav": null};
  this._time  = null;
  this._init();
};

FADE.prototype = {
  _init: function(){
    this._load(this._set);
  },

  _set: function(){
    var images = this._$("fadeImages"),
        navs   = this._$("fadeNav");
    if(!images || !navs) return;
    var img = images.getElementsByTagName("img"),
        nav = navs.getElementsByTagName("img"),
        flag, i, e;
    for(flag=0, i=0; e=img[i]; i++){
      flag++;
      this._elems.img[this._elems.img.length] = e;
    }
    for(i=0; e=nav[i]; i++){
      this._elems.nav[this._elems.nav.length] = e;
      this._click(e, i);
    }
    if(flag < 2) return;
    this._stay.img = this._elems.img[0];
    this._stay.nav = this._elems.nav[0];
    this._time = setTimeout(this._bind(this._start, this), this._timer);
  },

  _start: function(num){
    if(num || num === 0){
      this._num = num;
    }else{
      this._num = this._num < this._elems.img.length - 1 ? this._num + 1 : 0;
    }
    this._replace(this._stay.nav, "_on", "_off");
    this._stay.nav = this._elems.nav[this._num];
    this._replace(this._stay.nav, "_off", "_on");
    clearInterval(this._time);
    this._fade(this._elems.img[this._num], this._stay.img);
  },

  _fade: function(e1, e2){
    e1.style.display = "inline";
    this._anime._init(e1, e2, this._bind(function(){
      this._stay.img = this._elems.img[this._num];
      e2.style.display = "none";
      this._time = setTimeout(this._bind(this._start, this), this._timer);
    }, this));
  },

  _click: function(e, num){
    this._add("click", e, function(){
      if(this._anime._running || num == this._num) return;
      clearInterval(this._time);
      this._time = null;
      this._start(num);
    });
  },

  _replace: function(e, s1, s2){
    e.setAttribute("src", e.getAttribute("src").replace(s1, s2));
  },

  _load: function(f){
    if(w.addEventListener) w.addEventListener("load", this._bind(f, this), false);
    else if(w.attachEvent) w.attachEvent("onload", this._bind(f, this));
  },

  _add: function(e, t, f){
    if(w.addEventListener) t.addEventListener(e, this._bind(f, this), false);
    else if(w.attachEvent) t.attachEvent("on"+e, this._bind(f, this));
  },

  _bind: function(f, b, a){
    return function() { f.apply(b, a || []); }
  },

  _$: function(i){
    return d.getElementById(i);
  }
};

FADE = new FADE();



})(window, document, navigator.userAgent);

