(function($){
	$.fn.tabsExt = function(options, func_param){
		var defaults = {
			expandButton: null,
			collapseButton: null,
			interval: 2,
			startVisibleCount: 4,
			duration: null,
			minimumVisible: 2
		};
		
		$(this).data("_tabsExt_param_options", options);
		
		return this.each(function() {
			var objUL = $(this); //first and main UL element
			var options = objUL.data("_tabsExt_param_options"); objUL.data("_tabsExt_param_options", null);
			
			if (objUL.data("_tabsExt_loaded")){ //If navbardrop has already loaded on this element then call the function instead. (Use func_param for any parameters required in the function).
				var settings = objUL.data("_tabsExt_settings");
				var opts = objUL.data("_tabsExt_options");
				var duration = opts.duration;
				
				switch(options){
					case "viewCountJump":
						duration = 0;
					case "viewCount":
						var totalHeight = 0, showCount = func_param * 1, ch = objUL.children("li");
						
						if (showCount > ch.length) showCount = ch.length;
						if (showCount < opts.minimumVisible) showCount = opts.minimumVisible;
						if (showCount < 0) showCount = 0;
						if (showCount != opts.minimumVisible) showCount = showCount + (showCount % opts.interval);
						
						if (showCount != settings.currentlyVisible){
							ch.slice(0, showCount).each(function(e){
								totalHeight += $(this).outerHeight();
							});
							settings.currentlyVisible = showCount;
							objUL.animate({height: totalHeight}, duration);
						};
						
						break;
					case "_settings":
						settings = func_param;
						/*
						if (!func_param) return objUL.data("_tabsExt_settings");
						objUL.data("_tabsExt_settings", func_param);
						*/
						
						break;
					default:
						//do nothing.
						break;
				};
			}else{
				options = $.extend(defaults, options);
				var settings = {currentlyVisible: 0};
				
				if (options.expandButton){
					options.expandButton = $(options.expandButton);
					
					options.expandButton.data("_tabsExt", objUL);
					
					options.expandButton.click(function(e){
						var objUL = $(this).data("_tabsExt");
						var options = objUL.data("_tabsExt_options");
						var settings = objUL.data("_tabsExt_settings");
						objUL.tabsExt("viewCount", settings.currentlyVisible + options.interval);
					});
				};
				
				if (options.collapseButton){
					options.collapseButton = $(options.collapseButton);
					
					options.collapseButton.data("_tabsExt", objUL);
					
					options.collapseButton.click(function(e){
						var objUL = $(this).data("_tabsExt");
						var options = objUL.data("_tabsExt_options");
						var settings = objUL.data("_tabsExt_settings");
						objUL.tabsExt("viewCount", settings.currentlyVisible - options.interval);
					});
				};
				
				objUL.data("_tabsExt_options", options);
				objUL.data("_tabsExt_settings", settings);
				
				objUL.data("_tabsExt_loaded", true);
				
				objUL.tabsExt("viewCountJump", options.startVisibleCount); //Select default button.
			};
		});
	};
})(jQuery);

$(function(){
	$("#mostreadtabs").tabs({
		/*select: function(e, ui){
			//var selected = $(ui).tabs("option", "selected");
			
			if (!$(ui.panel).data("_ajax_loaded")){
				alert("AJAX");
				
				$(ui.panel).data("_ajax_loaded", true);
			};
		},*/
		load: function(e, ui){
			var pnl = $(ui.panel);
			pnl.children("ul")
			.tabsExt({expandButton: pnl.children(".extender_images").children(".extender_add"), collapseButton: pnl.children(".extender_images").children(".extender_minus")});
		},
		cache: true
	});
	/*
	.tabs("url", 0, "http://www.newarkadvertiser.co.uk/test/1.htm")
	.tabs("url", 1, "http://www.newarkadvertiser.co.uk/test/1.htm")
	.tabs("url", 2, "http://www.newarkadvertiser.co.uk/test/1.htm");
	*/
});


//This is a quick fix to counter old code...
function CreateExtenders(){};
