$(document).ready(function()
{
	$('.dc').mouseover(function(e)
	{
		getEntryData($(this), e);
	});
});

function setPopupPosition(dictEntry, popup, e)
{
	var bottom = true;
	var right = true;

	var pos = dictEntry.offset();
	popup.css({ 'left': pos.left, 'top': pos.top }).show();

	// obliczanie pozycji popup
	var clientWidth = (window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth);
	var clientHeight = (window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight);
	var scrollLeft = (window.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft);
	var scrollTop = (window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop);
	
	var positionX = pos.left;
	var popupW = popup.width();
	//czy wyskakuje za ekran
	if (clientWidth < e.pageX + popupW - scrollLeft)
	{
		var positionX = positionX - popupW - 1;
		right = false;
	}

	var positionY = pos.top - 12;
	var popupH = popup.height();
	if (clientHeight < e.pageY + popupH - scrollTop)
	{
		var positionY = positionY + 12 - popupH - 20;
		bottom = false;
	}
	
	// ponowne spozycjonowanie popup
	popup.css({ 'left': positionX, 'top': positionY });
	
	if(bottom && right) popup.addClass('posDefault');
	if(bottom && !right) popup.addClass('posBottomLeft');
	if(!bottom && !right) popup.addClass('posTopLeft');
	if(!bottom && right) popup.addClass('posTopRight');
}

function removePopup()
{
	//$('.dictPopup').remove();
	$('.dictPopup').each(function() {
		$(this).remove();
	});
}



function getEntryData(dictEntry, e)
{
	entryId = dictEntry.attr('rel');

	// usuniecie poprzednich widoków chmurki (powodowało błędy wyświetlania przy róznych typach chmurek)
	$('.dictPopup').each(function() {
		$(this).remove();
	});

	// włączenie loadingu
	$('body').append(
	'<div style="background:none Transparent;" id=\"loading\" onmouseout=\"if (this == event.srcElement || this == event.target){$(this).remove();}\">' +
	'<img src="Themes/polanowscy/Images/lightbox/lightbox-ico-loading.gif" style="display:block;margin-left:auto;margin-right:auto;"/>' +
	'</div>');
	setPopupPosition(dictEntry, $('#loading'), e);
	var url = base.replace('Themes/polanowscy/', '') + 'API/pl/Custom.PropertyDictionary/GetEntryData/' + entryId;

	$.ajax({
		type: 'GET',
		url: url,
		dataType: 'text',
		success: function(html)
		{
			//usunięcie loadingu
			$('#loading').remove();
			//załadowanie zawartości
			$('body').append(
			//'<div class=\"dictPopup\" onmouseout=\"if (this == event.srcElement || this == event.target){removePopup();}\">' +
			'<div class=\"dictPopup\">' +
			'</div>');
			$('.dictPopup').html(html);
			setPopupPosition(dictEntry, $('.dictPopup'), e);
		}
	});
}
