function $2(s)
{
	return document.getElementById(s);
}

function InitElements()
{
	var dvBgZoom = document.createElement('div');
	var dvLoad = document.createElement('div');
	var flashLoad;
	var dvZoom = document.createElement('div');
	var dvImage = document.createElement('div');
	var dvImageClose = document.createElement('div');
	var txtClose = document.createElement('span');
	var linkClose = document.createElement('a');
	var dvCaption = document.createElement('div');
	var txtCaption = document.createElement('span');
	var image = new Image();
	var arrPageSize = getPageSize();
	var arrPageScroll = getPageScroll();

	dvBgZoom.setAttribute('id', 'dvBgZoom');
	dvBgZoom.style.position = 'absolute';
	dvBgZoom.style.width = arrPageSize[0] + 'px';
	dvBgZoom.style.height = arrPageSize[1] + 'px';
	dvBgZoom.style.top = '0px';	
	dvBgZoom.style.zIndex = '5';
	dvBgZoom.style.left = '0px';
	dvBgZoom.style.backgroundColor = '#000000';
	dvBgZoom.style.opacity = 0.3;
	dvBgZoom.style.filter = 'alpha(opacity=30)';
	dvBgZoom.style.visibility = 'hidden';
	
	flashLoad = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" ';
	flashLoad += 'codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" ';
	flashLoad += 'width="90"  ';
	flashLoad += 'height="30">';
	flashLoad += '<param name="movie" value="/imagens/fla_loading.swf">';
	flashLoad += '<param name="quality" value="high">';
	flashLoad += '<param name="wmode" value="transparent">';
	flashLoad += '<embed src="/imagens/fla_loading.swf" quality="high" ';
	flashLoad += 'pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" ';
	flashLoad += 'width="90" height="30" wmode="transparent">';
	flashLoad += '</embed>';
	flashLoad += '</object>';
	
	dvLoad.setAttribute('id', 'dvLoad');
	dvLoad.style.position = 'absolute';
	dvLoad.style.width = '200px';
	dvLoad.style.height = '50px';
	dvLoad.style.top = '50%';
	dvLoad.style.left = '50%';
	dvLoad.style.marginLeft = '-100px';
	dvLoad.style.marginTop = + (arrPageScroll[1] == 0) ? '-25' : (arrPageScroll[1] - 25) + 'px';
	dvLoad.style.textAlign = 'center';
	dvLoad.style.visibility = 'hidden';
	dvLoad.innerHTML = flashLoad;
	
	dvZoom.setAttribute('id', 'dvZoom');
	dvZoom.style.position = 'absolute';
	dvZoom.style.top = '0';
//	dvZoom.style.left = (document.width / 2) - (dvZoom.offsetWidth / 2)+'px';
	dvZoom.style.visibility = 'hidden';
	dvZoom.style.zIndex = 6;
	dvZoom.appendChild(dvImage);
	
	txtClose.setAttribute('id', 'txtClose');
	txtClose.style.fontSize = '11px';
	txtClose.style.fontFamily = 'arial';
	txtClose.style.color = '#666666';
	txtClose.innerHTML = 'FECHAR [X]';
	
	dvImageClose.style.marginBottom = '3px';
	dvImageClose.style.textAlign = 'right';
	dvImageClose.appendChild(txtClose);
	
	image.setAttribute('id', 'imgZoom');
	image.setAttribute('alt', 'Fechar');
	image.style.display = 'block';
	image.style.border = '0px';
	image.style.visibility = 'hidden';
	
	linkClose.appendChild(txtClose);
	linkClose.setAttribute('href', 'javascript: void(0)');
	linkClose.appendChild(image);
	
	if(navigator.appName.indexOf('Microsoft Internet Explorer') == 0)
		linkClose.attachEvent('onclick', CloseZoom);
	else
		linkClose.setAttribute('onclick', 'CloseZoom()');
	
	txtCaption.setAttribute('id', 'txtCaption');
	
	dvCaption.setAttribute('id', 'dvCaption');
	dvCaption.style.width = image.width + 'px';
	dvCaption.style.height = '22px';
	dvCaption.style.paddingTop = '5px';
	dvCaption.style.display = 'none';
	dvCaption.style.fontSize = '11px';
	dvCaption.style.fontFamily = 'arial';
	dvCaption.style.backgroundColor = '#E7E7E7';
	dvCaption.appendChild(txtCaption);
	
	dvImage.setAttribute('id', 'dvImage');
	dvImage.style.border = 'solid 15px #FFFFFF';
	dvImage.style.textAlign = 'right';
	dvImage.style.backgroundColor = '#FFFFFF';
	dvImage.appendChild(dvImageClose);
	dvImage.appendChild(linkClose);
	dvImage.appendChild(dvCaption);
	
	document.body.appendChild(dvBgZoom);
	document.body.appendChild(dvLoad);
	document.body.appendChild(dvZoom);
}

function OpenZoom(imageUrl, caption)
{
	CloseZoom();
	InitElements();
	
	if((caption != null) && (caption != ''))
	{
		$2('txtCaption').innerHTML  = caption;
		$2('dvCaption').style.display = 'block';
	}
	
	$2('dvBgZoom').style.visibility = 'visible';
	$2('dvLoad').style.visibility = 'visible';
	
	$2('imgZoom').src = imageUrl;
	
	if(navigator.appName.indexOf('Microsoft Internet Explorer') == 0)
		$2('imgZoom').onload = new function() { ProcessZoom(); }
	else
		$2('imgZoom').setAttribute('onload', 'ProcessZoom()');
		
	$2('imgZoom').onmouseover = function() { $2('txtClose').style.color = '#FF0000'; }
	$2('imgZoom').onmouseout = function() { $2('txtClose').style.color = '#666666'; }
	
	$2('txtClose').onmouseover = function() { $2('txtClose').style.color = '#FF0000'; }
	$2('txtClose').onmouseout = function() { $2('txtClose').style.color = '#666666'; }
}

function ProcessZoom()
{
	var x = parseInt($2('imgZoom').width / 2);
	var y = parseInt($2('imgZoom').height / 2);
	var arrPageScroll = getPageScroll();
	
	y = ($2('txtCaption').innerHTML == '') ? (y + 15) : (y + 35);
	
	$2('dvCaption').style.width = $2('imgZoom').width + 'px';
	//$2('dvZoom').style.top = '50%';
	//$2('dvZoom').style.left = '50%';
	//$2('dvZoom').style.marginLeft = '-' + x + 'px';
	//$2('dvZoom').style.marginLeft = '100px';
	//$2('dvZoom').style.marginTop = (arrPageScroll[1] == 0) ? ('-' + y) : (arrPageScroll[1] - y) + 'px';
	$2('dvZoom').style.marginTop = '58px';
	$2('dvLoad').style.visibility = 'hidden';
	$2('imgZoom').style.visibility = 'visible';
	$2('dvZoom').style.visibility = 'visible';
	//$2('dvZoom').style.left = (document.width / 2) - ($2('dvZoom').offsetWidth / 2)+'px';
	$2('dvZoom').style.left = (window.document.body.offsetWidth / 2) - ($2('dvZoom').offsetWidth / 2)+'px';
}

function CloseZoom()
{
	if($2('dvBgZoom') != null)
		document.body.removeChild($2('dvBgZoom'));
		
	if($2('dvZoom') != null)
		document.body.removeChild($2('dvZoom'));
}

//
// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.com
//
function getPageScroll()
{
	var xScroll, yScroll;

	if (self.pageYOffset) 
	{
		yScroll = self.pageYOffset;
		xScroll = self.pageXOffset;
	} 
	else if (document.documentElement && document.documentElement.scrollTop)
	{	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
		xScroll = document.documentElement.scrollLeft;
	} 
	else if (document.body) 
	{// all other Explorers
		yScroll = document.body.scrollTop;
		xScroll = document.body.scrollLeft;	
	}

	arrayPageScroll = new Array(xScroll,yScroll) 
	return arrayPageScroll;
}

//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.com
// Edit for Firefox by pHaez
//
function getPageSize()
{
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) 
	{	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} 
	else if (document.body.scrollHeight > document.body.offsetHeight)
	{ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} 
	else 
	{ // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}	
	
	var windowWidth, windowHeight;
	
//	console.log(self.innerWidth);
//	console.log(document.documentElement.clientWidth);

	if (self.innerHeight) 
	{	// all except Explorer
		if(document.documentElement.clientWidth)
			windowWidth = document.documentElement.clientWidth; 
		else
			windowWidth = self.innerWidth;
			
		windowHeight = self.innerHeight;
	} 
	else if (document.documentElement && document.documentElement.clientHeight) 
	{ // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} 
	else if (document.body) 
	{ // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight)
		pageHeight = windowHeight;
	else 
		pageHeight = yScroll;

//	console.log("xScroll " + xScroll)
//	console.log("windowWidth " + windowWidth)

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth)
		pageWidth = xScroll;		
	else
		pageWidth = windowWidth;
		

//	console.log("pageWidth " + pageWidth)

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}
