var ProductImages = new function() {	
  this.cancelCache = true;
  this.path = 'assets/images/products/';
  this.thumbPath = 'thumbs/';  
  this.largePath = 'large/';
  this.showError = true;
  this.largeHolder = null;
  
  this.init = function() {
    if (!document.getElementsByTagName) {
	  return;
	}
	ProductImages.largeHolder = document.getElementById('largeholder');
    var productImageThumbs = getElementsByAttribute('class', 'productthumb');	
    for (var i = 0; i < productImageThumbs.length; i++) {
	  // make the thumb the current loop and create a link for it 
	  var productImageThumb = productImageThumbs[i];  
	  var imageLink = document.createElement('a');
	  imageLink.setAttribute('href', 'javascript:;');
	  // get the current thumb's parent and append the link to it 
      productImageThumb.parentNode.appendChild(imageLink);
	  // append the current thumb to the new link 
	  imageLink.appendChild(productImageThumb);	
  
	  addEvent(imageLink, 'click', ProductImages.loadLarge, false);
	}
	return true;	  
  };

  this.loadLarge = function(e) {
    var t = window.event ? window.event.srcElement : e ? e.target : null;
    // the large image reference
    var largeImage = ProductImages.largeHolder.getElementsByTagName('img')[0];
    // get the target image
    var targetImg = getEventTarget(e);
    var newProduct = new Image(); 
    // handle any errors
	newProduct.onerror = function(e) {
      if (ProductImages.showError) {
		alert('Failed to load the image');
	  }
    }; 
    // when the new product loads set the large image's data to the new image 
    newProduct.onload = function() {
  	  largeImage.src = newProduct.src;
	  largeImage.width = newProduct.width;
	  largeImage.height = newProduct.height;
    }; 
    // set the new product image to the src of the thumb - but in the large folder 
    newProduct.src = ProductImages.path + ProductImages.largePath + targetImg.src.substring(targetImg.src.indexOf(ProductImages.thumbPath) + ProductImages.thumbPath.length, targetImg.src.length) + (ProductImages.cancelCache ? '?rnd='+Math.random() : '');
    return true;
  }; 
};

addLoadListener(ProductImages.init);
