/**
* 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();
*/

//source: http://snipplr.com/view.php?codeview&id=561
// Cross-browser implementation of element.addEventListener()
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(imageID){
    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, imageID);
      }
    }, false);
    this.image.src = this.url;
  },
  getImage:function(){
    return this.image;
  }
};

function LoadImage(param){
	var arrImages = param.split("<endline>");
	for (var i=0; i<arrImages.length; i++) {
		var arrParameter = arrImages[i].split("<path>");
		var imageID = arrParameter[0];
		var imageName = arrParameter[1];
		
		var loader = new ImageLoader(imageName, imageID);
//		showLoadId = setTimeout('document.getElementById("att_loader'+imageID+'").style.display ="block"',0);
		//showLoadId = document.getElementById("att_loader'+imageID+'").style.display ="block";
		loader.loadEvent = function(url, imageName, imageID){
//		  clearTimeout(showLoadId);
		  document.getElementById('att_loader'+imageID).style.display = 'none';
		  putImage(imageID, imageName);
		}
		loader.load(imageID);
	}
};

function putImage(imageID, imageName){
  var h = document.getElementById('att_holder'+imageID);
  while(h.firstChild){
    h.removeChild(h.firstChild);
  }
  h.appendChild(imageName);
  document.getElementById('att_loader'+imageID).style.display = 'none';
};