/* MarcGrabanski.com */

/* Pop-Up Calendar Built from Scratch by Marc Grabanski */
/* charset in zh-tw : edit by hsin */
var popUpCal_2 = {
    selectedMonth: new Date().getMonth(), // 0-11
    selectedYear: new Date().getFullYear(), // 4-digit year
    selectedDay: new Date().getDate(),
    calendarId: 'calendarDiv',
    inputClass: 'calendarSeleactDate',
	init: function ( inCalDiv ) {
		this.calendarId = inCalDiv ;

				popUpCal_2.selectedMonth = new Date().getMonth();

                popUpCal_2.drawCalendar(this); 
                popUpCal_2.setupLinks(this);
		/*
        var x = getElementsByClass(popUpCal.inputClass, document, 'input');
        var y = document.getElementById(popUpCal.calendarId);
        // set the calendar position based on the input position
        for (var i=0; i<x.length; i++) {
            x[i].onfocus = function () {
				popUpCal.selectedMonth = new Date().getMonth();
                setPos(this, y); // setPos(targetObj,moveObj)
                y.style.display = 'block';
                popUpCal.drawCalendar(this); 
                popUpCal.setupLinks(this);
            }
        }
		*/
    },
    
    drawCalendar: function (inputObj) {
		
		var html = '';
		html = '<a  style="display:none" id="closeCalender_2">Close</a>';//ORIGINAL: html = '<a id="closeCalender">Close Calendar</a>';
		html += '<table cellpadding="0" cellspacing="0" id="linksTable_2" style="display:none"><tr>';
    html += '	<td><a id="prevMonth_2">< 前一個月</a></td>';//ORIGINAL: html += '	<td><a id="prevMonth"><< Prev</a></td>';
		html += '	<td><a id="nextMonth_2">下一個月 ></a></td>';//ORIGINAL: html += '	<td><a id="nextMonth">Next >></a></td>';
		html += '</tr></table>';
/*
*/
		html += '<table id="calendar_2" cellpadding="0" cellspacing="0" align="center"><tr>';
		html += '<th colspan="7" class="calendarHeader_2" ><table width="100%" border="0" cellspacing="0" cellpadding="0"><tr>';
		html += '<td style="cursor:pointer; width:30px; text-align:center" title="前一個月" onclick="sub_prev_btm(); "> <img src="images/cal_b1.jpg" border="0"/> </td>';
		html += '<td style="cursor:pointer; width:30px; text-align:center" title="今天"  onclick="sub_go_home();" > <img src="images/cal_b2.jpg" border="0"/> </td>';
		html += '<td style="cursor:pointer; width:30px; text-align:center" title="下一個月"  onclick="sub_next_btm();" > <img src="images/cal_b3.jpg" border="0"/> </td>';
		html += '<td align="right"> '+getMonthName(popUpCal_2.selectedMonth)+' '+popUpCal_2.selectedYear+' </td></tr></table></th>';//ORIGINAL: html += '<th colspan="7" class="calendarHeader">'+getMonthName(popUpCal_2.selectedMonth)+' '+popUpCal_2.selectedYear+'</th>';
		html += '</tr><tr class="weekDaysTitleRow_2">';
        var weekDays = new Array('日','一','二','三','四','五','六');//ORIGINAL: var weekDays = new Array('S','M','T','W','T','F','S');
        for (var j=0; j<weekDays.length; j++) {
			html += '<td>'+weekDays[j]+'</td>';
        }
		
        var daysInMonth = getDaysInMonth(popUpCal_2.selectedYear, popUpCal_2.selectedMonth);
        var startDay = getFirstDayofMonth(popUpCal_2.selectedYear, popUpCal_2.selectedMonth);
        var numRows = 0;
        var 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
        if (startDay != 7) {
            var noPrintDays = startDay + 1; 
        } else {
            var noPrintDays = 0; // if sunday print right away	
        }
		var today = new Date().getDate();
		var thisMonth = new Date().getMonth();
		var thisYear = new Date().getFullYear();
        // create calendar rows
        for (var e=0; e<numRows; e++) {
			html += '<tr class="weekDaysRow_2">';
            // create calendar days
            for (var f=0; f<7; f++) {
				if ( (printDate == today) 
					 && (popUpCal_2.selectedYear == thisYear) 
					 && (popUpCal_2.selectedMonth == thisMonth) 
					 && (noPrintDays == 0)) {
					html += '<td id="today_2" class="weekDaysCell_2">';
				} else {
                	html += '<td class="weekDaysCell_2">';
				}
                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 = document.getElementById(popUpCal_2.calendarId);
        calendarDiv.innerHTML = html;
        
        // close button link
        document.getElementById('closeCalender_2').onclick = function () {
            calendarDiv.style.display = '';
        }
/*		
		// setup next and previous links
        document.getElementById('prevMonth_2').onclick = function () {
            popUpCal_2.selectedMonth--;
            if (popUpCal_2.selectedMonth < 0) {
                popUpCal_2.selectedMonth = 11;
                popUpCal_2.selectedYear--;
            }
            popUpCal_2.drawCalendar(inputObj); 
            popUpCal_2.setupLinks(inputObj);
        }
        document.getElementById('nextMonth_2').onclick = function () {
            popUpCal_2.selectedMonth++;
            if (popUpCal_2.selectedMonth > 11) {
                popUpCal_2.selectedMonth = 0;
                popUpCal_2.selectedYear++;
            }
            popUpCal_2.drawCalendar(inputObj); 
            popUpCal_2.setupLinks(inputObj);
        }
*/

		sub_go_home = function ()
		{
			popUpCal_2.selectedMonth = new Date().getMonth(), // 0-11
			popUpCal_2.selectedYear = new Date().getFullYear(), // 4-digit year
			popUpCal_2.selectedDay = new Date().getDate(),
			
			popUpCal_2.drawCalendar(this); 
			popUpCal_2.setupLinks(this);
			
		}

        sub_prev_btm = function () {
            popUpCal_2.selectedMonth--;
            if (popUpCal_2.selectedMonth < 0) {
                popUpCal_2.selectedMonth = 11;
                popUpCal_2.selectedYear--;
            }

			popUpCal_2.drawCalendar(inputObj); 
            popUpCal_2.setupLinks(inputObj);
        }
		
        sub_next_btm = function () {
            popUpCal_2.selectedMonth++;
            if (popUpCal_2.selectedMonth > 11) {
                popUpCal_2.selectedMonth = 0;
                popUpCal_2.selectedYear++;
            }
            
			
			popUpCal_2.drawCalendar(inputObj); 
            popUpCal_2.setupLinks(inputObj);
        }



    }, // end drawCalendar function
    
    setupLinks: function (inputObj) {
        // set up link events on calendar table
        var y = document.getElementById('calendar_2');
        var x = y.getElementsByTagName('a');
        for (var i=0; i<x.length; i++) {
            x[i].onmouseover = function () {
                this.parentNode.className = 'weekDaysCell_2Over';
            }
            x[i].onmouseout = function () {
                this.parentNode.className = 'weekDaysCell_2';
            }
            x[i].onclick = function () {
                document.getElementById(popUpCal_2.calendarId).style.display = '';
                popUpCal_2.selectedDay = this.innerHTML;
				

                inputObj.value = formatDate(popUpCal_2.selectedDay, popUpCal_2.selectedMonth, popUpCal_2.selectedYear);		

				//alert( inputObj.value );

				location.href = 'event_list_day.php?oid=' + inputObj.value ;


            }
        }
    }
    
}
	/*
	// Add calendar event that has wide browser support
	if ( typeof window.addEventListener != "undefined" )
		window.addEventListener( "load", popUpCal_2.init, false );
	else if ( typeof window.attachEvent != "undefined" )
		window.attachEvent( "onload", popUpCal_2.init );
	else {
		if ( window.onload != null ) {
			var oldOnload = window.onload;
			window.onload = function ( e ) {
				oldOnload( e );
				popUpCal_2.init();
			};
		}
		else
			window.onload = popUpCal_2.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
    var dateString = Year+'-'+Month+'-'+Day;//ORIGINAL: var dateString = Month+'/'+Day+'/'+Year;
    return dateString;
}

function getMonthName(month) {
    var monthNames = new Array('一月','二月','三月','四月','五月','六月','七月','八月','九月','十月','十一月','十二月');//ORIGINAL: var monthNames = new Array('January','February','March','April','May','June','July','August','September','October','November','December');
    return monthNames[month];
}

function getDayName(day) {
    var dayNames = new Array('星期一','星期二','星期三','星期四','星期五','星期六','星期日');//ORIGINAL: var 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;
}

/* Common Scripts */

function getElementsByClass(searchClass,node,tag) {
    var classElements = new Array();
    if ( node == null ) node = document;
    if ( tag == null ) tag = '*';
    var els = node.getElementsByTagName(tag);
    var elsLen = els.length;
    var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
    for (i = 0, j = 0; i < elsLen; i++) {
        if ( pattern.test(els[i].className) ) {
            classElements[j] = els[i];
            j++;
        }
    }
    return classElements;
}

/* Position Functions */

function setPos(targetObj,moveObj) {
    var coors = findPos(targetObj);
    moveObj.style.position = 'absolute';
    moveObj.style.top = coors[1]+20 + 'px';
    moveObj.style.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];
}
