//####################//
//## カレンダー部分 ##//
//####################//
/* PHPを利用して、

var start_year = <?= $start_year ?>;
var start_month = <?= $start_month ?>;
var year = <?= $year ?>;
var month = <?= $month ?>;
var limit_year = <?= $limit_year ?>;
var limit_month = <?= $limit_month ?>;
var program = "<?= $program ?>";

と、宣言しているものとする */

var CtrlCalendar = {
	'init' : function() {
		var parent = document.getElementById("insertPointForm");
		
		var div = document.createElement("div");
		div.setAttribute("id", "wrapper");
		
		var form = document.createElement("form");
		form.setAttribute("id", "jump");
		form.setAttribute("method", "GET");
		form.setAttribute("action", program);
		
		var textY = document.createTextNode(" 年 ");
		var textM = document.createTextNode(" 月 ");
		
		var input = document.createElement("input");
		input.setAttribute("type", "submit");
		input.setAttribute("value", "GO");
		
		div.appendChild(this.roadYears());
		div.appendChild(textY);
		div.appendChild(this.roadMonths());
		div.appendChild(textM);
		div.appendChild(input);
		
		form.appendChild(div);
		
		parent.appendChild(form);
	},
	
	'roadYears' : function() {
		var select = document.createElement("select");
		select.setAttribute("id", "year")
		select.setAttribute("name", "year");
		select.onchange = function() {
			CtrlCalendar.setMonth();
		}
		
		for(var i = limit_year; i >= start_year; i--) {
			var text = document.createTextNode(i);
			
			var option = document.createElement("option");
			option.setAttribute("value", i);
			if(i == year) option.setAttribute("selected", "selected");
			
			option.appendChild(text);
			select.appendChild(option);
		}
		
		return select;
	},
	
	'roadMonths' : function() {
		var select = document.createElement("select");
		select.setAttribute("id", "month")
		select.setAttribute("name", "month");
		
		var text = document.createTextNode(" 月 ");
		
		if(year == limit_year) {
			if(year != start_year) {
				for (var i = limit_month; i >= 1; i--) {
					var text = document.createTextNode(i);
					
					var option = document.createElement("option");
					option.setAttribute("value", i);
					if(i == month) option.setAttribute("selected", "selected");
					
					option.appendChild(text);
					select.appendChild(option);
				}
			} else {
				for (var i = limit_month; i >= start_month; i--) {
					var text = document.createTextNode(i);
					
					var option = document.createElement("option");
					option.setAttribute("value", i);
					if(i == month) option.setAttribute("selected", "selected");
					
					option.appendChild(text);
					select.appendChild(option);
				}
			}
		} else if(year == start_year) {
			if(year != limit_year) {
				for (var i = 12; i >= start_month; i--) {
					var text = document.createTextNode(i);
					
					var option = document.createElement("option");
					option.setAttribute("value", i);
					if(i == month) option.setAttribute("selected", "selected");
					
					option.appendChild(text);
					select.appendChild(option);
				}
			} else {
				for (var i = limit_month; i >= start_month; i--) {
					var text = document.createTextNode(i);
					
					var option = document.createElement("option");
					option.setAttribute("value", i);
					if(i == month) option.setAttribute("selected", "selected");
					
					option.appendChild(text);
					select.appendChild(option);
				}
			}
		} else {
			for (var i = 12; i >= 1; i--) {
				var text = document.createTextNode(i);
				
				var option = document.createElement("option");
				option.setAttribute("value", i);
				if(i == month) option.setAttribute("selected", "selected");
				
				option.appendChild(text);
				select.appendChild(option);
			}
		}
		
		return select;
	},
	
	'setMonth' : function() {
		var ref = document.getElementById("year");
		var selectedYear = parseInt(ref.options[ref.selectedIndex].value);
		var target = document.getElementById("month");
		var count = 0;
		
		if(selectedYear == limit_year) {
			if(selectedYear != start_year) {
				target.length = limit_month;
				for (var i = limit_month; i >= 1; i--) {
					target.options[count].text = i;
					target.options[count].value = i;
					count++;
				}
			} else {
				target.length = limit_month - start_month + 1;
				for (var i = limit_month; i >= start_month; i--) {
					target.options[count].text = i;
					target.options[count].value = i;
					count++;
				}
			}
		} else if(selectedYear == start_year) {
			if(selectedYear != limit_year) {
				target.length = 12 - start_month + 1;
				for (var i = 12; i >= start_month; i--) {
					target.options[count].text = i;
					target.options[count].value = i;
					count++;
				}
			} else {
				target.length = limit_month - start_month + 1;
				for (var i = limit_month; i >= start_month; i--) {
					target.options[count].text = i;
					target.options[count].value = i;
					count++;
				}
			}
		} else {
			target.length = 12;
			for (var i = 12; i >= 1; i--) {
				target.options[count].text = i;
				target.options[count].value = i;
				count++;
			}
		}
		
		target.options[0].selected = "selected";
	}
}

//####################//
//## イニシャライズ ##//
//####################//
YAHOO.util.Event.onDOMReady(function() {
    if(YAHOO.env.ua.webkit == 0 || YAHOO.env.ua.webkit >= 522) Nifty("#release", "transparent");
    if(YAHOO.env.ua.webkit == 0 || YAHOO.env.ua.webkit >= 522) Nifty("#release dl", "transparent");
});
