var thisLayer = '';
var fid = new Array();
var wWidth = 0;
var wHeight = 0;
var scrollX = 0;
var scrollY = 0;
var picwidthglobal = 0;
var picheightglobal = 0;
var absolutewidth = 0;
var absoluteheight = 0;

function defineDimensions() {
  wWidth = getSize('w');
  wHeight = getSize('h');
  scrollX = getScrollXY('x');
  scrollY = getScrollXY('y');
  absolutewidth = getPageSizeWithScroll('w');
  absoluteheight = getPageSizeWithScroll('h');
}

function toggleLayer(picwidth, picheight)
{
  defineDimensions();
  for (var i = 0 ; i < fid.lenght ; i++) {
    document.getElementById('img' + fid[i]).style.display = "none";
  }
  picwidthglobal = picwidth;
  picheightglobal = picheight;
	positionLayer();
	document.getElementById('img' + thisLayer).style.display = "block";
	document.getElementById('img' + thisLayer).style.position = "absolute";
	defineDimensions();
  placeBackGround();
}

function getScrollXY(which) {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  if (which == 'x') {
    return scrOfX;
  }
  else {
    return scrOfY;
  }
}

function getSize(which) {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  if (which == 'w') {
    return myWidth;
  }
  else {
    return myHeight;
  }
}

function getPageSizeWithScroll(which){
	if (window.innerHeight && window.scrollMaxY) {// Firefox
		yWithScroll = window.innerHeight + window.scrollMaxY;
		xWithScroll = window.innerWidth + window.scrollMaxX;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		yWithScroll = document.body.scrollHeight;
		xWithScroll = document.body.scrollWidth;
	} else { // works in Explorer 6 Strict, Mozilla (not FF) and Safari
		yWithScroll = document.body.offsetHeight;
		xWithScroll = document.body.offsetWidth;
  }
  if (which == 'w') {
    return xWithScroll;
  }
  else {
    return yWithScroll;
  }
}

function redraw(resize) {
  if (thisLayer != '') {
    defineDimensions();
    if (resize == 1) {
      placeBackGround();
    }
    positionLayer();
  }
}

function positionLayer() {
	var xmid = ((wWidth - picwidthglobal) / 2) + scrollX;
	var ymid = ((wHeight - picheightglobal) / 2) + scrollY ;
	document.getElementById('img' + thisLayer).style.left=xmid;
	document.getElementById('img' + thisLayer).style.top=ymid;
}

function placeBackGround() {
  document.getElementById('backgroundgrey').style.height = absoluteheight;
  //document.getElementById('backgroundgrey').style.top = scrollY;
  document.getElementById('backgroundgrey').style.width = absolutewidth;
  //document.getElementById('backgroundgrey').style.left = scrollX;
  document.getElementById('backgroundgrey').style.display = "block";
}

function closeLayer() {
  if (thisLayer!='') {
    document.getElementById('img' + thisLayer).style.display='none'
    document.getElementById('backgroundgrey').style.display='none'
  }
  thisLayer = '';
}
