$(document).ready(function() {
	
	$.address.crawlable(true);

	$.ajaxSetup ({ cache: false });

	$.address.init(function(event) {
	
		
		
	}).change(function(event) {
		
		$('#mainLoader').fadeIn(50);
		//var url = event.value.replace("/","");
		var url = event.pathNames[0];
		var url2 = event.pathNames[1];
		var url3 = event.pathNames[2];
		var loadURL = "/i/"+ url +".php";
		var loadHome = "/i/home.php";
		
		var pages = $.address.pathNames().length;
		switch(pages) {
		
			case 0:
			$("#content").fadeOut(200, function() {
				$("#content").load(loadHome, function() {
					$('#mainLoader').fadeOut(100);
					$(this).fadeIn();
				});
			});
			break;
			
			case 1:
			$('.navitem a.'+ url +' img').fadeIn(50);
			$("#content").fadeOut(200, function() {
				$("#content").load(loadURL, function() {
					$('.navitem a.'+ url +' img').fadeOut(100);
					$('#mainLoader').fadeOut(50);
					$(this).fadeIn();
				});
			});
			break;
			
			case 2:
			$('.navitem a.'+ url2 +' img').fadeIn(50);
			$("#content").fadeOut(200, function() {
				$("#content").load(loadURL, function() {
					$('.navitem a.'+ url2 +' img').fadeOut(100);
					$('#mainLoader').fadeOut(50);
					$('.subpage').each(function() {
						if($(this).attr('id') == url2) {
							$(this).show();
							$('#title').text("/ " + $(this).attr('title'));
						} else {
							//$(this).hide();
						}
					});
					$(this).fadeIn();
				});
			});
			break;
			
			case 3:
			$('#mainLoader').fadeIn(50);
			$("#content").fadeOut(200, function() {
				$("#content").load(loadURL, function() {
					$('#mainLoader').fadeOut(100);
					$('.subpage').each(function() {
						if($(this).attr('id') == url3) {
							$(this).show();
							$('#title').text("/ " + $(this).attr('title'));
						} else {
							//$(this).hide();
						}
					});
					$(this).fadeIn();
				});
			});
			break;
		
		}
		
		$(".navitem a").each(function() {
			if ($(this).attr('href') == url) {
				$(this).css('background', '#111');
				$(this).css('color', '#fff');
			} else {
				$(this).css('background', '#2194cf');
				$(this).css('color', '#fff');
			}
		});
		
		$(".do a").each(function() {
			if ($(this).attr('href') == url2) {
				$(this).css('background', '#111');
				$(this).css('color', '#fff');
			} else {
				$(this).css('background', '#c32929');
				$(this).css('color', '#fff');
			}
		});
		
		$(".help a").each(function() {
			if ($(this).attr('href') == url2) {
				$(this).css('background', '#000');
				$(this).css('color', '#fff');
			} else {
				$(this).css('background', '#2cd2a1');
				$(this).css('color', '#fff');
			}
		});
	
	});
	
	$('.navitem a').hover(function() {
		$(this).fadeTo(200, 0.8);
	}, function() {
		$(this).fadeTo(200, 1);
	});
	
	//$('#menu li:not(.menuCat)').stop(true,true).fadeTo(0, 0.5);
	$('#menu li:not(.menuCat, .navitem-contact)').hover(function() {
		$(this).css('background', '#000');
	}, function() {
		$(this).css('background', '#222');
	});
	
	var options = { 
		success: function() { 
			$('#quickForm').fadeOut("normal", function() {
				$('#quickForm').html("<div id='getstarted' style='margin-top: 30px; padding: 20px; background: #000'><div style='font-size: 14px'><h2 style='color: #fff; font-size: 40px; margin: 0; padding: 0'>Thank You for taking that next step!</h2><br/><span style='color: #888; font-size: 18px'>Your answers have been successfully sent! We will be in contact with you within 1 business day.</span></div></div>");
				$('#quickForm').fadeIn('normal');
			});
		} 
	};
	
	$('#submit').livequery('click',function(){
		$('#getstarted').validate({
			submitHandler: function(form) {
				$('#submit').hide();
				$('#formLoading').stop().fadeTo(500,1);
				$(form).ajaxSubmit(options);
			}
		});
	});	

	
});
