var Rype = Rype || {};

Rype.tabs = function(container, startIndex) {
	var root = container || '#tabs';
	var speed = 500; //ms?
	startIndex = startIndex || 0;
	root = $(root);
	root.addClass('js-enabled');
	
	//add click events
	$.each(root.find('ul li a'),function(index) {
		var that = $(this);
		if(index == startIndex) {
			that.parent().addClass('current');
		}
		that.click(function(){
			//set current class
			$(this).parent().addClass('current');
			$.each($(this).parent().siblings(),function() {
				$(this).removeClass('current');
			});
			//show/hide panels
			$.each($(that.attr('href')).fadeIn(speed).siblings(),function(){
				$(this).hide();
			});
			return false;
		});
	});
	
	//hide all but start index
	$.each(root.find('#tab-panes > *'),function(index, el) {
		if(index != startIndex) {
			$(this).hide();
		} 
	});
};

$(document).ready(function(){
   //pix tabs
   //UI tabs require too strict markup, roll own tabs function.
	var tabs = $('#tabs');
	if(tabs.length)
		Rype.tabs(tabs);
	
	var acc = $('#accordion');
	if(acc.length)
		acc.accordion();
});