var _startX = 0;
var _startY = 0;
var _offsetX = 0;
var _offsetY = 0;
var _dragElement;
var _oldZIndex = 0;

if (window.addEventListener)
	window.addEventListener("load", InitDragDrop, false);
else if (window.attachEvent)
	window.attachEvent("onload", InitDragDrop, false);

function InitDragDrop()
{
  var postit = document.getElementById('postit');
  postit.style.position = 'absolute';
  postit.style.top = '100px';
  postit.style.left = '500px';
  /*postit.onmousedown = OnMouseDown;
	postit.onmouseup = OnMouseUp;*/
	for (var i = 0; i < postit.childNodes.length; i++)
	{
	   if (postit.childNodes[i].className == 'piTop' || postit.childNodes[i].className == 'piCenter' || postit.childNodes[i].className == 'piSubscribe' || postit.childNodes[i].className == 'piBlabla' || postit.childNodes[i].className == 'piLabel' || postit.childNodes[i].className == 'piBottom' || postit.childNodes[i].className == 'piClose')
     { 
	     postit.childNodes[i].onmousedown = OnMouseDown;
  	   postit.childNodes[i].onmouseup = OnMouseUp;
	   }
  }
  document.getElementById('piCloseButton').onclick = closePostit;
  window.setTimeout("showPostit()", 3000);
  
}

function showPostit()
{
  var postit = document.getElementById('postit');
  postit.style.display = '';
  document.getElementById('piMail').focus();
  document.getElementById('piMail').select();
}

function OnMouseDown(e)
{
	if (e == null)
		e = window.event;
	// var target = e.target != null ? e.target : e.srcElement;
	var target = document.getElementById('postit');
	if ((e.button == 1 && window.event != null || e.button == 0))
	{
		_startX = e.clientX; _startY = e.clientY;
		_offsetX = ExtractNumber(target.style.left);
		_offsetY = ExtractNumber(target.style.top);
		_oldZIndex = target.style.zIndex;
		target.style.zIndex = 10000;
		_dragElement = target;
		document.onmousemove = OnMouseMove;
		document.body.focus();
		document.onselectstart = function () { return false; };
		target.ondragstart = function() { return false; };
		return false;
	}
}
	
function OnMouseMove(e)
{
	if (e == null)
		var e = window.event;
	
	_dragElement.style.left = (_offsetX + e.clientX - _startX) + 'px';
	_dragElement.style.top = (_offsetY + e.clientY - _startY) + 'px';
}

function OnMouseUp(e)
{
	if (_dragElement != null)
	{
		_dragElement.style.zIndex = _oldZIndex;
		document.onmousemove = null;
		document.onselectstart = null;
		_dragElement.ondragstart = null;
		_dragElement = null;
	}
}

function ExtractNumber(value)
{
	var n = parseInt(value);
	return n == null || isNaN(n) ? 0 : n;
}

function closePostit()
{
  document.getElementById('postit').style.display = 'none';
}