function set_delegates() {
	// When clicked on any of the main navigation items
	$("#mainMenu").delegate("a", "click", function() {
		window.location.hash = $(this).attr("href");
		return false;
	});

	// When clicked on any of the sub-navigation items
	$("#subNav").delegate("a", "click", function() {
		window.location.hash = $(this).attr("href");
		return false;
	});
}


var oldHash = "";
var newHash = "";
var sameSection = false;

$(function() {
	
	set_delegates();
	
	$(window).bind('hashchange', function(){
		oldHash = newHash;
		newHash = window.location.hash.substring(1);
		
		if(newHash.substr(0, 16) == '?page=attorneys_' && oldHash.substr(0, 16) == '?page=attorneys_') {
			sameSection = true;
		} else if (newHash.substr(0, 14) == '?page=practice' && oldHash.substr(0, 14) == '?page=practice') {
			sameSection = true;
		} else {
			sameSection = false;
		}

		if (newHash) {
			$("#portrait").fadeOut(200);
			$("#content").fadeOut(200, function() {
				$("#mainMenu").fadeOut(200);
				$("#menuBehind").fadeOut(200);
				$("#menuBelow").fadeOut(200);
				if(!sameSection) {
					$("#photography").fadeOut(200);
				}
				$("#dynamic").load(newHash + " #swap", function() {
					
					set_delegates();
					
					$("#content").hide();
					$("#mainMenu").hide();
					$("#menuBehind").hide();
					$("#menuBelow").hide();
					$("#portrait").hide();
					if(!sameSection) {
						$("#photography").hide();
					}
					
					var vpos = 300;
					if(newHash == '') {
						vpos = 300;
					} else if(newHash == '?page=our-firm') {
						vpos = 300;
					} else if(newHash.substr(0, 14) == '?page=practice') {
						vpos = 412;
					} else if(newHash == '?page=attorneys') {
						vpos = 265;
					} else if(newHash.substr(0, 15) == '?page=attorneys') {
						vpos = 227;
					} else if(newHash == '?page=our-results') {
						vpos = 360;
					} else if(newHash == '?page=contact') {
						vpos = 320;
					}

					vpos -= 115; // subtract the header height

					/* Overrides */
					$("#mainMenu").css({'top' : vpos+'px'});
					$("#menuBehind").css({ 'top' : vpos+'px'});
					$("#content").css({ 'padding' : (vpos+75)+'px 0px 0px 0px' });
					$("#menuBelow").css({ 'top' : (vpos+75)+'px' });

					// $("#dynamic").show();

					$("#photography").fadeIn(600);

					$("#content").fadeIn(200, function() {
						$("#mainMenu").fadeIn(200);
						$("#menuBehind").fadeIn(200);
						$("#menuBelow").fadeIn(200);
						$("#portrait").fadeIn(200);
					});
				});
			});
			
			
			/*
			$("#content").find("#guts").fadeOut(200, function() {
				$("#content").hide().load(newHash + " #guts", function() {
					$("#content").fadeIn(200, function() {
						$("#page").animate({
							height: baseHeight + $("#content").height() + "px"
						});
					});
					$("nav a").removeClass("current");
					$("nav a[href="+newHash+"]").addClass("current");
				});
			});
			*/
		}
	});

	$(window).trigger('hashchange');
	
	// Preload images
	if (document.images) {
		photo1 = new Image(750, 600); 
		photo1.src = "images/photo-attorneys.jpg";
		photo2 = new Image(750, 600); 
		photo2.src = "images/photo-contact.jpg";
		photo3 = new Image(750, 600); 
		photo3.src = "images/photo-home.jpg";
		photo4 = new Image(750, 600); 
		photo4.src = "images/photo-our-firm.jpg";
		photo5 = new Image(750, 600); 
		photo5.src = "images/photo-our-results.jpg";
		photo6 = new Image(750, 600); 
		photo6.src = "images/photo-practice.jpg";
		photo7 = new Image(750, 600); 
		photo7.src = "images/photo-attorneys-main.jpg";

		back1 = new Image(634, 478); 
		back1.src = "images/background-contact.png";

		pic1 = new Image(310, 290); 
		pic1.src = "images/portraits/attorneys_cohen.jpg";
		pic2 = new Image(310, 290); 
		pic2.src = "images/portraits/attorneys_fine.jpg";
		pic3 = new Image(310, 290); 
		pic3.src = "images/portraits/attorneys_hochman.jpg";
		pic4 = new Image(310, 290); 
		pic4.src = "images/portraits/attorneys_melnick.jpg";
		pic5 = new Image(310, 290); 
		pic5.src = "images/portraits/attorneys_schindler.jpg";
		pic6 = new Image(310, 290); 
		pic6.src = "images/portraits/attorneys_maciel.jpg";
		pic7 = new Image(310, 290); 
		pic7.src = "images/portraits/attorneys_bulcao.jpg";
		pic8 = new Image(310, 290); 
		pic8.src = "images/portraits/attorneys_katz.jpg";
		pic9 = new Image(310, 290); 
		pic9.src = "images/portraits/attorneys_poler.jpg";
		pic10 = new Image(310, 290); 
		pic10.src = "images/portraits/attorneys_shaw.jpg";
		pic11 = new Image(310, 290); 
		pic11.src = "images/portraits/attorneys_steel.jpg";
	}
});
