function $(id) {
	return document.getElementById(id);
}

var Event = {
	stopClick : function(event) {
		if (event.srcElement) {
			event.cancelBubble = true;
			event.returnValue = false;
		} else {
			event.stopPropagation();
			event.preventDefault();	
		}
	}
};

var News = {
	isIE : false,
	links_id : "",
	entry_id : "",
	total : 0,
	playing : true,
	selected : 1,
	timeout : 0,
	interval : 4000,
	
	stop : function() {
		this.playing = false;
		clearTimeout(this.timeout);
	},
	
	setOpacity : function(elem, percent) {
		if (this.isIE) {
			elem.style.filter = "alpha(opacity=" + percent + ")";
		} else {
			var decimal = percent / 100;
			elem.style.opacity = decimal;
		}
	},
	
	fade : function(oldIndex, newIndex, percent) {
		fadeOut = $(this.entry_id + "_" + oldIndex);
		fadeIn = $(this.entry_id + "_" + newIndex);
		
		if (percent == 100) {
			this.setOpacity(fadeIn, 0);
			fadeIn.className = "selected";
		}

		this.setOpacity(fadeOut, percent);
		this.setOpacity(fadeIn, (100 - percent));
					
		if (percent == 0) {
			fadeOut.className = "";
			this.setOpacity(fadeOut, 100)
			return;
		}

		setTimeout("News.fade(" + oldIndex + "," + newIndex + ", " + (percent - 10) + ")", 50);
	},
	
	goToNum : function(num) {
		if (this.selected == num) { return; }
		
		$(this.links_id + "_" + this.selected).className = "";
		$(this.links_id + "_" + num).className = "selected";

		this.fade(this.selected, num, 100);
		this.selected = num;
	},
	
	goTo : function(event) {
		Event.stopClick(event);
		this.stop();
		
		var target = (event.srcElement) ? event.srcElement : event.target;
		var num = target.id.replace(this.links_id + "_", "");
		
		this.goToNum(num);
	},
	
	getNext : function() {
		return ((this.selected==this.total) ? 1 : (parseInt(this.selected, 10)+1));
	},

	timer : function() {
		this.goToNum(this.getNext());
		if (this.playing) {
			this.timeout = setTimeout("News.timer()", this.interval);
		}
	},
	
	setup : function() {
		var name = this.links_id;
		this.isIE = document.attachEvent ? true : false;
		
		var i = 1;
		for (var child = $(name).firstChild; child !== null; child = child.nextSibling) {
			if (child.tagName !== undefined && child.tagName.toLowerCase() == "a") {
				//Setup id
				child.id = name + "_" + i;
				
				//Setup event handler
				if (this.isIE) {
					child.attachEvent("onclick", function(event){News.goTo(event);});
				} else {
					child.addEventListener("click", function(event){News.goTo(event);}, false);
				}
				
				i++;
			}
		}
		
		//Start timer
		this.timeout = setTimeout("News.timer()", this.interval);
	}
};

var Email = {
	onFocus : function() {
		var email = $('email');
		if (email.value == "Email Address") {
			email.value = "";
			email.style.color = "#333333";
		}
	},
	
	onBlur : function() {
		var email = $('email');
		if (email.value == "") {
			email.style.color = "#999999";
			email.value = "Email Address";
		}
	}
};

function setup() {
	// News
	if ( $('events') !== null ) {
		News.links_id = "numbers";
		News.entry_id = "entry";
		News.total = $("numbers").childNodes.length;
		
		News.setup();
	}
	
	// Email
	if ( $('email') != null) {
		var email = $('email');
		email.style.color = "#999999";
		
		var isIE = document.attachEvent ? true : false;
		if (isIE) {
			email.attachEvent("onfocus", Email.onFocus);
			email.attachEvent("onblur", Email.onBlur);
		} else {
			email.addEventListener("focus", Email.onFocus, false);
			email.addEventListener("blur", Email.onBlur, false);
		}
	}
}

window.onload = setup;

