var myWidth=0;
var myHeight=0;
var opened=true;
var is_in=false;
var egwind=null;
var eg_overlay=null;
var eg_x=100;
var eg_y=100;
var nrp=-1;
var nrp_show=1;

window.setTimeout('opened=false;',5000);

function myPopupRelocate() {
  var scrolledX, scrolledY;
  if( self.pageYOffset ) {
    scrolledX = self.pageXOffset;
    scrolledY = self.pageYOffset;
  } else if( document.documentElement && document.documentElement.scrollTop ) {
    scrolledX = document.documentElement.scrollLeft;
    scrolledY = document.documentElement.scrollTop;
  } else if( document.body ) {
    scrolledX = document.body.scrollLeft;
    scrolledY = document.body.scrollTop;
  }

  var centerX, centerY;
  if( self.innerHeight ) {
    centerX = self.innerWidth;
    centerY = self.innerHeight;
  } else if( document.documentElement && document.documentElement.clientHeight ) {
    centerX = document.documentElement.clientWidth;
    centerY = document.documentElement.clientHeight;
  } else if( document.body ) {
    centerX = document.body.clientWidth;
    centerY = document.body.clientHeight;
  }

  var leftOffset = scrolledX + (centerX - 500) / 2;
  var topOffset = scrolledY + (centerY - 500) / 2;

  egwind.style.top = topOffset + "px";
  egwind.style.left = leftOffset + "px";
}


function mmove(e) {
	if (opened) return true;
	if (typeof(window.innerWidth) == 'number') {
		myWidth=window.innerWidth;
		myHeight=window.innerHeight
	}
	else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight )) {
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight
	}
	else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight
	}
	
	var posx = 0;
	var posy = 0;
	
	if (!e) var e = window.event;
	if (e.pageX || e.pageY) {
		posx = e.pageX;
		posy = e.pageY
	}
	else if (e.clientX || e.clientY) {
		posx = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
		posy = e.clientY + document.body.scrollTop + document.documentElement.scrollTop
	}

	eg_x = myWidth/2 - 500/2;
	eg_y = myHeight/2 - 500/2 + document.body.scrollTop;
	if (posy < 10 + document.body.scrollTop) {
		if (!is_in) nrp++;
		is_in = true;
		openWindow();
		return true
	}
	else { is_in = false }
	return true
};

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func
	}
	else {
		window.onload = function() {
			if (oldonload) {
				oldonload()
			}
		func()
	}
}

};

addLoadEvent(eg_init);

function eg_init() {
	document.onmousemove=mmove;
	var y = document.createElement('div');
	y.setAttribute('id','eg_overlay');
	document.body.appendChild(y);
	var x = document.createElement('div');
	x.setAttribute('id','egwind');
	y.appendChild(x);
	x.style.position='absolute';
	x.style.top='-1000px';
	x.style.left='-1000px';
	x.style.background='#3C73FF';
	x.style.border='1px solid #000000';
	x.style.width='500 px';
	x.style.height='500 px';
	x.style.display='block';
	var xtext="<div style='background: #3C73FF; height: 20;' align='right'><a href='' onClick='closeWindow(); return false;'><img style='position:absolute; right:2; top:2;' src='http://www.exitpollads.com/images/x.jpg' border='0'></a></div>";
	xtext +="<iframe style='background-color: #ffffff' id='eg_iframe' name='eg_iframe' src='http://www.exitpollads.com/popup.php' width='500' frameborder=0 scrolling=no height='480'></iframe>";
	x.innerHTML=xtext;
	egwind=x;
	eg_overlay=document.getElementById('eg_overlay')
};

function openWindow() {
	if (opened) return false;
	if (nrp%nrp_show!=0) {
		opened=false;
		return false
	} 
	opened=true;
	var x=egwind;
	x.style.top=eg_y+'px';
	x.style.left=eg_x+'px';
	x.style.display='block';
	var y=eg_overlay;
	y.style.position='absolute';
	y.style.top='0px';
	y.style.left='0px';
	y.style.width=document.body.scrollWidth+'px';
	y.style.height=document.body.scrollHeight+'px';
	y.style.zIndex='999';
	y.style.display='block';
	y.style.background="url('http://www.exitpollads.com/images/mask.png')";
	document.body.style.height='100%';
	document.body.onscroll = myPopupRelocate;
	window.onscroll = myPopupRelocate;
	myPopupRelocate();
	return false
};

function closeWindow() {
	egwind.style.display='none';
	eg_overlay.style.display='none';
	return false
};