var flashboxLoadingImage = 'Resources/imagens/ajax_loading.gif';

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;
}

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;
 }
 if(yScroll < windowHeight){
   pageHeight = windowHeight;
 } else {
   pageHeight = yScroll;
 }
 if(xScroll < windowWidth){
   pageWidth = windowWidth;
 } else {
   pageWidth = xScroll;
 }
 arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight)
 return arrayPageSize;
}

function pause(numberMillis) {
 var now = new Date();
 var exitTime = now.getTime() + numberMillis;
 while (true) {
   now = new Date();
   if (now.getTime() > exitTime)
     return;
   }
}

function getKey(e){
 if (e == null) { // ie
   keycode = event.keyCode;
 } else { // mozilla
   keycode = e.which;
 }

 key = String.fromCharCode(keycode).toLowerCase();
 if(key == 'x'){ hideFlashbox(); }
}

function listenKey () { document.onkeypress = getKey; }

function showFlashbox(objPath, objWidth, objHeight, site){
	hideFlash();
  var objOverlay = document.getElementById('flashboxOverlay');
  var objFlashbox = document.getElementById('flashbox');
  var objIframe = document.getElementById('flashboxIframe');
  var objLoadingImage = document.getElementById('flashboxLoadingImage');
  var arrayPageSize = getPageSize();
  var arrayPageScroll = getPageScroll();
  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';
  }
  objOverlay.style.height = (arrayPageSize[1] + 'px');
  objOverlay.style.display = 'block';
  sitePath = "Resources/Imagens/";
  if (site != null && site != ""){
    sitePath = site;
  }
  objIframe.style.width = objWidth;
  objIframe.style.height = objHeight;
  objIframe.style.display = 'block';
  objIframe.style.border = 'solid 0px black';
  var objetoFlash;
  objetoFlash = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="'+objWidth+'" height="'+objHeight+'">';
  objetoFlash += '<param name="movie" value="'+ sitePath + objPath + '">';
  objetoFlash += '<param name="quality" value="high">';
// WMODE comentado por causa de problema de charset no formulário
//  objetoFlash += '<param name="wmode" value="opaque">';
  objetoFlash += '<param name="menu" value="false">';
  objetoFlash += '<embed src="'+ sitePath + objPath +'" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="'+objWidth+'" height="'+objHeight+'"></embed>';
  objetoFlash += '</object>';
  objIframe.innerHTML = objetoFlash;
// alert(objetoFlash);
  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";
  if (navigator.appVersion.indexOf("MSIE")!=-1){ pause(250); }
  if (objLoadingImage) { objLoadingImage.style.display = 'none'; }
  selects = document.getElementsByTagName("select");
  for (i = 0; i != selects.length; i++) {
    selects[i].style.visibility = "hidden";
  }
  objFlashbox.style.display = 'block';
  arrayPageSize = getPageSize();
  objOverlay.style.height = (arrayPageSize[1] + 'px');
  listenKey();
}


function hideFlashbox(){
	showFlash();
  objOverlay = document.getElementById('flashboxOverlay');
  objFlashbox = document.getElementById('flashbox');
  objIframe = document.getElementById('flashboxIframe');
  objOverlay.style.display = 'none';
  objFlashbox.style.display = 'none';
  objIframe.style.display = 'none';
  selects = document.getElementsByTagName("select");
  for (i = 0; i != selects.length; i++) {
    selects[i].style.visibility = "visible";
  }
  document.onkeypress = '';
}


function initFlashbox() {
  if (!document.getElementsByTagName){ return; }
  var objBody = document.getElementsByTagName("body").item(0);
  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();
  var imgPreloader = new Image();
  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(){};
    return false;
 }
  imgPreloader.src = flashboxLoadingImage;
  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);

/*
  // Linka o model para fechar quando for clicado
  var objLink = document.createElement("a");
  objLink.setAttribute('href','#');
  objLink.setAttribute('title','Fechar');
  objLink.onclick = function () {hideFlashbox(); return false;}
  objFlashbox.appendChild(objLink);
*/
  var objIframe = document.createElement("div");
  objIframe.setAttribute('id','flashboxIframe');
  objIframe.style.display = 'none';
  objIframe.style.zIndex = '150';
  objFlashbox.appendChild(objIframe);
}


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;
   }
}


function showFlash(){
  var flashObjects = document.getElementsByTagName("object");
  var flashEmbeds = document.getElementsByTagName("embed");
// Gambis para pegar banners de topo e TvLs
  var ifrmTvls = document.getElementById('ifrmTvls');
  var bannersTop = document.getElementById('bannersTop');
  if (ifrmTvls){
    ifrmTvls.style.visibility = 'visible';
  }
  if (bannersTop){
    bannersTop.style.visibility = 'visible';
  }
//###
  for (i = 0; i != flashObjects.length; i++) {
    flashObjects[i].style.visibility = "visible";
  }
  for (i = 0; i != flashEmbeds.length; i++) {
    flashEmbeds[i].style.visibility = "visible";
 }
}

function hideFlash(){
  var flashObjects = document.getElementsByTagName("object");
  var flashEmbeds = document.getElementsByTagName("embed");
// Gambis para pegar banners de topo e TvLs
  var ifrmTvls = document.getElementById('ifrmTvls');
  var bannersTop = document.getElementById('bannersTop');
  if (ifrmTvls){
    ifrmTvls.style.visibility = 'hidden';
  }
  if (bannersTop){
    bannersTop.style.visibility = 'hidden';
  }
//###

  for (i = 0; i != flashObjects.length; i++) {	
    flashObjects[i].style.visibility = "hidden";
  }
  for (i = 0; i != flashEmbeds.length; i++) {
    flashEmbeds[i].style.visibility = "hidden";
  }
}


function abreFlash(varArquivo,varLargura,varAltura){
  showFlashbox(varArquivo,varLargura,varAltura,'http://www.leandrostormer.com.br/aprovacao/resources/imagens/');
    //showFlashbox(varArquivo,varLargura,varAltura,'/ls1024/resources/imagens/');

}

 addEvent(window, 'load', initFlashbox);

