$(document).ready(function()
{
	Members.membersToTable( "/info/rank.html?export=json&db=" + cu_db + "&rnd_m=" + rnd_m );
	$("body").click
	(
		function(e)
		{
			if( e.target.className !== "help_content_class" && e.target.className !== "help_clicker_class" )
			{
				$("#help_content").css('display', 'none');
			}
		}
	);
	$("#help_clicker").click
	(
		function()
		{
			$("#help_content").css('display', 'block');
		}
	);
});

Members =
{
	membersToTable: function( url )
	{
		var table = $('<table>');
		table.attr( "border", "0" );
		table.attr( "width", "100%" );
		table.attr( "cellspacing", "0" );
		table.attr( "cellpadding", "0" );
		table.addClass( "report_table3" );
		var headers = [
			'<tr>',
			'<th><div class="th_help_container" style="position: absolute;"><a href="/glossary.html#9" class="th_help" style="display: none; float: right;"><img src="' + media_url + '/images/th_sign.gif" border="0px" height="12px" width="12px" /></a><div class="th_help_content" style="display: none;">' + rtt_rank + '</div></div><div class="th_div">' + r_rank + '</div></th>',
			'<th><div class="th_help_container" style="position: absolute;"><a href="/glossary.html#62" class="th_help" style="display: none; float: right;"><img src="' + media_url + '/images/th_sign.gif" border="0px" height="12px" width="12px" /></a><div class="th_help_content" style="display: none;">' + rtt_domain + '</div></div><div class="th_div">' + rt_domain + '</div></th>',
			'<th><div class="th_help_container" style="position: absolute;"><a href="/glossary.html#2" class="th_help" style="display: none; float: right;"><img src="' + media_url + '/images/th_sign.gif" border="0px" height="12px" width="12px" /></a><div class="th_help_content" style="display: none;">' + rtt_ok + '</div></div><div class="th_div">' + rt_or + '</div></th>',
			'<th><div class="th_help_container" style="position: absolute;"><a href="/glossary.html#10" class="th_help" style="display: none; float: right;"><img src="' + media_url + '/images/th_sign.gif" border="0px" height="12px" width="12px" /></a><div class="th_help_content" style="display: none;">' + rtt_gse + '</div></div><div class="th_div">' + rt_gse + '</div></th>',
			'<th><div class="th_help_container thh_right" style="position: absolute;"><a href="/glossary.html#11" class="th_help" style="display: none; float: right;"><img src="' + media_url + '/images/th_sign.gif" border="0px" height="12px" width="12px" /></a><div class="th_help_content" style="display: none;">' + rtt_setp + '</div></div><div class="th_div">' + rt_c + '</div></th>',
			'</tr>'
		].join('');
		table.append( headers );

		$.ajax(
		{
			beforeSend: function()
			{
				time_start = new Date().getTime();
				$.getJSON( '/time.txt?status=start&query_id=' + rnd_m + '&report_type=rank&query=' + query, function(){});
			},
			url: url,
			timeout: var_timeout,
			success: function(response)
			{
				var data = jsonParse(response);
				if ( typeof(data.data) != "undefined" )
				{
					Members.logTimeQuery('rank','success_gooddata');
					var rank_data = data.data.rank_report.domains_info;
					var total_results = new String(data.data.rank_report.total_n_domains);
					if (cu_db == 'us') $('#container_results').html( r_top_100 + '&nbsp;17&nbsp;281&nbsp;007' );
					else if (cu_db == 'de') $('#container_results').html( r_top_100 + '&nbsp;5&nbsp;867&nbsp;052' );
					else if (cu_db == 'es') $('#container_results').html( r_top_100 + '&nbsp;4&nbsp;159&nbsp;758' );
					else if (cu_db == 'uk') $('#container_results').html( r_top_100 + '&nbsp;6&nbsp;493&nbsp;608' );
					else if (cu_db == 'fr') $('#container_results').html( r_top_100 + '&nbsp;4&nbsp;337&nbsp;255' );
					else $('#container_results').html(  r_top_100 + '&nbsp;' + breakToThree(total_results) );
					$.each( rank_data, function(i, member)
					{
						Members.memberToRow( member, table, i );
					});
					$('#container').html( table );
					$('#pagination_over').css( 'display', 'none' );
					
					$(".report_table th, .report_table3 th").mouseenter(function()
					{
						$(".th_div",this).css('background-color','#d9d9d9');
						$(".th_help",this).css('display','block');
					});
					
					$(".report_table th, .report_table3 th").mouseleave(function()
					{
						$(".th_div",this).css('background-color','#e0e0e0');
						$(".th_help",this).css('display','none');
						$(".th_help_content",this).css('display','none');
					});
					
					$(".th_help_container").mouseenter(function()
					{
						if ( $(this).hasClass("thh_right") )
						{
							$(".th_help_content", this).css('left', '-160px');
						}
						$(".th_help_content", this).css('display','block');
					});
					
					$(".th_help_container").mouseleave(function()
					{
						$(".th_help_content", this).css('display','none');
					});
					if ( user_check == 1 )
					{
						exl_export_dis = new Image();
						exl_export_dis.src = media_url + "/images/exl_export_dis.gif";
						csv_export_dis = new Image();
						csv_export_dis.src = media_url + "/images/csv_export_dis.gif";
						csv_semicolon_export_dis = new Image();
						csv_semicolon_export_dis.src = media_url + "/images/csv_semicolon_export_dis.gif";
						$(".exp_link").click
						(
							function()
							{
								var new_src = $("img", this).attr('src').slice(0,-4) + '_dis.gif';
								$(this).replaceWith('<div style="float: left; margin-left: 5px;"><img src="' + new_src + '" border="0" alt="' + $("img", this).attr('alt') + '" width="22px" height="22px" /></div>');
							}
						);
					}
				}
				else
				{
					Members.logTimeQuery('rank','success_baddata');
					$('#container').html('<div style="text-align: center; padding-top: 10px; border-right: 1px solid #bbb; background-color: #f0f0f0; font-family: Tahoma; font-size: 11px; font-weight: bold;">' + r_j_error + '</div>');
				}
				$('#footer').css('bottom','0px');
			},
			error: function (errorData, errorType)
			{
				if ( typeof ( errorType ) != "undefined" && errorType == "timeout" )
				{
					Members.logTimeQuery('rank','timeout');
					$('#container').html('<div style="text-align: center; padding-top: 10px; border-right: 1px solid #bbb; background-color: #f0f0f0; font-family: Tahoma; font-size: 11px; font-weight: bold;">' + r_j_error_t + '</div>');
				}
				else
				{
					if( errorData.status == 0 && errorData.readyState == 4 && typeof(errorData.responseBody) == "undefined" )
					{
						$('#container').html('<div style="text-align: center; padding-top: 10px; border-right: 1px solid #bbb; background-color: #f0f0f0; font-family: Tahoma; font-size: 11px; font-weight: bold;">' + r_j_error_c + '</div>');
						Members.logTimeQuery('rank','user_cancel');
					}
					else
					{
						$('#container').html('<div style="text-align: center; padding-top: 10px; border-right: 1px solid #bbb; background-color: #f0f0f0; font-family: Tahoma; font-size: 11px; font-weight: bold;">' + r_j_error + '</div>');
						Members.logTimeQuery('rank','error');
					}
				}
			}
		});
	},
	
	logTimeQuery: function(reportType,returnStatus)
	{
		var time_end = (new Date().getTime()) - time_start;
		$.getJSON( '/time.txt?status=end&query_id=' + rnd_m + '&report_type=' + reportType + '&return_status=' + returnStatus + '&time=' + time_end + '&query=' + query, function(){});
	},
	
	memberToRow: function( member, table, i )
	{
		var tr = $('<tr>');
		if ( i % 2 != 0 ) tr.attr( 'id', 'report_table_hl' );
		
		var td = $('<td>');		// Rank
		td.attr("align", "right");
		td.text(member[0]);
		tr.append(td);
		
		var td = $('<td>');		// Domain
		td.html('<a href="/info/' + member[1] + '?db=' + cu_db + '" title="' + member[1] + '">' + member[1] + '</a>');
		tr.append(td);
		
		var td = $('<td>');		// Organic results
		td.attr("align", "right");
		td.html(breakToThree(member[2]));
		tr.append(td);
		
		var td = $('<td>');		// Google SE Traffic
		td.attr("align", "right");
		td.html(breakToThree(member[3]));
		tr.append(td);
		
		var td = $('<td>');		// Costs
		td.attr("align", "right");
		td.html(breakToThree(member[4]));
		tr.append(td);
		
		table.append( tr );
	}
};
