
var latest = "";
function goto(szc,nos)
{
	if (nos)
	{
		$('html, body').animate({scrollTop:0},300);
	}
	
	$('#new-releases').html("<br/><br/><br/><br/><br/><br/><br/>").activity({segments: 8, width:10, space: 0, length: 6, color: '#fff', speed: 1.5});
	$('#new-releases').load('browse.php?count='+szc);
}

function load() {
	$("#content").html("");
	//	setTimeout('$("#load-txt").html("Removed album art temporarily because of slow response from last.fm")', 3000)
	setTimeout('$("#load-txt").html("Dont worry we are doing our best to serve you some new music")', 10000)
	setTimeout('$("#load-txt").html("If you have problems with timeouts, try limiting your search...")', 18000)

	setTimeout('$("#load-txt").html("Still here")', 28000)
	setTimeout('$("#load-txt").html("Hey-yy, Batter Batter Batter, Swing, Batter")', 38000)
	setTimeout('$("#load-txt").html("Nope, not done yet")', 58000)
	
	var username = $('#username').val();
  	var date = new Date();
	date.setTime(date.getTime() + (30000 * 24 * 60 * 60 * 1000));
	$.cookie("fmname",$("#lastfm").val(), { path: '/', expires: date })
  
	$('#dialog').slideUp();
	$('#loading').slideDown();

 	$.ajax({
		type: "POST",
		url: "feed_me.php",
		data: "usr="+$("#lastfm").val()+"&limit="+$("#limit").val()+"&c="+$("#c").val(),
		success: function(msg){
		
			$("#loading").slideUp();
			szHTML = '<a class="new-search slide" id="new-search">New search</a>'+msg+'<a class="new-search slide" id="new-search">New search</a><div id="some-space" class="some-space"></div>';
			$("#content").append(szHTML) 
			$(".new-search").click(function(){
				$('.dissa').slideUp();
				$("#dialog").slideDown();
			});
		$("#content").slideDown()
		loadImages();
		}
	});

}

function loadImages(){
var a = null;
	$("#content .entry").each(function(i){
	a = $(this).attr("id");
		szAlbum = $(this).find(".name").text()
  	  szArtist = $(this).find(".artist").text()
		fetchImage(a)
	});
}

function fetchImage(adiv)
{
		$.post('get_album_image.php',{ album: szAlbum, artist: szArtist }, function(data) {
		if(data.length>100)
		{
		a = $.parseJSON(data);
//console.log(a.toptags);
var tags = "";
var szHTML = "";
			if(typeof(a.toptags)=="object")
			{
				if(a.toptags)
				{
					tags = a.toptags['0']
				}
			}else
			{
				tags = a.toptags
			}
			$("#"+adiv+" .album-image").html("<img src='"+a.image['0']+"'>");
			
			szHTML += "<div class='info-margin'>Release date: "+(a.releasedate.length<10?" missing":a.releasedate)+" Tracks: "+a.tracks+"</div>"
			//szHTML += "<div class='info-margin'>Toptag: "+tags+"</div>"
			$("#"+adiv).append(szHTML);
		
			//$("#"+adiv).activity(false);
			
		}else
		{
			$("#"+adiv+" .album-image").html("<img src='/img/no-image.gif'>");
		}
			
		})
}

function getMyLatest(){
	$.get('get_mytracks.php', function(data) {
			if(data=="")
				$("#display-latest").html("..nothing");
			
  			if(data!=latest)
  			{
  				$("#display-latest").html(data)
  				latest = data;
  			}
	});
	setTimeout('getMyLatest()',20000)
	
}

$(document).ready(function() {
	//getMyLatest()
	$("ul.menu > li > a").click(function(){
		$("ul.menu > li > a").removeClass("current")
		$(this).addClass("current");
	})
	
	$(".slide").click(function(){
		$('.dissa').slideUp();
		$("#"+$(this).attr('rel')).slideDown();
	})
	$(".menu").click(function(){

		$('.menu').removeClass('current');
		$(this).addClass('current');
	})

	$("#about-link").click(function(){
		$("#content").slideUp();
		$("#dialog").slideUp();
		$("#about").slideDown();
	});
	
	$("#browse-link").click(
		function(){
			goto("0", true);
		}
	);
	
	$("#new-search").click(function(){
		$("#content").slideUp();
		$("#about").slideUp();
		$("#dialog").slideDown();
	});

	$('#submit').click(function(){load()});
//	$('#submit5').click(function(){load2()});
	$('#lastfm').focus().keypress(function(e) {
    	var c = e.which ? e.which : e.keyCode;
    	if (c == 13) {
      		load();
    	}
  	})

 	$('#limit-div').qtip(
	{
		content: 'Limit the search to nth number of artists from your artist library in last.fm.',
		style: { 
			width: 200,
			padding: 5,
			background: '#A2D959',
			color: 'black',
			textAlign: 'center',
			border: {
				width: 7,
				radius: 5,
				color: '#A2D959'
   			},
			tip: 'topLeft',
			name: 'dark' 
		}
	});
	$('#country-div').qtip(
	{
		content: 'Only return new albums that are available in selected country and the ones that are available to all users.',
		style: { 
			width: 200,
			padding: 5,
      		background: '#A2D959',
      		color: 'black',
      		textAlign: 'center',
			border: {
				width: 7,
				radius: 5,
				color: '#A2D959'
			},
			tip: 'topLeft',
			name: 'dark' 
		}
	});
	$('#follow_twitter').qtip(
	{
		content: 'Follow me on twitter',
		position: {
			corner: {
        	    target: 'topLeft',
            	tooltip: 'bottomRight'
         	}
      	},
		style: { 
			width: 200,
			padding: 5,
			background: '#A2D959',
			color: 'black',
			textAlign: 'center',
			border: {
				width: 7,
				radius: 5,
				color: '#A2D959'
   			},
   			tip : 'bottomRight',
			name: 'dark',
		}
			
	});
	$('#contact-link-letter').qtip(
	{
		content: 'Contact me',
		 position: {
         corner: {
            target: 'topLeft',
            tooltip: 'bottomRight'
         }
      },
		style: { 
			width: 200,
			padding: 5,
			background: '#A2D959',
			color: 'black',
			textAlign: 'center',
			border: {
				width: 7,
				radius: 5,
				color: '#A2D959'
   			},
   			tip : 'bottomRight',
			name: 'dark',

		}
			
	});
	
	if($.cookie("fmname"))
	{
		$("#lastfm").val($.cookie("fmname"))
	}
		$("#send").click(function(){
			var str = $("#contact-form").serialize();
			$.post("kon.php",str,function(data){
				$("#wrap-form").html("Thank you<br />Youre feedback have been sent.");
			})
		});

});

