if(typeof jQuery != 'undefined') {
	jQuery(function($) {
		$.fn.extend({
			tabcontrol: function(settings) {
				var settings = $.extend({}, $.fn.tabcontrol.defaults, settings);
				
				if (settings.mainxml) {
					$.ajax({
						url: settings.mainxml,
						type:"get",
						dataType:"xml",
						success:function(data){
							 mainloadComplete(data);
					    }
					})
				}
				
				function mainloadComplete(data){
					$('.tab-box div').remove();
					$("item", data).each(function(){
						thumbnailSitetopurl = "./news/" + $('thumbnail', this).text();
						var linkTxt = $('linkurl', this).text().substr(0, 4);
						if (linkTxt == "http") {
							$('.tab-box').append("<div class=" + $('category', this).text() + "><a href=" + $('linkurl', this).text() + " target='_blank'><div class='newsthumb'><img class='newsthumbimg' src= "+ thumbnailSitetopurl + " /></div><div class='newsdate'>" + $('date', this).text() + "</div><div class='newstext'><h4>" + $('title', this).text() + "</h4><p>" + $('content', this).text() + "</p></div></a></div>");
						}
						else{
							$('.tab-box').append("<div class=" + $('category', this).text() + "><a href=" + $('linkurl', this).text() + " target=''><div class='newsthumb'><img class='newsthumbimg' src= "+ thumbnailSitetopurl + " /></div><div class='newsdate'>" + $('date', this).text() + "</div><div class='newstext'><h4>" + $('title', this).text() + "</h4><p>" + $('content', this).text() + "</p></div></a></div>");
						}
					})
					
					if (settings.subxml) {
						$.ajax({
							url: settings.subxml,
							type:"get",
							dataType:"xml",
							success:function(data){
								 newsloadComplete(data);
						    }
						})
					}
					else{
						newsloadComplete(null);
					}
				}
				
				function newsloadComplete(data){
					
						$("item", data).each(function(){
							thumbnailSitetopurl = "./news/" + $('thumbnail', this).text(); //トップページ用サムネイルURLに置き換え
							
							var linkTxt = $('linkurl', this).text().substr(0, 4);
							if (linkTxt == "http") {
								$('.tab-box').append("<div class=" + $('category', this).text() + "><a href=" + $('linkurl', this).text() + " target='_blank'><div class='newsthumb'><img class='newsthumbimg' src= "+ thumbnailSitetopurl + " /></div><div class='newsdate'>" + $('date', this).text() + "</div><div class='newstext'><h4>" + $('title', this).text() + "</h4><p>" + $('content', this).text() + "</p></div></a></div>");
							}
							else{
								$('.tab-box').append("<div class=" + $('category', this).text() + "><a href=" + $('linkurl', this).text() + " target=''><div class='newsthumb'><img class='newsthumbimg' src= "+ thumbnailSitetopurl + " /></div><div class='newsdate'>" + $('date', this).text() + "</div><div class='newstext'><h4>" + $('title', this).text() + "</h4><p>" + $('content', this).text() + "</p></div></a></div>");
							}
						})

					    var tabContainer = $('div.tab-box ul');
						var tabidList = [];

						$('div.tab-box ul.nav li').each(function(){
							tabidList.push($(this).attr('id'))
						})

						var tabContainer = $('div.tab-box div');

						$('div.tab-box ul.nav li').click(function () {

							var filter = $(this).attr('id');
							tabContainer.hide();
							
							var displaycount = 0;
							
							if(filter == "tab_all"){
								$('div[class^="tab_item"]').each(function(){
									if (displaycount < settings.displaymaxcount) {
										$(this).show();
										displaycount ++;
									};
								})
								//var tabitems = tabContainer.filter(".tab_item01, .tab_item02, .tab_item03");
								//tabitems.filter(":lt(" + settings.displaymaxcount + ")").show();
							}
							else if(filter == "tab_item0102"){
								// var tabitems = tabContainer.filter(".tab_item01, .tab_item02");
								// tabitems.filter(":lt(" + settings.displaymaxcount + ")").show();
								$('div[class*="tab_item01"], div[class*="tab_item02"]').each(function(){
									if (displaycount < settings.displaymaxcount) {
										$(this).show();
										displaycount ++;
									};
								})
							}
							else {
								//tabContainer.filter('.' + filter + ':lt(' + settings.displaymaxcount + ')').show();
								$("div[class*=" + filter + "]").each(function(){
									if (displaycount < settings.displaymaxcount) {
										$(this).show();
										displaycount ++;
									};
								})
							}

							// $("div.tab-box .clr").show();
							$("div.tab-box .newsthumb").show();
							$("div.tab-box .newsdate").show();
							$("div.tab-box .newstext").show();
							$("div.tab-box .tab_item_container").show();

							$('div.tab-box ul.nav li').removeClass('selected');
						 	$(this).addClass('selected');

							return false;
						})

						.filter(':first').click();
				}
			}
		});
		$.fn.tabcontrol.defaults = {
			mainxml: "",
			subxml: "",
			displaymaxcount: 1000
		};
	});
}
