
$(document).ready(function(){
   var imgStudent = "";
   var imgPole = "-on";

	hash = window.location.hash;
	if(hash != ''){
		if (hash == "#pole"){
			$('.news.student').hide();
			$('#toggle-pole').addClass("selected");
		} else {
			$('.news.pole').hide();
			$('#toggle-student').addClass("selected");
		}
	} else {
		$('.news.pole').hide();
		$('#toggle-student').addClass("selected");
	}

	$('#toggle-pole').click(function(){
		$('#toggle-pole').addClass("selected");
		$('#toggle-student').removeClass("selected");
		$('.news.student').hide();
		$('.news.pole').show();
		window.location.hash = "#pole";
		return false;
	});
	$('#toggle-student').click(function(){
		$('#toggle-student').addClass("selected");
		$('#toggle-pole').removeClass("selected");
		$('.news.student').show();
		$('.news.pole').hide();
		window.location.hash = "#student";
		return false;
	});

	$("#nav a").mouseover(function(){
		src = $(this).find("img").attr('src');
		$(this).data("baseImage", src);
		src = src.substr(0, src.indexOf(".")) + "-hover.jpg";
		$(this).find("img").attr('src', src);
	}).mouseout(function(){
		$(this).find("img").attr('src', $(this).data('baseImage'));

	});

	$("#title a").mouseover(function(){
		$(this).find("img").attr("src", "content/img/template/logo-survol.png");
	}).mouseout(function(){
		$(this).find("img").attr("src", "content/img/template/logo.png");
	});

	$("#nav a:not(.subnav-toggler), #header").mouseover(function(){
		closeEvenementsSubnav();
		closeAnnoncesSubnav();
		closePratiqueSubnav();
		closeBdaSubnav();
	});

	$("#pratique-subnav-toggler").mouseover(function(){
		if(!pratiqueSubnavOpen)
			openPratiqueSubnav();

		closeEvenementsSubnav();
		closeAnnoncesSubnav();
		closeBdaSubnav();
		pratiqueSubnavTogglerHover = true;
	}).mouseout(function(){
		pratiqueSubnavTogglerHover = false;
	});


	$("#annonces-subnav-toggler").mouseover(function(){
		if(!annoncesSubnavOpen)
			openAnnoncesSubnav();
		
		closeEvenementsSubnav();
		closePratiqueSubnav();
		closeBdaSubnav();
		annoncesSubnavTogglerHover = true;
	}).mouseout(function(){
		annoncesSubnavTogglerHover = false;
	});

	$("#evenements-subnav-toggler").mouseover(function(){
		if(!evenementsSubnavOpen)
			openEvenementsSubnav();

		closePratiqueSubnav();
		closeAnnoncesSubnav();
		closeBdaSubnav();
		evenementsSubnavTogglerHover = true;
	}).mouseout(function(){
		evenementsSubnavTogglerHover = false;
	});

	$("#bda-subnav-toggler").mouseover(function(){
		if(!bdaSubnavOpen)
			openBdaSubnav();

		closeEvenementsSubnav();
		closePratiqueSubnav();
		closeAnnoncesSubnav();
		bdaSubnavTogglerHover = true;
	}).mouseout(function(){
		bdaSubnavTogglerHover = false;
	});
});

pratiqueSubnavOpen = false;
pratiqueSubnavHover = false;
pratiqueSubnavTogglerHover = false;

function openPratiqueSubnav(){
	pratiqueSubnav = $("#pratique-subnav")
	pratiqueSubnav.unbind("mouseout", closePratiqueSubnav)
	pratiqueSubnav.find("li").mouseover(function(){
		pratiqueSubnavHover = true;
	}).mouseout(function(){
		pratiqueSubnavHover = false;
	});;
	pratiqueSubnav.slideDown(100, function(){
		pratiqueSubnavOpen = true;
	});
	pratiqueSubnav.mouseover(function(){
		$(this).bind("mouseout", delayClosePratique);
	});

}

function delayClosePratique(){
	setTimeout(closePratiqueSubnav, 100);
}

function closePratiqueSubnav(){
	if(!pratiqueSubnavHover && !pratiqueSubnavTogglerHover && pratiqueSubnavOpen){
		$("#pratique-subnav").slideUp(100, function(){
			pratiqueSubnavOpen = false;
		});
	}
}

evenementsSubnavOpen = false;
evenementsSubnavHover = false;
evenementsSubnavTogglerHover = false;

function openEvenementsSubnav(){
	evenementsSubnav = $("#evenements-subnav")
	evenementsSubnav.unbind("mouseout", closeEvenementsSubnav)
	evenementsSubnav.find("li").mouseover(function(){
		evenementsSubnavHover = true;
	}).mouseout(function(){
		evenementsSubnavHover = false;
	});;
	evenementsSubnav.slideDown(100, function(){
		evenementsSubnavOpen = true;
	});
	evenementsSubnav.mouseover(function(){
		$(this).bind("mouseout", delayCloseEvenements);
	});

}

function delayCloseEvenements(){
	setTimeout(closeEvenementsSubnav, 100);
}

function closeEvenementsSubnav(){
	if(!evenementsSubnavHover && !evenementsSubnavTogglerHover && evenementsSubnavOpen){
		$("#evenements-subnav").slideUp(100, function(){
			evenementsSubnavOpen = false;
		});
	}
}

annoncesSubnavOpen = false;
annoncesSubnavHover = false;
annoncesSubnavTogglerHover = false;

function openAnnoncesSubnav(){
	annoncesSubnav = $("#annonces-subnav")
	annoncesSubnav.unbind("mouseout", closeAnnoncesSubnav)
	annoncesSubnav.find("li").mouseover(function(){
		annoncesSubnavHover = true;
	}).mouseout(function(){
		annoncesSubnavHover = false;
	});;
	annoncesSubnav.slideDown(100, function(){
		annoncesSubnavOpen = true;
	});
	annoncesSubnav.mouseover(function(){
		$(this).bind("mouseout", delayCloseAnnonces);
	});

}

function delayCloseAnnonces(){
	setTimeout(closeAnnoncesSubnav, 100);
}

function closeAnnoncesSubnav(){
	if(!annoncesSubnavHover && !annoncesSubnavTogglerHover && annoncesSubnavOpen){
		$("#annonces-subnav").slideUp(100, function(){
			annoncesSubnavOpen = false;
		});
	}
}

bdaSubnavOpen = false;
bdaSubnavHover = false;
bdaSubnavTogglerHover = false;

function openBdaSubnav(){
	bdaSubnav = $("#bda-subnav")
	bdaSubnav.unbind("mouseout", closeBdaSubnav)
	bdaSubnav.find("li").mouseover(function(){
		bdaSubnavHover = true;
	}).mouseout(function(){
		bdaSubnavHover = false;
	});;
	bdaSubnav.slideDown(100, function(){
		bdaSubnavOpen = true;
	});
	bdaSubnav.mouseover(function(){
		$(this).bind("mouseout", delayCloseBda);
	});

}

function delayCloseBda(){
	setTimeout(closeBdaSubnav, 100);
}

function closeBdaSubnav(){
	if(!bdaSubnavHover && !bdaSubnavTogglerHover && bdaSubnavOpen){
		$("#bda-subnav").slideUp(100, function(){
			bdaSubnavOpen = false;
		});
	}
}

