function Log(text) {
	if (window.console) console.log(text);
}

/**
 * Marquee Class
 * @param {Object} mar
 */
var Marquee = new Class({
	initialize: function (mar) {
		this.marquee = mar;
		this.moves = this.marquee.getElements('.move');
		this.events = this.marquee.getElement('.events');
		this.window = this.marquee.getElement('.window');

		this.position = 0;
		this.max = (this.events.getStyle('width').toInt() * -1) + this.window.getStyle('width').toInt() - 4;

		this.margin_increment = 3;	// pixel move increment
		this.interval = 30;			// timing increment

		if (this.events.getStyle('width').toInt() > this.window.getStyle('width').toInt()) {

			this.moves.each(function(move_button){
				move_button.addEvent('mouseover', function(){
					move_button.addClass('move_hover');
					this.move_timer = this.move.periodical(this.interval, this, move_button.get('direction'));
				}.bind(this));

				move_button.addEvent('mouseout', function(){
					move_button.removeClass('move_hover');
					$clear(this.move_timer);
				}.bind(this));
			}.bind(this));
		}

		this.events.getElements('.event').each(function(event){
			event.addEvent('mouseover', function(){
				event.addClass('event_hover');
			});

			event.addEvent('mouseout', function(){
				event.removeClass('event_hover');
			});
		});
	},

	move: function ( direction ) {
		var edge = (this.position == 0 || this.position == this.max);

		this.position = this.position + (direction.toInt() * this.margin_increment);

		if (this.position >= 0) {
			this.position = 0;
			if (!edge) this.moves[0].addClass('move_end');
		}
		else if (this.position <= this.max) {
			this.position = this.max;
			if (!edge) this.moves[1].addClass('move_end');
		}

		// Check for changing colors to edge colors
		if (edge) {
			if (this.position != 0 && this.position != this.max) {
				this.moves[0].removeClass('move_end');
				this.moves[1].removeClass('move_end');
			}
		}

		this.events.setStyle('margin-left', this.position);
	}
});

/**
 * Menu Class
 * @param {Object} el
 */
var Menu = new Class({
	initialize: function ( el ) {
		this.base = el;

		this.base.getElements('td').each(function( item ) {

			item.getElements('dl').each(function (list) {
				list.fade('hide');
			} );

			item.set('tween', {duration: 'short', link: 'cancel'});

			item.addEvent('mouseenter', function() {
				item.addClass("hover");
				item.getElements('dl').each(function (list) {
					list.fade('in');
				} );
				item.tween('background-color', '#cbdced');

			});
			item.addEvent('mouseleave', function() {
				item.removeClass("hover");
				item.getElements('dl').each(function (list) {
					list.fade('hide');
				} );
				item.tween('background-color', '#fc5');
			});

		});
	}
});

/**
 * Generic rollover object
 */
var Rollover = new Class({
	initialize: function (img) {
		off = img.src;
		name_split = off.split('.');
		name_split[name_split.length-2] += "-over";
		on = name_split.join('.');

		// Preload
		image = new Image();
		image.src = on;

		img.addEvent('mouseenter', function () {
			this.src = on;
		});

		img.addEvent('mouseleave', function () {
			this.src = off;
		});
	}
});

window.addEvent('domready', function() {
	if ($('marquee'))
		m = new Marquee( $('marquee') );

	var menu = new Menu( $('menu') );

	$$('img.rollover').each(function (img) {
		new Rollover(img);
	});
});