var xml;

function loadXML() {
	if (window.XMLHttpRequest) xml = new XMLHttpRequest();
	else if (window.ActiveXObject) xml = new ActiveXObject('Microsoft.XMLHTTP');
	if (xml) {
		xml.onreadystatechange = state_change;
		xml.open('GET','http://www.ballstonbeachcottage.com/data/data.xml',true);
		xml.send(null);
	} else alert('problem loading xml');
}

var plus = true;
var minus = false;

var weeks;
var prices;
var winters;

var date = new Date();
date.setDate(1);
date.setHours(0,0,0,0);
var today = new Date();
today.setHours(0,0,0,0);
var num_days = new Array(31,28,31,30,31,30,31,31,30,31,30,31);

function do_month() {

	var month_names = new Array('January','February','March','April','May','June','July','August','September','October','November','December');
	var day_names = new Array('Su','M','T','W','Th','F','Sa');
	
	var jan = 0;
	var feb = 1;
	var mar = 2;
	var apr = 3;
	var may = 4;
	var jun = 5;
	var jul = 6;
	var aug = 7;
	var sep = 8;
	var oct = 9;
	var nov = 10;
	var dec = 11;

	var div;
	var week_price;
	var day;
	var week;
	if ((Math.round(date.getFullYear()/4)==date.getFullYear()/4) && !((Math.round(date.getFullYear()/100)==date.getFullYear()/100) && (Math.round(date.getFullYear()/400)!=date.getFullYear()/400))) num_days[feb] = 29;
	
	var colors = new Object({gray_text:'#9b9b9b', red:'#ef9090', red_today:'#d07d7d', light_red:'#efcaca', light_red_today:'#d0b0b0', green:'#c3ef90', green_today:'#aad07d', light_green:'#dff0cc', light_green_today:'#c2d1b2', yellow:'#efef90', yellow_today:'#d0d07d', light_yellow:'#f0f0ce', light_yellow_today:'#d1d1b3', blue:'#90c8f0', blue_today:'#7daed1', light_blue:'#cce1f0', light_blue_today:'#b2c4d1'});
		
	var finished = false;
	
	for (var i=0; i<42; i++) {
		div = document.getElementById('d_'+i);
		with (div.style) {
			color = '#000000';
			backgroundColor = '#ffffff';
			textDecoration = 'none';
		}
		div.innerHTML = '';
	}
	
	for (i=0; i<6; i++) {
		document.getElementById('price_'+i).innerHTML = '';
	}
	
	for (i=0; i<42; i++) {
	
		div = document.getElementById('d_'+i);
						
		day = Math.round(((i/7)-Math.floor(i/7))*7);
		week = Math.floor(i/7);
		
		var new_year = date.getFullYear();
		var new_month;
		var new_date;
		
		if (i<date.getDay()) {
		
			if (date.getMonth()-1 < 0) {
				new_year -= 1;
				new_month = 12+(date.getMonth()-1);
			} else {
				new_month = date.getMonth()-1;
			}
			
			new_date = num_days[new_month]-date.getDay()+i+1;
		}
		
		else if (i>=date.getDay() && i<num_days[date.getMonth()]+date.getDay()) {
		
			new_month = date.getMonth();
			new_date = i+1-date.getDay();
			
			if (new_date == num_days[date.getMonth()] && day==6) finished = true;
			
		} else {
			new_month = date.getMonth()+1;
			new_date = i-num_days[date.getMonth()]-date.getDay()+1;
			
			if (day==6) finished = true;
		}
		
		var cur_date = new Date(new_year,new_month,new_date);
		
		div.innerHTML = cur_date.getDate();
		data = document.createElement('div');
		data.style.display = 'none';
		data.innerHTML = cur_date.valueOf();
		div.appendChild(data);
				
		var div_found = false;
		
		for (var j=0; j<winters.length; j++) {
			
			var year_1 = parseInt(winters[j].getAttribute('id').substr(2,4));
			var year_2 = parseInt(winters[j].getAttribute('id').substr(7));
			
			if (cur_date.getFullYear()>=year_1 && cur_date.getFullYear()<=year_2) {
			
				var offseason_begin = date_from_string(winters[j].getElementsByTagName('begin')[0].getAttribute('id'));
				var offseason_end = date_from_string(winters[j].getElementsByTagName('end')[0].getAttribute('id'));
				
				if (cur_date>=offseason_begin && cur_date<offseason_end) {
				
					div_found = true;
					
					div.style.textDecoration = 'line-through';
					div.style.backgroundColor = cur_date.getMonth()==date.getMonth()?colors.blue:colors.light_blue;
					
				}
			}
		}
		
		for (j=0; j<weeks.length; j++) {
			
			var week_date = date_from_string(weeks[j].getAttribute('id'));
			var week_date_end = new Date(week_date.getFullYear(),week_date.getMonth(),week_date.getDate()+(weeks[j].getAttribute('days') > 0 ? parseInt(weeks[j].getAttribute('days')) : 7));
			//var week_date_end = new Date(week_date.getFullYear(),week_date.getMonth(),week_date.getDate()+(weeks[j].hasAttribute('days')?parseInt(weeks[j].getAttribute('days')):7));

			if (cur_date>=week_date && cur_date<week_date_end) {
			
				div_found = true;
				
				if (weeks[j].getAttribute('available')!='true') {
					div.style.textDecoration = 'line-through';
					div.style.backgroundColor = cur_date.getMonth()==date.getMonth()?colors.red:colors.light_red;
				} else div.style.backgroundColor = cur_date.getMonth()==date.getMonth()?colors.green:colors.light_green;
				
			}					
		}
		
		if (cur_date.getDay()==0) {
			for (j=0; j<prices.length; j++) {
				if (prices[j].getAttribute('id')==string_from_date(cur_date)) {
					var price = prices[j].getAttribute('value');
					var format_price = '$';
					for (var k=0; k<price.length; k++) {
						if (-(k-price.length)/3==Math.round(-(k-price.length)/3)) {
							format_price += ',';
						}
						format_price += price.charAt(k);
					}
					document.getElementById('price_'+week).innerHTML = format_price;
				}
			}
		}
			
		if (i<date.getDay() || i>=num_days[date.getMonth()]+date.getDay()) div.style.color = colors.gray_text;
		
		if (cur_date<today) {
			div_found = true;
			div.style.textDecoration = 'line-through';
			if (i<date.getDay() || i>=num_days[date.getMonth()]+date.getDay()) div.style.backgroundColor = colors.light_red;
			else div.style.backgroundColor = colors.red;
		}
		
		if (!div_found) div.style.backgroundColor = cur_date.getMonth()==date.getMonth()?colors.yellow:colors.light_yellow;
		if (cur_date.getTime()==today.getTime()) {
			
			var decimal = div.style.backgroundColor.substring(div.style.backgroundColor.indexOf('(')+1,div.style.backgroundColor.length-1);
			var rgb = decimal.split(',');
			var hex = '#';
			for (j=0; j<rgb.length; j++) {
				hex += parseInt(rgb[j]).toString(16);
			}
			for (j in colors) {
				if (hex == colors[j]) {
					div.style.backgroundColor = colors[j+'_today'];
				}
			}
		}

		if (finished) break;
	}
	
	document.getElementById('month').innerHTML = month_names[date.getMonth()];
	document.getElementById('year').innerHTML = date.getFullYear();
	
}

function date_from_string(string) {
	var date = new Date(parseInt('20'+string.substr(8)),parseInt(string.substr(2,2),10)-1,parseInt(string.substr(5,2),10));
	return date;
}

function string_from_date(date) {
	var string = 'd_'+((date.getMonth()+1)<10?'0'+(date.getMonth()+1):(date.getMonth()+1))+'_'+(date.getDate()<10?'0'+date.getDate():date.getDate())+'_'+date.getFullYear().toString().substr(2);
	return string;
}

var month = true;
var year = false;

function change_date(time,direction) {
	
	var _new_year = date.getFullYear();
	var _new_month = date.getMonth();
	
	if (time==month) {
		_new_month = date.getMonth()+(direction==plus?1:-1);
		if (_new_month<0) {
			_new_month = 12+_new_month;
			_new_year -= 1;
		}
	} else if (time==year) _new_year = date.getFullYear()+(direction==plus?1:-1);
	
	date.setFullYear(_new_year,_new_month,1);
	do_month();
}