$.fn.extend({'ClickStatistic': ClickStatistic});

function ClickStatistic(groupId, description)
{
	var CLICK_STATISTICS_URL = '/stat/click/1,';
	
	this.data('description', description);
	this.each(function(){
		if($(this).attr('tagName') == 'A')
		{
				var description = $(this).data('description');
				if( !description )
				{
						description =  $.trim($(this).text());
						description = (!description) ? $.trim($(this).attr('title')) : description;
						
						if( !(/\S/.test(description)) )
						{
								var oImg = $(this).children("img");
								if(oImg.size() > 0)
								{
									oImg = $(oImg).get();
									if($(oImg).attr('title'))
									{
										description = $.trim($(oImg).attr('title'));
									}
									else if($(oImg).attr('alt'))
									{
										description = $.trim($(oImg).attr('alt'));
									}
								}
						}
				}
				
				if( groupId && description && $(this).attr('href') )
				{
						data = {
							group_id:    groupId,
							description: 				 description,
							url: $(this).attr('href')
						};

						$(this).data('group_id', groupId);
						$(this).data('description', description);
						$(this).data('url', $(this).attr('href'));
						$(this).data('stat_url', CLICK_STATISTICS_URL + (Base64.encode(JSON.stringify(data))).replace(/\//g, '-').replace(/\+/g,'*'));
						
						$(this).click(function(){
									$(this).attr('href',  $(this).data('stat_url'));
						});
						
						$(this).mouseover(function(){
									$(this).attr('href',  $(this).data('url'));
						});
				}
		}
	});
}

