Event.observe(window,"load",function(ev) {

	$$("a.alert_close").click(function(ev) {
		new Ajax.Request("http://www.interbev.com/ajax/close-alert/?id=" + this.readAttribute("href").substr(1));
		new Effect.Fade(this.up("div"), { duration: 0.5 });
		ev.stop();
	});
		
	$$("#home_tabs a").click(function(ev) {
		$$(".tab_content div").invoke("hide");
		$$("#home_tabs a").invoke("removeClassName","selected");
		$(this.readAttribute("href").substr(1)).show();
		this.addClassName("selected");
		ev.stop();
	});
	
	$$(".expand_all").click(function(ev) {
		ev.stop();
		if (this.hasClassName("expand_all_expanded")) {
			$$("#expandable_list dd").invoke("hide");
			$$("#expandable_list dt span").invoke("removeClassName","expanded");
			this.removeClassName("expand_all_expanded");
			this.innerHTML = "Expand All";
		} else {
			$$("#expandable_list dd").invoke("show");
			$$("#expandable_list dt span").invoke("addClassName","expanded");
			this.addClassName("expand_all_expanded");
			this.innerHTML = "Contract All";
		}
	});
	
	$$(".expandable_list_title, #expandable_list dt span").click(function(ev) {
		ev.stop();
		dd = this.up("dt").next();
		if (dd.style.display == "none") {
			dd.show();
			dd.previous("dt").down("span").addClassName("expanded");
		} else {
			dd.hide();
			dd.previous("dt").down("span").removeClassName("expanded");	
		}
	});
	
	
	
	
	
	// A carousel script that actually works with variable width entities
	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;
	
	$$(".carousel .previous").click(function(ev) {
		ev.stop();
		autoSliding = false;
		clickPrevious();
	});
	
	$$(".carousel .next").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");
		}
	}


	slideTimer = setTimeout(autoSlideRight, slideTime);
	
	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);
	}

});