var Tabs = Fx.Elements.extend({

	getExtended: function(){
		return {
			wait: false
		};
	},

	initialize: function(togglers, elements, options){
		this.setOptions(this.getExtended(), options);
		this.previous = -1;
		this.togglers = $$(togglers);
		this.elements = $$(elements);
		this.togglers.each(function(tog, i){
			tog.addEvent('click', this.display.bind(this, i));
		}, this);
		this.elements.each(function(el, i){
			el.setProperty('id','');
		}, this);
		this.parent(this.elements, this.options);
	},

	display: function(index){
		if (index === this.previous) return this;
		this.previous = index;
		var obj = {};
		this.elements.each(function(el, i){
			obj[i] = {};
			if ((i != index) || (el.getStyle('display') == 'block')){
				this.togglers[i].removeClass('selected');
				el.setStyle('display','none');
			} else {
				this.togglers[i].addClass('selected');
				this.togglers[i].blur();
				el.setStyle('display','block');
			}
		}, this);
		return this.start(obj);
	},

	showThisHideOpen: function(index){return this.display(index)}

});


window.onload = function(){ //safari cannot get style if window isnt fully loaded
	
	var togglers = $$('#collection-nav li');
	var containers = $$('div.collection-item');
	var titles = $$('.iframe-title');
	if (titles) { 
		titles.each(function(title) {
				title.setStyle('display','none');
		});
	
	}
	containers.each(function(item){
		item.setStyles({'display': 'none'});
	});
	
	var subNav = new Tabs( togglers, containers);
	
		
	//anchors
	function checkHash(){
		var found = false;
		$$('#collection-nav a').each(function(link, i){
			if (window.location.hash.test(link.hash)){
				subNav.showThisHideOpen(i);
				found = true;
			}
		});
		return found;
	};

	if (!checkHash()) subNav.showThisHideOpen(0);
	
};
