/* ------ JLJS_Selector ----- */

function JLJS_Selector (selectNode){
	this.setNode(selectNode);
}

JLJS_Selector.prototype = {
	setNode : function(node){
		if (typeof node == 'object' && node.nodeName && node.nodeName.match(/^select$/i)){
			this.node = node;
		}
	},

	adjust : function(target, arg){
		if (this.node && typeof target == 'string' && arg){
			this.node.options[0].selected = true;
			for (var i = 0; i < this.node.options.length; i++) {
				var value = this.node.options[i][target];
				if (value && (typeof arg == 'number' && parseInt(value, 10) == arg || typeof arg == 'string' && value == arg)){
					this.node.options[i].selected = true;
					break;
				}
			}
		}
	},
	
	adjustByLabel : function(arg){
		this.adjust('text', arg);
	},

	adjustByValue : function(arg){
		this.adjust('value', arg);
	}
}

/* ------ JLJS_DateSelector ----- */

function JLJS_DateSelector() {

	this.Date = new Date();
	this.today = new Date();
	this.days = new Array(7);
	this.dayOfWeekFlag = true;
	
	this.end = new Date();
	this.endDay = null;
	
	this.zeroPadFlg = true;
	
	this.calendarLocalConfig;
	
	if(arguments.length == 3){
		this.year   = new JLJS_Selector(arguments[0]);
		this.month  = new JLJS_Selector(arguments[1]);
		this.day    = new JLJS_Selector(arguments[2]);
		this.format = (this.year.node && this.month.node && this.day.node) ? 3 : 0;
	} else if(arguments.length == 2){
		this.month  = new JLJS_Selector(arguments[0]);
		this.day    = new JLJS_Selector(arguments[1]);
		this.format = (this.month.node && this.day.node) ? 2 : 0;
	}
	
	this.getDays();
}

JLJS_DateSelector.prototype = {
	getDays : function(){
		if(JLJS_DateSelector.prototype.calendarLocalConfig){
			this.calendarLocalConfig = JLJS_DateSelector.prototype.calendarLocalConfig;
		} else {
			if(JLJS_CalNav && JLJS_CalNav.calendarLocalConfig){
				this.calendarLocalConfig = JLJS_CalNav.calendarLocalConfig;
			} else {
				this.calendarLocalConfig = this.getLocalText();
			}
			JLJS_DateSelector.prototype.calendarLocalConfig = this.calendarLocalConfig;
		}
		
		if(!this.calendarLocalConfig){
			this.dayOfWeekFlag = false;
			return;
		}
		
		var week = {};
		if(this.calendarLocalConfig.weekS){
			week = this.calendarLocalConfig.weekS[0];
		} else {
			week = this.calendarLocalConfig.week[0];
		}
		
		if(!week){
			this.dayOfWeekFlag = false;
			return;
		}
		
		for(var i = 0; i < 7; i++){
			this.days[i] = week["w" + (i+1)];
		}
	},
	
	getLocalText : function(){
		var timestampe = new Date().getTime();
		var xmlFile = "/world/fp/common/xml/calendarConfig.xml?t=" + timestampe;
		var key_calendar = "calendar";
		var key_language = "language";
		var key_id = "id";
		
		var http = new JKL.ParseXML(xmlFile);
		http.setOutputArrayAll();
		var dataArray = http.parse();
		var langDataArray = dataArray[key_calendar][0][key_language];
		for(var i=0;i<langDataArray.length;i++){
			if (langDataArray[i][key_id] == JLJS02.site.lang.code) {
				return langDataArray[i];
			}
		}
	},
	
	adjust : function(){
		if (this.year)  this.year.adjustByValue(this.Date.getFullYear());
		if (this.month) this.month.adjustByValue(this.Date.getMonth() + 1);		
		if (this.day)   this.day.adjustByValue(this.Date.getDate());
	},

	applyOffset : function () {
		var offset = { y : 0, m : 0, d : 0 };
		var ptn    = /^((\+|\-)\d+)(y|m|d)$/;
		for(var i = 0; i < arguments.length; i++){
			if (typeof arguments[i] == 'string' && arguments[i].match(ptn)) {
				offset[RegExp.$3] = parseInt(RegExp.$1);
			}
		}
		this.Date.setFullYear(this.Date.getFullYear() + offset.y);
		this.Date.setMonth(this.Date.getMonth() + offset.m);
		this.Date.setDate(this.Date.getDate() + offset.d);
		if(this.dayOfWeekFlag){
			this.setDay();
		} else {
			this.adjust();
		}
	},

	adjustToToday : function () {
		this.Date = new Date();
		if(this.dayOfWeekFlag){
			this.setDay();
		} else {
			this.adjust();
		}
	},

	adjustToDate : function (arg) {
		if(arg.constructor == Date){
			this.Date = arg
		} else if (typeof arg == 'string' && arg.match(/^[\d\-\/]*$/)) {
			var date = (arg.match(/\-/)) ? arg.split('-') : arg.split('/');
			if(date.length == 3){
				this.Date.setDate(1);
				this.Date.setFullYear(parseInt(date[0], 10));
				this.Date.setMonth(parseInt(date[1], 10) - 1);
				this.Date.setDate(parseInt(date[2], 10));
			} else if(date.length == 2){
				this.Date.setDate(1);
				this.Date.setMonth(parseInt(date[0], 10) - 1);
				this.Date.setDate(parseInt(date[1], 10));
			} else {
				return;
			}
		}
		if(this.dayOfWeekFlag){
			this.setDay();
		} else {
			this.adjust();
		}
	},
	
	setDateBySelector : function () {
		this.Date.setDate(1);
		if(this.month) this.Date.setMonth(parseInt(this.month.node.value, 10) - 1);
		if(this.year){
			this.Date.setFullYear(this.year.node.value);	
		} else {
			this.Date.setFullYear(this.getFullYear());
		}
		if(this.day) {
			if(this.dayOfWeekFlag){
				var endDate = new Date(this.Date.getFullYear(), this.Date.getMonth() + 1, 1)
				endDate.setDate(0);
				if(endDate.getDate() >= this.day.node.value) {
					this.Date.setDate(this.day.node.value);
				}
				this.setDay();
			} else {
				this.Date.setDate(this.day.node.value);
				this.adjust();
			}
		}
	},
	
	setDay : function () {
		
		this.day.node.options.length = 0;

		var fullYear = this.getFullYear();
		var dLabel = "";
		if(this.calendarLocalConfig.dayLabel){
			dLabel = this.calendarLocalConfig.dayLabel[0];
		}
		
		this.end.setFullYear(fullYear);
		this.end.setDate(1);
		this.end.setMonth(this.Date.getMonth() + 1);
		this.end.setDate(0);
		this.endDay = parseInt(this.end.getDate());
		for(i=0;i<this.endDay;i++) {
			this.end.setDate(i+1);
			var value = (this.zeroPadFlg) ? this.padZero(i+1, 2) : i+1;
			var oneDay = parseInt(i+1) + dLabel;
			var text = oneDay + "(" + this.days[this.end.getDay()] + ")";
			
			this.day.node.options[i] = new Option(text, value);
		}
		this.adjust();
	},
	
	padZero : function (val, argLength) {
		val = "" + val;
		var result = val;
		for(var i = 1 ; i <= (argLength - val.length); i++){
			result = "0" + result;
		}
		return result;		
	},
	
	getFullYear : function () {
		var fullYear = 0;
		if(this.year){
			fullYear = parseInt(this.year.node.value, 10);
		} else if(this.Date.getMonth()< this.today.getMonth()){
			fullYear = this.today.getFullYear() + 1;
		} else {
			fullYear = this.today.getFullYear();
		}
		return fullYear;
	}
};

