// Global functions for Ricoh Innovations
// Requires Mootools 1.2

window.addEvent('domready', function() {
	// Set up the whole-div linking for badges
	$$('.badge').each(function(el){
		var arrow_link = el.getElement('a.arrow');
		el.addEvents({
			'click': function() {
				location.href = arrow_link.get('href');
			},
			'mouseenter': function() {
				arrow_link.setStyle('background-image', 'url(/images/common/arrow-active.png)');
			},
			'mouseleave': function() {
				arrow_link.setStyle('background-image', 'url(/images/common/arrow.png)');
			}
		});
		el.setStyle('cursor', 'pointer');
	});
	// Set up whole-div linking for iCandy on the homepage
	if ($('labs')) {
		var linked_div = $('labs');
		var lab_arrow_link = $('labs').getElement('a.arrow');
		linked_div.addEvents({
			'click': function() {
				location.href = lab_arrow_link.get('href');
			},
			'mouseenter': function() {
				lab_arrow_link.setStyle('background-image', 'url(/images/common/arrow-active.png)');
			},
			'mouseleave': function() {
				lab_arrow_link.setStyle('background-image', 'url(/images/common/arrow.png)');
			}
		});
		linked_div.setStyle('cursor', 'pointer');
	}
	// Set up the hover text replacement for "post to ..." text
	if ($('post-controls')) {
		$$('#post-controls li a').each(function(el) {
			var hoverText = el.get('title');
			el.addEvents({
				'mouseenter': function() {
					changePostText(hoverText);
				},
				'mouseleave': function() {
					changePostText("...");
				}
			});
		});
	}
});

// getSize() has to be placed in onload for Safari compatibility
window.addEvent('load', function() {
	// Set up the top navigation animations
	if ($('top-nav')) {
		// We have to override some of the default CSS
		var nav = $('top-nav');
		nav.setStyles({
			'padding-bottom': '7px',
			'position': 'relative',
			'overflow': 'hidden'
		});
		$$('#top-nav a').setStyle('border-bottom', 'none');
		$$('#top-nav li.active a').setStyle('border-bottom', 'none');
		var bar = new Element('div', {'class': 'select-bar', 'html': '&nbsp;'});
		bar.inject(nav, 'bottom');
		var bar_slide = new Fx.Morph(bar, {'duration': 500, 'transition': Fx.Transitions.Sine.easeOut, 'link': 'cancel'});
		var active = nav.getElement('.active');
		var nav_width = nav.getSize().x;
		if (active) {
			var active_coords = active.getCoordinates(nav);
			bar.setStyles({
				'left': active_coords.left + 'px',
				'width': active_coords.width + 'px'
			});
		}
		var reset_coords = (active ? active_coords : {'left': -150, 'width': 150});
		$$('#top-nav li').addEvents({
			'mouseenter': function() {
				if (!this.hasClass('active')) {
					var hover_coords = this.getCoordinates(nav);
					this.setStyle('background-image', 'url(/images/common/nav-active.png)');
					bar_slide.start({
						'left': hover_coords.left + 'px',
						'width': hover_coords.width + 'px'
					});
				}
			},
			'mouseleave': function() {
				if (!this.hasClass('active')) {
					this.setStyle('background-image', 'url(/images/common/nav.png)');
					bar_slide.start({
						'left': (bar.getStyle('left').toInt() > (nav_width / 2) && !active ? nav_width : reset_coords.left) + 'px',
						'width': reset_coords.width + 'px'
					});
				}
			}
		});
	}


	// Setup the scroller, if we have one
	if ($('scroller')) {
		$('scroller').setStyles({
			'overflow': 'hidden',
			'position': 'relative'
		});
		var scrollbody = $('scroller').getElement('div.body');
		scrollbody.setStyles({
			'position': 'absolute',
			'top': '0px',
			'left': '0px'
		});
		var scroll_height = scrollbody.getSize().y;
		var visible_height = $('scroller').getSize().y;
		
		if (scroll_height > visible_height) {
			// It's too tall, so we have to scroll it
			// Add the duplicate element for circular scrolling
			var scrollalt = new Element('div', {
				'class': 'body-alt',
				'html': scrollbody.get('html'),
				'styles': {
					'position': 'absolute',
					'top': scroll_height + 'px',
					'left': '0px'
				}
			});
			var alt_top = false;
			scrollalt.inject('scroller', 'bottom');
			// Start the scroll
			var default_scroll = -5;
			var scroll_step = default_scroll; // Default vertical scroll
			var scrollbody_fx = new Fx.Tween(scrollbody, {'link': 'cancel', 'duration': 500, 'transition': Fx.Transitions.linear});
			var scrollalt_fx = new Fx.Tween(scrollalt, {'link': 'cancel', 'duration': 500, 'transition': Fx.Transitions.linear});
			var reset_scroll = function() {
				scroll_step = default_scroll;
			};
			var reset_scroll_fn = '';
			var run_scroll = function() {
				if (alt_top) {
					var top_el = scrollalt;
					var bottom_el = scrollbody;
				} else {
					var top_el = scrollbody;
					var bottom_el = scrollalt;
				}
				if (scroll_step < 0 && top_el.getStyle('top').toInt() < -scroll_height) {
					top_el.setStyle('top', (scroll_height + bottom_el.getStyle('top').toInt()) + 'px');
					alt_top = (alt_top ? false : true);
				} else if (scroll_step > 0 && top_el.getStyle('top').toInt() > 0) {
					bottom_el.setStyle('top', (-scroll_height + top_el.getStyle('top').toInt()) + 'px');
					alt_top = (alt_top ? false : true);
				}
				scrollbody_fx.start('top', (scrollbody.getStyle('top').toInt() + scroll_step) + 'px');
				scrollalt_fx.start('top', (scrollalt.getStyle('top').toInt() + scroll_step) + 'px');
			};
			run_scroll.periodical(250);
			// Set up the mouse events
			var vertical_offset = $('scroller').getPosition().y;
			$('scroller').addEvents({
				'mouseenter': function() {
					reset_scroll_fn = $clear(reset_scroll_fn);
					$('scroller').addEvent('mousemove', function(e) {
						var mouse_y = e.page.y - vertical_offset;
						if (mouse_y < visible_height / 3) {
							// Upper third; scroll down
							scroll_step = ((visible_height / 3) - mouse_y).toInt();
						} else if (mouse_y < (visible_height * 2/3)) {
							// Middle third, stop scrolling
							scroll_step = 0;
						} else {
							// Bottom third, scroll up
							scroll_step = -(mouse_y - (visible_height * 2 / 3).toInt());
						}
					});
				},
				'mouseleave': function() {
					$('scroller').removeEvents('mousemove');
					reset_scroll_fn = reset_scroll.delay(1000);
				}
			});
		}
	}
});