$(function() {
	var getRank = function (url) {
		var url = $('#url').val();
		var myRand = parseInt(Math.random()*99999999); // cache buster
		$('.loading').slideToggle("slow");
		$("#results").empty();
		$.ajax({
		   	type: "POST",
		   	dataType: "xml",
		   	url: "getResults.php",
		   	data: "url="+url+"&opID="+myRand,
		   	error: function() {alert("An error occured!")},
		   	success: function(xml, textStatus){
		   		$('.loading').slideToggle("fast");
		   		if(textStatus != 'success') {
		   			alert("Error!");
		   		}
		   		else {
		   			var options = new Array();
					options[1] = 'Google Page Rank';
					options[2] = 'Alexa Rank';
					options[3] = 'Listed in DMOZ';
					options[4] = 'Backlinks from Google';
					options[5] = 'Backlinks from Yahoo';
					options[6] = 'Results from AltaVista';
					options[7] = 'Results from All the Web';
					options[8] = 'Website Thumbnail';
					var x = 0;
					$(xml).find('results > *').each(function() {						
				   		x++;
				   		var value = $(this).text();
		   				var style = (x % 2) ? '' : ' style="background-color: #EEEEEE"';
		   				if(options[x] == 'Listed in DMOZ') {
		   					if($(this).text() == 1)
		   						value = 'Yes';
		   					else
		   						value = 'No';
		   				}
		   				if(options[x] == 'Website Thumbnail') {
		   					img = $(this).text();
		   					value = '<a href="'+ url +'" rel="nofollow" target="_blank"><img src="' + img + '" alt="Thumbnail Not Available" /></a>';
		   				}
		   				$("#results").append('<div' + style + '><span>' + value + '</span>' + options[x] + '<b class="clear"></b></div>').slideDown("slow");
		   			});
		   		}
		   		$.facebox($("#resultsContainer"));
   			}
 		});
	}
	$("#get_rank").click(getRank);
});
