
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 ($('q')) {
		$('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 ($('social_box')) { socialCritters(); }
	
	equalize_columns('.foot-container .column');

	if (document.id('sprinklepenny')) {
		var publisherId = 500;
		var element = document.id('sprinklepenny');
		var jsnp = new Request.JSONP({
			callbackKey: 'jsoncallback',
			url: 'http://www.sprinklepenny.com/visits/add',
			data: {
				publisher: publisherId,
				articleTitle: encodeURIComponent(document.title)
			},
			onComplete: function(data) {
				var fx = new Fx.Tween(element, {
					'property': 'opacity',
					'link':'chain'
				})
				.start(0)
				.chain(function() { element.set('html', data.link); fx.start(1); });
			}
		}).send();
	}
});


// 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,
		'cloneID': 'accessClone'
	},

	initialize: function(_elem, _opts) {
		this.setOptions((_opts || {}));

		this.elem = $(_elem).clone();
		this.coords = $(_elem).getCoordinates();
		this.eXy = this.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 = $$(_selector);
	elems.each(function(_el, _idx) {
		tallest = (_el.getSize().y > tallest ? _el.getSize().y : tallest);
	});
	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);
	}
});
