$(document).ready(function() {
	/*
	 * creates the nice dropdown for the first top_nav element
	 */
	var current_cat = false;
	$(".top_nav li:eq(0)").hover(function() {
		current_cat = $(this).hasClass('current-cat');
		if (current_cat == false) {
			$(this).addClass('current-cat');
		}
		$(this).children("a:eq(0)").attr("id", "top_nav_first");
		$(".top_nav_expand").show();
	}, function() {
		if (current_cat == false) {
			$(this).removeClass('current-cat');
		}
		$(".top_nav_expand").hide();
		$(this).children("a:eq(0)").removeAttr("id");
	})
	$(".top_nav_expand li a").prepend("&bull; ");
	
	if ($.cookie("popup_shown")==null || $.cookie("popup_shown")==undefined) {
		$.cookie("popup_shown", "true");
	} else if ($.cookie("popup_shown")=="true") {
		$("#ipsc_container").css('opacity',0).hide();
		$(".top_nav .cat-item-32").removeClass("current-cat");
		ipsc_displayed = false;
	}
	if ($("#ipsc_container").css('opacity')>0) {
		ipsc_displayed = true; 
	} 
	var time = 250;
	var hideDelay = 500;
	var hideDelayTimer = null;
	$(".top_nav .cat-item-32 a").attr('href', 'javascript:;');
	$(".top_nav .cat-item-32").click(function(e) {
		e.stopPropagation();
		if (ipsc_displayed == true) {
			if (hideDelayTimer) clearTimeout(hideDelayTimer);
			hideDelayTimer = setTimeout(function() {
				hideDelayTimer = null;
				$("#ipsc_container").animate({
					top: '-=10px',
					opacity: 0
				}, time, 'swing', function() {
					$("#ipsc_container").hide();
					$(".top_nav .cat-item-32").removeClass('current-cat');
					ipsc_displayed = false;
				});
			}, hideDelay);
		} else {
			if (hideDelayTimer) clearTimeout(hideDelayTimer);
			hideDelayTimer = setTimeout(function() {
				hideDelayTimer = null;
				$("#ipsc_container").show().animate({
					top: '+=10px',
					opacity: 100
				}, time*2, 'swing', function() {
					//$("#ipsc_container").hide();
					$(".top_nav .cat-item-32").addClass('current-cat');
					ipsc_displayed = true;
				});
			}, 0);
		}
	});
	
	$(document).click(function() {
		if (ipsc_displayed == true) {
			if (hideDelayTimer) clearTimeout(hideDelayTimer);
			hideDelayTimer = setTimeout(function() {
				hideDelayTimer = null;
				$("#ipsc_container").animate({
					top: '-=10px',
					opacity: 0
				}, time, 'swing', function() {
					$("#ipsc_container").hide();
					$(".top_nav .cat-item-32").removeClass('current-cat');
					ipsc_displayed = false;
				});
			}, hideDelay);
		}
	});
	
	if (ipsc_displayed==true) {
		setTimeout(function() {
			if (hideDelayTimer) clearTimeout(hideDelayTimer);
			hideDelayTimer = setTimeout(function() {
				hideDelayTimer = null;
				$("#ipsc_container").animate({
					top: '-=10px',
					opacity: 0
				}, time, 'swing', function() {
					$("#ipsc_container").hide();
					$(".top_nav .cat-item-32").removeClass('current-cat');
					ipsc_displayed = false;
				});
			}, hideDelay);
		}, 12000);
	}
});
