var monthArrayLong = new Array('Ocak', '&#350ubat', 'Mart', 'Nisan', 'May&#305s', 'Haziran', 'Temmuz', 'A&#287ustos', 'Eylül', 'Ekim', 'Kas&#305m', 'Aral&#305k');

function buildDatePicker(yr, mn, dy) {
	var thisDay = new Date(yr, mn, dy);
	var monthnumber = thisDay.getMonth();
	var monthday = thisDay.getDate();
	var year = thisDay.getYear();
	var fullYear = thisDay.getFullYear();
	var monthName = monthArrayLong[monthnumber];

	html = "<ul class='top'>";
	html += " <li class='onceYIL'>" + btnYear(thisDay, -1) + "</li><li class='onceAY'>" + btnMonth(thisDay, -1) + "</li>";
	html += " <li class='sonraYIL'>" + btnYear(thisDay, 1) + "</li><li class='sonraAY'>" + btnMonth(thisDay, 1) + "</li>";
	html += " <li class='ay'>" + monthName + " " + fullYear + "</li>";
	html += "</ul><ul class='hafta'><li>Pts</li><li>Sal</li><li>Çrş</li><li>Prş</li><li>Cm</li><li class='tatil'>Cts</li><li class='tatil'>Pz</li></ul><ul class='gunler'>";

	var thisDate = false;
	if ((today.getFullYear() == fullYear) && (today.getMonth() == monthnumber)) thisDate = true;

	thisDay.setDate(1);
	tDay = thisDay.getDay() == 0 ? 7 : thisDay.getDay();
	for (j = 1; j < tDay; j++) html += "<li></li>";

	do {
	    if (thisDate && (thisDay.getDate() == today.getDate())) html += "<li class='today'><a href='#' onClick='goTo(\"" + getDateString(thisDay) + "\");return false;'>" + thisDay.getDate() + "</a></li>";
		else {
			altArchiveTxt = thisDay.getDate() + " " + monthName + " " + fullYear;
			if ((today >= thisDay) && (thisDay >= startDate))
				html += "<li><a href='#' alt='" + altArchiveTxt + "' title='" + altArchiveTxt + "' onClick='goTo(\"" + getDateString(thisDay) + "\");return false;'>" + thisDay.getDate() + "</a></li>";
			else html += "<li>" + thisDay.getDate() + "</li>";
		}
		thisDay.setDate(thisDay.getDate() + 1);
	} while (thisDay.getDate() > 1)
	html += "</ul>";
	
	$('.arc').html(html);
}

function btnMonth(dateVal, adjust) {
	var newMonth = (dateVal.getMonth() + adjust) % 12;
	var newYear = dateVal.getFullYear() + parseInt((dateVal.getMonth() + adjust) / 12);
	if (newMonth < 0) {
		newMonth += 12; newYear += -1;
	}
	altTxt = "sonraki ay";
	if (adjust <= 0) altTxt = "önceki ay";
	var adjustedDate = new Date(newYear, newMonth, 31); var adjustedDate2 = new Date(newYear, newMonth, 1);
	if((adjustedDate2 > today) || (adjustedDate < startDate)) return "";
	return "<a href='#' alt='" + altTxt + "' title='" + altTxt + "' onClick='buildDatePicker(" + newYear + "," + newMonth + ",1);return false;'>'" + altTxt + "'</a>";
}

function btnYear(dateVal, adjust) {
	newYear = dateVal.getFullYear() + adjust;
	newMonth = dateVal.getMonth();
	altTxt = "sonraki yıl";
	if (adjust <= 0) altTxt = "önceki yıl";
	var adjustedDate = new Date(newYear, newMonth, 31); var adjustedDate2 = new Date(newYear, newMonth, 1);
	if((adjustedDate2 > today) || (adjustedDate < startDate)) return "";
	return "<a href='#' alt='" + altTxt + "' title='" + altTxt + "' onClick='buildDatePicker(" + newYear + "," + newMonth + ",1);return false;'>'" + altTxt + "'</a>";
}

function getDateString(dateVal) {
	var dayString = "00" + dateVal.getDate();
	var monthString = "00" + (dateVal.getMonth() + 1);
	dayString = dayString.substring(dayString.length - 2);
	monthString = monthString.substring(monthString.length - 2);
	return dateVal.getFullYear() + "-" + monthString + "-" + dayString;
}

function goTo(url) {
    var url = '/egazete/' + '?sayfa=1&tarih=' + url;
	window.location.href = url;
}

$(document).ready(function() {
	buildDatePicker(today.getFullYear(), today.getMonth(), today.getDate());
});