poll ="";
timeOut="";
credits="";
myListener="";
//audio player

             


$j(document).ready( function()
{
	
	
	
	// asSeen button
	$j('#seenOn').click(function(){
		$j('#asSeenPopup').fadeIn('slow');
	});
	$j('#closeAsSeen').click(function(){
		$j('#asSeenPopup').fadeOut('slow');
	});
	
	
	
	
	
	
	// mitico callback
	Blacktrend.form.registerCallback(document.getElementById('form__newsletter'), function(response){
	//alert("Risposta:"+response.message);
		$j('#form_newsletter_data').slideUp('slow');
		if (response.status == 'success') {
			$j('#form_newsletter_ok').slideDown('slow');
		} else {
			$j('#form_newsletter_err').slideDown('slow');			
		}
	});


	Blacktrend.form.registerCallback(document.getElementById('form__share_friends'), function(response){
	    $j('#share_friends_data').slideUp('slow');
		//$j('#share_friends_data').hide();
		//$j('#share_friends_ok').show();
	    $j('#share_friends_ok').slideDown('slow');
	});	
	
	
	Blacktrend.form.registerCallback(document.getElementById('form__hire_us'), function(response){
	//alert("Risposta:"+response.message);
	
		
		if(response.status == "success")
		{
			var heightSign=$j('#signup').css('height');
			var heightForm=$j('#form__hire_us').css('height');
			$j('#formContainerHireUs').hide();
			$j('#form__hire_us').animate({height: '20px'},100,'linear');
			
			$j('#signup').animate({height: '90px'},200,'linear');
			$j('#signup h1').html('Data successfully sent. We will get in touch with you');
			setTimeout(function()
			{
				$j('#signup').slideUp('slow');
				$j.address.path($j.address.path().replace(/\/hire_us$/, ''));
			},6000);	
			
			setTimeout(function()
			{
				$j('#formContainerHireUs').show();
				$j('#signup').css('height',heightSign);
				$j('#form__hire_us').css('height',heightForm);
				$j('#signup h1').html('Hire Us');
				
			},7000);	
		}
		
	});
		
	// shit!
	Blacktrend.form.registerCallback(document.getElementById('form__add_comment'), function(response){
		//alert("Risposta:"+response.status);
		$j('#commentaBox').hide();
		/*
		$j('#comments_container').empty();
		var comments = '';
		for (i = 0; i < response.last_comments.length ; i++) {		
			comments += '<div class="comment"><h2 class="title">COMMENT BY ' + response.last_comments[i].nickname + '</h2><div class="date">' + response.last_comments[i].timestamp + '</div><div class="content">' + response.last_comments[i].comment + '</div></div>';
		}
		$j('#comments_container').html(comments);
		*/
	});	
		            
		
		//twitter
		var last = 0;
		

	   getTweets =	function (id){
				$j.getJSON("http://www.blacktrend.com/handlers/twitter_handler.php?start="+id,
				function(data){
					//	alert(data)
						
						$j.each(data, function(count,item){
							if(item.Id != last)
							{
								
								addNew(item);
								last = item.Id;
								myJavascriptObject.kookoo();
								setTimeout(function(){
								Blacktrend.tooltip.tooltips.koo_koo.show();
									},4000);
								setTimeout(function(){
									Blacktrend.tooltip.tooltips.koo_koo.hide();
								},10000);
							}
						});
				});
		}
		
	   addNew =	function (item){
				if($j('#tweets div.tweet').length>0){ //If we have more than nine tweets
						$j('#tweets div.tweet:first').toggle(300);//remove it form the screen
						$j('#tweets div.tweet:first').removeClass('tweet');//and it's class
						$j("#tweets div:hidden").remove(); //sweeps the already hidden elements
				}
				$j('#tweets').append(renderTweet(item, 'hidden'));
		}

		renderTweet = function (item){
				//importanceColor=getImportanceColor(item.followers_count);
				importanceColor="black";
				return '<div class="tweet" id="'+item.Id+'">'+
				'<strong><a href="http://twitter.com/'+item.Utente+'" target="_blank" >'+ // style="color:'+importanceColor+'"
				item.Utente+'</a></strong><span class="text">'+
				item.Testo
				+'</span><br /><span class="created_at">'+
				//'<a href="http://twitter.com/'+item.utente+'/status/'+item.Id+'">'+
				item.Data+
				//'</a>'+
				'</span></span></div>';
		}

	   getImportanceColor =	function (number){
				rgb = 255-Math.floor(16*(Math.log(number+1)+1)); //should return about 0 for 0 followers and 255 for 4million (Ashton Kutchner? Obama?)
				return 'rgb('+rgb+',0,0)';
		}

		poll =	function (){
				timeOut = setTimeout('poll()', 30000);//It calls itself every 200ms
				getTweets(last);
		}

		//credits ... tutto in un unico bel file "personalizzato"
		hideCredits= function()
		{
			
			$j("#creditsFilm").fadeOut('slow');
		}
		credits= function()
		{
			$j(".autoscroll").stop();
			$j(".autoscroll").css({top: '0'});
			$j("#creditsFilm").fadeIn('slow');
			
			$j(".autoscroll").animate({top: '-=3000'},40000,'linear');
			setTimeout(function	(){
					hideCredits();
			},32000);
			
		}
		
		
		//un po' di gestione di eventi alla JQuery... senza quegli onClick che fanno molto vecchia scuola,MCP
		$j('#showCreditsFilm').click(function()
		{
			credits();
		});
		
		$j('#hideCreditsFilm').click(function()
		{
			hideCredits();
		});
		
		
			
	
		
});

