var today = new Date();
var fromDate = today;
var toDate = today;

var monthNames = "January^February^March^April^May^June^July^August^September^October^November^December^";
var dayNames = "sun^mon^tue^wed^thu^fri^sat";

var fromOffset = 0;
var toOffset = 0;

function doErrorCheck()
{
	if ( toDate < fromDate )
	{
		setToDate ( fromDate.getDate() + 1, fromDate.getMonth(), fromDate.getFullYear() );
	}
}

function setToDate ( day, month, year )
{
	$('.ppn_cal_popup').hide();
	
	toOffset = ( month - today.getMonth() ) + ( 12 * ( year - today.getFullYear() ));
	toDate = new Date ( year, month, day );
	
	$('.ppn_cal_to #rs_chk_out').attr("value", (month+1) + "/" + day + "/" + year);
	$('.ppn_cal_to #rs_chk_out_vis').attr("value", $('.ppn_cal_to #rs_chk_out').attr("value") );
	$('.ppn_cal_to .ppn_cal_popupHeader .ppn_cal_currDate').html( $('.ppn_cal_to #rs_chk_out').attr("value") );

	$.get("/calendar/inc/double_cal/printDoubleCal.php?monthNames=" + monthNames + "&dayNames=" + dayNames + "&offset=" + toOffset + "&cal=to&currDate=" + (toDate.getMonth()+1) + "/" + toDate.getDate() + "/" + toDate.getFullYear() + "&printDate=" + (toDate.getMonth()+1) + "/" + 1 + "/" + toDate.getFullYear() + "&minDate=" + (fromDate.getMonth()+1) + "/" + (fromDate.getDate()) + "/" + fromDate.getFullYear(), function (data) { $(".ppn_cal_to .ppn_cal_popupBody").html ( data ); } );
	
	doErrorCheck();
}
function setFromDate ( day, month, year )
{
	$('.ppn_cal_popup').hide();
	
	fromOffset = ( month - today.getMonth() ) + ( 12 * ( year - today.getFullYear() ));
	fromDate = new Date ( year, month, day );
	
	$('.ppn_cal_from #rs_chk_in').attr("value", (month+1) + "/" + day + "/" + year);
	$('.ppn_cal_from #rs_chk_in_vis').attr("value", $('.ppn_cal_from #rs_chk_in').attr("value") );
	$('.ppn_cal_from .ppn_cal_popupHeader .ppn_cal_currDate').html( $('.ppn_cal_from #rs_chk_in').attr("value") );
	
	$.get("/calendar/inc/double_cal/printDoubleCal.php?monthNames=" + monthNames + "&dayNames=" + dayNames + "&offset=" + fromOffset + "&cal=from&currDate=" + (fromDate.getMonth()+1) + "/" + fromDate.getDate() + "/" + fromDate.getFullYear() + "&printDate=" + (fromDate.getMonth()+1) + "/" + 1 + "/" + fromDate.getFullYear() + "&minDate=" + (today.getMonth()+1) + "/" + today.getDate() + "/" + today.getFullYear(), function (data) { $(".ppn_cal_from .ppn_cal_popupBody").html ( data ); } );
	$.get("/calendar/inc/double_cal/printDoubleCal.php?monthNames=" + monthNames + "&dayNames=" + dayNames + "&offset=" + toOffset + "&cal=to&currDate=" + (toDate.getMonth()+1) + "/" + toDate.getDate() + "/" + toDate.getFullYear() + "&printDate=" + (toDate.getMonth()+1) + "/" + 1 + "/" + toDate.getFullYear() + "&minDate=" + (fromDate.getMonth()+1) + "/" + (fromDate.getDate()) + "/" + fromDate.getFullYear(), function (data) { $(".ppn_cal_to .ppn_cal_popupBody").html ( data ); } );
	
	doErrorCheck();	
}

function drawFromNext ( month, year )
{
	if ( fromOffset < 11 )
	{
		fromOffset ++;
		$.get("/calendar/inc/double_cal/printDoubleCal.php?monthNames=" + monthNames + "&dayNames=" + dayNames + "&offset=" + fromOffset + "&cal=from&currDate=" + (fromDate.getMonth()+1) + "/" + fromDate.getDate() + "/" + (fromDate.getFullYear()) + "&printDate=" + (month) + "/" + 1 + "/" + (year) + "&minDate=" + (today.getMonth()+1) + "/" + today.getDate() + "/" + today.getFullYear(), function (data) { $(".ppn_cal_from .ppn_cal_popupBody").html ( data ); } );				
	}
}
function drawToNext ( month, year )
{
	if ( toOffset < 11 )
	{
		toOffset ++;
		$.get("/calendar/inc/double_cal/printDoubleCal.php?monthNames=" + monthNames + "&dayNames=" + dayNames + "&offset=" + toOffset + "&cal=to&currDate=" + (toDate.getMonth()+1) + "/" + toDate.getDate() + "/" + (toDate.getFullYear()) + "&printDate=" + (month) + "/" + 1 + "/" + (year) + "&minDate=" + (fromDate.getMonth()+1) + "/" + (fromDate.getDate()) + "/" + fromDate.getFullYear(), function (data) { $(".ppn_cal_to .ppn_cal_popupBody").html ( data ); } );
	}
}

function drawFromPrev ( month, year )
{
	if ( fromOffset > 0 )
	{
		fromOffset --;
		$.get("/calendar/inc/double_cal/printDoubleCal.php?monthNames=" + monthNames + "&dayNames=" + dayNames + "&offset=" + fromOffset + "&cal=from&currDate=" + (fromDate.getMonth()+1) + "/" + fromDate.getDate() + "/" + (fromDate.getFullYear()) + "&printDate=" + (month) + "/" + 1 + "/" + (year) + "&minDate=" + (today.getMonth()+1) + "/" + today.getDate() + "/" + today.getFullYear(), function (data) { $(".ppn_cal_from .ppn_cal_popupBody").html ( data ); } );
	}
}
function drawToPrev ( month, year )
{
	if ( toOffset > 0 )
	{
		toOffset --;
		$.get("/calendar/inc/double_cal/printDoubleCal.php?monthNames=" + monthNames + "&dayNames=" + dayNames + "&offset=" + toOffset + "&cal=to&currDate=" + (toDate.getMonth()+1) + "/" + toDate.getDate() + "/" + (toDate.getFullYear()) + "&printDate=" + (month) + "/" + 1 + "/" + (year) + "&minDate=" + (fromDate.getMonth()+1) + "/" + (fromDate.getDate()) + "/" + fromDate.getFullYear(), function (data) { $(".ppn_cal_to .ppn_cal_popupBody").html ( data ); } );
	}
}

function drawFromOffset ( newOff )
{
	fromOffset = newOff;
	var month = today.getMonth() + ( (newOff+(today.getMonth()-1)) % 11 )-1;
	var year = today.getFullYear() + Math.floor(( (newOff+(today.getMonth()-1)) / 11 ));
	
	if ( Math.floor(( (newOff+(today.getMonth()-1)) / 11 )) != 0 )
	{
		month --;
	}
	
	$.get("/calendar/inc/double_cal/printDoubleCal.php?monthNames=" + monthNames + "&dayNames=" + dayNames + "&offset=" + fromOffset + "&cal=from&currDate=" + (fromDate.getMonth()+1) + "/" + fromDate.getDate() + "/" + (fromDate.getFullYear()) + "&printDate=" + (month) + "/" + 1 + "/" + (year) + "&minDate=" + (today.getMonth()+1) + "/" + today.getDate() + "/" + today.getFullYear(), function (data) { $(".ppn_cal_from .ppn_cal_popupBody").html ( data ); } );
}
function drawToOffset ( newOff )
{
	toOffset = newOff;
	var month = today.getMonth() + ( (newOff+(today.getMonth()-1)) % 11 )-1;
	var year = today.getFullYear() + Math.floor(( (newOff+(today.getMonth()-1)) / 11 ));
	
	if ( Math.floor(( (newOff+(today.getMonth()-1)) / 11 )) != 0 )
	{
		month --;
	}
	
	$.get("/calendar/inc/double_cal/printDoubleCal.php?monthNames=" + monthNames + "&dayNames=" + dayNames + "&offset=" + toOffset + "&cal=to&currDate=" + (toDate.getMonth()+1) + "/" + toDate.getDate() + "/" + (toDate.getFullYear()) + "&printDate=" + (month) + "/" + 1 + "/" + (year) + "&minDate=" + (fromDate.getMonth()+1) + "/" + (fromDate.getDate()) + "/" + fromDate.getFullYear(), function (data) { $(".ppn_cal_to .ppn_cal_popupBody").html ( data ); } );
}

function setDates ( newFrom, newTo )
{
	if ( newFrom != "" )
	{
		var splitted = newFrom.split("/");
		fromDate = new Date(splitted[2], (splitted[0]-1), splitted[1]);
		setFromDate ( fromDate.getDate(), fromDate.getMonth(), fromDate.getFullYear() );
		$.get("/calendar/inc/double_cal/printMonthYearSelect.php?monthNames=" + monthNames + "&printDate=" + (fromDate.getMonth()+1) + "/" + 1 + "/" + fromDate.getFullYear() + "&minDate=" + (fromDate.getMonth()+1) + "/" + 1 + "/" + fromDate.getFullYear(), function (data) { $(".ppn_cal_from .ppn_cal_monthSelect").html ( data ); } );
	}
	else
	{
		fromDate = new Date ();
		setFromDate ( fromDate.getDate(), fromDate.getMonth(), fromDate.getFullYear() );
		$.get("/calendar/inc/double_cal/printMonthYearSelect.php?select=no&monthNames=" + monthNames + "&printDate=" + (fromDate.getMonth()+1) + "/" + 1 + "/" + fromDate.getFullYear() + "&minDate=" + (fromDate.getMonth()+1) + "/" + fromDate.getDate() + "/" + fromDate.getFullYear(), function (data) { $(".ppn_cal_from .ppn_cal_monthSelect").html ( "<option>" + $('.month_translation').text() + "</option>" + data ); } );
	}
	
	if ( newTo != "" )
	{
		var splitted = newTo.split("/");
		toDate = new Date(splitted[2], (splitted[0]-1), splitted[1]);
		setToDate ( toDate.getDate(), toDate.getMonth(), toDate.getFullYear() );
		$.get("/calendar/inc/double_cal/printMonthYearSelect.php?monthNames=" + monthNames + "&printDate=" + (toDate.getMonth()+1) + "/" + 1 + "/" + toDate.getFullYear() + "&minDate=" + (toDate.getMonth()+1) + "/" + toDate.getDate() + "/" + toDate.getFullYear(), function (data) { $(".ppn_cal_to .ppn_cal_monthSelect").html ( data ); } );
	}
	else
	{
		toDate = new Date();
		toDate.setDate ( fromDate.getDate() + 1 );
		setToDate ( toDate.getDate(), toDate.getMonth(), toDate.getFullYear(), true );
		$.get("/calendar/inc/double_cal/printMonthYearSelect.php?select=no&monthNames=" + monthNames + "&printDate=" + (toDate.getMonth()+1) + "/" + 1 + "/" + toDate.getFullYear() + "&minDate=" + (toDate.getMonth()+1) + "/" + toDate.getDate() + "/" + toDate.getFullYear(), function (data) { $(".ppn_cal_to .ppn_cal_monthSelect").html ( "<option>" + $('.month_translation').text() + "</option>" + data ); } );
	}	
}

$(document).ready(
	function()
	{			
		setDates ( "", "" );
		
		
		//FUNCTIONS
		$('.ppn_cal_to .ppn_cal_showButton').click ( function () { $('.ppn_cal_from .ppn_cal_popup').hide(); $('.ppn_cal_to .ppn_cal_popup').show(); } );
		$('.ppn_cal_from .ppn_cal_showButton').click ( function () { $('.ppn_cal_to .ppn_cal_popup').hide(); $('.ppn_cal_from .ppn_cal_popup').show(); } );
		
		$('#rs_chk_out_vis').click ( function () { $('.ppn_cal_from .ppn_cal_popup').hide(); $('.ppn_cal_to .ppn_cal_popup').show(); } );
		$('#rs_chk_in_vis').click ( function () { $('.ppn_cal_to .ppn_cal_popup').hide(); $('.ppn_cal_from .ppn_cal_popup').show(); } );
		
		$('.ppn_cal_closeButton').click ( function () { $('.ppn_cal_popup').hide(); } );
		
		$(document).click (
			function(e)
			{
				var $clicked=$(e.target);
				if ( ! ( $clicked.is('#rs_chk_out_vis') || $clicked.is('#rs_chk_in_vis') || $clicked.is('img') || $clicked.parents().is('.ppn_cal') || $clicked.parents().is('.ppn_cal_popupHeader') || $clicked.parents().is('.ppn_cal_popupBody') ) )
				{
					$('.ppn_cal_popup').hide();
				}
			}
		);
	}
);