var tweetServer = "servers/tweets.php5";
var lastfmServer = "servers/lastfm.php5";
var mailServer = "servers/mail.php";
var blogServer = "servers/blog.php5";
$(function() {
	$('#tweets div').load(tweetServer, function(){
		$('#tweets .loading_bar').fadeOut('normal', function(){
							$('#tweets div').slideDown('normal');
						});
		
	});
	$('#lastfm div').load(lastfmServer, {type:'tracks'}, function(){
			$('#lastfm .loading_bar').fadeOut('normal', function(){
					$('#lastfm div').slideDown('normal');
					$('#lastfm div').before('<p class="lastfm_selector">recent tracks | <a href="#">top weekly artists</a></p>');
					lastFmToggleTracks = true;
					addHandlers();
			});
	});
	
	function addHandlers() {
		$('#lastfm p.lastfm_selector a').click(function(e){
							e.preventDefault();
							$('#lastfm div').slideUp('normal', function() {
											  	 $('#lastfm div').html('');
											   	 $('.lastfm_selector').remove();
												 $('#lastfm .loading_bar').fadeIn();
											   });							
							
							if (lastFmToggleTracks == true) {
								$('#lastfm div').load(lastfmServer, {type:'artist'}, function(){
									$('#lastfm .loading_bar').fadeOut('normal', function(){
												$('#lastfm div').slideDown('normal');
												$('#lastfm div').before('<p class="lastfm_selector"><a href="#">recent tracks</a> | top weekly artists</p>');
												lastFmToggleTracks = true;
												addHandlers();
										});
									});
							} else {
								$('#lastfm div').load(lastfmServer, {type:'tracks'}, function(){
									$('#lastfm .loading_bar').fadeOut('normal', function(){
												$('#lastfm div').slideDown('normal');
												$('#lastfm div').before('<p class="lastfm_selector">recent tracks | <a href="#">top weekly artists</a></p>');
												lastFmToggleTracks = true;
												addHandlers();
										});
									});
							}
			});
	}
	
	$('#blog div').load(blogServer, function() {
					$('#blog p.loading_bar').fadeOut('normal', function() {
										$('#blog div').slideDown('#blog ul');
								});
			 });
	
	
	$('input, textarea').removeAttr('disabled');
	
	$('#contact_content').slideDown('normal');
	$('#contact_content .loading_bar').hide();
	
	$('#contact_content .btn').click(function(e) {
				e.preventDefault();
				$('#contact_content textarea').removeClass("form_red");
				$('#contact_content input.email').removeClass("form_red");
				
				var regExp  = /^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*(\.[a-z]{2,3})$/;
				var emailValue = $('#contact_content input.email').attr('value');
				var messageValue = $('#contact_content textarea').attr('value');
				if (regExp.test(emailValue)) {
					if (messageValue != ""){
						$('#contact_content input, #contact_content textarea').attr('disabled', 'disabled');
						$('#contact_content form').slideUp();
						$('#contact_content .loading_bar').fadeIn();
						$('#contact_content .mail_result').load(mailServer, {uemail:$('#contact_content input').attr('value'), umessage:$('#contact_content textarea').attr('value')}, function(){
										$('#contact_content .loading_bar').fadeOut('normal', function(){
														$('#contact_content .mail_result').slideDown();
													});
									});
					} else {
						$('#contact_content textarea').addClass("form_red");
					}
				} else {
					$('#contact_content input.email').addClass("form_red");
				}
				
			});
	
	
} )
