/**
* A simple JavaScript image loaderimage loader
* @author Cuong Tham
* @url http://thecodecentral.com/2008/02/21/a-useful-javascript-image-loader
* @usage
* var loader = new ImageLoader('IMAGE_URL');
* //set event handler
* loader.loadEvent = function(url, image){
*   //action to perform when the image is loaded
*   document.body.appendChild(image);
* }
* loader.load();
*/

function addListener(element, type, expression, bubbling) {
  bubbling = bubbling || false;
  if(window.addEventListener)	{ // Standard
    element.addEventListener(type, expression, bubbling);
    return true;
  } else if(window.attachEvent) { // IE
    element.attachEvent('on' + type, expression);
    return true;
  } else return false;
}

var ImageLoader = function(url){
  this.url = url;
  this.image = null;
  this.loadEvent = null;
};

ImageLoader.prototype = {
  load:function(){
    this.image = document.createElement('img');
    var url = this.url;
    var image = this.image;
    var loadEvent = this.loadEvent;
    addListener(this.image, 'load', function(e){
      if(loadEvent != null){
        loadEvent(url, image);
      }
    }, false);
    this.image.src = this.url;
  },
  getImage:function(){
    return this.image;
  }
};

/*
swapArgs = new Array('className','id','name','style');
function swapImage(newImage,oldImage) {
	for(var i = 0; i< swapArgs.length; i++) {
		if (oldImage[swapArgs[i]]) {
			try {
				if (swapArgs[i] == 'style') {
					for(var a in oldImage.style) {
						if (oldImage.style[a] != '' && typeof(oldImage.style[a]) != 'function') {
							newImage.style.setProperty(a,oldImage.style[a],null);
						}
					}
				}
				else {
					newImage[swapArgs[i]] = oldImage[swapArgs[i]];
				}
			}
			catch(e) {
				console.log(oldImage[swapArgs[i]]+'  '+swapArgs[i]+'  '+e);
			}
		}
	}
	document.body.replaceChild(newImage,oldImage);
}*/
