var flashboxLoadingImage = '../images/loading.gif';		
var closeButton = '../images/close_grey.png';		

//
// getPageScroll()
// Returns array with x,y page scroll values.
//
function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}

//
// getPageSize()
// Returns array with page width, height and window width, height
//
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		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;
	if (self.innerHeight) {	// all except Explorer
		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;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}


//
// pause(numberMillis)
// Pauses code execution for specified time. Uses busy code, not good.
//
function pause(numberMillis) {
	var now = new Date();
	var exitTime = now.getTime() + numberMillis;
	while (true) {
		now = new Date();
		if (now.getTime() > exitTime)
			return;
	}
}

//
// getKey(key)
// Gets keycode. If 'x' is pressed then it hides the flashbox.
//
function getKey(e){
	if (e == null) { // ie
		keycode = event.keyCode;
	} else { // mozilla
		keycode = e.which;
	}
	key = String.fromCharCode(keycode).toLowerCase();
	
	if(key == 'x'){ hideFlashbox(); }
}


//
// listenKey()
//
function listenKey () {	document.onkeypress = getKey; }
	

//
// showFlashbox()
// Preloads images. Pleaces new image in flashbox then centers and displays.
//
function showFlashbox(objPath, objWidth, objHeight, site, oUseTopo)
{
	hideFlash();
	// prep objects
	var objOverlay = document.getElementById('flashboxOverlay');
	var objFlashbox = document.getElementById('flashbox');
	var objCaption = document.getElementById('flashboxCaption');
	var objIframe = document.getElementById('flashboxIframe');
	var objLoadingImage = document.getElementById('flashboxLoadingImage');
	var objFlashboxDetails = document.getElementById('flashboxDetails');
	var objTopoFlashbox = document.getElementById('topoFlashbox');
	

	
	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();

	// center flashboxLoadingImage if it exists
	if (objLoadingImage) {
		objLoadingImage.style.top = (arrayPageScroll[1] + ((arrayPageSize[3] - objLoadingImage.height) / 2) + 'px');
		objLoadingImage.style.left = (((arrayPageSize[0] - objLoadingImage.width) / 2) + 'px');
		objLoadingImage.style.display = 'block';
	}

	// set height of Overlay to take up whole page and show
	objOverlay.style.height = (arrayPageSize[1] + 'px');
	objOverlay.style.display = 'block';

	/* site do flash */
	sitePath = "../swf/";

	if (site != null && site != "")
	{
		sitePath = site;

	}
	
	var parameters = document.location.toString();
	var startParameters = parameters.search("\\?");
	
	if( startParameters > 1 && parameters.substring(startParameters + 1).search("=") > 0) {   
		parameters = parameters.substring(startParameters);
	} else {
		parameters = "";
	}
	
	//Retira .xml da url parametars //  Fabiano Gonçalves  24/07/2008
	
	remXml = parameters.indexOf(".xml");
	
	if( remXml > -1 ) {
	parameters = parameters.substring(0,remXml) + parameters.substring(remXml+4);
	} 
	

	//display topo if value == Sim
	if (oUseTopo){
		if (oUseTopo != "" && oUseTopo == "Sim"){
			objTopoFlashbox.src = sitePath + 'topoFlash.jpg';
			objTopoFlashbox.style.display = 'block';
		}
		
	}

	objIframe.src = sitePath + objPath + parameters;
	objIframe.width = objWidth;
	objIframe.height = objHeight;
	objIframe.marginheight="0";
	objIframe.marginwidth="0";
	objIframe.style.display = 'block';
	objIframe.style.overflow = 'hidden';


	// center flashbox and make sure that the top and left values are not negative
	// and the image placed outside the viewport
	var flashboxTop = arrayPageScroll[1] + ((arrayPageSize[3] - objHeight) / 2);
	var flashboxLeft = ((arrayPageSize[0] - objWidth) / 2);
	
	objFlashbox.style.top = (flashboxTop < 0) ? "0px" : flashboxTop + "px";
	objFlashbox.style.left = (flashboxLeft < 0) ? "0px" : flashboxLeft + "px";


	objFlashboxDetails.style.width = objWidth + 'px';
	
	objCaption.style.display = 'none';
	
	// A small pause between the image loading and displaying is required with IE,
	// this prevents the previous image displaying for a short burst causing flicker.
	if (navigator.appVersion.indexOf("MSIE")!=-1){
		pause(250);
	} 

	if (objLoadingImage) {	objLoadingImage.style.display = 'none'; }

	// Hide select boxes as they will 'peek' through the image in IE
	selects = document.getElementsByTagName("select");
	for (i = 0; i != selects.length; i++) {
			selects[i].style.visibility = "hidden";
	}


	objFlashbox.style.display = 'block';

	// After image is loaded, update the flashboxOverlay height as the new image might have
	// increased the overall page height.
	arrayPageSize = getPageSize();
	objOverlay.style.height = (arrayPageSize[1] + 'px');
	
	// Check for 'x' keypress
	listenKey();

}

//
// hideFlashbox()
//
function hideFlashbox()
{
	showFlash();
	// get objects
	objOverlay = document.getElementById('flashboxOverlay');
	objFlashbox = document.getElementById('flashbox');
	objIframe = document.getElementById('flashboxIframe');

	// hide flashbox and flashboxOverlay
	objOverlay.style.display = 'none';
	objFlashbox.style.display = 'none';
	objIframe.style.display = 'none';
	objIframe.src = 'about:blank';

	// make select boxes visible
	selects = document.getElementsByTagName("select");
    for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = "visible";
	}

	// disable keypress listener
	document.onkeypress = '';
}


//
// initFlashbox()
// Function inserts html markup at the top of the page which will be used as a
// container for the flashboxOverlay pattern and the inline image.
//
var initiated = false;
function initFlashbox() {
	if (!document.getElementsByTagName){ return; }
	
	var objBody = document.getElementsByTagName("body").item(0);
	
	// create flashboxOverlay div and hardcode some functional styles (aesthetic styles are in CSS file)
	var objOverlay = document.createElement("div");
	objOverlay.setAttribute('id','flashboxOverlay');
	objOverlay.onclick = function () {hideFlashbox(); return false;}
	objOverlay.style.display = 'none';
	objOverlay.style.position = 'absolute';
	objOverlay.style.top = '0';
	objOverlay.style.left = '0';
	objOverlay.style.zIndex = '90';
 	objOverlay.style.width = '100%';
	objBody.insertBefore(objOverlay, objBody.firstChild);
	
	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();

	// preload and create loader image
	var imgPreloader = new Image();
	
	// if loader image found, create link to hide flashbox and create loadingimage
	imgPreloader.onload=function(){

		var objLoadingImageLink = document.createElement("a");
		objLoadingImageLink.setAttribute('href','#');
		objLoadingImageLink.onclick = function () {hideFlashbox(); return false;}
		objOverlay.appendChild(objLoadingImageLink);
		
		var objLoadingImage = document.createElement("img");
		objLoadingImage.src = flashboxLoadingImage;
		objLoadingImage.setAttribute('id','flashboxLoadingImage');
		objLoadingImage.style.position = 'absolute';
		objLoadingImage.style.zIndex = '150';
		objLoadingImage.style.width = 'auto';
		objLoadingImage.style.height = 'auto';
		objLoadingImageLink.appendChild(objLoadingImage);

		imgPreloader.onload=function(){};	//	clear onLoad, as IE will flip out w/animated gifs

		return false;
	}

	imgPreloader.src = flashboxLoadingImage;

	// create flashbox div, same note about styles as above
	var objFlashbox = document.createElement("div");
	objFlashbox.setAttribute('id','flashbox');
	objFlashbox.style.display = 'none';
	objFlashbox.style.position = 'absolute';
	objFlashbox.style.zIndex = '100';
	objBody.insertBefore(objFlashbox, objOverlay.nextSibling);
	
	// create link
	var objLink = document.createElement("a");
	objLink.setAttribute('href','#');
	objLink.setAttribute('title','Fechar');
	objLink.onclick = function () {hideFlashbox(); return false;}
	objFlashbox.appendChild(objLink);
	
	// create topDiv div, if top exists for project
	var objTopoFlashbox = document.createElement("img");
	objTopoFlashbox.setAttribute('id','topoFlashbox');
	objTopoFlashbox.style.display = 'none';
	objLink.appendChild(objTopoFlashbox);
	
	// preload and create close button image
	var imgPreloadCloseButton = new Image();

	// if close button image found, 
	imgPreloadCloseButton.onload=function(){

		var objCloseButton = document.createElement("img");
		objCloseButton.src = closeButton;
		objCloseButton.setAttribute('id','closeButton');
		objCloseButton.style.position = 'absolute';
		objCloseButton.style.zIndex = '200';
		//objCloseButton.style.top = objLink.height+'px';
		objLink.appendChild(objCloseButton);

		return false;
	}

	//imgPreloadCloseButton.src = closeButton;

	// create iframe
	var objIframe = document.createElement("iframe");
	objIframe.setAttribute('id','flashboxIframe');
	objIframe.style.display = 'none';
	objLink.appendChild(objIframe);
	
	// create details div, a container for the caption and keyboard message
	var objFlashboxDetails = document.createElement("div");
	objFlashboxDetails.setAttribute('id','flashboxDetails');
	objFlashbox.appendChild(objFlashboxDetails);

	// create caption
	var objCaption = document.createElement("div");
	objCaption.setAttribute('id','flashboxCaption');
	objCaption.style.display = 'none';
	objFlashboxDetails.appendChild(objCaption);

	// create keyboard message
	var objKeyboardMsg = document.createElement("div");
	objKeyboardMsg.setAttribute('id','flashboxKeyboardMsg');
	objKeyboardMsg.innerHTML = '<a href="#" onclick="hideFlashbox(); return false;"><img src="../icon/fechar.gif" /></a>';
	objFlashboxDetails.appendChild(objKeyboardMsg);

	initiated = true;
}


// 30-08-2007 - Fernando Tessmann --------------------
function addEvent(obj, evType, fn)
{
	if (obj.addEventListener){
	   obj.addEventListener(evType, fn, false);
	   return true;
	}
	else if (obj.attachEvent){
	   var r = obj.attachEvent("on"+evType, fn);
	   return r;
	}else{
	   return false;
	}
}
addEvent(window, 'load', initFlashbox);

function getParameters()
{

	var parameters = new Object();

	var url = document.location.toString();
	var start = url.search("\\?");
	
	if( start > 1 && url.substring(start + 1).search("=") > 0) {   
		
		url = url.substring(start+1);
		var parms = url.split("&");
		
		for (i= 0 ; i < parms.length; i++) {
			var expr = parms[i].split("=");
			
			parameters[expr[0]]=expr[1];
		}
		
	}	
	
	return parameters;
}

function showFlashOnLoad(){
	if (initiated){
		window.clearInterval(showFlashOnLoadInterval);
		showFlashbox(parms["url"] , parms["width"] , parms["height"] , parms["site"]);
	}
}

function showFlash(){
	var flashObjects = document.getElementsByTagName("object");
	for (i = 0; i != flashObjects.length; i++) {
		flashObjects[i].style.visibility = "visible";
	}

	var flashEmbeds = document.getElementsByTagName("embeds");
	for (i = 0; i != flashEmbeds.length; i++) {
		flashEmbeds[i].style.visibility = "visible";
	}
}

function hideFlash(){
	var flashObjects = document.getElementsByTagName("object");
	for (i = 0; i != flashObjects.length; i++) {
		flashObjects[i].style.visibility = "hidden";
	}

	var flashEmbeds = document.getElementsByTagName("embeds");
	for (i = 0; i != flashEmbeds.length; i++) {
		flashEmbeds[i].style.visibility = "hidden";
	}

}

var parms = getParameters();
var showFlashOnLoadInterval;

if (parms["url"] && parms["width"] && parms["height"] && parms["site"]){
	showFlashOnLoadInterval = window.setInterval("showFlashOnLoad()", 100);
}