$(window).resize(
	function() {
		pageSize();
		navTo( $("#current_page").attr("value"), false );
	}
);

$(document).ready(
	function() {
		pageSize();
		var loadAnimNum = 750;
		$("#o").hide().fadeIn(loadAnimNum);
		$("#logo-without-o").delay(loadAnimNum*5).fadeIn(loadAnimNum);
		$(window).hashchange();
	}
);
		
$('.form input').live('focus', function() {
	$(this).css('color', '#000');
	$(this).parent().parent().parent().find('div.label span').css({'color': '#000', 'fontWeight': 'normal'});
	navTo($("#current_page").val(), false);
});

$('.form input').live('blur', function() {
	if($(this).val() == ''){
		$(this).css('color', '#999');
		$(this).parent().parent().parent().find('div.label span').css({'color': '#999', 'fontWeight': 'normal'});
	}else{		
		$(this).css('color', '#444');
		$(this).parent().parent().parent().find('div.label span').css({'color': '#444', 'fontWeight': 'normal'});
	}
});


function pageSize() {
	var bodyWidth = $("body").width();
	var bodyHeight = $("body").height();
	var contentHeight = bodyHeight - 100;
	var logoHeight = $("#logo-without-o").height();
	var logoMargin = (bodyHeight-logoHeight)/2;
	var logoODefault = logoMargin-65;
	$(".page").css('height', bodyHeight + 'px');
	$(".content").css('height', contentHeight + 'px');
	$(".page").css('width', bodyWidth + 'px');
	$("#logo-without-o").css('marginTop', logoMargin + 'px');
	$("#o").offset({top: logoODefault});
	$("#watermark").css('height', bodyHeight + 'px');
	$("#watermark").css('width', bodyWidth + 'px');
}


function navTo(loc, an) {
	if(loc == 'blank') {
		loc = 'logo';
	} 
	var newOffset = $(".page-" + loc).offset().top - $('html').offset().top;
	if(an == true) {
		$('html').animate({ top: "-" + newOffset }, 'slow', 'linear', function() {
			var bodyHeight = $("body").height();
			var htmlOffset = $('html').offset().top * -1;
			if(htmlOffset >= bodyHeight) {
				$("#corner-o").fadeIn(250);
				$("#corner-o").css('bottom', '0px');
				$("#corner-o").css('right', '0px');
			}else{
				$("#corner-o").fadeOut(250);
			}
		});
	}
	if(an == false) {
		$('html').offset({ top: "-" + newOffset });
	}
	if(loc == 'what') { 
		$("#corner-o a").attr("href", "#");
	}else{
		$("#corner-o a").attr("href", "#");
	}
	$('#watermark').animate({ top: newOffset }, 'slow', 'linear');
	$("#current_page").attr("value", loc);
}

$(window).hashchange( 
	function() {
		var hash = location.hash;
		var hashPiece = ( hash.replace( /^#!/, '' ) || 'blank' );
		var hashPiece = hashPiece.replace('/', '');
		if(hashPiece == 'blank'){
			navTo('logo', true);
		}
		navTo(hashPiece, true);
	}
);
