$(function() {

	// filename
	var url = window.location.pathname;
	url = nameFromPath(url);
	
	// hash
	var hash = window.location.hash;
	if (hash == "") {
		hash = "#1";	
	}
	hash = hash.substring(1);
	
	// hide all sections
	$(".section").css("display","none");
	
	//add class to subnav & show proper section
	$("#subnav li:nth-child("+hash+") a").addClass("active");
	$(".section:nth-child("+hash+")").css("display","block");
	
	// change the active link / show & hide the proper sections
	$("#subnav li").click(function() {
		$(".section").css("display","none");
		$(".section:nth-child("+($("#subnav li").index(this) + 1)+")").css("display","block");
		$("#subnav li a").removeClass("active");
		$(this).children("a").addClass("active");
	});
	
	if (url == "index" || url == "") {
		
		//add class to nav
		$("#nav li#nav_home").addClass("active");
		
		// rotate text (images) on home page
		fadeImages(0);
		
		// strip excess padding to sub col list items
		$('#home #content #columns #sub li:nth-child(3n+3)').css({'padding-right':'0px', 'width':'141px'});
		
		// equal main and sub col heights on home page
		if ($("#home #content #columns #main .guts").height() > $("#home #content #columns #sub .guts").height()) {
			$("#home #content #columns #sub .guts").height($("#home #content #columns #main .guts").height());
		} else {
			$("#home #content #columns #main .guts").height($("#home #content #columns #sub .guts").height());
		}
		
		// add top and bottom to main col
		$("#home #content #columns #main .guts").before("<div class='top png'>&nbsp;</div>");
		$("#home #content #columns #main .guts").after("<div class='bot png'>&nbsp;</div>");
		
		// add top and bottom to sub col
		$("#home #content #columns #sub .guts").before("<div class='top png'>&nbsp;</div>");
		$("#home #content #columns #sub .guts").after("<div class='bot png'>&nbsp;</div>");
		
	} else if (url == "about-us") {
				
		//add class to nav
		$("#nav li#nav_about").addClass("active");
		
	} else if (url == "mitral-valve-repair") {
		
		//add class to nav
		$("#nav li#nav_repair").addClass("active");
		
	} else if (url == "our-technology") {
		
		//add class to nav
		$("#nav li#nav_technology").addClass("active");
		
	} else if (url == "contact-us") {
		
		//add class to nav
		$("#nav li#nav_contact").addClass("active");
		
		// display section
		$(".contact .section").css("display","block");
		
	}
	
	
});

function fadeImages(x) {
	var total = $("#home #content .rotate li").size();
	var y = x;
	if(x == total) { y=1; } else { y++; }
	$("#image"+x).fadeOut("slow");
	$("#image"+y).fadeIn("slow");
	setTimeout('fadeImages('+y+')',6000);
}


function nameFromPath(path)
{
	var re=/\/([^\.\/]+\.[^\.\/]+)$/.exec( path );
	return re ? re[1].replace(/\.[^\.]+/,'') : "" ;
}
