$(document).ready(function(){
	
	var shareTwitterLink;
	
	$('#fb').html('<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fijustfoundthem.com&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;font=lucida+grande&amp;colorscheme=light&amp;height=80" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:450px; height:80px;" allowTransparency="true"></iframe>').show();
	
	$('#h a').mouseover(function() {
		$('#h img').attr('src', '/images/logo_hover_min.png');
	});
	$('#h a').mousedown(function() {
		$('#h img').attr('src', '/images/logo_focus_min.png');
		$('#h a').css('outline', '0');
	});
	$('#h a').mouseout(function() {
		$('#h img').attr('src', '/images/logo_min.png');
	});
	
	var query = document.location.search;
	if (query.length > 6)
	{
		document.location.hash = '#!' + query.substr(6, query.length-6);
		document.location.search = '';
	}
	
	var hash = document.location.hash;
	if (hash.length > 2)
	{
		$('#u').val(hash.substr(2, hash.length-2));
		submitForm();
	}
	
	$('form').submit(function(){
		submitForm();
		return false;
	});
	
	$('#powered-by').append('<li><!-- Host Europe GmbH - Partnerprogramm - Beginn -->' +
	'<a href="http://affiliate.hosteurope.de/click.php/xKl4Zmvdo1OpK4IfxubYwvLOv0kga_g1EqnZ9RlpJMM," title="hosted by Host Europe" target="_self">hosted by Host Europe</a><img src="http://affiliate.hosteurope.de/view.php/xKl4Zmvdo1OpK4IfxubYwvLOv0kga_g1EqnZ9RlpJMM," width="0" height="0" alt="" border="0" />' +
	'<!-- Host Europe GmbH - Partnerprogramm - Ende --></li>');
	
});

function submitForm() {
	// clean current
	$('#r').html('');
	
	$('#r').append($('<p>Be patient. This can take some minutes.</p>'))
	
	// Display load icon
	$('#r').show();
	$('#r').addClass('load');
	
	document.location.hash = '#!' + $('#u').val();
	
	$.ajax({
		url: 'ajax.php?user=' + $('#u').val(),
		dataType: 'json',
		success: function(data) {
			// Tracking code for Google Analytics
			var _gaq = _gaq || [];
			_gaq.push(function() {
			  var tracker = _gat._getTracker('UA-17285889-1');
			  tracker._trackPageview('/?user=' + $('#u').val());
			});
			
			var shareMessage = 'Last week I discovered ';
			
			$('#r').removeClass('load');
			$('#r').html('');
			$('#r').append($('<h2>Artists <em>' + $('#u').val() + '</em> recently discovered</h2>'));
			var a = $('<ol id="a" class="clearfix"></ol>');
			for (var i = 0; i < data.artists.length; ++i)
			{
				var row = $('<li></li>');
				row.append($('<span><strong>' + data.artists[i].artist + '</strong> '
						+ data.artists[i].playcount + '</span>'));
				a.append(row);
				if (i < 3)
				{
					shareMessage += data.artists[i].artist;
					if ((i == 1 && data.artists.length >= 3) || (i+2) == data.artists.length) { shareMessage += ' and '; }
					if (i == 0 && data.artists.length >= 3) { shareMessage += ', '; }
				}
			}
			
			$('#from_date').text(data.week.from);
			$('#to_date').text(data.week.to);
			$('#r').append(a);
			$('#a').slideDown();
			
			var fbPermalink = document.location.protocol + '//' + document.location.host + document.location.pathname
					+ '?user=' + $('#u').val();
			var twPermalink = document.location.href;
			var fbMessage = shareMessage;
			
			$('#share').show();
			if (shareMessage.length > (140-twPermalink.length))
			{
				shareMessage = shareMessage.substr(0, (140-twPermalink.length)) + '...';
			}
			shareMessage += ' ' + twPermalink;
			shareTwitterLink = 'http://twitter.com/home?status=' + escape(shareMessage);
			$('#share-facebook').attr('share_url', fbPermalink);
			
			$('#share-twitter').click(function() {
				location.href = shareTwitterLink;
				return false;
			});
		}
	});
}

