$(document).ready(function(){
	var result = '',
	title_text = '',
	link_text = '',
	summary_text = '',
	startDate_text = '',
	startMon_text = '',
	startDay_text = '',
	endDate_text = '',
	time_text = '',
	selet = 0,
	xmlLength = '',
	indexA = '',
	indexB = '',
	indexC = '',
	month = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'],
	checking = 1,
	$events = $('.events'),
	$mon = $('.events .mon'),
	$day = $('.events .day'),
	$link_text = $('.events .event_name'),
	$time = $('.events .event_info'),
	$next = $('.events .links_next'),
	$prev = $('.events .links_prev'),
	objLength = $events.find('ul > li').length;
	$next.on("click", function(){
		if (checking&&selet < xmlLength) {
			$('.links_prev').removeClass('disabled')
			selet+=objLength;
			$events.find('ul').fadeOut("fast", function () {
				resetText()
				$events.find('ul').fadeIn("fast" ,getEvent(selet));
			});
			if ((xmlLength-selet) == objLength) {
				$('.links_next').addClass('disabled')
				checking = 0;
			} else if ((xmlLength-selet) < objLength) {
				$('.links_next').addClass('disabled')
				checking = 0;
			}
		}
		return false;
	});
	$prev.on("click", function(){
		if (selet > 0) {
			$('.links_next').removeClass('disabled')
			checking = 1;
			selet-=objLength;
			$events.find('ul').fadeOut("fast", function () {
			   $events.find('ul').fadeIn("fast" ,getEvent(selet));
			});
			if ((xmlLength-selet) == xmlLength ) {
				$('.links_prev').addClass('disabled')
			}
		}
		return false;
	});
	getEvent(selet);
	function resetText() {
		$events.find('li').find($mon).html('');
		$events.find('li').find($day).html('');
		$events.find('li').find($link_text).html('');
		$events.find('li').find($link_text).attr('href', '');
		$events.find('li').find($time).html('');
	}
	function getEvent(selet){
		$.ajax({
			type: "GET",
			url: "/static/nccu_calendar.xml",
			dataType: "xml",
			timeout: 1000,
			error: function(xml){
				$events.find('.links_next').addClass('disabled')
				$events.find('.error_mag').show();
				$events.find('ul').hide();
				$events.find('.error_mag').html("讀取錯誤");
			},
			success: function(xml) {
				xmlLength = $(xml).find('entry').length;
				$(xml).find('entry').slice(selet, selet+objLength).each(function(i){
					title_text = $(this).find('title').text()
					link_text = $(this).find('link').attr('href')
					summary_text = $(this).find('summary').text()
					indexA = summary_text.indexOf("-");
					indexB = summary_text.indexOf(" ");
					indexC = summary_text.indexOf("/");
					//清除前後空白字元
					summary_text = jQuery.trim(summary_text);
					//篩選標題
					title_text = title_text.slice(0, 16)
					if (summary_text.length == 31) {
						startDate_text = summary_text.slice(0, indexA);
						startMon_text = startDate_text.slice((indexC+1), (indexC+3));
						startDay_text = startDate_text.slice(7);
						endDate_text = summary_text.slice((indexA+1), indexB);
						time_text = summary_text.slice((indexB+1));
					} else {
						startDate_text = summary_text.slice(0, indexB);
						startMon_text = startDate_text.slice((indexC+1), (indexC+3));
						startDay_text = startDate_text.slice(7);
						endDate_text = '';
						time_text = summary_text.slice((indexB+1));
					}
					if(startMon_text.slice(0, 1) == '0'){
						startMon_text = month[startMon_text.slice(1)-1];
					} else {
						startMon_text = month[startMon_text-1];
					}
					$events.find('li').eq(i).find($mon).html(startMon_text);
					$events.find('li').eq(i).find($day).html(startDay_text);
					$events.find('li').eq(i).find($link_text).html(title_text+"…");
					$events.find('li').eq(i).find($link_text).attr('href', link_text);
					$events.find('li').eq(i).find($time).html('時間:'+time_text);
					//console.log();
					/*$('<li></li>')
					.html('<div class="event_date"><span class="mon">'+startMon_text+'</span><span class="day">'+startDay_text+'</div>')
					.appendTo('.events ul');*/
				});//close each(
			}
		}); //close $.ajax(
	}
});