$(function(){
	
	if($("#mainCont .innerSec > ul > li").size() > 0){
		
		//Cookieが使用可能かチェック 
		var cookieFlagT = navigator.cookieEnabled;
	 
		//クッキー読み込み
		var fsnT = getCookieT("topiNum");
		//alert(fsn);
		if(fsnT != ""){
			//Cookieの有効期限を延長
			setCookieT("topiNum",parseInt(fsnT, 10),72);
		}
		
		
		var nendoArray = [];
		
		$("#mainCont .innerSec > ul > li").each(function(index, element) {
			var liclass = "";
			var upday = $.trim($(this).find('dt').text()).split('/');
			upday[0] = $.trim(upday[0]);
			var monthdays = upday[0] + ZeroFormat(upday[1], 2) + ZeroFormat(upday[2], 2);
			var daysnum = parseInt(monthdays, 10);
			
			/*
			if($.trim($(this).find('dt').text()) == '2015/3/3'){
				alert(daysnum + ' > ' + parseInt(upday[0] + "0000", 10) + ' && ' + daysnum + ' < ' + parseInt(upday[0] + "0401", 10));
			}
			*/
			
			if( daysnum > parseInt(upday[0] + "0000", 10) &&  daysnum < parseInt(upday[0] + "0401", 10) ){
				liclass = "nendo" + (parseInt(upday[0], 10) - 1);
				nendoArray[index] = parseInt(upday[0], 10) - 1;
			}else{
				liclass = "nendo" + parseInt(upday[0], 10);
				nendoArray[index] = parseInt(upday[0], 10);
			}
			
			
			$(this).addClass(liclass);
	
		});
		
		nendoArray = nendoArray.filter(function (x, i, self) {
            return self.indexOf(x) === i;
        });
		
		nendoArray.sort(function(a, b){ return ( a < b ? 1 : -1); });
		
		//console.log(nendoArray);
		
		//$("#mainCont .innerSec > ul > li:not(.nendo2015)").hide();
		
		var insertCont = "";
		$.each(nendoArray, function(index,val){
			var classes = (index > 0) ? val : val + ' vis';
			insertCont += '<li><a href="javascript:void(0);" class="n' + classes + '">' + nengoFormat(val) + '</a></li>';
		});
		
		insertCont = '<ul id="yearChanger">' + insertCont + '</ul>';
		
		$("#mainCont > .secTrd > h1").after(insertCont);
		
		//初期表示
		$("#mainCont .innerSec > ul > li:not(.nendo" + nendoArray[0] + ")").hide();
		
		//クッキーがある場合
		if(cookieFlagT == true && fsnT != ""){
			$("#yearChanger > li > a.vis").removeClass("vis");
			$('#yearChanger > li:eq(' + fsnT + ') > a').addClass("vis");
			var targetsS = $('#yearChanger > li:eq(' + fsnT + ') > a').attr('class').slice(1, 5);
			$("#mainCont .innerSec > ul > li.nendo" + targetsS).show();
			$("#mainCont .innerSec > ul > li:not(.nendo" + targetsS + ")").hide();
		}
		
		
		//click
		$('body').on('click', "#yearChanger > li > a", null, function(){
			$("#yearChanger > li > a.vis").removeClass("vis");
			
			if (cookieFlagT == true) {
				//Cookieへの書込
				setCookieT("topiNum",$(this).parent('li').index(),72);
			}
			
			$(this).addClass("vis");
			var targets = $(this).attr('class').slice(1, 5);
			$("#mainCont .innerSec > ul > li.nendo" + targets).show();
			$("#mainCont .innerSec > ul > li:not(.nendo" + targets + ")").hide();
		});
		
	}

	
	function getCookieT(c_name){
		var st="";
		var ed="";
		if (document.cookie.length>0){
			st=document.cookie.indexOf(c_name + "=");
			if (st!=-1){ 
				st=st+c_name.length+1;
				ed=document.cookie.indexOf(";",st);
				if (ed==-1) ed=document.cookie.length;
				return unescape(document.cookie.substring(st,ed));
			} 
		}
		return "";
	}
	
	function setCookieT(c_name,value,expireTime){
		var kigen = new Date();
		kigen.setTime(kigen.getTime() + (expireTime * 1000 * 60 * 60));
		
		var expDay = kigen.toGMTString();    // 期限の日時をGMTに変換
		
		// クッキーに保存する文字列を生成
		var s="";
		s+=c_name+"="+escape(value);
		s+=(expireTime==null)?"":"; expires="+expDay;
		s+="; domain=" + escape(location.hostname) + "; path=" + escape("/");
		// クッキーに保存
		document.cookie=s;
	}
	
	function ZeroFormat(num, n){// ZeroFormat(1,3) => "001"
		var ret=""+num;
		while(ret.length < n){
			ret = "0" + ret;
		}
		 return (ret);
	}
	
	function nengoFormat(year){
		return year + '年度';
		/*
		var nengo = ['平成','新年号'];
		var nengoStart = [1989,2100];
		var baseNengoStart;
		var setNengo;
		if(year >= nengoStart[0] && year < nengoStart[1]){
			baseNengoStart = nengoStart[0]; 
			setNengo = nengo[0];
		}else{
			baseNengoStart = nengoStart[1];
			setNengo = nengo[1];
		}
		return (year === baseNengoStart) ? setNengo + "元年度" : setNengo + (year - baseNengoStart + 1) + '年度';
		*/
	}
 	
});