
var pagenav, squared;
window.addEvent('load', function() {
	// create page access bar
	pagenav = new PageAccess('site_access');
	// for smooth scrolling of page links.
	new Fx.SmoothScroll();

	// set default effect for search input
	if (document.id('q')) {
		document.id('q').addEvents({
			'focus': function() { if (this.value == '' || this.value == this.defaultValue) { this.value = ''; } },
			'blur': function() { if (this.value == '') { this.value = this.defaultValue; } }
		});
	}

	// set all rel=external links to open in new window
	$$('a[rel=external]').set('target', '_blank');

	// init social animations if present
	if (document.id('social_box')) { socialCritters(); }
	
	var footerColumns = $$('.foot-container .column');
	if (footerColumns) {
		equalize_columns(footerColumns);
	}
});


// animation for social network links in sidebar
function socialCritters() {
	$$('a.global-social').each(function(_el) {
		var bpX = $(_el).getStyle('background-position').split(" ")[0];

		_el.set('morph', {'duration':200,'link':'cancel'})
		.store('bpx', bpX)
		.addEvents({
			'mouseenter': function() {
				this.morph({'opacity':1, 'background-position': this.retrieve('bpx') + ' 8px'});
				$each(this.getSiblings('a'), function(_elem) {
					_elem.morph({'opacity':0.3, 'background-position': _elem.retrieve('bpx') + ' 13px'});
				});
			},
			'mouseleave': function() {
				this.morph({'opacity':1, 'background-position': this.retrieve('bpx') + ' 13px'});
				$each(this.getSiblings('a'), function(_elem) {
					_elem.morph({'opacity':1, 'background-position': _elem.retrieve('bpx') + ' 13px'});
				});
			}
		});
	});
}


var PageAccess = new Class({
	Implements: [Options, Events],
	options: {
		'elem': null,
		'cloneElem': true,
		'cloneID': 'accessClone'
	},

	initialize: function(_elem, _opts) {
		this.setOptions((_opts || {}));

		this.elem = (this.options.cloneElem === true ? $(_elem).clone() : $(_elem));
		this.coords = $(_elem).getCoordinates();
		this.pageY = $(document.body).getScroll().y;

		this.elem.set({
			'id': this.options.cloneID,
			'focused': 0,
			'tween': {'duration':200}
		})
		.setStyle('bottom', '-' + (this.coords.height + 6) + 'px')
		.addEvents({
			'mouseenter': this.elemMouseEnter.bind(this),
			'mouseleave': this.elemMouseLeave.bind(this)	
		}).inject(document.body);

		window.addEvents({
			'mousemove': this.trackPageMouse.bind(this),
			'scroll': this.trackPageScroll.bind(this)
		});
	},

	elemMouseEnter: function() {
		this.elem.set('focused', 1);
	},
	elemMouseLeave: function() {
		var self = this;
		this.elem.set('focused', 0);
		(function() {
			if (self.elem.get('focused') == 0) { 
				self.elem.tween('bottom', '-' + (self.coords.height + 6) + 'px'); 
			}
		}).delay(500);	
	},

	trackPageScroll: function() {
		this.pageY = $(document.body).getScroll().y;
		if (this.pageY <= (this.coords.bottom) && this.elem.get('focused') == 1) {
			this.elem.fireEvent('mouseleave');
		}
	},

	trackPageMouse: function(_e) {
		var evt = new Event(_e), hasTimer = !!(this.elem.get('tween').timer), h = (window.clientHeight || window.innerHeight);
		if (!hasTimer && this.pageY > this.coords.bottom) {
			if (evt.client.y >= (h - this.coords.height) && this.elem.get('focused') == 0) {
				this.elem.set('focused', 1);
				this.elem.tween('bottom', 0);
			} else if (evt.client.y < (h - this.coords.height) && this.elem.get('focused') == 1) { 
				if (this.elem.getStyle('bottom').toInt() === 0) { this.elem.fireEvent('mouseleave'); }
			}
		}
	}
});


//given the selector, sets the height of all matching elements to the 
//height of the tallest element
function equalize_columns(_selector) {
	var tallest = 0, elems = (typeof _selector == 'string' ? $$(_selector) : _selector);
	elems.each(function(_el, _idx) {
		var y = _el.getSize().y;
		if (y > tallest) { tallest = y; }
	});
	elems.setStyle('height', tallest + 'px');
}

//swaps class _remove for _add
Element.implement({
	swapClass: function(remove, add) {
		this.removeClass(remove).addClass(add);
	},
	getSiblings: function(match, nocache) {
		return this.getParent().getChildren(match, nocache).erase(this);
	}
});

