var body_height = 0; 
var body_width = 0; 
var page_height = 0; 
var page_width = 0; 
var scroll_top  = 0; 

function PhotoZoom(photo,legend){
	getBodySize();
 	CreateBackground();
	CreateLoad();
	CreateView();
	var photoZoom = new Image();
	photoZoom.onload = function(){
		PhotoView(this,legend);
	}
	photoZoom.src = photo;
}


function PhotoView(photo,legend){
	
	getBodySize();
	var photoHeight = photo.height + 100;
	var photoWidth = photo.width + 20;
	
	var div = document.getElementById('PhotoView');
	
	//div.style.height = photoHeight +'px';
	div.style.height = 'auto';
	div.style.width = photoWidth +'px';
	div.style.top = ((body_height - photoHeight) / 2) + scroll_top +'px';
	div.style.left = ((body_width - photoWidth) / 2) +'px';
	
	document.getElementById('photo_view').appendChild(photo);
	document.getElementById('photo_legend').innerHTML = legend;
	document.getElementById('PhotoLoad').style.display = 'none';
	div.style.display = 'block';
}


function setLayerOpacity(opacidade,obj) { 
    obj.opacity = (opacidade / 100); 
    obj.MozOpacity = (opacidade / 100); 
    obj.KhtmlOpacity = (opacidade / 100); 
    obj.filter = 'alpha(opacity=' + opacidade + ')'; 
} 

function CreateBackground(){
	if(!document.getElementById('PhotoZoom')){	
		var criar = document.createElement('div');
		criar.setAttribute('id','PhotoZoom');
		criar.setAttribute('title','Clique para fechar');
		document.body.appendChild(criar);
		
		var div = document.getElementById('PhotoZoom');	
		setLayerOpacity(60,div.style);
		div.onclick = function(){
			ClosePhotoView();	
		}
	}
	var div = document.getElementById('PhotoZoom');	
	var layerHeight = page_height > body_height ? page_height : body_height;
	var layerWidth = page_width > body_width ? page_width : body_width;
	
	div.style.height = layerHeight +'px';
	div.style.width = layerWidth +'px';	
		
	div.style.display = 'block';
}

function ClosePhotoView(){
	document.getElementById('PhotoZoom').style.display = 'none';
	document.getElementById('PhotoLoad').style.display = 'none';
	document.getElementById('PhotoView').style.display = 'none';
}

function CreateLoad(){
	if(!document.getElementById('PhotoLoad')){	
		var criar = document.createElement('div');
		criar.setAttribute('id','PhotoLoad');
		document.body.appendChild(criar);
	}	
	
	var div = document.getElementById('PhotoLoad');	
	div.innerHTML = 'Carregando...';
	
	div.style.top =  ((body_height - 50) / 2 ) + scroll_top +'px';
	div.style.left = ((body_width - 50) / 2 )+'px';
		
	div.style.display = 'block';
}


function CreateView(){
	if(!document.getElementById('PhotoView')){	
		var criar = document.createElement('div');
		criar.setAttribute('id','PhotoView');
		document.body.appendChild(criar);
	}	
	var div = document.getElementById('PhotoView');	
	div.innerHTML = '<a href="javascript:ClosePhotoView()">Fechar</a><div id="photo_view"></div><div id="photo_legend"></div>';
}

function getBodySize(){
		body_height = document.documentElement.clientHeight;
		body_width = document.documentElement.clientWidth;
		scroll_top = document.documentElement.scrollTop;
		
		page_height = document.documentElement.scrollHeight;
		page_width = document.documentElement.scrollWidth;
		
		if (window.opera){ 
			body_height = document.body.clientHeight;
			body_width = document.body.clientWidth;
			scroll_top = document.body.scrollTop;
		}
}

