//activecontent v2.1 by kris
//define a ".active" css class to fire it up (it might need "!important")

$.fn.is_on_screen = function(){
    var win = $(window);
    var viewport = {
        top : win.scrollTop(),
        left : win.scrollLeft()
    };
    viewport.right = viewport.left + win.width();
    viewport.bottom = viewport.top + win.height();
 
    var bounds = this.offset();
    bounds.right = bounds.left + this.outerWidth();
    bounds.bottom = bounds.top + this.outerHeight();
 
    return (!(viewport.right < bounds.left || viewport.left > bounds.right || viewport.bottom < bounds.top || viewport.top > bounds.bottom));
};

var ids = [];

$(document).ready(function() {
	$("body>*").each(function() {
	    var n = $(this);
	    if (n.attr("id") == undefined) {
	    } 
	    else if (n.attr("id") == "cover") {
	    }
	    else if (n.attr("id") == "generation_errors") {
	    }
	    else {
		   ids.push ($(this).attr("id"))
	    };
	});
});


$(window).scroll(function() {
	$.each(ids, function (index, spreadX) {
			var i = "#" + spreadX;
			var c = "." + spreadX;
		if ($(i + " " + ".smoothScroll").is_on_screen()) {
			$(c).addClass('active');
			$(i).addClass('activeSpread');
		} else {
			$(c).removeClass('active');
			$(i).removeClass('activeSpread');			
		};
		if ($(i + " " + ".smoothScroll").is_on_screen()) {
			if ($(c).hasClass('subitem')) {
				$(c).parents("li").find(".item").addClass('active');
			};
		} else {
			$(c).removeClass('active');
		};
	});
	
});