/**
 * Frontpage Text-Rotate
 */
window.addEvent('domready', function() {
	if($('frontMain')) {
		var timeoutId = 0;
		var delay = 8500;

		var dts = $('frontMain').getElements('dt');
		var index = 0;

		var rotateNext = function() {
			if(index < dts.length) {
				dts[index++].fireEvent('mouseenter');
			}

			if(index == dts.length) {
				index = 0;
			}

			timeoutId = setTimeout(arguments.callee, delay);
		};

		$('frontMain').getElements('dt').addEvents({
			'mouseenter':function() {
				timeoutId && clearTimeout(timeoutId);
				this.getParent().getElements('.active').removeClass('active');
				this.addClass('active');
				this.getNext().addClass('active');
			},
			'mouseleave':function() {
				// find index of current
				for(var i = 0; i < dts.length; i++) {
					if(dts[i].hasClass('active')) {
						index = i;
					}
				}

				index++;
				if(index == dts.length) {
					index = 0;
				}

				timeoutId = setTimeout(rotateNext, delay/3);
			}
		});

		rotateNext();
	}
});

/**
 * Menu
 */
window.addEvent('domready', function() {
	if($$('ul.menu')) {
		var delay = 200;
		var timeoutId = 0;

		$$('ul.menu li.parent').addEvents({
			'mouseenter':function() {
				var that = this;

				if(timeoutId) {
					clearTimeout(timeoutId);
					timeoutId = 0;
				}

				timeoutId = setTimeout(function() {
					that.addClass('hover');
					that.getElement('a').addClass('hover');
					/*that.getElement('ul').setStyles({
						//'left':that.getLeft() - that.getParent().getLeft() - 3,
						'left':-3,
						//'top':that.getTop() - that.getParent().getTop() + 22
						'top':22
					});*/
				}, delay);
			},
			'mouseleave':function() {
				var that = this;
				if(timeoutId) {
					clearTimeout(timeoutId);
					timeoutId = 0;
				}

				timeoutId = setTimeout(function() {
					that.removeClass('hover');
					that.getElement('a').removeClass('hover');
					/*that.getElement('ul').setStyles({
						'left':-3000
					});*/

				}, delay);
			}
		});
	}
});

/**
 * Prototype
 */
window.addEvent('domready', function() {
	if($('prototype')) {
		$('prototype').getElements('dt').addEvent('click', function() {
			if(this.hasClass('active')) {
				// do nothing
			}
			else {
				$('prototype').getElement('dt.active').removeClass('active');
				$('prototype').getElement('dd.active').removeClass('active');
				this.addClass('active');
				this.getNext().addClass('active');
			}
		});
	}
});

/**
 * Video
 */
window.addEvent('domready', function() {
	if($('video') && $('video').getElement('a')) {
		$('video').getElement('a').setProperty('id', 'player');
		flowplayer('player', {
			src:'/templates/tour_engine/flash/flowplayer-3.1.5.swf',
			wmode:'opaque'
		}, {
			clip:{
				autoPlay:false,
				autoBuffering:true
			}
		});
	}
});

