var active_left = 1;
var active_right = 1;
var busy = false;
var weekspan = 0;


	var current_carousel_item = 0;
	var carousel_width = 900;
	var carousel_inner_width = 0;
	var carousel_current_scroll = 0;
	var carousel_moving = false;
	var autoSliding = true;
	var slideTime = 5000;
	var slideTimer;

//slideTimer = setTimeout(autoSlideRight, slideTime);

Event.observe(window,"load",function(ev) {
	if($$(".thermometer")[0])
	{
		var goal = 20000;
		var total = parseInt($$(".thermometer")[0].identify());
		var newPos = (total / goal) * 270;
		new Effect.Morph($$(".thermometer .thermo_fill")[0], { style: "top: -" + newPos + "px;", duration: 1.3 });
	}
	
	
	// Swap out the homepage right and left features, woohoo.
	$$(".fnav a").invoke("observe","click",function(ev) {
		ev.stop();
		
		if (this.hasClassName("active") || busy)
			return;

		busy = true;
		p = this.up("div");
		nid = this.readAttribute("href").substr(1);
		
		if (p.hasClassName("feature_right")) {
			aid = active_right;
			active_right = nid;
		} else {
			aid = active_left;
			active_left = nid;
		}
		
		p.down(".fnav a",aid-1).removeClassName("active");
		this.addClassName("active");
		new Effect.Parallel([
			new Effect.Fade(p.down("div",aid)),
			new Effect.Fade(p.down("img",aid-1)),
			new Effect.Appear(p.down("div",nid)),
			new Effect.Appear(p.down("img",nid-1))],
			{ duration: 0.8, afterFinish: function() { busy = false; } }
		);
	});
	
	
	// A carousel script that actually works with variable width entities ------------------------------------------
	$$(".carousel .previous").invoke("observe","click",function(ev) {
		ev.stop();
		autoSliding = false;
		clickPrevious();
	});
	
	$$(".carousel .next").invoke("observe","click",function(ev) {
		ev.stop();
		autoSliding = false;
		clickNext();
	});
});


	function clickPrevious() {
		clearTimeout(slideTimer);
		if(autoSliding) {
			slideTimer = setTimeout(autoSlideRight, slideTime);
		}
	
		carousel_inner_width = 0;
		$$(".carousel_items li").each(function(el) {
			carousel_inner_width += el.getWidth();
		});
		if (!$$(".carousel .previous")[0].hasClassName("disabled") && !carousel_moving) {
			carousel_moving = true;
			x = 0;
			to_scroll = 0;
			done = false;
			previous = [];
			$$(".carousel_items li").each(function(el) {
				if (x < current_carousel_item) {
					previous[previous.length] = el;
				}
				x++;
			});
			previous.reverse();
			x = 0;
			while (x < previous.length && done == false) {
				width = previous[x].getWidth();
				if (to_scroll + width > 900)
					done = true;
				else {
					current_carousel_item--;
					to_scroll += width;
				}
				x++;
			}
			
			if (x >= previous.length) {
				$$(".carousel .previous").invoke("addClassName","disabled");
			}
			
			carousel_current_scroll -= to_scroll;
			new Effect.Morph($$("ul.carousel_items")[0], { style: "margin-left: " + (carousel_current_scroll * -1) + "px;", duration: 0.5, afterFinish: function() { carousel_moving = false; } });
			$$(".carousel .next").invoke("removeClassName","disabled");
		}
	}

	function clickNext() {
		clearTimeout(slideTimer);
		if(autoSliding) {
			slideTimer = setTimeout(autoSlideRight, slideTime);
		}
	
		carousel_inner_width = 0;
		$$(".carousel_items li").each(function(el) {
			carousel_inner_width += el.getWidth();
		});
		if (!$$(".carousel .next")[0].hasClassName("disabled")) {
			carousel_moving = true;
			x = 0;
			to_scroll = 0;
			done = false;
			more = false;
			$$(".carousel_items li").each(function(el) {
				x++;
				if (x > current_carousel_item && done == false) {
					width = el.getWidth();
					if (to_scroll + width > 900) {
						done = true;
						more = true;
					} else {
						current_carousel_item++;
						to_scroll += width;
					}
				}
			});
			
			carousel_current_scroll += to_scroll;
			if (carousel_current_scroll + 900 >= carousel_inner_width) {
				$$(".carousel .next").invoke("addClassName","disabled");
				if(autoSliding) {
					clearTimeout(slideTimer);
					slideTimer = setTimeout(autoSlideLeft, slideTime);
				}
			}
			new Effect.Morph($$("ul.carousel_items")[0], { style: "margin-left: " + (carousel_current_scroll * -1) + "px;", duration: 0.5, afterFinish: function() { carousel_moving = false; } });
			$$(".carousel .previous").invoke("removeClassName","disabled");
		}
	}
	
	function autoSlideRight() {
		clickNext();
	}
	
	function autoSlideLeft() {
		carousel_moving = true;
		carousel_current_scroll = 0;
		current_carousel_item = 0;
		new Effect.Morph($$("ul.carousel_items")[0], { style: "margin-left: 0px;", duration: 1.5, afterFinish: function() { carousel_moving = false; } });
		$$(".carousel .previous").invoke("addClassName","disabled");
		$$(".carousel .next").invoke("removeClassName","disabled");
		slideTimer = setTimeout(autoSlideRight, slideTime);
	}

