
$(document).ready(function(){
	window.toptenbyid={};
	for(var i=0;i<window.topten.length;i++){
		var t=window.topten[i];
		window.toptenbyid[t.id]=i+1;
	}
	
	$('#topten-panel #topten-key .th_score').mouseover(function(){ window.karmatimeout=0;$('#topten-panel .th_noise_tooltip').hide();$('#topten-panel .th_karma_tooltip').show();});
	$('#topten-panel #topten-key .th_score').mouseout(function(){ window.karmatimeout=setTimeout("$('#topten-panel .th_karma_tooltip').hide()",3000);});
	$('#topten-panel #topten-key .th_noise').mouseover(function(){ window.noisetimeout=0;$('#topten-panel .th_karma_tooltip').hide(); $('#topten-panel .th_noise_tooltip').show();});
	$('#topten-panel #topten-key .th_noise').mouseout(function(){ window.noisetimeout=setTimeout("$('#topten-panel .th_noise_tooltip').hide()",3000);});
					
	if(window.loggedin){
		$('#latest-twinges-panel #latest-twinges .favourite').show();
	}
	
	$('#tweet_form').ajaxForm(
		{ beforeSubmit:checkTweet,
		  success: function(){
				$('#tweet_form').before('<p id="tweet_confirm">Tweet sent! <a href="#" onclick="$(\'#tweet_confirm\').remove(); setTweetDefaults(); $(\'#tweet_form\').show();">Post another review</a></p>');
				$('#tweet_form').hide();		
		  }
		});
	
	
	$('#inp-tweet-act').autocomplete('tweetacts.php',{ onItemSelect:selectActForTweet, formatItem:formatAct, loadingClass:'throbbing' });
	$('#inp-tweet-review').keyup(calculateTweetSpace);	
	$('#inp-tweet-act').keyup(calculateTweetSpace);	
	
	
	
	setInterval('updateTopTen();',10000);	
	setTimeout('getLatestTwinges();',5000);	
});


function selectActForTweet(li){
	var title=$(li).html();		
	calculateTweetSpace();
}

function checkTweet(){	
	if(!$('#inp-tweet-act').attr('value') || $('#inp-tweet-act').attr('value')==defaultActName || !$('#inp-tweet-review').attr('value') || $('#inp-tweet-review').attr('value')==defaultReview) return false;
	return calculateTweetSpace()>=0;
}

function calculateTweetSpace(){

	var actname=$('#inp-tweet-act').attr('value');
	if(actname==defaultActName) actname="";

	var review=$('#inp-tweet-review').attr('value');
	if(review==defaultReview) review="";
	

	var tweet="#edtwinge "+actname+" "+review;
	var total=140-tweet.length;
	$('#tweetCount').html(total);
	if(total<0){
		$('#tweetCount').addClass('toomuchtweet');
	}else{
		$('#tweetCount').removeClass('toomuchtweet');
	}
	return total;
}


function setTweetDefaults(){
	 $('#inp-tweet-act').attr('value',defaultActName);
	 $('#inp-tweet-review').attr('value',defaultReview);
}
function getLatestTwinges(){
	
	$.get('latesttweets.php?date='+new Date(),{},function(data){
		var count=data.length;
		var seen=0;				
		for(var i=0;i<data.length;i++){
			if(data[i].id<=window.lastTweet){
				seen=i+1;
			}
		}		
		var toshow=count-seen;
				
		if(toshow>0){					
			var delay=60000/(toshow+1);
			window.newtweets=data;
			window.tweetposition=seen;					
				
			window.tweetInterval=setInterval('addNewTweet();',delay);		
		}else{
			setTimeout('getLatestTwinges()',10000);
		}
	},'json');
	

}


function addNewTweet(){	
	if(window.paused) return;
	if(window.tweetposition<window.newtweets.length){
		pushTweet(window.newtweets[window.tweetposition]);		
		window.tweetposition++;		
	}else{
		window.newtweets=[];
		window.tweetposition=0;
		clearInterval(window.tweetInterval);
		getLatestTwinges();
	}
}

function pushTweet(tweet){
	window.lastTweet=tweet.id;	
	$('#latest-twinges').children(":gt(49)").remove();
	var d=document.createElement('li');
	var html='<img class="twavatar" width="48" height="48" src="'+tweet.avatar+'" alt="" /><div class="content"><span class="name">'+tweet.sender+'</span><p><a class="performer_link" href="'+tweet.performerLink+'">'+tweet.message+'</a></p><div class="when">'+tweet.when+' via '+tweet.source+'</div></div>';	
	html+='<a href="'+tweet.retweet+'" class="retweet" target="_blank"><img width="18" height="14" alt="re-tweet" src="img/home/btn_retweet.gif" /></a>';
	if(window.loggedin) html+='<a href="'+tweet.favourite+'" onclick="addFavourite('+tweet.idTweet+'); return false" target="_blank" id="favourite_'+tweet.idTweet+'" class="favourite"><img width="14" height="12" alt="favourite" src="img/home/btn_favourite.gif" /></a>';
	$(d).html(html);		
	$(d).slideUp();			
	$('#latest-twinges-panel #latest-twinges').prepend(d);	
	$(d).slideDown('slow');
	
}

function limitChars($str,$num){

	if($str.length>$num){
		return $str.substr(0,$num-3)+"...";
	}else{
		return $str;
	}
}

function updateTopTen(){
	$.get('topten.php?idGenre='+window.genre,{},function(data){
		var newbyid={};
		for(var i=0;i<10;i++){
			if(data[i]) newbyid[data[i].id]=i+1;
		}
		var insertPos=0;
		var lastInsert=0;
		for(var i=0;i<10;i++){
			var newperf=data[i];
			var oldperf=window.topten[i];			
			var newpos=newbyid[newperf.id];
			var oldpos=window.toptenbyid[newperf.id];
			
			
			
			var html='<img class="number" src="img/home/lbl_num'+(i+1)+'.gif" alt="'+(i+1)+'" />'+
								'<a class="name" href="performer-'+newperf.url+'">'
								+limitChars(newperf.name,50)+'<br/><span class="show">'+limitChars(newperf.showName,50)+'</span></a> <span class="noise">'
								+newperf.noise+'</span> <span class="karma">'
								+newperf.karma+'</span><div class="clear"></div>';
											
			if(newperf.id!=oldperf.id){		
								
				
				var d=0;
				if(!oldpos){	
					// new entry									
					d=document.createElement('li');
					d.id='performer-'+newperf.id;					
					$(d).html(html);		
					$(d).addClass("clear");
					$(d).hide();														
					$('#performer-'+oldperf.id).before(d);					
					if(!newbyid[oldperf.id]){$('#performer-'+oldperf.id).remove();}						
					$(d).show('slow');
				
				}else if(oldpos>newpos){	
					// moving up the charts	
					//alert('moving up: '+newperf.name);			
				 	d=$('#performer-'+newperf.id).get(0);
		
					
					if(lastInsert){
						d.mtarget=$(lastInsert);
					}
					
					d.oldtarget=$('#performer-'+oldperf.id);
				
					d.removeOld=!newbyid[oldperf.id];	
					if(d.removeOld){
						oldperf.taint=true;
					}								
					d.newhtml=html;
					$(d).hide('slow',function(){	
						if(this.mtarget){					
							this.mtarget.after($(this));
						}else{
							this.oldtarget.before($(this));
						}
						$(this).show('slow');
						$(this).html(this.newhtml);							
						if(this.removeOld){ this.oldtarget.remove();}
					});
				}else{
					// moving down the charts
					//alert("moving down the charts - do nothing "+newperf.name);
					$('#performer-'+newperf.id).html(html);										
				}	
				lastInsert=d;						
			}else{
				$('#performer-'+newperf.id).html(html);		
			}							
				
		}
		
		for(var i=0;i<10;i++){
			var oldperf=window.topten[i];	
			if(!newbyid[oldperf.id] && !oldperf.taint){
				$('#performer-'+oldperf.id).remove();
			}
		}
		
		window.topten=data;
		window.toptenbyid=newbyid;
	},'json');	

}

function addFavourite(id){
	if($('#favourite_'+id).get(0).sent) return;

	$.get('favourite.php?id='+id);	
	$('#favourite_'+id).get(0).sent=true;;
	$('#favourite_'+id).get(0).href='#';
	
	$('#favourite_'+id).html('<img width="14" height="12" alt="favourite" src="img/home/btn_favourite_on.gif" />');
	$('#favourite_'+id).hide('slow',function(){ $(this).remove();});
	return false;
}

