/* Blog gifts system */

var gifts = new Array(); 

function pollBlogGifts(blogID) {
 $.getJSON('/functions/gifts/poll/' + blogID, {}, function(data) {
	var fullCount = 0;
	$.each(data.gifts, function(i, gift) {
		gift.index = i;
		fullCount++;
		gifts[gift.id] = gift;
		overpopup[gift.id] = false;
		overgift[gift.id] = false;
		var link = $("<a/>")
			.attr("giftid", gift.id)
			.hover(
				function() { 
					overgift[$(this).attr('giftid')] = true;
					showGiftInformation($(this).attr('giftid'))
				},
				function() {
					overgift[$(this).attr('giftid')] = false;
					removeGiftInformation($(this).attr('giftid'));
				})
			.html('<img src="http://mediaserver-2.vuodatus.net/images/pixel.gif" width="100" height="120"/>');
 		$("<div/>")
			.css({"background-image": "url('http://mediaserver-2.vuodatus.net/images/gifts/" + gift.gift_id + ".jpg')",
				"left": (5 + (gift.index * 70))})
			.addClass("blogGift")
			.addClass("fadedGift")
			.attr("id", "gift" + gift.id)
			.appendTo("#blogGifts");
		link.appendTo($("#gift" + gift.id));
	});
	$('#blogGifts').css("width", (5 + (fullCount * 70)));
 });
}

var overpopup = new Array();
var overgift = new Array();

function removeGiftInformation(id) {
	setTimeout("removeGiftInformationImpl(" + id + ")", 500);
}

function removeGiftInformationImpl(id) {
	if(overgift[id] || overpopup[id]) {
		return;
	}
	$('#giftPopup' + id).remove();
	$("#gift" + id).removeClass("normalGift");		
	$("#gift" + id).addClass("fadedGift");

}

function hideGifts() {
	if(confirm("Haluatko varmasti piilottaa lahjat? Sinulle ei silloin esitetä lahjoja missään Vuodatus.net blogeissa")) {
		$.get("/functions/gifts/setblockingcookie", {}, function(t) {
			alert("Lahjat on nyt piilotettu tältä koneelta. Saat lahjat takaisin näkyviin poistamalla evästeet koneelta.");
			self.location.reload(true);
		});
	}
}

function giftDialog() {
	$('<iframe/>').attr("src", "/functions/gifts/give/" + blogID).attr("width", "500").attr("height", "350")
		.attr("frameborder", "no").attr("border", "0")
		.appendTo($('#giftDialogWrapper'));
	$('#giftDialogWrapper').show();
}

function clearOthers(id) {
	for(key in gifts) {
		if(id == key) {
			continue;
		}	
		$("#giftPopup" + key).remove();
		$("#gift" + key).removeClass("normalGift");		
		$("#gift" + key).addClass("fadedGift");
	}
	$("#gift" + id).removeClass("fadedGift");	
	$("#gift" + id).addClass("normalGift");
}

function showGiftInformation(id) {
	var gift = gifts[id];
	i = gift.index;
	overgift[id] = true;
	clearOthers(id);
	$("<div/>")
		.css("left", (50 + (i * 65)))
		.attr("class","giftInfoPopup")
		.attr("id", "giftPopup" + id)
		.hover(
			function() {
				overpopup[id] = true;
			},
			function() {
				overpopup[id] = false;
				removeGiftInformation(id);
			}
		)
		.html('<div class="popupText"><div><b>Lahjan antoi ' + 
		((gift.homepage != null && gift.homepage != '') ? '<a href="' + gift.homepage + '">': "") + gift.sender + (gift.homepage != '' ? '</a>' : '') 
		+ '</b></div>' +
		'<div style="margin-top: 0.4em; height: 85px; overflow: auto;">' +
		(gift.message == null ? '' : '<i>&quot;' + gift.message + '&quot;</i>') +
		'</div>' +
		'<div class="bubbleLinks"><a href="javascript://" onclick="javascript:giftDialog();">ANNA LAHJA</a> | <a href="javascript://" onclick="javascript:hideGifts()">PIILOTA</a> | <a href="/functions/gifts/all">KAIKKI LAHJAT</a></div></div>')
		.appendTo($("body"));
	
}