/* Tab Swapper */

var DivSwitcher = Class.create();

DivSwitcher.prototype = {
	
	initialize: function(links, items) {
		this.links = $$(links);
		this.items = $$(items);
		if (this.items.length == 0 || this.links.length == 0) { // return false if links and items do not exist 
			return;
		}
		for (var i=0; i < this.links.length; i++) {     // if link gets clicked
			Event.observe(this.links[i], "click", this._linkClick.bindAsEventListener(this));
		}
		
		// if there is a hash value in the URL, open the corresponding tab, otherwise show the first item in the array
		var linkClass = window.location.hash.replace(/.*#+/, '');
		if (linkClass) {
		    var listItemArray = $$("ul#tabs li a."+linkClass);
		    if (listItemArray[0]) {
		        var listItem = listItemArray[0].parentNode;
		        listItem.className = "on";
		        this.hideAll();
		        this.setContent(linkClass);
		    } else {
		        this.setItem(this.links[0]);
		    }
		} else {
		    this.setItem(this.links[0]);
		}
	},
	
	_linkClick: function(e) {
		var e = e || window.event;  // ie check
		
		var cleaned = Event.element(e).innerHTML.stripTags().toLowerCase().replace(/[\s-]+/g,"_").replace(/\W+/g,"");
		urchinTracker (currentPath + '/' + cleaned);
		
		Event.stop(e);  // stop event
		var link = Event.findElement(e, 'a');
		this.setItem(link); // show item             
	},
	
	hideAll: function() {
		for (var i=0; i < this.items.length; i++) { // hide all items
			this.items[i].style.position = "absolute";
			this.items[i].style.top = "-50001px";
		}    
	},          
	
	classOff: function() {
		for (var i=0; i < this.links.length; i++) {
			this.links[i].parentNode.className = " off";    // change link parent item to off
		}   
	},
	
	setItem: function(oLnk) {
		this.classOff();    // turn all links off
		oLnk.parentNode.className = "on";   // change this specific link parent to "on"
		var hrefAttr = oLnk.getAttribute("href").replace(/.*#+/, '');
		this.hideAll();     // hide all items
		this.setContent(hrefAttr);
	},
	
	setContent: function(hrefAttr) {                 
		for (var i=0; i < this.items.length; i++) {
			if (this.items[i].id == hrefAttr) {  // if item id is equal to link href then show item
				this.items[i].style.position = "static";
				break;
			} 
		}  
	}
	
}

Event.observe(window, 'load', function(){
	var tabbedBox = new DivSwitcher("ul#tabs li a", "div.tab_content div");
});

