var initMonth = 0;
var initDayOfMonth = 0;
var currentDisplayMonth = 0;
var currentDisplayYear = 0;
var now = new Date();
function initCalendar()
{
	initDayOfMonth = now.getDate();
	currentDisplayMonth = now.getMonth();
	initMonth = now.getMonth();
	currentDisplayYear = now.getFullYear();
	refreshCalendar();
	refreshRemainigDaysText();
	setInterval(refreshCalendarAndRmainingText, 60000);
	$('#calendar').show();
	//now.setMonth(9);
	//now.setDate(31);
}

function refreshCalendarAndRmainingText()
{
	now = new Date();
	
	//now.setHours(now.getHours()+24,now.getMinutes(),0,1);
	//now.setMonth(11);	
	//now.setFullYear(2011);
	
	if (initMonth != now.getMonth())
	{
		currentDisplayMonth = now.getMonth();
		currentDisplayYear = now.getFullYear();
		initMonth = now.getMonth();
	}
	
	if (initDayOfMonth != now.getDate())
	{	
		refreshCalendar();
		initDayOfMonth = now.getDate();
	}
	refreshRemainigDaysText();

}

function prevMonth()
{
	var minYear = 0;
	if (now.getMonth() <= 6)
		minYear = now.getFullYear() - 1;
	else
		minYear = now.getFullYear();
			
	currentDisplayMonth--;
	if (currentDisplayMonth < 0)
	{
		currentDisplayMonth = 11;
		currentDisplayYear--;

		if (currentDisplayYear < minYear)
		{
			currentDisplayYear = minYear;
			currentDisplayMonth = 0;
		}		
	}
	
	if (currentDisplayMonth == 0 && currentDisplayYear == minYear)
	{
		$('#calendar-prevmonth').fadeOut('fast');
	}
	else
		$('#calendar-nextmonth').fadeIn('fast');
	
	refreshCalendar();
}

function nextMonth()
{
	var maxYear = 0;
	if (now.getMonth() >= 6)
		maxYear = now.getFullYear() + 1;
	else
		maxYear = now.getFullYear();
			
	currentDisplayMonth++;
	if (currentDisplayMonth > 11)
	{
		currentDisplayMonth = 0;
		currentDisplayYear++;
		if (currentDisplayYear > maxYear)
		{
			currentDisplayYear = maxYear;
			currentDisplayMonth = 11;
		}
	}
	
	if (currentDisplayMonth == 11 && currentDisplayYear == maxYear)
	{
		$('#calendar-nextmonth').fadeOut('fast');
	}
	else
		$('#calendar-prevmonth').fadeIn('fast');

		
	
	refreshCalendar();
}

function refreshCalendar()
{
	var month=new Array(12);
	month[0]="JANVĀRIS";
	month[1]="FEBRUĀRIS";
	month[2]="MARTS";
	month[3]="APRĪLIS";
	month[4]="MAIJS";
	month[5]="JŪNIJS";
	month[6]="JŪLIJS";
	month[7]="AUGUSTS";
	month[8]="SEPTEMBRIS";
	month[9]="OKTOBRIS";
	month[10]="NOVEMBRIS";
	month[11]="DECEMBRIS";
	//d.getMonth()
	$('#month').html(month[currentDisplayMonth] + ' ' + currentDisplayYear);
	var daysinmonth = daysInMonth(currentDisplayMonth + 1, currentDisplayYear);
	
	var theFirst = new Date();
	theFirst.setMonth(currentDisplayMonth);
	theFirst.setDate(1);
	theFirst.setYear(currentDisplayYear);
	var firstday = theFirst.getDay();
	if (firstday == 0)
		firstday = 7;
		
	var d = 0;
	var row6visible = false;
	var holidaytext = '';
	$('.calendar-cell').each(function(index) {
		if (index+1 >= firstday)
		{
			d++;
			if (d > daysinmonth)
			{
				$(this).text('');
				$(this).removeClass('activedate');
				$(this).removeClass('today');
				$(this).removeClass('christmas');
				$(this).removeAttr('title');
				$(this).removeClass('specialholiday');
			}
			else
			{
				
				$(this).text(d);
				$(this).addClass('activedate');
				//$(this).attr('title', theFirst);
				holidaytext = isholiday(currentDisplayMonth + 1, d);
				if (holidaytext.length > 0)
				{
					if (currentDisplayMonth == 11 && (d == 24 || d == 25 || d == 26 ))
						$(this).addClass('christmas');
					else
						$(this).addClass('specialholiday');

					$(this).attr('title', holidaytext);
				}	
				else
				{
					$(this).removeClass('specialholiday');
					$(this).removeAttr('title');
					$(this).removeClass('christmas');
				}
				
				if (currentDisplayYear == now.getFullYear() && currentDisplayMonth == now.getMonth() && d == now.getDate())
					$(this).addClass('today');
				else
					$(this).removeClass('today');
				
				//if (currentDisplayMonth == 11 && (d == 24 || d == 25 || d == 26 ))
				//{
				//	$(this).addClass('christmas');
				//	$(this).attr('title', 'Ziemassvētki');
				//}
				//else
				//{
				//	$(this).removeClass('christmas');
				//	$(this).removeAttr('title');
				//}
				
				if (index == 35)
					row6visible = true;
			}
		}
		else
		{
			$(this).text('');
			$(this).removeClass('activedate');
			$(this).removeClass('today');
			$(this).removeClass('christmas');
			$(this).removeAttr('title');
			$(this).removeClass('specialholiday');
		}	
	});
	
	
	if (row6visible == true)
		$('.row6').show();
	else
		$('.row6').hide();
	
	
}

function daysInMonth(month, year) {
    return new Date(year, month, 0).getDate();
}

function isholiday(moonth, dayy)
{
	//currentDisplayYear
	//currentDisplayMonth
	//dayofmonth
	if (moonth == 1 && dayy == 1)
		return "Jaungada diena";	
		
	if (currentDisplayYear == 2011 && moonth == 4)
	{
		if (dayy == 22)
			return "Lielā piektdiena";		
			
		if (dayy == 24)
			return "Pirmās Lieldienas";	
			
		if (dayy == 25)
			return "Otrās Lieldienas";			
	}
	
	if (currentDisplayYear == 2012 && moonth == 4)
	{
		if (dayy == 6)
			return "Lielā piektdiena";		
			
		if (dayy == 8)
			return "Pirmās Lieldienas";	
			
		if (dayy == 9)
			return "Otrās Lieldienas";			
	}
	
	if (moonth == 5 && dayy == 1)
		return "Latvijas Republikas Satversmes sapulces sasaukšanas diena. Darba svētki.";	
		
	if (moonth == 5 && dayy == 4)
		return "Latvijas Republikas Neatkarības deklarācijas pasludināšanas diena.";	
		
	if (moonth == 6 && dayy == 23)
		return "Līgo diena";		
		
	if (moonth == 6 && dayy == 24)
		return "Jāņu diena";	
		
	if (moonth == 11 && dayy == 18)
		return "Latvijas Republikas proklamēšanas diena.";
		
	if (moonth == 12 && dayy == 24)
		return "Ziemassvētku vakars";	
		
	if (moonth == 12 && (dayy == 25 || dayy == 26))
		return "Ziemassvētki";
	
	if (moonth == 12 && dayy == 31 )
		return "Vecgada diena";
		
	return "";
}

function refreshRemainigDaysText() {
	//var now = new Date();
	
	//now.setDate(1);
	//now.setMonth(0);
	//now.setHours(23,59,0,1);
	
    var indate = new Date();
	indate.setHours(0,0,0,1);
    indate.setDate(24);
    indate.setMonth(11);
    indate.setFullYear(now.getFullYear());
    var one_day = 1000 * 60 * 60 * 24;
    var diff = Math.ceil((indate.getTime() - now.getTime()) / (one_day));
	
	var daystext = "dienas!";
	var remaintext = "Līdz Ziemassvētkiem atlikušas";
	var neaizmirstitext = "Atceries par eglīti!";
	if (diff % 10 == 1 && diff != 11 && diff != 111 && diff != 211 && diff != 311)
	{
		daystext = "diena!";
		remaintext = "Līdz Ziemassvētkiem atlikusi";
	}

	if (diff < 1)
	{
		if (diff >= -2)
		{
			$('#days-num').html('');
			$('#days-txt').html('Priecīgus Ziemassvētkus!');
			$('#remain-text').html('');
			$('#neaizmirsti-text').html('');
		}
		else
		{
			indate.setHours(0,0,0,1);
			indate.setDate(1);
			indate.setMonth(0);
			indate.setFullYear(now.getFullYear() + 1);
			diff = Math.ceil((indate.getTime() - now.getTime()) / (one_day));
			if (diff > 2)
			{
				daystext = "dienas!";
				remaintext = "Līdz Jaunajam gadam atlikušas";
				if (diff % 10 == 1 && diff != 11 && diff != 111 && diff != 211 && diff != 311)
				{
					daystext = "diena!";
					remaintext = "Līdz Jaunajam gadam atlikusi";
				}
				$('#days-num').html(diff);
				$('#days-txt').html(daystext);
				$('#remain-text').html(remaintext);
				$('#neaizmirsti-text').html(neaizmirstitext);
			}
			else
			{
				var one_minute = 1000 * 60;
				diff = Math.ceil((indate.getTime() - now.getTime()) / (one_minute));
				var minute_reminder = diff % 60; 
				var hours_left = (diff - minute_reminder) / 60;
				var minutes_text = "minūtes";
				daystext = "stundas";
				remaintext = "Līdz Jaunajam gadam atlikušas";
				if (hours_left % 10 == 1 && hours_left != 11 && hours_left != 111 && hours_left != 211 && hours_left != 311)
				{
					daystext = "stunda";
					remaintext = "Līdz Jaunajam gadam atlikusi";
				}
				
				if (minute_reminder % 10 == 1 && minute_reminder != 11)
				{
					minutes_text = "minūte";
					if (hours_left < 1)
						remaintext = "Līdz Jaunajam gadam atlikusi";
				}
				
				
				$('#days-num').html('');
								
				if (hours_left < 1)
				{
					$('#days-num').html(minute_reminder + ' ' + minutes_text);
					$('#days-txt').html('');
				}
				else
					$('#days-txt').html(hours_left + ' ' + daystext + ' un ' + minute_reminder + ' ' + minutes_text);

				$('#remain-text').html(remaintext);
			}
		}
	}
	else
	{
		if (diff >= 337)
		{
			$('#days-num').html('');
			$('#days-txt').html('Laimīgu Jauno gadu!');
			$('#remain-text').html('');
			$('#neaizmirsti-text').html('');
		}
		else
		{
			$('#days-num').html(diff);
			$('#days-txt').html(daystext);
			$('#remain-text').html(remaintext);
			$('#neaizmirsti-text').html(neaizmirstitext);
		}
	}
}
