// Anchor Fix
var AnchorFix = Class.create();
AnchorFix.prototype = {
	AnchorLinks: [],

	initialize: function(triggers, items, swapper) {
		this.parent = triggers[0].up();

		this.items = items;
		this.swapper = swapper;
		
		this.getAnchorLinks();
		this.fixItemIds();
	},

	getAnchorLinks: function() {
		// get every link on the page and see if it's an anchor link
		var allLinks = $$('#main a');
		for (var i=0; i<allLinks.length; i++) {
			var link = allLinks[i];
			if (link.href.match(/#/)) {
				this.fixAnchorLink(link);
			}
		}
	},

	doesContentExist: function(id) {
		// if the javascript can find the id on the page
		var status = false;
		for (var i=0; i<this.items.length; i++) {
			if (id == this.items[i].id || id == this.items[i].originalId) {
				status = {index:i};
			}
		}
		return status;
	},

	fixAnchorLink: function(link) {
		var id = link.href.match(/#(.*)/)[1];
		
		// if we already don't have the event
	 	if (link.up().up() != this.parent) {

	 		// if we have something to switch to
			var doesContentExist = this.doesContentExist(id);
			if (doesContentExist) {

				// add the swap effect
				var selectorIndex = doesContentExist.index;
				Event.observe(link, 'click', this.swapper.swapContent.bindAsEventListener(this.swapper, selectorIndex), false);
			}
		}
	},

	fixItemIds: function() {
		for (var i=0; i<this.items.length; i++) {
			this.items[i].originalId = this.items[i].id;
			this.items[i].id += "-AnchorFix";
		}
	}
}

