function EventCalendar(){}
EventCalendar.prototype={
	_tabSelect:"",
	_dateDivSelect:"",
	_theYear:new Date().getFullYear(), //定义年的变量的初始值
	_theMonth:new Date().getMonth()+1,//定义月的变量的初始值
	_theDay:1,
	_today:new Date().getDate(),//当天日期
	_monthDayArr:new Array(31,28,31,30,31,30,31,31,30,31,30,31),//定义阳历中每个月的最大天数
	_monthNameArr:new Array('','January','February','March','April','May','June','July','August','September','October','November','December'),
	
	isLeapYear:function(year)            //判断是否闰平年
  	{
    	if (0==year%4&&((year%100!=0)||(year%400==0))) return true;else return false;
  	},
  	
  	getMonthCount:function(year,month)  //获取当月天数,闰年二月为29天
  	{
    	var c=this._monthDayArr[month-1];
		if((month==2)&&this.isLeapYear(year)){
			c++;
		}
		return c;
  	},

  	getDayOfWeek:function(year,month,day)//获取某天是星期几
  	{
    	var dt=new Date(year,month-1,day).getDay();
    	return dt;
  	},
  	
  	goToToday:function() //返回当前月份.
  	{
  		this._theYear = new Date().getFullYear();
  		this._theMonth = new Date().getMonth()+1;
  		this.createCalendar(this._tabSelect,this._dateDivSelect);
  	},
  	
  	prevM:function()  //往前翻月份
  	{
    	if(this._theMonth>1)
    	{
    		this._theMonth--;
    	}else{
    		this._theYear--;
    		this._theMonth=12;
    	}
    	this.createCalendar(this._tabSelect,this._dateDivSelect);
  	},
  	
	nextM:function()  //往后翻月份
  	{
    	if(this._theMonth==12)
    	{
    		this._theYear++;
    		this._theMonth=1;
    	}else{
    		this._theMonth++;
    	}
    	this.createCalendar(this._tabSelect,this._dateDivSelect);
  	},
  	
  	getOffset:function(){
  		var offset = this.getDayOfWeek(this._theYear,this._theMonth,this._theDay);
  		return 0==offset?6:offset-1;
  	},
  	
  	isToday:function(today){
  		return ((new Date().getDate() == today)&&(new Date().getMonth()+1 == this._theMonth)&&(new Date().getFullYear() == this._theYear))
  	},
  	
  	getClass:function(day){
  		if(this.isToday(day)){
  			return " class=\"currday\"";
  		}
  		if(day == undefined){
  			return " class=\"padding\"";
  		}else{
  			return " ";
  		}
  	},
  	
  	insertDayNumber:function(day){
  		if(day != undefined){
  			return day;
  		}else{
  			return "";
  		}
  	},

  	createCalendar:function(tabSelect,dateDivSelect){
  		this._tabSelect = tabSelect;
  		this._dateDivSelect = dateDivSelect;
  		$(dateDivSelect).html(this._monthNameArr[this._theMonth] + " " + this._theYear);
  		var monthCount = this.getMonthCount(this._theYear,this._theMonth);
  		var offset=this.getOffset();
  		
  		var dateArr = new Array(42);
  		
  		for ( var int = 0; int < monthCount ; int++) {
  			dateArr[(int+offset)] = int + 1;
		}
  		
  		var caHtml="";
  		for ( var int = 0; int < 6; int++) {
			caHtml+="<tr style='height:29px;'>";
				for ( var w = 0; w < 7; w++) {
					var dateNumber = (int*7)+w;
					caHtml+="<td"+(this.getClass(dateArr[dateNumber]))+">"+this.insertDayNumber(dateArr[dateNumber])+"</td>";
				}
			caHtml+="</tr>";
		}
  		$(tabSelect).html(caHtml);
  		this.getEventJson();
  	},
  	
  	getEventJson:function(){
  		var url = "eventCalendar.html?date="+this._theYear+this._theMonth;
  		var offset = this.getOffset();
  		var theYear = this._theYear;
  		var theMonth = this._theMonth;
  		var theMonthName = this._monthNameArr[theMonth];
  		
  		$.ajax({
  			type: "GET", 
  			url: url, 
  			dataType : "json",
  			success: function(data){
  				if(null != data.ec){
  					for ( var int = 0; int < data.ec.length; int++) {
  						var monthDay = parseInt(data.ec[int].dayOfMonth);
  	  					insertEvent(theYear,theMonth,theMonthName,monthDay,data.ec[int].startTime,data.ec[int].location,data.ec[int].title,data.ec[int].summary,data.ec[int].uri);
  					}
  					$('td a.gotevent').tooltip({ offset: [30, 2], effect:'slide' }).dynamic({ bottom: { direction: 'down', bounce: true } });
  	  				bindEvent();
  				}
  		  }
  	    });
  	}
}

function bindEvent() {
	$('.event, .cdevent').each(function () {
		// options
		var distance = 10;
		var time = 250;
		var hideDelay = 500;

		var hideDelayTimer = null;

		// tracker
		var beingShown = false;
		var shown = false;

		var trigger = $(this);
		var popup = $('.tooltip span', this).css('opacity', 0);

		// set the mouseover and mouseout on both element
		$([trigger.get(0), popup.get(0)]).mouseover(function () {
			// stops the hide event if we move from the trigger to the popup element
			if (hideDelayTimer) clearTimeout(hideDelayTimer);

			// don't trigger the animation again if we're being shown, or already visible
			if (beingShown || shown) {
				return;
			} else {
				beingShown = true;
				var arVersion = navigator.appVersion.split("MSIE")
				var version = parseFloat(arVersion[1])
				var ie6fix = 0;
				if ((version >= 5.5 && version < 7.0) && (document.body.filters)){
					ie6fix = 20;
				}

				// reset position of popup box
				popup.css({
					bottom: (20+ie6fix),
					left: -76,
					display: 'block' // brings the popup back in to view
				})

				// (we're using chaining on the popup) now animate it's opacity and position
				.animate({
					bottom: '+=' + distance + 'px',
					opacity: 1
				}, time, 'swing', function() {
					// once the animation is complete, set the tracker variables
					beingShown = false;
					shown = true;
				});
			}
		}).mouseout(function () {
			// reset the timer if we get fired again - avoids double animations
			if (hideDelayTimer) clearTimeout(hideDelayTimer);

			// store the timer so that it can be cleared in the mouseover if required
			hideDelayTimer = setTimeout(function () {
				hideDelayTimer = null;
				popup.animate({
					bottom: '-=' + distance + 'px',
					opacity: 0
				}, time, 'swing', function () {
					// once the animate is complete, set the tracker variables
					shown = false;
					// hide the popup entirely after the effect (opacity alone doesn't do the job)
					popup.css('display', 'none');
				});
			}, hideDelay);
		});
	});
}