var mouPositionX = 0, mouPositionY = 0;
var TimerId = -1;
$(document).ready(function(){
	$("#ShareImg").hover(
		function(){
			if (TimerId != -1)
				clearInterval(TimerId);
			elementX = this.x;
			elementY = this.y;
			if(!elementX){//hack for IE
			   this.style.position = "absolute";
			   elementX = this.offsetLeft;
			   elementX -= this.clientWidth;
			   elementY = this.offsetTop;
			   elementY -= 8;
			   this.style.position = '';
			}
			$("#SharingTools").show('slow');
			$("#SharingTools")[0].style.left = elementX + 'px';
			$("#SharingTools")[0].style.top = (elementY + $("#ShareImg")[0].clientHeight + 2) + 'px';
		},function(){
			if (TimerId != -1)
				clearInterval(TimerId);
			TimerId = setTimeout('HideSharingTools()', 1000);
		}
	);
	$("#SharingTools").mouseout(
		function(){
			if (TimerId != -1)
				clearInterval(TimerId);
			TimerId = setTimeout('HideSharingTools()', 1000);
		}
	);
	$().mousemove(
		function(e){
			mouPositionX = e.pageX;
			mouPositionY = e.pageY;
		}
	);
});

function HideSharingTools()
{
	if ((mouPositionX < $("#SharingTools")[0].offsetLeft || mouPositionX > $("#SharingTools")[0].offsetLeft + $("#SharingTools")[0].clientWidth)
	  || (mouPositionY < $("#SharingTools")[0].offsetTop || mouPositionY > $("#SharingTools")[0].offsetTop + $("#SharingTools")[0].clientHeight)){
		$("#SharingTools").hide('fast');
		TimerId = -1;
		setTimeout(function()
		{
			$('#SharingTable').show();
			$('#EmailTable').hide();
		}, 200);
	}
}

function AddBookmark()
{
	if (window.sidebar)
		window.sidebar.addPanel('Alinea Newsletter', location.href, '');
	else if (document.all)
		window.external.AddFavorite(location.href,'Alinea Newsletter');
	else if (window.opera)
		alert(BookMarkErrorMsg);
	else
		alert(BookMarkErrorMsg);
}

function ShowEmailItems()
{
	$('#SharingTable').animate({ height: 'hide', opacity: 'hide' }, 'fast');
	$('#EmailTable').animate({ height: 'show', opacity: 'show' }, 'slow');
}

function SendEmail()
{
	var rx = new RegExp("\\w+([-+.']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");
	Result = rx.exec($('#txtTo').val());
	if (Result == null || Result[0] !=$('#txtTo').val())
	{
		$('#txtTo').css({ display:'inline' });
		$('#txtTo').css({ borderColor:'#df953b' }).Pulsate(300,4);
		$('#txtTo').Pulsate(300,4);
		$('#txtTo')[0].focus();
		return;
	}
	Result = rx.exec($('#txtFrom').val());
	if (Result == null || Result[0] !=$('#txtFrom').val())
	{
		$('#txtFrom').css({ display:'inline' });
		$('#txtFrom').css({ borderColor:'#df953b' }).Pulsate(300,4);
		$('#txtFrom').Pulsate(300,4);
		$('#txtFrom')[0].focus();
		return;
	}
	$.post("../../includes/sendemail.asp?lang="+lang, { to: encodeURIComponent($('#txtTo').val()), body: encodeURIComponent($('#txtNote').val()), url: UrlLocation, from: encodeURIComponent($('#txtFrom').val()) },
	  function(data){
		alert(EmailSentMsg); 
	  }
	);
}