// JavaScript Document
//TABLES
var imgload = null;
var gwidth = 700;
var gheight = 700;

function createTable(width,height,cellpadding,cellspacing) {
	var table = document.createElement('table');
	table.cellPadding = cellpadding;
	table.cellSpacing = cellspacing;
	table.border = '0px';
	table.width = width;
	table.style.height = height; //no tiene height!
	var tr = table.insertRow(0);
	var td = tr.insertCell(0);
	td.setAttribute('align','center');
	td.setAttribute('valign','middle');	
	return table;
}

function viewImage(src,w,h) {
	
	if (src.length==0) return false;
	
	var div = document.createElement('div');

	div.id = 'fadeBackground199';
	
	if (window.opera) {
		div.style.position = 'fixed';	
	} else if (document.all) {
		div.style.position = 'absolute';	
	} else
		div.style.position = 'fixed';	
		
			
	div.style.width = '100%';
	div.style.height = '100%';
	div.style.top = '0px'; 
	div.style.left = '0px';
	div.style.backgroundColor = '#000';
	
	if (window.opera) {
		div.style.opacity = '0.5'; 	
	} else if (document.all) {
		div.style.filter = 'alpha(opacity=50)';
	} else {
		div.style.opacity = '0.5'; 	
	}
	
	var info_div = document.createElement('a');
	info_div.id = 'infoBackground199';
	if (window.opera) {
		info_div.style.position = 'fixed';	
	} else if (document.all) {
		info_div.style.position = 'absolute';	
	} else
		info_div.style.position = 'fixed';	
	info_div.style.top = '20px';
	info_div.style.left = '20px';
	info_div.style.backgroundColor = '#FFF';
	info_div.style.border = '1px solid #AAA';
	info_div.style.padding = '10px';	
	
	var table = createTable('100%','100%',0,0);
	table.id = 'tableBackground199'
	table.style.border = '1px solid #FFFFFF';
	
	if (window.opera) {
		table.style.position = 'fixed';	
	} else if (document.all) {
		table.style.position = 'absolute';	
	} else
		table.style.position = 'fixed';	
		
	table.style.top = '0px'; //document.body.scrollTop;
	table.style.left = '0px'; //document.body.scrollLeft;
	
	var img = document.createElement('img');

	img.id = 'imageDisplay199';
	//img.style.display = 'none';
	img.title = 'Haz click para cerrar';
	img.alt = 'Haz click para cerrar';
	img.style.border = '1px solid #FFFFFF';
	
	imgload = img;

	proctemp = function (img) {
		//var img = document.getElementById('imageDisplay199');
		var info_div = document.getElementById('infoBackground199');
		var table = document.getElementById('tableBackground199');		
	
		if (img.complete) {
			
			info_div.innerHTML = 'Cerrar';
			calculateResolutionImage(img,w,h);
			table.rows[0].cells[0].appendChild(img);	
			
			info_div.onclick = function() {
				var info_div = document.getElementById('infoBackground199');		
				var div = document.getElementById('fadeBackground199');
				var table = document.getElementById('tableBackground199');
				document.body.removeChild(table);
				document.body.removeChild(info_div);			
				document.body.removeChild(div);		
				document.body.style.overflow = 'auto';
				return false;
			}
				
			img.onclick = info_div.onclick
			
		} else {
			setTimeout("proctemp(imgload)",500);			
		}
	}
	
	//programamos un temporizador para saber si cargo la imagen


	info_div.innerHTML = 'Cargando ...';
	
	document.body.style.overflow = 'hidden';
		
	document.body.appendChild(div);
	document.body.appendChild(table);
	document.body.appendChild(info_div);	
	
	
	img.src = src; //+'?='+Math.random()*1000;	
	proctemp(imgload);
	
}

function calculateResolutionImage(img, destwidth, destheight) {
	
	var width = img.width;
	var height = img.height;
	
	//todo: no calcular imagenes que son inferiores a la resolucion de destino
	if (img.width<=destwidth && img.height<=destheight) {
		width = img.width;
		height = img.height;
	} else {
		if (width>height) { //calculamos alto 
			
			width = destwidth;
			height = Math.round(destwidth*img.height) / img.width;
			
			
		} else if (width<height) {
			
			height = destheight;
			width = Math.round(destheight*img.width) / img.height;
			
		} else { //son iguales
			height = destheight;
			width = destwidth;
		}
	}

	img.width = width;
	img.height = height;
}
