function _reset() {
	var from = $.datepick.determineDate(0);
	var to = $.datepick.determineDate(+1);
	$('#date').datepick('setDate', from);
	$('#from').val('');
	$('#to').val('');
	_updateStat('from');
	$('.week span').text(' - ');
	$('#submit').hide();
	$('#submit_off').show();
}

function _updateLinked(dates) {	
	if(dates.length) {
		if($('#from').val() != $.datepick.formatDate(dates[0])) {
			$('#from').val($.datepick.formatDate(dates[0]));
		}
		if(dates[0].getTime() != dates[1].getTime()){
			_updateStat('from');
		} else {
			dates[1] = $.datepick.newDate(dates[0].getFullYear(), dates[0].getMonth() + 1, dates[0].getDate() + 1);
			_updateStat('to');
		}
		if($('#to').val() != $.datepick.formatDate(dates[1])) {
			$('#to').val($.datepick.formatDate(dates[1]));
		}
		
		var daycount = parseInt((dates[1].getTime() - dates[0].getTime()) / 86400000);
		if(daycount < 1) {
			$('.week_table').hide();
		} else {
			$('.week span').text(daycount);
			$('.week_table').show();
		}
		
		$('.dpCheckin').removeClass('dpCheckin');
		$('.dpCheckout').removeClass('dpCheckout');
		$('.dp' + dates[0].getTime()).addClass('dpCheckin');
		$('.dp' + dates[1].getTime()).addClass('dpCheckout');
		$('#submit').show();
		$('#submit_off').hide();
	}
}

function _linkedUpdate(ele, w) {
	var from = new Date($('#from').val());
	var to = new Date($('#to').val());
	if(from.getTime() && to.getTime() && (from.getTime() != to.getTime())) {
		$('#date').datepick('setDate',[from,to]);
	} else if(from.getTime() && ele.id == 'from') {
		var to = $.datepick.newDate(from.getFullYear(), from.getMonth() + 1, from.getDate() + 1);
		$('#date').datepick('setDate',[from,to]);
	} else {
		$('.week span').text(' - ');
		$('#submit').hide();
		$('#submit_off').show();
		_updateStat('from');
		if(w) {
			alert('日付が正しくありません');
		}
	}
}

function _updateStat(active) {
	if(active == 'from') {
		$('.checkin').addClass('activein');
		$('.checkout').removeClass('activeout');
	} else {
		$('.checkin').removeClass('activein');
		$('.checkout').addClass('activeout');
	}
}

function _init(f) {
	try {
		var from = $.datepick.newDate(
			document.getElementById('checkinY').value,
			document.getElementById('checkinM').value,
			document.getElementById('checkinD').value
		);
		var to = $.datepick.newDate(
			document.getElementById('checkoutY').value,
			document.getElementById('checkoutM').value,
			document.getElementById('checkoutD').value
		);
		if(from && to && (from.getTime() != to.getTime())) {
			$('.calendar_div #date').datepick('setDate',[from,to]);
			$('.calendar_div .week span').text(parseInt((to.getTime() - from.getTime()) / 86400000));
			$('.calendar_div #submit').show();
			$('.calendar_div #submit_off').hide();
		}
		else{
			$('.calendar_div #submit').hide();
			$('.calendar_div #submit_off').show();
		}
		//parent._resize_cal($('.calendar_div .datepick-multi').css('width'));
		
	} catch(e) {}
	
	// Check if the checkout date has to be selected directly:
	if(f=='checkout') {
		var instObj = $('.calendar_div #date').data("datepick");
		// Set obj instance variable to say: pick the second date now:
		instObj.pickingRange = true;
		_updateStat('to');
	}
	
}

function calendar_bindESC() {
	$(document).bind('keydown', closeCalendarOnESC);
}

function calendar_unbindESC() {
	$(document).unbind('keydown', closeCalendarOnESC);
}

function closeCalendarOnESC(e) {
	if (e.keyCode == 27) {
		$('.calendar_div #cancel').click();
	}
}

$(function() {
	
	var Holiday = [ // 祝日
		[2010, 7, 19],
		[2010, 9, 20],
		[2010, 9, 23],
		[2010, 10, 11],
		[2010, 11, 3], 
		[2010, 11, 23],
		[2010, 12, 23], 
		[2011, 1, 1],
		[2011, 1, 10],
		[2011, 2, 11],
		[2011, 3, 21],
		[2011, 4, 29],
		[2011, 5, 3],
		[2011, 5, 4],
		[2011, 5, 5],
		[2011, 7, 18],
		[2011, 9, 19],
		[2011, 9, 23],
		[2011, 10, 10],
		[2011, 11, 3],
		[2011, 11, 23],
		[2011, 12, 23],
		[2012, 1, 1],
		[2012, 1, 2],
		[2012, 1, 9],
		[2012, 2, 11],
		[2012, 3, 20],
		[2012, 4, 29],
		[2012, 4, 30],
		[2012, 5, 3],
		[2012, 5, 5],
		[2012, 7, 16],
		[2012, 9, 17],
		[2012, 9, 22],
		[2012, 10, 8],
		[2012, 11, 3],
		[2012, 11, 23],
		[2012, 12, 23],
		[2012, 12, 24] // 最後の一つは最後の , なしで。
	];
	
	function _nationalDays(date, inMonth) {
		if (inMonth) { 
			for (i=0; i<Holiday.length; i++) { 
				if(date.getFullYear() == Holiday[i][0] && 
					date.getMonth()+1 == Holiday[i][1] && 
					date.getDate() == Holiday[i][2]) {
					return {dateClass: 'datepick-dow-0'}; 
				}
			}
		}
		return {};
	}
	
	<!-- build datepicker into #date dom element (jQuery plugin) -->
	$('.calendar_div #date').datepick({ 
		rangeSelect: true,
		monthsToShow: 3,
		changeMonth: false,
		minDate: new Date(), // today
		maxDate: '+3y',
		onDate: _nationalDays,
		onSelect: _updateLinked
	});
	_init();
	_updateStat('from');
	
	// Event bindings:
	
	$('.calendar_div #prevJump').click(function(){
		$('#date').datepick('changeMonth', -1);
		//_init();
		return false;
	});
	
	$('.calendar_div #nextJump').click(function(){
		$('#date').datepick('changeMonth', 1);
		return false;
	});
	
	$('.calendar_div #from, .calendar_div #to').change(function() {
		_linkedUpdate(this, 1);
	}).keyup(function() {
		if($('#from').val().length == 10 && $('#to').val().length == 10) {
			_linkedUpdate(this);
		}
	}).focus(function(){
		_updateStat(this.id);
	});
	
	$('.calendar_div #cancel, .calendar_div #close').click(function() {
		$('.calendar_div').hide();
		
		// Bind ESC key
		calendar_unbindESC();
		
		return false;
	});
	
	$('.calendar_div #submit').click(function(){
	    _linkedUpdate('to');
	    var from = new Date($('#from').val());
	    var to = new Date($('#to').val());
		// set to fields:
		$('input.checkinY').val(from.getFullYear());
		$('input.checkinM').val(from.getMonth()+1);
		$('input.checkinD').val(from.getDate());
		$('input.checkoutY').val(to.getFullYear());
		$('input.checkoutM').val(to.getMonth()+1);
		$('input.checkoutD').val(to.getDate());
		$('#output_nights').text($('.week_table td.week span').first().text());
		$('.calendar_div').hide();
		
		// Bind ESC key
		calendar_unbindESC();
	
		return false;
    });
	
	var checkinTriggers  = [ '#checkinY', '#checkinM', '#checkinD', '#calendar_picker_checkin' ];
	var checkoutTriggers = [ '#checkoutY', '#checkoutM', '#checkoutD', '#calendar_picker_checkout' ];

	$.each(checkinTriggers, function(i, el) {
		// Page "grand_right":
		$('.grand_right '+el).click(function(event) {
			$('.calendar_div').css('top', '95px');
			$('.calendar_div').css('right', '216px');
			_init('checkin');
			calendar_bindESC();
			$('.calendar_div').show();
		});
		// Page "local_right":
		$('.local_right '+el).click(function(event) {
			$('.calendar_div').css('top', '108px');
			$('.calendar_div').css('left', '4px');
			_init('checkin');
			calendar_bindESC();
			$('.calendar_div').show();
		});
		// Page "grand_left":
		$('.grand_left '+el).click(function(event) {
			$('.calendar_div').css('top', '267px');
			$('.calendar_div').css('left', '5px');
			_init('checkin');
			calendar_bindESC();
			$('.calendar_div').show();
		});
		// Page "local_left":
		$('.local_left '+el).click(function(event) {
			$('.calendar_div').css('top', '170px');
			$('.calendar_div').css('left', '0px');
			_init('checkin');
			calendar_bindESC();
			$('.calendar_div').show();
		});
	});
	
	$.each(checkoutTriggers, function(i, el) {
		// Page "grand_right":
		$('.grand_right '+el).click(function(event) {
			$('.calendar_div').css('top', '95px');
			$('.calendar_div').css('right', '216px');
			$('#to').val('');
//			_init('checkout');
			calendar_bindESC();
			$('.calendar_div').show();
		});
		// Page "local_right":
		$('.local_right '+el).click(function(event) {
			$('.calendar_div').css('top', '108px');
			$('.calendar_div').css('left', '4px');
			$('#to').val('');
//			_init('checkout');
			calendar_bindESC();
			$('.calendar_div').show();
		});
		// Page "grand_left":
		$('.grand_left '+el).click(function(event) {
			$('.calendar_div').css('top', '267px');
			$('.calendar_div').css('left', '5px');
			$('#to').val('');
//			_init('checkout');
			calendar_bindESC();
			$('.calendar_div').show();
		});
		// Page "local_left":
		$('.local_left '+el).click(function(event) {
			$('.calendar_div').css('top', '170px');
			$('.calendar_div').css('left', '0px');
			$('#to').val('');
//			_init('checkout');
			calendar_bindESC();
			$('.calendar_div').show();
		});
	});
	
	// Make date fields readonly (use calendar):
	var fields = ['checkinY','checkinM','checkinD','checkoutY','checkoutM','checkoutD'];
	$.each(fields, function(i, el) {
		$('#'+el).attr('readonly', 'readonly');
	});

	
});
