var krest = krest ? krest : {};
krest.imageZoom = {};	

krest.imageZoom.getDiv = function () {
	if (!window.krestImageDiv) {
		window.krestImageDiv = new KrestImageDiv();
	}
	return window.krestImageDiv;
}

krest.imageZoom.init = function() {
	var images = document.getElementsByTagName('img');
	for (var i = 0; i < images.length; i++) {
		var image = images[i];
		var file = image.getAttribute('imageZoom');
		var title = image.getAttribute('imageZoomTitle');
		if (file) {
			if (!title) {
				title = '';
			}
			image.onclick = new Function("krest.imageZoom.getDiv().load('" + file + "', '" + title + "');");
		}
	}
}


function KrestImageDiv() {
	//exploreObject(document.body);

	this.opacityDiv = document.createElement('div');
	this.opacityDiv.style.position = 'absolute';
	this.opacityDiv.style.top = 0;
	this.opacityDiv.style.left = 0;
	this.opacityDiv.style.width = document.body.clientWidth;
	this.opacityDiv.style.height = document.body.scrollHeight + 50;
	this.opacityDiv.style.display = 'none';
	this.opacityDiv.style.backgroundColor = 'black';
	this.opacityDiv.style.filter = 'alpha(opacity=50)';
	this.opacityDiv.style.MozOpacity = 0.5;
	
	this.opacityDiv.style.zIndex = 1;
	document.body.appendChild(this.opacityDiv);	
	
	
	
	this.div = document.createElement('div');
	this.div.style.position = 'absolute';
	this.div.style.top = document.body.scrollTop;
	this.div.style.left = 0;
	this.div.style.width = '100%';
	this.div.style.height = '100%';				
	this.div.style.display = 'none';
	this.div.style.zIndex = 2;
	document.body.appendChild(this.div);			
	
	var table = document.createElement('table');
	table.style.width ='100%';
	table.style.height ='100%';
	this.cell = table.insertRow(0).insertCell(0);
	this.cell.valign = 'middle';
	this.cell.align = 'center';
	this.div.appendChild(table);
	
	var t2 = document.createElement('table');
	t2.style.border = '2px solid black';
	t2.style.backgroundColor = 'white';
	this.closeCell = t2.insertRow(0).insertCell(0);
	this.closeCell.style.textAlign = 'right';
	this.closeCell.innerHTML = '<img src="/resources/images/imageZoom/close.png" onClick="krest.imageZoom.getDiv().hide()"/>';
	this.imageCell = t2.insertRow(1).insertCell(0);
	this.textCell = t2.insertRow(2).insertCell(0);
	this.textCell.style.padding = '5px';
	this.cell.appendChild(t2);				
	this.textCell.innerHTML = '';
	
	
	this.show = function() {					
		this.div.style.display = 'inline';
		this.div.style.top = document.body.scrollTop;
		this.opacityDiv.style.display = 'inline';
	}
	
	this.hide = function() {
		this.div.style.display = 'none';
		this.opacityDiv.style.display = 'none';
	}
	
	this.load = function(fileName, podpis) {
		if (this.image) {
			this.imageCell.removeChild(this.image);
		}
		this.image = document.createElement('img');
		this.image.src = fileName;
		this.image.onclick = function() {
			krest.imageZoom.getDiv().hide();
		};
		this.imageCell.appendChild(this.image);
		this.textCell.innerHTML = podpis;
		this.show();
	}
	
	return this;
}


if (window.addEventListener){
    window.addEventListener("load", krest.imageZoom.init, false);
} else if (window.attachEvent){
    window.attachEvent("onload", krest.imageZoom.init);
}
