function $() {
    var elements = new Array();
    for (var i = 0; i < arguments.length; i++) {
        var element = arguments[i];
        if (typeof element == 'string')
            element = document.getElementById(element);
        if (arguments.length == 1)
            return element;
        elements.push(element);
    }
    return elements;
}

function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof(window.onload)!= 'function') {
        window.onload = func;
    }else{
        window.onload = function() {
            oldonload();
            func();
        }
    }
}

function addUnLoadEvent(func) {
    var oldonunload = window.onunload;
    if (typeof(window.onunload)!= 'function') {
        window.onunload = func;
    }else{
        window.onunload = function() {
            oldonunload();
            func();
        }
    }
}

/**
 *  
 *  @param param
 *
 */
function ImgRotator(param){
  var stop=true;
  var delay=3000;

  var slide = new Array;
  for (var i = 0; i < param.pictureName.length; i++) {
       slide[i] = new Image();
       slide[i].src = param.imgUrl+param.pictureName[i];
  }

  var bo = 100;
  var ao = 0;
  var nextImage = 0;
  var timeout = null;

  var context=null;
  var divb=null;
  var diva=null;
  var imgb=null;
  var imga=null;

  var addElements=function(){
      context=$(param.context);
      divbg=document.createElement("DIV");
      divb=document.createElement("DIV");
      diva=document.createElement("DIV");
      imgbg=document.createElement("IMG");
      imgb=document.createElement("IMG");
      imga=document.createElement("IMG");

      divbg.appendChild(imgbg);
      divb.appendChild(imgb);
      diva.appendChild(imga);

      diva.style.position="absolute";
      diva.style.top=param.top;
      diva.style.left=param.left;
      divb.style.position="absolute";
      divb.style.top=param.top;
      divb.style.left=param.left;
      divbg.style.position="absolute";
      divbg.style.top=param.top;
      divbg.style.left=param.left
      
      imgb.src=slide[0].src;
      imga.src=slide[1].src;
      imgbg.src=slide[1].src;

      context.appendChild(divbg);
      context.appendChild(diva);
      context.appendChild(divb);

  }

  var next=function() {
      if (stop) return;
      if (slide.length<2) return;
      if (timeout!=null) return;
      if (context==null) addElements();
      ao = 0;
      bo = 100;
      nextImage = nextImage + 1;
      if (nextImage == slide.length) { nextImage = 0; }
      imgbg.src = slide[nextImage].src;
      imga.src = slide[nextImage].src;
      change_slide();
  }

  var change_slide=function() {
      ++ao;
      --bo;
      divb.style.opacity = bo/100;
      diva.style.opacity = ao/100;
      diva.style.filter="alpha(opacity="+ao+")";
      divb.style.filter="alpha(opacity="+bo+")";
      if (ao > 98) {
         imgb.src = slide[nextImage].src;
         divb.style.opacity = 100;
         divb.style.filter="alpha(opacity=0)";
         timeout = null;
         setTimeout(next,delay);
      }else{
         timeout = setTimeout(change_slide,5);
      }
  }

  this.start=function(){stop=false;setTimeout(next,delay)}
  this.stop=function(){stop=true}
}
