$.fn.notebookList = function(settings, functions) {

	return this.each(function(){
	
		var notebookUl = $(this);
		
		var notebook_settings =		{	'width':			notebookUl.parent().width(),
										'height':			notebookUl.parent().height(),
										'labelSelector':	'.label',
										'labelSize':		150,
										'autoStyles':		true,
										'autoLiWidth':		true,
										'type':				'top',
										'addDataToLi':		false,
										'event':			'click'
									};
								
		var notebook_functions =	{	'beginBuild':			0,
										'readyBuild':			0,
										
										'clickLabel_before':	0,
										'clickLabel_after':		0,
										
										'checkPage_before':		0,
										'checkPage_after':		0
									};
									
		$.extend(notebook_settings, 	settings);
		$.extend(notebook_functions, 	functions);
							
		var notebook_li = notebookUl.children("li");
									
		var notebook_data = {	'notebook_setting':		notebook_settings,
								'notebook_functions':	notebook_functions,
								
								'notebook_ul':			notebookUl,
								'notebook_pages':		new Array(),
								
								'page_count':			notebook_li.size(),
								'position':				notebookUl.offset(),
								'activePage':			0
							};
		
		var i = -1;
		var active = false;
		var top = notebook_data.position.top;
		var leftPosition = -(Math.round($("body").width()/2) - notebook_data.position.left);
		var left = leftPosition;
		
		var vertical_size = Math.round(notebook_data.notebook_setting.height / notebook_data.page_count);
		var horisontal_size = Math.round(notebook_data.notebook_setting.width / notebook_data.page_count);
		
		if ($.isFunction(notebook_data.notebook_functions.beginBuild)&&notebook_data.notebook_functions.beginBuild(notebook_data)) {
			return notebook_data.notebook_ul;}
		
		notebook_li.each(function(){
			++i;
			var page = $(this)
			notebook_data.notebook_pages[i] = {	'page':		page,
												'label':	page.find(notebook_settings.labelSelector),
												'num':		i,
												'active':	false};
												
			notebook_data.notebook_pages[i].label.insertBefore(notebook_data.notebook_pages[i].page);
			
			if (notebook_settings.autoStyles) {
				page.css({	'width':	notebook_settings.width,
							'height':	notebook_settings.height});
			}
			
			if ((page.hasClass('active'))&&(active == false)) {
				page.css({'display':'block'});
				notebook_data.notebook_pages[i].active = true;
				notebook_data.notebook_pages[i].label.addClass('active');
				active = true;
				notebook_data.activePage = i;}
			else {
				page.css({'display':'none'});}
			
			if (notebook_settings.autoStyles) {
				if (notebook_settings.type == 'right') {
					notebook_data.notebook_pages[i].label.css({	'position':		'absolute',
																'left':			'50%',
																'top':			top,
																'width':		notebook_data.notebook_setting.labelSize,
																//'height':		vertical_size,
																'marginLeft':	leftPosition + notebook_data.notebook_setting.width + 'px'
															});		
					if (notebook_settings.autoLiWidth) {
						notebook_data.notebook_pages[i].label.css({'height':		vertical_size});
						top += vertical_size;}
					else {
						top += notebook_data.notebook_pages[i].label.outerHeight();}
				}else if (notebook_settings.type == 'left') {
					notebook_data.notebook_pages[i].label.css({	'position':		'absolute',
																'left':			'50%',
																'top':			top,
																'width':		notebook_data.notebook_setting.labelSize,
																//'height':		vertical_size,
																'marginLeft':	leftPosition - notebook_data.notebook_setting.labelSize + 'px'});					
					if (notebook_settings.autoLiWidth) {
						notebook_data.notebook_pages[i].label.css({'height':		vertical_size});
						top += vertical_size;}
					else {
						top += notebook_data.notebook_pages[i].label.outerHeight();}
				}else if (notebook_settings.type == 'top') {
					notebook_data.notebook_pages[i].label.css({	'position':		'absolute',
																'left':			'50%',
																'marginLeft':	left,
																'top':			notebook_data.position.top - notebook_data.notebook_setting.labelSize + 'px',
//																'width':		horisontal_size,
																'height':		notebook_data.notebook_setting.labelSize
															});				
					if (notebook_settings.autoLiWidth) {
						notebook_data.notebook_pages[i].label.css({'width':		horisontal_size});
						left += horisontal_size;}
					else {
						left += notebook_data.notebook_pages[i].label.outerWidth();}
				}else if (notebook_settings.type == 'bottom') {
					notebook_data.notebook_pages[i].label.css({	'position':		'absolute',
																'left':			'50%',
																'marginLeft':	left,
																'top':			notebook_data.position.top + notebook_data.notebook_setting.height + 'px',
//																'width':		horisontal_size,
																'height':		notebook_data.notebook_setting.labelSize
															});
					if (notebook_settings.autoLiWidth) {
						notebook_data.notebook_pages[i].label.css({'width':		horisontal_size});
						left += horisontal_size;}
					else {
						left += notebook_data.notebook_pages[i].label.outerWidth();}
				}
						
			}
				
				notebook_data.notebook_pages[i].label.data('pageNum', i);
				notebook_data.notebook_pages[i].label.data('notebook_data', notebook_data);
				notebook_data.notebook_pages[i].label.click(function(){
					var clickedLabel = $(this);
					var notebook_data = clickedLabel.data('notebook_data');
					
					if ($.isFunction(notebook_data.notebook_functions.clickLabel_before)&&notebook_data.notebook_functions.clickLabel_before(notebook_data, clickedLabel)) {
						return notebook_data.notebook_ul;}
					
					if (notebook_data.notebook_setting.event == 'click') {
						if (clickedLabel.hasClass('active') == false) {
							var pageNum = clickedLabel.data('pageNum');
							
							notebook_data.notebook_pages[notebook_data.activePage].page.css({'display':'none'}).removeClass('active');
							notebook_data.notebook_pages[notebook_data.activePage].label.removeClass('active');
							notebook_data.notebook_pages[pageNum].page.css({'display':'block'}).addClass('active');
							notebook_data.notebook_pages[pageNum].label.addClass('active');
							
							notebook_data.activePage = pageNum;
						}
					}
					
					if ($.isFunction(notebook_data.notebook_functions.clickLabel_after)&&notebook_data.notebook_functions.clickLabel_after(notebook_data, clickedLabel)) {
						return notebook_data.notebook_ul;}
						
					if (notebook_data.notebook_setting.event == 'click') {
						return false;}
				});
				
				notebook_data.notebook_pages[i].label.hover(function(){
					var clickedLabel = $(this);
					var notebook_data = clickedLabel.data('notebook_data');
					
					if ($.isFunction(notebook_data.notebook_functions.mouseoverLabel_before)&&notebook_data.notebook_functions.mouseoverLabel_before(notebook_data, clickedLabel)) {
						return notebook_data.notebook_ul;}
					
					if (notebook_data.notebook_setting.event == 'hover') {
						if (clickedLabel.hasClass('active') == false) {
							var pageNum = clickedLabel.data('pageNum');
							
							notebook_data.notebook_pages[notebook_data.activePage].page.css({'display':'none'}).removeClass('active');
							notebook_data.notebook_pages[notebook_data.activePage].label.removeClass('active');
							notebook_data.notebook_pages[pageNum].page.css({'display':'block'}).addClass('active');
							notebook_data.notebook_pages[pageNum].label.addClass('active');
							
							notebook_data.activePage = pageNum;
						}
					}
					
					if ($.isFunction(notebook_data.notebook_functions.mouseoverLabel_after)&&notebook_data.notebook_functions.mouseoverLabel_after(notebook_data, clickedLabel)) {
						return notebook_data.notebook_ul;}
				}, function(){
					var clickedLabel = $(this);
					var notebook_data = clickedLabel.data('notebook_data');
					
					if ($.isFunction(notebook_data.notebook_functions.mouseoutLabel_before)&&notebook_data.notebook_functions.mouseoutLabel_before(notebook_data, clickedLabel)) {
						return notebook_data.notebook_ul;}
					
					if ($.isFunction(notebook_data.notebook_functions.mouseoutLabel_after)&&notebook_data.notebook_functions.mouseoutLabel_after(notebook_data, clickedLabel)) {
						return notebook_data.notebook_ul;}
				});
			
		});
		
		if (active == false) {
			notebook_data.notebook_pages[0].active = true;
			notebook_data.notebook_pages[0].page.css({'display':'block'});
			notebook_data.notebook_pages[0].label.addClass('active');
		}
		
		notebook_data.page_count = i;	
		
		if ($.isFunction(notebook_data.notebook_functions.readyBuild)&&notebook_data.notebook_functions.readyBuild(notebook_data)) {
			return notebook_data.notebook_ul;}
			
	});
	
};
