$(function(){
	createCalendar();

	var cal_move = 242;
	
	$("#arrive_pick").click(function(){
		$('#cal-header p').html('Choose your Arrival date');
		showCalendar($(this));
		return false;
	});
	$("#depart_pick").click(function(){
		$('#cal-header p').html('Choose your Departure date');
		showCalendar($(this));
		return false;
	});
	
	$("#cal-close, #overlay").click(function(){
		$("#cal").css({'margin-top':'0'});
		$('#overlay, #calendar-container').hide();
		return false;
	});
	
	$("div.next a.next").click(function(){
		var top = $('#cal').css('margin-top').replace('px', '');
		$("#cal").css({'margin-top':(top - cal_move) + 'px'});
		return false;
	});
	$("div.next a.previous").click(function(){
		var top = $('#cal').css('margin-top').replace('px', '');
		$("#cal").css({'margin-top':(parseInt(top) + cal_move) + 'px'});
		return false;
	});
	
	$("a.pick").click(function(){
		var type = $(this).attr('data-id');
		var dateval = $(this).attr('data-date');
		var monthval = $(this).attr('data-month');
		var yearval = $(this).attr('data-year');
		pickDate(type, dateval, monthval, yearval);
		$('#overlay, #calendar-container').hide();
		return false;
	});
});

function showCalendar(div)
{
	var id = $(div).attr('id').replace('_pick', '');
	$('a.pick').attr('data-id', id);
	var pos = $(div).offset();
	var width = $(div).width();
	var height = $(div).height();
	$('#overlay').show();
	/*var left_pos = (pos.left - 395);
	if (left_pos < 0)
	{
		left_pos = 0;
	}*/
	var left_pos = ($(window).width() / 2) - 330;
	var top_pos = (pos.top + height + 5);
	$("#calendar-container").css({'left': left_pos + 'px', 'top':top_pos + 'px'});
	$("#calendar-container").show();
}

function getDate(m)
{
	date = new Array();
	var now = new Date();
	now.setDate(1);
	now.setMonth(now.getMonth() + m);

	for (count=0; count<=42; count++)
	{
		if (count <= now.getDay())
			date[count] = '';
		else if ((now.getMonth()=='3')||(now.getMonth()=='5')||(now.getMonth()=='8')||(now.getMonth()=='10'))
		{
			if (count > 30 + now.getDay())
				date[count] = '';
			else
				date[count] = count - now.getDay();
		}
		else if ((now.getMonth()=='0')||(now.getMonth()=='2')||(now.getMonth()=='4')||(now.getMonth()=='6')||(now.getMonth()=='7')||(now.getMonth()=='9')||(now.getMonth()=='11'))
		{
			if (count > 31 + now.getDay())
				date[count] = '';
			else
				date[count] = count - now.getDay();
		}
		else if (now.getFullYear() % 4 != 0)
		{
			if (now.getMonth()=='1')
			{
				if (count > 28 + now.getDay())
					date[count] = '';
				else
					date[count] = count - now.getDay();
			}
		}
		else if (now.getFullYear() % 4 == 0)
		{
			if (now.getMonth()=='1')
			{
				if (count > 29 + now.getDay())
					date[count] = '';
				else
					date[count] = count - now.getDay();
			}
		}
	}
}

function getMonth(m)
{
	var now = new Date();
	now.setDate(1);
	now.setMonth(now.getMonth() + m);
	return now.getMonth();
}

function getMonthName(m)
{
	var now = new Date();
	now.setDate(1);
	now.setMonth(now.getMonth() + m);
	return month_name[now.getMonth()];
}

function getYear(m)
{
	var now = new Date();
	now.setMonth(now.getMonth() + m);
	return now.getFullYear(); 
}

function getDayName(d,m,y)
{
	var now = new Date(y,m,d);
	return days[now.getDay()];
}

function getToday()
{
	var now = new Date();
	return now.getDate();
}

function pickDate(type, dateval, monthval, yearval)
{
	//w = window.opener;
	//if (w && !w.closed)
	//{
		if (dateval <=9)
			dateval = "0" + dateval;

		document.form[type+'date'].selectedIndex = dateval;
		date = monthval + "|" + yearval;
		for (i=0; i < document.form[type+'date2'].options.length; i++)
		{
			check = document.form[type+'date2'].options[i].value;
			if (date == check)
			{
				document.form[type+'date2'].selectedIndex = i;
				break;
			}
		}
		if (type == 'arrive')
			setEndDate2(document.form.nights.selectedIndex + 1);
		else if (type == 'depart')
			setNights2();
		//window.close();
	//}
}

function changeButtonClass(b)
{
	if ((b.style.color == '#000000') || (b.style.color == 'rgb(0, 0, 0)'))
	{
		b.style.color='#008000';
	}
	else
	{
		b.style.color='#000000';
		b.style.cursor='pointer';
	}
}

function setEndDate2(n)
{
	//w = window.opener;
	f = document.form;
	now = new Date();
	smonth = getMonthValue(f.arrivedate2.options[f.arrivedate2.selectedIndex].value);
	syear = getMonthValue(f.arrivedate2.options[f.arrivedate2.selectedIndex].value);
	now.setDate(f.arrivedate.selectedIndex);
	now.setFullYear(syear);
	now.setMonth(smonth);

	now.setTime(now.getTime() + n * 24 * 60 * 60 * 1000);
	f.departdate.selectedIndex = now.getDate();
	if (now.getMonth() < smonth)
		f.departdate2.selectedIndex = f.arrivedate2.selectedIndex + ((now.getMonth() + 12) - smonth);
	else
		f.departdate2.selectedIndex = f.arrivedate2.selectedIndex + (now.getMonth() - smonth);

	setDayName2('arrive',f.arrivedate.selectedIndex,f.arrivedate2.options[f.arrivedate2.selectedIndex].value);
	setDayName2('depart',f.departdate.selectedIndex,f.departdate2.options[f.departdate2.selectedIndex].value);
}

function setNights2()
{
	//w = window.opener;
	f = document.form;
	f.nights.length = 20;

	stime = getTimeValue(f.arrivedate.selectedIndex,f.arrivedate2.options[f.arrivedate2.selectedIndex].value);
	etime = getTimeValue(f.departdate.selectedIndex,f.departdate2.options[f.departdate2.selectedIndex].value);

	setDayName2('arrive',f.arrivedate.selectedIndex,f.arrivedate2.options[f.arrivedate2.selectedIndex].value);
	setDayName2('depart',f.departdate.selectedIndex,f.departdate2.options[f.departdate2.selectedIndex].value);

	dtime = (etime - stime) / (24 * 60 * 60 * 1000);

	if (dtime > 20)
	{
		for (i=21; i<=dtime; i++)
		{
			len = f.nights.length++;
			f.nights.options[len].value = i;
			f.nights.options[len].text = i;
		}
	}
	f.nights.selectedIndex = Math.ceil(dtime - 1);
}

function setDayName2(t,d,m)
{
	//w = window.opener;
	f = document.form;
	f[t+'day'].value = days[getDayName2(d,m)];
}

function getDayName2(d,m)
{
	mon = getMonthValue(m);
	year = getYearValue(m);
	now = new Date(year, mon, d, 0, 0, 0, 0);
	return now.getDay();
}

function createCalendar()
{
	var cal = '<div id="cal-header"><p></p><a href="" id="cal-close">Close X</a></div><div id="cal">';
	var temp_cal = '';
	
	for (i = 0; i <= 11; i++)
	{
		temp_cal += '<div class="month" id="' + i + '"><h5>' + getMonthName(i) + '</h5><h6>S</h6><h6>M</h6><h6>T</h6><h6>W</h6><h6>T</h6><h6>F</h6><h6 class="last">S</h6>';
		getDate(i);
		today = getToday();
		for (loop = 1; loop < 43; loop++)
		{
			if (date[loop] == '')
			{
				if ((loop % 7) == 0)
				{
					temp_cal += '<div class="empty last"></div>';
				}
				else
				{
					temp_cal += '<div class="empty"></div>';
				}
			}
			else
			{
				if ((loop % 7) == 0)
				{
					if ((date[loop] < today) && (i == 0))
					{
						temp_cal += '<div class="passed last">' + date[loop] + '</div>';
					}
					else
					{
						temp_cal += '<a href="" data-date="' + date[loop] + '" data-month="' + getMonth(i) + '" data-year="' + getYear(i) + '" class="pick last">' + date[loop] + '</a>';
					}
				}
				else
				{
					if ((date[loop] < today) && (i == 0))
					{
						temp_cal += '<div class="passed">' + date[loop] + '</div>';
					}
					else
					{
						temp_cal += '<a href="" data-date="' + date[loop] + '" data-month="' + getMonth(i) + '" data-year="' + getYear(i) + '" class="pick">' + date[loop] + '</a>';
					}
				}
			}
		}
		temp_cal += '</div>';
		n = i + 1;
		if (n % 3 == 0)
		{
			temp_cal += '<div class="next">';
			if (n > 3)
			{
				temp_cal += '<a href="" class="previous">&lt; Previous 3 Months</a>';
			}
			if (n < 12)
			{
				temp_cal += '<a href="" class="next">Next 3 Months &gt;</a>';
			}
			temp_cal += '</div>';
		}
	}
	
	cal += temp_cal + '</div>';
	
	$('#calendar-container').html(cal);
}
