var ISOSiteIndexHelper = Class.create( {

	/**
	 * define the link anchor pattern
	 */
	regexExp : /#(\.?\w.+)/,

	/**
	 * @param containerId -
	 *            the id of the element
	 * @param opts -
	 *            options
	 * @return
	 */
	initialize : function(containerId, opts) {


		var container = this.element = $(containerId);

		this.menu = this.element.select('a'); // get the list of links('<a>')
		// within the container

		this.anchors = this.menu.map(function(e) {
			return e.href.match(/#(\w.+)/) ? RegExp.$1 : null;
		}); // get all the anchors

		this.options = Object.extend( {
			anchorPolicy : 'disable' // allow is also an option

		}, opts || {});

		// CSS names used for the effects
		this.cssNames = Object.extend( {
			onTitle : 'actived-index-link',
			onBody : 'actived-index-body'
		});

		// load the default block
		this.activate(this.getInitBlock());

		this.element.observe('click', this.trigger.bindAsEventListener(this));

		var triggers = [];
		this.anchors.each(function(id) {
			$$('a[href="#' + id + '"]').reject(function(elm) {
				return elm.descendantOf(parent);
			}).each(function(trig) {
				triggers.push(trig);
			});
		});


	},

	/**
	 * 
	 * find the default block that gets activated when the page gets loaded
	 * 
	 */
	getInitBlock : function() {

		if (this.options.anchorPolicy !== 'disable'
				&& document.location.hash) {
			var hash = document.location.hash.substring(1);

			return this.element.select('a[href="#' + hash + '"]')[0];
		} else {
			return this.menu[0];
		}
	},

	getId : function(elm) {
		return elm.href.match(this.regexExp)[1];
	},

	activate : function(elm) {
		if (typeof elm == 'string') {
			elm = this.element.select('a[href="#' + elm + '"]')[0];
		}
		this.turnOn(elm);
		this.menu.without(elm).each(this.turnOff.bind(this));
	},

	turnOn : function(elm) {
		$(elm).addClassName(this.cssNames.onTitle);
		$(this.getId(elm)).addClassName(this.cssNames.onBody);
	},

	turnOff : function(elm) {
		$(elm).removeClassName(this.cssNames.onTitle);
		$(this.getId(elm)).removeClassName(this.cssNames.onBody);
	},

	trigger : function(event) {
		if (this.options.anchorPolicy !== 'allow') {
			event.stop();
		}

		var elm = event.findElement("a");
		this.activate(elm);

	}

});

var hash = location.hash;

document.observe("dom:loaded", function() {
	new ISOSiteIndexHelper('indexLink');

});

/*
 * setInterval(function() { if (location.hash != hash) { //alert("Changed from " +
 * hash + " to " + location.hash); hash = location.hash; new
 * ISOSiteIndexHelper('tabs'); } }, 100);
 */
