/* MarcGrabanski.com */
/* Pop-Up Calendar Built from Scratch by Marc Grabanski */
var popUpCal = {
    selectedMonth: new Date().getMonth(), // 0-11
    selectedYear: new Date().getFullYear(), // 4-digit year
    selectedDay: new Date().getDate(),
    /* harmonia */
    currlanguage: $('html').attr('xml:lang'),
	init: function () {		
		$('<div id="calendarDiv"></div>').appendTo("body");
		var x = $('input.calendarSelectDate');
		var y = $('#calendarDiv');
		x.focus( function () {
			popUpCal.input = $(this);
			y.hide();
			setPos(this, y); // setPos(targetObj,moveObj)
			setDateFromField();
			popUpCal.drawCalendar(); 
		});
    }, // end init function
    drawCalendar: function () {
		html = (popUpCal.currlanguage=='it')?'<a id="closeCalender">Chiudi</a>':'<a id="closeCalender">Close</a>';
		html += '<table cellpadding="0" cellspacing="0" id="linksTable"><tr>';
    html += (popUpCal.currlanguage=='it')?'	<td><a id="prevMonth"><< Prec</a></td>':'	<td><a id="prevMonth"><< Prev</a></td>';
		html += (popUpCal.currlanguage=='it')?'	<td><a id="nextMonth">Succ >></a></td>':'	<td><a id="nextMonth">Next >></a></td>';
		html += '</tr></table>';
		html += '<table id="calendar" cellpadding="0" cellspacing="0"><tr>';
		html += '<th colspan="7" class="calendarHeader">'+getMonthName(popUpCal.selectedMonth)+' '+popUpCal.selectedYear+'</th>';
		html += '</tr><tr class="weekDaysTitleRow">';
        var weekDays = (popUpCal.currlanguage=='it')?new Array('D','L','M','M','G','V','S'):new Array('S','M','T','W','T','F','S');
        for (var j=0; j<weekDays.length; j++) {
			html += '<td>'+weekDays[j]+'</td>';
        }
        daysInMonth = getDaysInMonth(popUpCal.selectedYear, popUpCal.selectedMonth);
        startDay = getFirstDayofMonth(popUpCal.selectedYear, popUpCal.selectedMonth);
        numRows = 0;
        printDate = 1;
        if (startDay != 7) { numRows = Math.ceil(((startDay+1)+(daysInMonth))/7); } // calculate the number of rows to generate
        // calculate number of days before calendar starts
        (startDay != 7) ? noPrintDays = startDay + 1 : noPrintDays = 0; // if sunday print right away	
		// function with cruft for figuring out what day to highlight
		/* harmonia */
		selectedDate = popUpCal.input.val().split('-');
		if (selectedDate.length == 3) {
			thisYear = parseFloat(selectedDate[0]);
			thisMonth = parseFloat(selectedDate[1])-1;
			thisDay = parseFloat(selectedDate[2]);
			/* fine harmonia */
		} else {
			thisDay = new Date().getDate();
			thisMonth = new Date().getMonth();
			thisYear = new Date().getFullYear();
		}
        // create calendar rows
        for (var e=0; e<numRows; e++) {
			html += '<tr class="weekDaysRow">';
            // create calendar days
            for (var f=0; f<7; f++) {
				if ( (printDate == thisDay) 
					 && (popUpCal.selectedYear == thisYear) 
					 && (popUpCal.selectedMonth == thisMonth) 
					 && (noPrintDays == 0)) {
					html += '<td id="today" class="weekDaysCell">';
				} else {
                	html += '<td class="weekDaysCell">';
				}
                if (noPrintDays == 0) {
					if (printDate <= daysInMonth) {
						html += '<a>'+printDate+'</a>';
					}
                    printDate++;
                }
                html += '</td>';
                if(noPrintDays > 0) noPrintDays--;
            }
            html += '</tr>';
        }
		html += '</table>';
        // add calendar to element to calendar Div
        var calendarDiv = $('#calendarDiv');
		calendarDiv.empty().append(html).show("medium");
		popUpCal.setupLinks();
        // close button link
        $('#closeCalender').click( function () {
            calendarDiv.hide(250);
        });
		// setup next and previous links
		$('#prevMonth').click( function () {
            popUpCal.selectedMonth--;
            if (popUpCal.selectedMonth < 0) {
                popUpCal.selectedMonth = 11;
                popUpCal.selectedYear--;
            }
            popUpCal.drawCalendar(); 
        });
		$('#nextMonth').click( function () {
			popUpCal.selectedMonth++;
            if (popUpCal.selectedMonth > 11) {
                popUpCal.selectedMonth = 0;
                popUpCal.selectedYear++;
            }
            popUpCal.drawCalendar(); 
        });
    }, // end drawCalendar function
    setupLinks: function () {
        // set up link events on calendar table
        var x = $('#calendar a');
		x.mouseover( function () {
			this.parentNode.className = 'weekDaysCellOver';
		});
		x.mouseout( function () {
			this.parentNode.className = 'weekDaysCell';
        });
		x.click( function () {
                $('#calendarDiv').hide(250);
                popUpCal.selectedDay = $(this).html();
				setVal = formatDate(popUpCal.selectedDay, popUpCal.selectedMonth, popUpCal.selectedYear);
                popUpCal.input.val(setVal);		
        });
    } // end setupLinks function
}
// Initialize the calendar
$(document).ready(function(){
	 /* harmonia - se siamo nella pagina della timeline c'e' un conflitto e non abilito il date picker */
	 if (document.getElementById('tl')) return;
   popUpCal.init();
});
/* Functions Dealing with Dates */
function formatDate(Day, Month, Year) {
    Month++; // adjust javascript month
    if (Month <10) Month = '0'+Month; // add a zero if less than 10
    if (Day < 10) Day = '0'+Day; // add a zero if less than 10
    /* harmonia */
    /*var dateString = Month+'/'+Day+'/'+Year;*/
    var dateString = Year+'-'+Month+'-'+Day;
    return dateString;
}
function setDateFromField() {
	/* harmonia */
	selectedDate = popUpCal.input.val().split('-');
	if (selectedDate.length == 3) {
		popUpCal.selectedYear = parseFloat(selectedDate[0]);
		popUpCal.selectedMonth = parseFloat(selectedDate[1])-1;
		popUpCal.selectedDay = parseFloat(selectedDate[2]);
		/* fine harmonia */
	} else {
		popUpCal.selectedDay = new Date().getDate();
		popUpCal.selectedMonth = new Date().getMonth();
		popUpCal.selectedYear = new Date().getFullYear();
	}
}
function getMonthName(month) {
		var monthNames;
		if (popUpCal.currlanguage == 'it') {
    	monthNames = new Array('Gennaio','Febbraio','Marzo','Aprile','Maggio','Giugno','Luglio','Agosto','Settembre','Ottobre','Novembre','Dicembre');
	  } else {
	  	monthNames = new Array('January','February','March','April','May','June','July','August','September','October','November','December');
  	}
    return monthNames[month];
}
function getDayName(day) {
    var dayNames;
		if (popUpCal.currlanguage == 'it') {
			dayNames = new Array('Lunedi','Martedi','Mercoledi','Giovedi','Venerdi','Sabato','Domenica');
		} else {
			dayNames = new Array('Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday');
		}
    return dayNames[day];
}
function getDaysInMonth(year, month) {
    return 32 - new Date(year, month, 32).getDate();
}
function getFirstDayofMonth(year, month) {
    var day;
    day = new Date(year, month, 0).getDay();
    return day;
}
/* Position Functions */
function setPos(targetObj,moveObj) {
    var coors = findPos(targetObj);
    moveObj.css('position','absolute');
    moveObj.css('top',coors[1]+18+'px');
    moveObj.css('left',coors[0]+'px');
}
function findPos(obj) {
    var curleft = curtop = 0;
    if (obj.offsetParent) {
        curleft = obj.offsetLeft
        curtop = obj.offsetTop
        while (obj = obj.offsetParent) {
            curleft += obj.offsetLeft
            curtop += obj.offsetTop
        }
    }
    return [curleft,curtop];
}