var Runners = [];
var Runner;
var IE = false, IE6 = false;

function RunEffect(el, effect, obj){
  if(IE6) return true;
  if(!Effects[effect]) return;
  if(typeof(el) !== "object"){
    if(el[0] == "#"){
      if(!(el = document.getElementById(el.substring(1,el.length)))) return;
    }else return;
  }
  if(typeof(obj) !== "object") obj = {};

  el.effect = new Effects[effect]();
  el.effect.el = el;
  LoadConf(el.effect,obj,'speed',1000);
  LoadConf(el.effect,obj,'callback',null);
  if(typeof(el.effect.conf) === 'object')
    for(var i in el.effect.conf) LoadConf(el.effect,obj,i,el.effect.conf[i]);

  if(typeof(el.effect.Pre) === "function") el.effect.Pre();

  el.finished = false;
  el.effect.last = Number(new Date());
  el.effect.start = Number(new Date());
  AddRunner(el);
}

function AddRunner(el){
  for(var i=0; i<Runners.length; i++) if(Runners[i] === el) RemoveRunner(el);
  Runners.push(el);
  if(Runners.length == 1) Runner = setInterval(function(){RunEffects();},10);
}
function RemoveRunner(el){
  for(var i=0; i<Runners.length; i++)
    if(Runners[i] === el){
      Runners.splice(i,1);
      break;
    }
  if(!Runners.length) clearInterval(Runner);
}

function RunEffects(){
  var el;
  var t = Number(new Date());
  for(var i=0;i<Runners.length; i++){
    el = Runners[i];
    el.effect.Run(t-el.effect.start, t-el.effect.last);
    el.effect.last = t;
    StopEffect(el, false);
  }
}

function StopEffect(el){
  if(el.effect.finished){
    var callback;
    RemoveRunner(el);
    if(typeof(el.effect.Post) === "function") el.effect.Post();
    if(el.effect.callback) callback = el.effect.callback;
    el.effect = undefined;
    if(typeof(callback) == 'function') callback(el);
  }
}

function LoadConf(el, conf, key, val){
  if(typeof(conf[key]) !== 'undefined') el[key] = conf[key];
  else el[key] = val;
}

function changeOpac(obj,opacity){
  if(!obj || !obj.style) return;
  obj.style.opacity=(opacity/100);
  obj.style.MozOpacity=(opacity/100);
  obj.style.KhtmlOpacity=(opacity/100);
  obj.style.filter="alpha(opacity="+opacity+")";
}

var Effects = {
  "fly": function(){
    this.conf = {"from": 0, "to": -865, "f": function(r){return r}},
    this.Pre = function(){
      if(!this.el.style.marginLeft) this.el.style.left = this.from+'px';
    },
    this.Run = function(t,tt){
      var ratio = this.f(Math.min(1,t/this.speed));
      this.el.style.marginLeft = Math.round(this.from + ratio*(this.to-this.from))+'px';
      if(ratio >= 1) this.finished = true;
    }
  },
  "roll": function(){
    this.conf = {"from": 0, "to": 55, "f": function(r){return r}},
    this.Pre = function(){
      if(!this.el.style.width) this.el.style.width = this.from+'px';
    },
    this.Run = function(t,tt){
      var ratio = this.f(Math.min(1,t/this.speed));
      this.el.style.width = Math.round(this.from + ratio*(this.to-this.from))+'px';
      if(ratio >= 1) this.finished = true;
    }
  }
};