var AVCommon = new Class({
	initialize: function() {
		this.setHashClass();
		this.hijackLinks();
	},
	hijackLinks: function() {
		$$('a').filter(this.productLinksFilter).each(this.addHashToLink);
		
		$$('a').each(function(link) {
			if(link.href.substr(link.href.length-1, 1) == '#') link.addEvent('click', function() {
				return false;
			});
		});
		
	},
	
	addHashToLink: function(link) {
		var new_class = $(document.body).className.replace('home', '').replace('product', '').replace(' ', '');
		
		if(link.href.indexOf("#") > -1) {
			link.href = link.href.substr(0, link.href.lastIndexOf("#"));
		}
		
		link.href = link.href + "#" + new_class;
	},
	
	productLinksFilter: function(link) {
		return (link.href.indexOf("/products/") > -1);
	},
	
	setHashClass: function() {
		if(location.hash) {
			var hash = location.hash.substr(1);
			$(document.body).addClass(hash);
		}
	}
});

var av_common;
document.addEvent('domready', function() {
	av_common = new AVCommon();
});
