/**
 * @author quickest
 */
var ScanImages = Class.create();
ScanImages.prototype = {
	initialize : function(imageContainer, imageWaitingContainer, imagesIdContainer, url) {
		this.imageContainer = imageContainer;
		this.imageWaitingContainer = imageWaitingContainer;
		this.imagesIdContainer = imagesIdContainer;
		this.urlTemplate = new Template(url, /(^|.|\r|\n)({{(\w+)}})/);
		this.images = $A($(imagesIdContainer).getElementsByTagName('img'));
		
		this.images.each(function(image) {
			Event.observe(image, 'mouseover', this.onMouseOverImage.bind(this));
		}.bind(this));
	},
	
	onMouseOverImage: function(event) {
		var image = Event.element(event);
		
		var productId = image.readAttribute('src').match(/\S+Small\/([0-9]+)\/\S*/);

		if (productId == null || productId.length != 2) {
			Event.stop(event);
			return;
		}
		
		productId = productId[1];
		
		var imageId = image.readAttribute('src').match(/^\S+\/([0-9]+)\.jpg$/);
		if (imageId == null || imageId.length != 2) {
			Event.stop(event);
			return;
		}
		 
		imageId = imageId[1];
		
		var data = {'productId': productId, 'imageId': imageId};
		var url = this.urlTemplate.evaluate(data);
		
		this.setLoadWaiting(true);
		var image = new Image();
		image.src = url;
		$(this.imageContainer).src = url;
		this.setLoadWaiting(false);
	},
	
	setLoadWaiting : function(wait) {
		var waiting = $(this.imageWaitingContainer);
		var photo = $(this.imageContainer);
		if(wait) {
			waiting.setStyle({width: photo.getWidth() + "px", height: photo.getHeight() + "px"});
			photo.hide();
			waiting.show();
		}
		else {
			waiting.hide();
			photo.show();
		}
	}
};

