var SCROLL_UP = -1;
var SCROLL_DOWN = 1;
var isScrollingDown = false;
var isScrollingUp = false;
var isScrolling = false;
var ScrollCount = 0;
var maxPages = 0;

$(document).ready(function() {
	//kollar hur många menyalternativ det finns
	maxPages = ($("#menu li").length)-1;
	parent.location.hash = ''
	/*var hash = location.hash.replace("#","");
	console.log(hash);
	$("#menu li a").each(function(index) {
		$(this).parent().removeClass('active');
    	if($(this).attr('href') == "#"+hash){
			$(this).parent().addClass('active');
			Cufon.replace('#menu a', { hover: true, textShadow: '#fff 1px 1px', fontFamily: 'BlairMdITC TT' });
		}
		
  	});
	 $("#currentPage").html($("#menu li.active").index());*/
	// kollar vilket håll man scrollar åt
	/*$('#cont').mousewheel(function(event, delta) {
		var velocity = Math.abs(delta), direction = (delta > 0) ? SCROLL_UP : SCROLL_DOWN;
		var speed = 30 * velocity;

		isScrolling = true;
		if (direction == SCROLL_UP) {
			isScrollingDown = false;
			isScrollingUp = true;
			$("#cont").scrollTo("-=" + speed, 20, function() { isScrolling = false; isScrollingUp = false; fnCheckPageNum(direction); });
		} else if (direction == SCROLL_DOWN) {
			isScrollingDown = true;
			isScrollingUp = false;
			$("#cont").scrollTo("+=" + speed, 20, function() { isScrolling = false; isScrollingDown = false; fnCheckPageNum(direction); });
		}
	});*/
	var lastScrollTop = 0;
    var currentScrollTop = 0;
	$(window).resize(function() {
		if($(document).width() < 1015){
	     	Cufon.replace('#menu a', { hover: true, textShadow: '#fff 1px 1px', fontFamily: 'BlairMdITC TT' });
		}
	});
	$(window).scroll(function() {
		offset = (($(window).scrollTop()));
		$('#menu').animate({ top: offset + "px" }, { duration: 300, queue: false });
	   	lastScrollTop = currentScrollTop;
       	currentScrollTop = $(document).scrollTop()
	   	if (currentScrollTop > lastScrollTop) {
       		direction = 1;
		}else {
           direction = -1;
		}
	  fnCheckPageNum(direction);
	  //menuTop = $(window).scrollTop();
	  //$('#menu').css("top", menuTop);
	  //$('#menu').animate({top: menuTop  }, 500, function() {});
	});
	var fnCheckPageNum = function(direction) {
		var currentPageDiv = $("#currentPage");
		var currPage = parseInt(currentPageDiv.html());
		var nextPage = currPage + direction;
		//alert(nextPage);
		//console.log($(window).scrollTop());
		if (nextPage < 1 || nextPage > maxPages)
			return;
		if (((($(window).scrollTop()) < $("#page" + nextPage + " .pagebottom").position().top) && (direction == -1)) || ((($(window).scrollTop()) > $("#page" + nextPage + " .pagetop").position().top) && (direction == 1))) {
			currActive = nextPage-1;
			//console.log(currActive);
			$('#menu li').each(function(index) {
				$(this).removeClass('active');
				Cufon.replace('#menu a', { hover: true, textShadow: '#fff 1px 1px', fontFamily: 'BlairMdITC TT' });
			});
			$('#menu li').eq(currActive).addClass('active');
			Cufon.replace('#menu a', { hover: true, textShadow: '#fff 1px 1px', fontFamily: 'BlairMdITC TT' });
			currentPageDiv.html(nextPage);

			/*if (nextPage != 1) {
				$(".pageup").attr("href", "#page" + (nextPage - 1));
			}
			if (nextPage != maxPages) {
				$(".pagedown").attr("href", "#page" + (nextPage + 1));
			}*/
		}
	}
});

