//preload nav-menu backgrounds
var image1 = $('<img />').attr('src', '../images/nav-back2.png');
image1 = $('<img />').attr('src', '../images/nav-back3.png');
image1 = $('<img />').attr('src', '../images/nav-back4.png');
image1 = $('<img />').attr('src', '../images/nav-back5.png');
image1 = $('<img />').attr('src', '../images/nav-back6.png');
image1 = $('<img />').attr('src', '../images/nav-back7.png');

$(document).ready(function () {
	var timeout_id;
		
	//home main menu
	$("#page.home-page #nav-menu li a").bind("click", function(e){
		$("#page.home-page #nav-data div.data-item").each(function(){
			if(!$(this).hasClass("hidden"))
				$(this).addClass("hidden");
		});
	
	
		$("#page.home-page #nav-data div#"+$(this).parent().attr("id")+"-data").removeClass("hidden");
		$("#page.home-page #nav-menu").removeClass("home-item")
		.removeClass("welcome-item")
		.removeClass("academic-calendar-item")
		.removeClass("parish-diocese-item")
		.removeClass("prospectus-item")
		.removeClass("admissions-item")
		.removeClass("contact-us-item")
		.addClass($(this).parent().attr("id"));
		;

		//stop slideshow on home tab
		if($("#page.home-page #nav-menu").hasClass("home-item")){
			timeout_id = setTimeout(galleryRun, 4000);
		} else {
			clearTimeout(timeout_id)
		}
	});
	
	
	//home gallery
	var current = 1;
	var total = $("#home-item-data ul li").size();
	$("#home-item-data ul li:nth-child("+current+")").show();

	//next
	$("#home-gal-nav-next").bind("click", function(e){
		clearTimeout(timeout_id)
		$("#home-item-data ul li:nth-child("+current+")").fadeOut(500);
		if(current == total) {
			current = 1;
		} else {
			current += 1;
		}
		$("#home-item-data ul li:nth-child("+current+")").fadeIn(500);
		timeout_id = setTimeout(galleryRun, 4000);
	});

	//prev
	$("#home-gal-nav-prev").bind("click", function(e){
		clearTimeout(timeout_id)
		$("#home-item-data ul li:nth-child("+current+")").fadeOut(500);
		if(current == 1) {
			current = total;
		} else {
			current -= 1;
		}
		$("#home-item-data ul li:nth-child("+current+")").fadeIn(500);
		timeout_id = setTimeout(galleryRun, 4000);
	});

	
	function galleryRun(){
		$("#home-gal-nav-next").click();
		timeout_id = setTimeout(galleryRun, 4000);
	}
	timeout_id = setTimeout(galleryRun, 4000);
	
	
	//add custom scrollbar to .data-item .data-content
	$('#nav-data .data-content').each(function(){
		$(this).jScrollPane({scrollbarWidth: 18});
	});
	
	$('.jScrollPaneDrag').css("height", "35px");

	
});

