window.addEvent('domready', function() {
	$$('a[rel=external]').set('target', '_blank');

	new Tips('.social', {
		'fixed':true,
		'text':'',
		'offset':{'x':0,'y':34},
		'hideDelay': 0,
		'onShow': function(tip, el) { tip.fade('in'); },
		'onHide': function(tip, el) { tip.fade('out'); }
	});

	if ($('pfocus_content')) {
		pagefocus = new PageFocus({
			'links_elem': 'focus_linkarea',
			'descr_elem': 'focus_descarea',
			'display_elem': 'focus_displayarea'
		});
	}
});


var PageFocus = new Class({
	'Implements': [Options,Events],
	'options': {
		'links_elem': null,
		'descr_elem': null,
		'display_elem': null,
		'ulClass': 'focus-ul'
	},
	'position': 0,
	'elements': null,
	
	initialize: function(_options) {
		this.setOptions(_options);

		this.elements = {
			'links': $$('#' + this.options.links_elem + ' > li'),
			'descr': $$('#' + this.options.descr_elem + ' > li'),
			'displ': $$('#' + this.options.display_elem + ' > li')
		};

		this.build();
		this.flip(0, true);
	},

	build: function() {
		$$('.' + this.options.ulClass + ' li').each(function(_el) {
			_el.set('opacity', 0).setStyle('display', 'block');
		});

		this.elements.links.each(function(_el, _idx) {
			_el.addEvent('click', this.flip.bind(this, _idx));
		}, this);
	},
	
	flip: function(_dir, _force) {
		var flipTo = null, pos = this.position;

		if ($type(_dir) == 'number') {
			flipTo = (_dir > -1 && _dir < this.elements.links.length) ? _dir : pos;
		}
		else if ($type(_dir) == 'boolean') {
			flipTo = pos + (_dir ? 1 : -1);
			flipTo = (flipTo < 0 ? 0 : (flipTo > this.elements.links.length ? this.elements.links.length : pos));
		}

		if (flipTo != pos || _force) {
			if (flipTo != pos) {
				this.elements.links[pos].removeClass('selected');
				this.elements.descr[pos].fade('out');
				this.elements.displ[pos].fade('out');
			}

			this.elements.links[flipTo].addClass('selected');
			this.elements.descr[flipTo].fade('in');
			this.elements.displ[flipTo].fade('in');
		}

		this.position = flipTo;
	}
});

