function isMouseLeaveOrEnter(e, handler) {
	if (e.type != 'mouseout' && e.type != 'mouseover')
		return false;
	var reltg = e.relatedTarget ? e.relatedTarget : e.type == 'mouseout' ? e.toElement : e.fromElement;
	while (reltg && reltg != handler) reltg = reltg.parentNode;
  return (reltg != handler);
}

var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};

interfaceClass = Class.create();
interfaceClass.prototype = {
	initialize: function() {
		this.scrollerSpeed		 = 80;	// ms
		this.scrollerUpdateSpeed = 40;  // seconds
		this.songUpdateSpeed     = 40;  // seconds
		this.statsUpdateSpeed    = 20;  // seconds

		this.events = {
			windowLoad			:	this.windowLoad.bindAsEventListener(this),
			hoverFadeMouseOver	:	this.hoverFadeMouseOver.bindAsEventListener(this),
			hoverFadeMouseOut	:	this.hoverFadeMouseOut.bindAsEventListener(this),
			keypress			:	this.keypress.bindAsEventListener(this)
		};

		Event.observe(window, 'load', 		this.events.windowLoad);
		Event.observe(window, 'keypress', 	this.events.keypress);
	},
	windowLoad: function() {
		this.hoverFade();
		this.initScroller();
		this.initStats();
		this.initSongUpdater();
	},
	requestComplete: function() {
		this.hoverFade();
	},
	keypress: function(e) {
		key = e.which || e.keyCode;
	},
	hoverFade: function() {
		if (BrowserDetect.browser != "MSIE") {
			$$('.hoverFade').each(function(e) {
				e.style.cursor = "pointer";
				Element.setOpacity(e, 0.75);

				Event.stopObserving(e, 'mouseover', this.events.hoverFadeMouseOver);
				Event.observe(e, 'mouseover', 		this.events.hoverFadeMouseOver);

				Event.stopObserving(e, 'mouseout', 	this.events.hoverFadeMouseOut);
				Event.observe(e, 'mouseout', 		this.events.hoverFadeMouseOut);
			}.bind(this));
		}
	},
	hoverFadeMouseOver: function(event) {
		el = Event.element(event);
		if (isMouseLeaveOrEnter(event, el) && el.hasClassName("hoverFade")) {
			new Effect.Fade(el, { from: 0.7, to: 1.0, duration: 0.25, fps: 25 });
		}
	},
	hoverFadeMouseOut: function(event) {
		el = Event.element(event);
		if (isMouseLeaveOrEnter(event, el) && el.hasClassName("hoverFade")) {
			new Effect.Fade(el, { from: 1.0, to: 0.7, duration: 0.25, fps: 25 });
		}
	},
	initScroller: function() {
		this.scrollerInterval		= setInterval(function(){this.doScroller();}.bind(this), this.scrollerSpeed);
		this.scrollerUpdateInterval = setInterval(function(){scroller_output();}.bind(this),this.scrollerUpdateSpeed*1000);

		this.scrollerDraggable = new Draggable('scroller_content',{constraint:'horizontal'});
	},
	doScroller: function() {
		if ($('scroller_content').offsetLeft <= ($('scroller_content').offsetWidth * -1)+320) {
			$('scroller_content').style.left = $('scroller_content').offsetWidth + "px";
		}
		$('scroller_content').style.left = $('scroller_content').offsetLeft-1 + "px"
	},
	initStats: function() {
		if ($('stats_content')) {
			this.statsUpdateInterval = setInterval(function(){stats_output();}.bind(this),this.statsUpdateSpeed*1000);
		}
	},
	initSongUpdater: function() {
		if ($('tuneIn')) {
			this.songUpdateInterval = setInterval(function(){song_output();}.bind(this),this.songUpdateSpeed*1000);
		}
	}
}

interface = new interfaceClass();