(function( $ ){
	$.fn.listtabs = function(o){
		if(typeof(o)!='object') o = {};
		var list = this;
		var idPrefix = o.idPrefix || 'listtabs_';
		
		// initialization + support for multiple lists
		this.init = function(){
			if(list.length>0){
				if(list.length>1){
					var cnt = 0;
					list.each(function(){
						o.idPrefix = idPrefix+cnt+'_';
						$(this).listtabs(o);
						cnt++;
					});
				}
				else{
					list.createTabList();
				}
			}
		}
		
		
		// from here and down only describes one list.
		this.createTabList = function(){
			$('<div id="'+idPrefix+'content_space" class="tab_content_space"></div>').insertAfter(list);
			$(list).attr('id', idPrefix+'list');
			var cnt = 0;
			$(list).find('>li').each(function(){
				$(this).attr('id', idPrefix+'label_'+cnt);
				$(this).find('ul').appendTo('#'+idPrefix+'content_space').attr('id', idPrefix+'tab_'+cnt);
				cnt++;
			});
			$(list).find('>li').eq(0).addClass('first');
			$(list).find('>li').eq($(list).find('>li').length-1).addClass('last');
			list.attachEvents();
			list.show(0);
			
		}
		
		this.attachEvents = function(){
			$(list).find('>li').click(function(){
				list.show($(list).find('>li').index($(this)));
			});
		}
		
		this.show = function(index){
			$(list).find('li').removeClass('active');
			$(list).find('li').eq(index).addClass('active');
			$('#'+idPrefix+'content_space').find('ul').removeClass('active');
			$('#'+idPrefix+'tab_'+index).addClass('active');
		}
		
		
		
		
		
		list.init();
	};
})( jQuery );

$(function(){
	$('.listtabs').listtabs();
});

