var Globals = function () {};
var ImagesFloat = Class.create();
ImagesFloat.prototype = {

	initialize : function (zoom, refs, path) {

		// private members
		this.path	= '';
		this.holder	= null;
		
		// wtf fix
		zoom = $(zoom.id);

		// ctor 
		
		if (!zoom && refs.length == 0)
			throw new Error('Ошибка инициализации менеджера увеличения изображения');
			
		this.path = path;
		this.createZoom(zoom);
		
		for (var i = 0; i < refs.length; i++)
			this.addBehavior($(refs[i]));
	},


	// private methods
	
	addBehavior : function (ref) {
		var fl = new Floatup(this.zoom, ref);
		fl.onBeforeFloat = this.show.bind(this, ref);
		fl.onHide = this.hide.bind(this);
		Element.observe(this.holder, 'click', fl.hide.bind(fl));
	},

	createZoom : function (zoom) {
		this.holder = $('overall');
		
		if (!this.holder)
			setTimeout(this.createZoom.bind(this), 1000);
		else {
			this.zoom = this.holder.appendChild(zoom);
			this.zoom.style.display = 'block';
		}
	},
	
	show : function (ref) {
		if (this.holder.style.visibility != 'visible')
			this.holder.style.visibility = 'visible';
			
		this.holder.style.display = 'block';
		
		var thumb = ref.getElementsByTagName('img')[0];
		
		if (!thumb)
			throw new Error('Ошибка');
		
		var divs = this.zoom.getElementsByTagName('div');
		for (var i = 0; i < divs.length; i++) {
			switch (divs[i].className) {

				case 'image-holder' :
					var image = divs[i].getElementsByTagName('img')[0];
					image.onload = this.imageLoad.bind(this, image);
					/*image.style.visibility = 'hidden';*/

					if (image.src != this.path+thumb.id+'.jpg')
						image.src = this.path+thumb.id+'.jpg';
					/*image.title = thumb.title;*/
					break;
				
				case 'title' :
					divs[i].innerText = thumb.title;
					break;
				
				default :
					break;
			}
		}
	},
	
	imageLoad : function (image) {
		this.zoom.style.marginLeft = -1*Math.round(image.width/2) + 'px';
		this.zoom.style.marginTop = -1*Math.round(image.height/2) + 'px';
		image.style.visibility = 'visible';
	},
	
	hide : function () {
		this.holder.style.display = 'none';
	}
	
}