var Analytics =
{
	getTotals : function ()
	{
		$.ajax
		(
			{
				type : "POST",
				url : mainUrl + "mods/analytics/receiver.php",
				data : "req=totals",
				dataType : "json",
				success : function (response)
				{
					$("#h3PromoNum").text (response.ttlpromos);
					$("#h3AvgTip").text (Misc.convertCurrency( (response.avgtip == null ? 0 : response.avgtip)));
					$("#h3AllTips").text (Misc.convertCurrency( (response.ttltip == null ? 0 : response.ttltip)));
				}
			}
		);
	},

	getTotals2 : function ()
	{
		$.ajax
		(
			{
				type : "POST",
				url : mainUrl + "mods/analytics/receiver.php",
				data : "req=totals2",
				dataType : "json",
				success : function (response)
				{
					$("#h3PromoNum").text (response.ttlpromos);
					$("#h3AvgTip").text (Misc.convertCurrency( (response.avgtip == null ? 0 : response.avgtip)));
					$("#h3MonthlyTips").text (Misc.convertCurrency(  response.thismonth));
					$("#h3AllTips").text (Misc.convertCurrency((response.alltime == null ? 0 : response.alltime)));
					$("#h3TipsCash").text (Misc.convertCurrency(  response.cashout));
					$("#h3WaitingTips").text (Misc.convertCurrency( response.waiting));
				}
			}
		);
	},

	askRedeemTips : function ()
	{
		$(document.body).append ("<div class=\"wait\">Please wait</div>");
		$.ajax
		(
			{
				type : "POST",
				url : mainUrl + "mods/analytics/receiver.php",
				data : "req=askredeem",
				dataType : "json",
				success : function (response)
				{
					$("#divRedeemTips").html (response.msg);
					$("#divRedeemTips").show ();
					$(".wait").remove ();
				}
			}
		);
	},

	redeemTips : function ()
	{
		var emailAddr = $("#txtPayPal").val ().match (/[^\@]+\@[^\@]+/i);
		if (emailAddr != null)
		{
			$(document.body).append ("<div class=\"wait\">Please wait</div>");
			$.ajax
			(
				{
					type : "POST",
					url : mainUrl + "mods/analytics/receiver.php",
					data : "req=redeem&email=" + encodeURIComponent (emailAddr),
					dataType : "json",
					success : function (response)
					{
						$("#divRedeemTips").html (response.msg);
						$(".wait").remove ();
					}
				}
			);
		}
	},

	closeRedeem : function ()
	{
		$("#divRedeemTips").hide ();
	},

	getPromoList : function ()
	{
		$.ajax
		(
			{
				type : "POST",
				url : mainUrl + "mods/analytics/receiver.php",
				data : "req=promolist",
				dataType : "json",
				success : function (response)
				{
					var ulSrc = "";
					for (var i = 0;i < response.length;i++)
					{
						ulSrc += "<li><a href=\"javascript:Analytics.loadPromotion (" + response[i].pid + ", 'ov');\">";
						ulSrc += "<img src=\"" + mainUrl + "media/promoxsmall/" + response[i].image  + "\" style=\"width:26px; margin-right:5px;\"/>";
						ulSrc += response[i].title + "</a></li>";
					}
					$("#ulPromoStatList").html (ulSrc);
					Analytics.loadPromotion (response[0].pid, "ov");
				}
			}
		);
	},

	getPromoList2 : function ()
	{
		$.ajax
		(
			{
				type : "POST",
				url : mainUrl + "mods/analytics/receiver.php",
				data : "req=promolist2",
				dataType : "json",
				success : function (response)
				{
					var ulSrc = "";
					for (var i = 0;i < response.length;i++)
					{
						ulSrc += "<li><a href=\"javascript:Analytics.loadPromotion2 (" + response[i].pid + ", 'ov');\">";
						ulSrc += "<img src=\"" + mainUrl + "media/promoxsmall/" + response[i].image  + "\" style=\"width:26px; margin-right:5px;\"/>";
						ulSrc += response[i].title + "</a></li>";
					}
					$("#ulPromoStatList").html (ulSrc);
					Analytics.loadPromotion2 (response[0].pid, "ov");
				}
			}
		);
	},

	loadPromotion : function (refPromo, refFilter)
	{
		$(document.body).append ("<div class=\"wait\">Please wait</div>");
		$.ajax
		(
			{
				type : "POST",
				url : mainUrl + "mods/analytics/receiver.php",
				data : "req=loadpromo&promotion=" + refPromo + "&filter=" + refFilter,
				dataType : "json",
				success : function (response)
				{
					/* Earned by date and promoter */
					var tblSrc = "";
					var ttlLeadsYou = 0;
					var ttlLeadsSupporters = 0;
					var ttlLeadsContacts = 0;
					var ttlLeadsNetworks = 0;
					var ttlLeadsFollows = 0;
					var ttlLeadsStrangers = 0;
					for (uid in response.tips)
					{
						switch (response.tips[uid].source)
						{
							case "you":
								ttlLeadsYou += parseFloat (response.tips[uid].leads);
								break;
							case "contact":
								ttlLeadsContacts += parseFloat (response.tips[uid].leads);
								ttlLeadsSupporters += parseFloat (response.tips[uid].leads);
								break;
							case "nwkmember":
								ttlLeadsNetworks += parseFloat (response.tips[uid].leads);
								ttlLeadsSupporters += parseFloat (response.tips[uid].leads);
								break;
							case "follower":
								ttlLeadsFollows += parseFloat (response.tips[uid].leads);
								ttlLeadsSupporters += parseFloat (response.tips[uid].leads);
								break;
							default:
								ttlLeadsStrangers += parseFloat (response.tips[uid].leads);
								ttlLeadsSupporters += parseFloat (response.tips[uid].leads);
								break;
						}

						tblSrc += "<tr class=\"title\">";
						tblSrc += "<td class=\"title\"><img src=\"" + mainUrl + "media/avatars/" + response.tips[uid].avatar + "\" />" + response.tips[uid].name + "</td>";
						tblSrc += "<td colspan=\"5\" class=\"genstats\">" + response.tips[uid].leads + " Leads&nbsp;&nbsp;&nbsp;&nbsp;" + Misc.convertCurrency(response.tips[uid].tips) + " Earned tips&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"#\">Contact supporter</a><a href=\"#\">Block supporter</a><a href=\"#\">View details</a></td>";
						tblSrc += "</tr>";
						tblSrc += "<tr class=\"subtitle\">";
						tblSrc += "<td class=\"empty\"></td>";
						tblSrc += "<td>Referred on</td>";
						tblSrc += "<td>Views</td>";
						tblSrc += "<td>Clicks</td>";
						tblSrc += "<td>Leads</td>";
						tblSrc += "<td>Earned</td>";
						tblSrc += "</tr>";
						for (var i = 0;i < response.tips[uid].contribution.length;i++)
						{
							tblSrc += "<tr>";
							tblSrc += "<td class=\"empty\"></td>";
							tblSrc += "<td>" + response.tips[uid].contribution[i].date + "</td>";
							tblSrc += "<td>" + response.tips[uid].contribution[i].views + "</td>";
							tblSrc += "<td>" + response.tips[uid].contribution[i].clicks + "</td>";
							tblSrc += "<td>" + response.tips[uid].contribution[i].leads + "</td>";
							tblSrc += "<td>" +Misc.convertCurrency(response.tips[uid].contribution[i].amount) + "</td>";
							tblSrc += "</tr>";
						}
					}
					$("#tblSupporters").html (tblSrc);


					/* Leads per source */
					var ttlLeads = ttlLeadsYou + ttlLeadsSupporters;
					$("span[rel='you']").text (ttlLeads == 0 ? "0%" : Math.round (ttlLeadsYou * 100 / ttlLeads) + "%");
					$("span[rel='supporters']").text (ttlLeads == 0 ? "0%" : Math.round (ttlLeadsSupporters * 100 / ttlLeads) + "%");
					$("span[rel='source_you']").text (ttlLeads == 0 ? "0%" : Math.round (ttlLeadsYou * 100 / ttlLeads) + "%");
					$("div[rel='source_you_graph']").animate
					(
						{
							width : ttlLeads == 0 || ttlLeadsYou == 0 ? 1 : Math.round (ttlLeadsYou * 120 / ttlLeads)
						},
						500
					);
					$("span[rel='source_contacts']").text (ttlLeads == 0 ? "0%" : Math.round (ttlLeadsContacts * 100 / ttlLeads) + "%");
					$("div[rel='source_contacts_graph']").animate
					(
						{
							width : ttlLeads == 0 || ttlLeadsContacts == 0 ? 1 : Math.round (ttlLeadsContacts * 120 / ttlLeads)
						},
						500
					);
					$("span[rel='source_networks']").text (ttlLeads == 0 ? "0%" : Math.round (ttlLeadsNetworks * 100 / ttlLeads) + "%");
					$("div[rel='source_networks_graph']").animate
					(
						{
							width : ttlLeads == 0 || ttlLeadsNetworks == 0 ? 1 : Math.round (ttlLeadsNetworks * 120 / ttlLeads)
						},
						500
					);
					$("span[rel='source_follows']").text (ttlLeads == 0 ? "0%" : Math.round (ttlLeadsFollows * 100 / ttlLeads) + "%");
					$("div[rel='source_follows_graph']").animate
					(
						{
							width : ttlLeads == 0 || ttlLeadsFollows == 0 ? 1 : Math.round (ttlLeadsFollows * 120 / ttlLeads)
						},
						500
					);
					$("span[rel='source_other']").text (ttlLeads == 0 ? "0%" : Math.round (ttlLeadsStrangers * 100 / ttlLeads) + "%");
					$("div[rel='source_other_graph']").animate
					(
						{
							width : ttlLeads == 0 || ttlLeadsStrangers == 0 ? 1 : Math.round (ttlLeadsStrangers * 120 / ttlLeads)
						},
						500
					);


					/* Analysis */
					var scaleRatio = response.analysis.impressions == 0 ? 0 : 135 / Math.max (response.analysis.impressions, response.analysis.uimpressions, response.analysis.shares, response.analysis.uclicks);
					$("div[rel='impressions']").text (response.analysis.impressions);
					$("div[rel='impressions_graph']").animate
					(
						{
							height : response.analysis.impressions * scaleRatio
						},
						500
					);
					$("div[rel='impressions_arrow']").text (response.analysis.impressions == 0 ? "0%" : Math.round (response.analysis.uimpressions * 1000 / response.analysis.impressions) / 10 + "%");
					$("div[rel='unique_impressions']").text (response.analysis.uimpressions);
					$("div[rel='unique_impressions_graph']").animate
					(
						{
							height : response.analysis.uimpressions == 0 ? 1 : response.analysis.uimpressions * scaleRatio
						},
						500
					)
					$("div[rel='unique_impressions_arrow']").text (response.analysis.uimpressions == 0 ? "0%" : Math.round (response.analysis.shares * 1000 / response.analysis.uimpressions) / 10 + "%");
					$("div[rel='shares']").text (response.analysis.shares);
					$("div[rel='shares_graph']").animate
					(
						{
							height : response.analysis.shares == 0 ? 1 : response.analysis.shares * scaleRatio
						},
						500
					);
					$("div[rel='shares_arrow']").text (response.analysis.shares == 0 ? "0%" : Math.round (response.analysis.uclicks * 1000 / response.analysis.shares) / 10 + "%");
					$("div[rel='unique_clicks']").text (response.analysis.uclicks);
					$("div[rel='unique_clicks_graph']").animate
					(
						{
							height : response.analysis.uclicks == 0 ? 1 : response.analysis.uclicks * scaleRatio
						},
						500
					);


					/* Soc. networks */
					$("span[rel='channel_fb']").text (response.channels["fb"]);
					$("span[rel='channel_fb_perc']").text (ttlLeads == 0 ? "0%" : Math.round (response.channels["fb"] * 100 / ttlLeads) + "%");
					$("span[rel='channel_tw']").text (response.channels["tw"]);
					$("span[rel='channel_tw_perc']").text (ttlLeads == 0 ? "0%" : Math.round (response.channels["tw"] * 100 / ttlLeads) + "%");
					$("span[rel='channel_in']").text (response.channels["in"]);
					$("span[rel='channel_in_perc']").text (ttlLeads == 0 ? "0%" : Math.round (response.channels["in"] * 100 / ttlLeads) + "%");
					$("span[rel='channel_g']").text (response.channels["g"]);
					$("span[rel='channel_g_perc']").text (ttlLeads == 0 ? "0%" : Math.round (response.channels["g"] * 100 / ttlLeads) + "%");
					var otherLeads = parseFloat (response.channels["wg"]) + parseFloat (response.channels["em"]) + parseFloat (response.channels["0"]);
					$("span[rel='channel_other']").text (otherLeads);
					$("span[rel='channel_other_perc']").text (ttlLeads == 0 ? "0%" : Math.round (otherLeads * 100 / ttlLeads) + "%");


					/* Pie chart */
					var channelData =
					[
						{
							label : "fb",
							data : parseFloat (response.channels["fb"]),
							color : "#1221e7"
						},
						{
							label : "tw",
							data : parseFloat (response.channels["tw"]),
							color : "#01a2ba"
						},
						{
							label : "in",
							data : parseFloat (response.channels["in"]),
							color : "#00013c"
						},
						{
							label : "g",
							data : parseFloat (response.channels["g"]),
							color : "#b90606"
						},
						{
							label : "other",
							data : otherLeads,
							color : "#e59804"
						},
					];

					$("#divChannelPie").html ("");
					$.plot ($("#divChannelPie"), channelData,
					{
						series :
						{
							pie :
							{
								show : true,
								label :
								{
									show : false
								}
							}
						},
						legend :
						{
							show : false
						}
					});


					/* Performance */
					var socnetsPerformance = parseFloat (response.channels["fb"]) + parseFloat (response.channels["tw"]) + parseFloat (response.channels["in"]) + parseFloat (response.channels["g"]);
					var emailingPerformance = parseFloat (response.channels["em"]);
					var widgetsPerformance = parseFloat (response.channels["wg"]);
					var directPerformance = parseFloat (response.channels["0"]);
					$("div[rel='socnets']").text (ttlLeads == 0 ? "0.00%" : Math.round (socnetsPerformance * 10000 / ttlLeads) / 100 + "%");
					$("div[rel='socnets_graph']").animate
					(
						{
							height : ttlLeads == 0 ? 1 : socnetsPerformance * 90 / ttlLeads
						},
						500
					)
					$("div[rel='emails']").text (ttlLeads == 0 ? "0.00%" : Math.round (emailingPerformance * 10000 / ttlLeads) / 100 + "%");
					$("div[rel='emails_graph']").animate
					(
						{
							height : ttlLeads == 0 ? 1 : emailingPerformance * 90 / ttlLeads
						},
						500
					)
					$("div[rel='widgets']").text (ttlLeads == 0 ? "0.00%" : Math.round (widgetsPerformance * 10000 / ttlLeads) / 100 + "%");
					$("div[rel='widgets_graph']").animate
					(
						{
							height : ttlLeads == 0 ? 1 : widgetsPerformance * 90 / ttlLeads
						},
						500
					)
					$("div[rel='direct']").text (ttlLeads == 0 ? "0.00%" : Math.round (directPerformance * 10000 / ttlLeads) / 100 + "%");
					$("div[rel='direct_graph']").animate
					(
						{
							height : ttlLeads == 0 ? 1 : directPerformance * 90 / ttlLeads
						},
						500
					)


					/* General information */
					var Tpl = ttlLeads == 0 ? response.details.tip : Math.round (response.details.tip * 100 / ttlLeads) / 100;
					$("#h2PromoTitle").text (response.details.title);
					$("span[rel='pstart']").text (response.details.pstart);
					$("span[rel='pend']").text (response.details.pend);
					$("span[rel='tip']").text (Misc.convertCurrency(response.details.tip));
					$("span[rel='tpl']").text (Misc.convertCurrency(Tpl));
					$("span[rel='impressions']").text (response.analysis.impressions);
					$("span[rel='leads']").text (General.formatNumber (response.analysis.uimpressions * 0.2 + response.analysis.uclicks * 0.8));
					$("span[rel='views']").text (response.analysis.uimpressions);
					$("span[rel='clicks']").text (response.analysis.uclicks);


					/* Time filters */
					$("#hrefOverview").attr ("href", "javascript:Analytics.loadPromotion (" + response.details.pid + ", 'ov')");
					$("#hrefYesterday").attr ("href", "javascript:Analytics.loadPromotion (" + response.details.pid + ", 'y')");
					$("#hrefTWeek").attr ("href", "javascript:Analytics.loadPromotion (" + response.details.pid + ", 'tw')");
					$("#hrefLWeek").attr ("href", "javascript:Analytics.loadPromotion (" + response.details.pid + ", 'lw')");
					$("#hrefLMonth").attr ("href", "javascript:Analytics.loadPromotion (" + response.details.pid + ", 'lm')");
					$("#ulPromoAnalyticsMenu li").removeClass ("selected");
					$("#ulPromoAnalyticsMenu li a[rel='" + refFilter + "']").parent ().addClass ("selected");

					$(".wait").remove ();
				}
			}
		);
	},

	loadPromotion2 : function (refPromo, refFilter)
	{
		$(document.body).append ("<div class=\"wait\">Please wait</div>");
		$.ajax
		(
			{
				type : "POST",
				url : mainUrl + "mods/analytics/receiver.php",
				data : "req=loadpromo2&promotion=" + refPromo + "&filter=" + refFilter,
				dataType : "json",
				success : function (response)
				{
					/* Earned by date */
					var ttlContribution = 0;
					var ttlLeads = 0;
					var tblSrc = "";
					for (i = 0;i < response.earned.length;i++)
					{
						tblSrc += "<tr>";
						tblSrc += "<td>" + response.earned[i].udate + "</td>";
						tblSrc += "<td>" + response.earned[i].impressions + "</td>";
						tblSrc += "<td>" + response.earned[i].uimpressions + "</td>";
						tblSrc += "<td>" + response.earned[i].uclicks + "</td>";
						tblSrc += "<td>" + (response.earned[i].uimpressions == 0 ? 0 : Math.round (response.earned[i].uleads * 100 / response.earned[i].uimpressions) / 100) + "</td>";
						tblSrc += "<td>" + Misc.convertCurrency(response.earned[i].contribution) + "</td>";
						tblSrc += "</tr>";
						ttlContribution += parseFloat (response.earned[i].contribution);
						ttlLeads += parseFloat (response.earned[i].uleads);
					}
					$("#tblTipsDaily tbody").html (tblSrc);


					/* Promotion info + general stats */
					$("#h2PromoTitle").text (response.details.title);
					$("span[rel='pstart']").text (response.details.pstart);
					$("span[rel='pend']").text (response.details.pend);
					$("span[rel='tip']").text (Misc.convertCurrency( response.details.tip));
					$("span[rel='tpl']").text (Misc.convertCurrency(response.details.tpl));
					$("#hrefViewPromo").attr ("href", mainUrl + currLang + "/promotion/" + response.details.ftitle + "." + response.details.pid + ".html");

					var daysleft = parseInt (parseInt (response.details.timeleft) / 24);
					var hoursleft = parseInt (response.details.timeleft) - daysleft * 24;
					$("span[rel='timeleft']").text (daysleft + " days " + hoursleft + " hours");
					$("#divOwnerInfo").html ("<img src=\"" + mainUrl + "media/avatars/" + response.details.uavatar + "\" style=\"width:26px;\" /><span id=\"spanPromoOwner\">" + response.details.uname + "</span><a href=\"javascript:Follow.followUser(" + response.details.uid + ");\" class=\"bluebutton2\">Follow this promoter</a>");
					$("#divYouEarned").text ( Misc.convertCurrency(General.formatNumber (ttlContribution)));
					$("#divCurrTpl").text ( Misc.convertCurrency(response.details.tpl));

					$("span[rel='leads']").text (General.formatNumber (response.analysis.uimpressions * 0.2 + response.analysis.uclicks * 0.8));
					$("span[rel='unique_impressions']").text (response.analysis.uimpressions);
					$("span[rel='clicks']").text (response.analysis.uclicks);


					/* Analysis */
					var scaleRatio = response.analysis.impressions == 0 ? 0 : 135 / Math.max (response.analysis.impressions, response.analysis.uimpressions, response.analysis.shares, response.analysis.uclicks);
					$("div[rel='impressions']").text (response.analysis.impressions);
					$("div[rel='impressions_graph']").animate
					(
						{
							height : response.analysis.impressions * scaleRatio
						},
						500
					);
					$("div[rel='impressions_arrow']").text (response.analysis.impressions == 0 ? "0%" : Math.round (response.analysis.uimpressions * 1000 / response.analysis.impressions) / 10 + "%");
					$("div[rel='unique_impressions']").text (response.analysis.uimpressions);
					$("div[rel='unique_impressions_graph']").animate
					(
						{
							height : response.analysis.uimpressions == 0 ? 1 : response.analysis.uimpressions * scaleRatio
						},
						500
					)
					$("div[rel='unique_impressions_arrow']").text (response.analysis.uimpressions == 0 ? "0%" : Math.round (response.analysis.shares * 1000 / response.analysis.uimpressions) / 10 + "%");
					$("div[rel='shares']").text (response.analysis.shares);
					$("div[rel='shares_graph']").animate
					(
						{
							height : response.analysis.shares == 0 ? 1 : response.analysis.shares * scaleRatio
						},
						500
					);
					$("div[rel='shares_arrow']").text (response.analysis.shares == 0 ? "0%" : Math.round (response.analysis.uclicks * 1000 / response.analysis.shares) / 10 + "%");
					$("div[rel='unique_clicks']").text (response.analysis.uclicks);
					$("div[rel='unique_clicks_graph']").animate
					(
						{
							height : response.analysis.uclicks == 0 ? 1 : response.analysis.uclicks * scaleRatio
						},
						500
					);


					/* Soc. networks */
					$("span[rel='channel_fb']").text (response.channels["fb"]);
					$("span[rel='channel_fb_perc']").text (ttlLeads == 0 ? "0%" : Math.round (response.channels["fb"] * 100 / ttlLeads) + "%");
					$("span[rel='channel_tw']").text (response.channels["tw"]);
					$("span[rel='channel_tw_perc']").text (ttlLeads == 0 ? "0%" : Math.round (response.channels["tw"] * 100 / ttlLeads) + "%");
					$("span[rel='channel_in']").text (response.channels["in"]);
					$("span[rel='channel_in_perc']").text (ttlLeads == 0 ? "0%" : Math.round (response.channels["in"] * 100 / ttlLeads) + "%");
					$("span[rel='channel_g']").text (response.channels["g"]);
					$("span[rel='channel_g_perc']").text (ttlLeads == 0 ? "0%" : Math.round (response.channels["g"] * 100 / ttlLeads) + "%");
					var otherLeads = parseFloat (response.channels["wg"]) + parseFloat (response.channels["em"]) + parseFloat (response.channels["0"]);
					$("span[rel='channel_other']").text (otherLeads);
					$("span[rel='channel_other_perc']").text (ttlLeads == 0 ? "0%" : Math.round (otherLeads * 100 / ttlLeads) + "%");


					/* Pie chart */
					var channelData =
					[
						{
							label : "fb",
							data : parseFloat (response.channels["fb"]),
							color : "#1221e7"
						},
						{
							label : "tw",
							data : parseFloat (response.channels["tw"]),
							color : "#01a2ba"
						},
						{
							label : "in",
							data : parseFloat (response.channels["in"]),
							color : "#00013c"
						},
						{
							label : "g",
							data : parseFloat (response.channels["g"]),
							color : "#b90606"
						},
						{
							label : "other",
							data : otherLeads,
							color : "#e59804"
						},
					];

					$("#divChannelPie").html ("");
					$.plot ($("#divChannelPie"), channelData,
					{
						series :
						{
							pie :
							{
								show : true,
								label :
								{
									show : false
								}
							}
						},
						legend :
						{
							show : false
						}
					});


					/* Performance */
					var socnetsPerformance = parseFloat (response.channels["fb"]) + parseFloat (response.channels["tw"]) + parseFloat (response.channels["in"]) + parseFloat (response.channels["g"]);
					var emailingPerformance = parseFloat (response.channels["em"]);
					var widgetsPerformance = parseFloat (response.channels["wg"]);
					var directPerformance = parseFloat (response.channels["0"]);
					$("div[rel='socnets']").text (ttlLeads == 0 ? "0.00%" : Math.round (socnetsPerformance * 10000 / ttlLeads) / 100 + "%");
					$("div[rel='socnets_graph']").animate
					(
						{
							height : ttlLeads == 0 ? 1 : socnetsPerformance * 90 / ttlLeads
						},
						500
					)
					$("div[rel='emails']").text (ttlLeads == 0 ? "0.00%" : Math.round (emailingPerformance * 10000 / ttlLeads) / 100 + "%");
					$("div[rel='emails_graph']").animate
					(
						{
							height : ttlLeads == 0 ? 1 : emailingPerformance * 90 / ttlLeads
						},
						500
					)
					$("div[rel='widgets']").text (ttlLeads == 0 ? "0.00%" : Math.round (widgetsPerformance * 10000 / ttlLeads) / 100 + "%");
					$("div[rel='widgets_graph']").animate
					(
						{
							height : ttlLeads == 0 ? 1 : widgetsPerformance * 90 / ttlLeads
						},
						500
					)
					$("div[rel='direct']").text (ttlLeads == 0 ? "0.00%" : Math.round (directPerformance * 10000 / ttlLeads) / 100 + "%");
					$("div[rel='direct_graph']").animate
					(
						{
							height : ttlLeads == 0 ? 1 : directPerformance * 90 / ttlLeads
						},
						500
					)


					/* Supporters */
					var tblSrc = "";
					for (var i = 0;i < response.top10.length;i++)
					{
						tblSrc += "<tr>";
						tblSrc += "<td><strong>" + (i + 1) + ".</strong> <img src=\"" + mainUrl + "media/avatars/" + response.top10[i].avatar + "\" /><strong>" + response.top10[i].referral_name + "</strong></td>";
						tblSrc += "<td>" + response.top10[i].uleads + "</td>";
						tblSrc += "<td>" + Misc.convertCurrency(response.top10[i].contribution)  + "</td>";
						tblSrc += "<td>" + Misc.convertCurrency(Math.round (response.details.tip * 10 / response.top10.length) / 100 )+ "</td>";
						tblSrc += "</tr>";
					}
					$("#tblTops tbody").html (tblSrc);
					$("#tblTops").parent ().parent ().css ({"min-height" : $("#tblTops").height () + 40});


					/* Time filters */
					$("#hrefOverview").attr ("href", "javascript:Analytics.loadPromotion2 (" + response.details.pid + ", 'ov')");
					$("#hrefYesterday").attr ("href", "javascript:Analytics.loadPromotion2 (" + response.details.pid + ", 'y')");
					$("#hrefTWeek").attr ("href", "javascript:Analytics.loadPromotion2 (" + response.details.pid + ", 'tw')");
					$("#hrefLWeek").attr ("href", "javascript:Analytics.loadPromotion2 (" + response.details.pid + ", 'lw')");
					$("#hrefLMonth").attr ("href", "javascript:Analytics.loadPromotion2 (" + response.details.pid + ", 'lm')");
					$("#ulPromoAnalyticsMenu li").removeClass ("selected");
					$("#ulPromoAnalyticsMenu li a[rel='" + refFilter + "']").parent ().addClass ("selected");

					$(".wait").remove ();
				}
			}
		)
	},

	getPromoStats : function (refPromo)
	{
		$.ajax
		(
			{
				type : "POST",
				url : mainUrl + "mods/analytics/receiver.php",
				data : "req=promostats&promotion=" + refPromo,
				dataType : "json",
				success : function (response)
				{
					$("span[rel=impressions]").text (parseInt (response.impressions));
					$("span[rel=shares]").text (parseInt (response.shares));
					$("span[rel=clicks]").text (response.uclicks);
					var virality = Math.round (response.uclicks * 100 / response.shares);
					$("span[rel=virality]").text (response.shares == 0 ? "0%" : Math.round (response.uclicks * 100 / response.shares) + "%");
					$("#divEmails").text (response.emails);
				}
			}
		);
	},

	innerPageStats : function ()
	{
		$.ajax
		(
			{
				type : "POST",
				url : mainUrl + "mods/analytics/receiver.php",
				data : "req=allpromos",
				dataType : "json",
				success : function (response)
				{
					$("#spanTipsToShare").text ( Misc.convertCurrency((response.thismonth == null ? 0 : response.thismonth)));
					$("#spanTipsShared").text (Misc.convertCurrency((response.alltips == null ? 0 : response.alltips)));
					$("#spanYourTips").text (Misc.convertCurrency((response.yourtip == null ? 0 : response.yourtip)));
				}
			}
		);
	}
}

