$(document).ready(function() {

	var launchDate = new Date("May 31, 2011 20:00:00");
	var twitterUsername = 'spanishvillatv';
	var procentageDone = 80;
	
	var secondsRemaining = Math.floor(launchDate.getTime() / 1000) - Math.floor(new Date().getTime() / 1000);
	setCountdownDimensions(secondsRemaining);
	countdown(secondsRemaining);
	updateProgress(procentageDone);
	
	var contentHeight = $('.mainContent').height();
	
	$('#progress').show();
	
	var newHash;
	
	$('#followUsButton').click(function() {
	
		location.href = 'http://twitter.com/' + twitterUsername;
		
		return false;
	
	});
	
	$('.contentMenu').delegate('a', 'click', function() {
		window.location.hash = $(this).attr("href");
		return false;
	});
	
	$(window).bind('hashchange', function(){

		if(!$('.mainContent').is(':animated')) {

			newHash = (window.location.hash == '') ? '#progress' : window.location.hash;
			
			if(newHash != $('.contentMenu a.active').attr('href')) {
			
				$('.content > div').hide();
				$(newHash).show();
					
				$('.contentMenu a').removeClass('active');
				$('.contentMenu a[href=' + newHash + ']').addClass('active');
			
			}
		
		}

	});
	
	goToCurrentPage();
	
	$('.tweet').tweet({
	  username: twitterUsername,
	  count: 1,
	  loading_text: 'Loading tweet...'
	});
	
	$('#contactForm').validate({
		rules: {
			name: {
				required: true,
				minlength: 2
			},
			email: {
				required: true,
				email: true
			},
			message: {
				required: true,
				minlength: 2,
				maxlength: 10000
			}
		},
		onkeyup: false,
		errorPlacement: function(error, element) {
			
			element.effect('shake', { times: 3, distance: 5 }, 100);
			
		},
		submitHandler: function(form) {
		
			var content = $(form).serialize();
			
			$(form).find('input[type=image]').after('<img src="resources/img/loader.gif" alt="" id="loaderImage" style="margin: 0 0 12px 10px;" />');
			
			$.post("contact_handler.php", { content: content }, function(data) {
			
				$('#loaderImage').remove();
			
				if(data == "pass") {
					$(form)[0].reset();
					$(form).find('input[type=image]').fadeOut(function() {
						$(form).append('<div class="notification success"><div>Your e-mail was successfully sent</div></div>').children('.notification').hide().fadeIn();
					});
				} else if(data == "fail") {
					$(form).find('input[type=image]').fadeOut(function() {
						$(form).append('<div class="notification error"><div>Your e-mail was couldn\'t be sent</div></div>').children('.notification').hide().fadeIn();
					});
				}
			
			});
			
			return false;
			
		}
	});

});

function setCountdownDimensions(secondsRemaining) {

	var days = Math.floor(secondsRemaining / 86400),
		hours = Math.floor((secondsRemaining - (days * 86400)) / 3600),
		minutes = Math.floor((secondsRemaining - (days * 86400) - (hours * 3600)) / 60),
		seconds = secondsRemaining - (days * 86400) - (hours * 3600) - (minutes * 60),
		wrapperWidth = 48;
		
	if(secondsRemaining > 0) {
		
		wrapperWidth += (seconds.toString().length == 1) ? 123 : 149;
		wrapperWidth += (minutes.toString().length == 1) ? 123 : 149;
		wrapperWidth += (hours.toString().length == 1) ? 123 : 149;
		wrapperWidth += (days.toString().length == 1) ? 123 : 149;
		
		$('.countdownCells').width(wrapperWidth);
	
		if(seconds.toString().length == 2) {
			$('.seconds').parent('div').width(149).find('h1').html(seconds);
		} else {
			$('.seconds').parent('div').width(123).find('h1').html(seconds);
		}
		
		if(minutes.toString().length == 2) {
			$('.minutes').parent('div').width(149).find('h1').html(minutes);
		} else {
			$('.minutes').parent('div').width(123).find('h1').html(minutes);
		}
		
		if(hours.toString().length == 2) {
			$('.hours').parent('div').width(149).find('h1').html(hours);
		} else {
			$('.hours').parent('div').width(123).find('h1').html(hours);
		}
		
		if(days.toString().length == 2) {
			$('.days').parent('div').width(149).find('h1').html(days);
		} else {
			$('.days').parent('div').width(123).find('h1').html(days);
		}
	
	} else {
	
		$('.countdownCells').width(540);
	
	}

}

function countdown(secondsRemaining) {

	var days = Math.floor(secondsRemaining / 86400),
		hours = Math.floor((secondsRemaining - (days * 86400)) / 3600),
		minutes = Math.floor((secondsRemaining - (days * 86400) - (hours * 3600)) / 60),
		seconds = secondsRemaining - (days * 86400) - (hours * 3600) - (minutes * 60),
		wrapperWidth = 48;
	
	if(secondsRemaining > 0) {
	
		wrapperWidth += (seconds.toString().length == 1) ? 123 : 149;
		wrapperWidth += (minutes.toString().length == 1) ? 123 : 149;
		wrapperWidth += (hours.toString().length == 1) ? 123 : 149;
		wrapperWidth += (days.toString().length == 1) ? 123 : 149;
		
		if(wrapperWidth < $('.countdownCells').width()) {
			setTimeout(function() {
				$('.countdownCells').animate({ width: wrapperWidth + 'px' });
			}, 50);
		} else {
			$('.countdownCells').animate({ width: wrapperWidth + 'px' });
		}
		
		$('.days > h1').html(days);
		$('.hours > h1').html(hours);
		$('.minutes > h1').html(minutes);
		
		if(seconds.toString().length == 2) {
			if($('.seconds').parent('div').width() != 149) {
				$('.seconds').parent('div').animate({ width: '149px' }).find('h1').html(seconds);
			} else {
				$('.seconds').find('h1').html(seconds);
			}
		} else {
			if($('.seconds').parent('div').width() != 123) {
				$('.seconds').parent('div').animate({ width: '123px' }).find('h1').html(seconds);
			} else {
				$('.seconds').find('h1').html(seconds);
			}
		}
		
		if(minutes.toString().length == 2) {
			if($('.minutes').parent('div').width() != 149) {
				$('.minutes').parent('div').animate({ width: '149px' }).find('h1').html(minutes);
			} else {
				$('.minutes').find('h1').html(minutes);
			}
		} else {
			if($('.minutes').parent('div').width() != 123) {
				$('.minutes').parent('div').animate({ width: '123px' }).find('h1').html(minutes);
			} else {
				$('.minutes').find('h1').html(minutes);
			}
		}
		
		if(hours.toString().length == 2) {
			if($('.hours').parent('div').width() != 149) {
				$('.hours').parent('div').animate({ width: '149px' }).find('h1').html(hours);
			} else {
				$('.hours').find('h1').html(hours);
			}
		} else {
			if($('.hours').parent('div').width() != 123) {
				$('.hours').parent('div').animate({ width: '123px' }).find('h1').html(hours);
			} else {
				$('.hours').find('h1').html(hours);
			}
		}
		
		if(days.toString().length == 2) {
			if($('.days').parent('div').width() != 149) {
				$('.days').parent('div').animate({ width: '149px' }).find('h1').html(days);
			} else {
				$('.days').find('h1').html(days);
			}
		} else {
			if($('.days').parent('div').width() != 123) {
				$('.days').parent('div').animate({ width: '123px' }).find('h1').html(days);
			} else {
				$('.days').find('h1').html(days);
			}
		}
		
		secondsRemaining--;
		
	} else {
	
		if(secondsRemaining == 0) {
	
			window.location.reload();
		
		}
		
	}
	window.setTimeout(function() {
	
   		countdown(secondsRemaining);
   		
	}, 1000);
	
}

function goToCurrentPage() {

	newHash = (window.location.hash == '') ? '#progress' : window.location.hash;
	
	if(newHash != $('.contentMenu a.active').attr('href')) {
	
		$('.content > div').hide();
		$(newHash).show();
			
		$('.contentMenu a').removeClass('active');
		$('.contentMenu a[href=' + newHash + ']').addClass('active');
	
	}

}

function updateProgress(procent) {

	if(typeof procent !== "number") {
		procent = 0;
	}

	if(procent <= 7) {
		
		$('.progressBarFill').css({ width: '7%' });
		
	} else if(procent >= 100) {
	
		$('.progressBarFill').css({ width: '100%' });
	
	} else {
	
		$('.progressBarFill').css({ width: procent + '%' });
		
	}

}
