﻿var daysInMonth = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
var days = new Array("Mo", "Di", "Mi","Do", "Fr", "Sa","So");
var monthEn = new Array("Jan.", "Feb.", "März", "April", "Mai", "Juni","Juli","Aug.","Sep.","Oct.","Nov.","Dec.");
var classTemp;
var today=new getToday();
var year=today.year;
var month=today.month;
var newCal;
var dayMemsArray = null;
var sDay = today.day;

var todayYear = today.year;
var todayMonth = today.month;
var isFirst = 1;

var bStart = true;
var iCount = 0;
var oLastDay = null;

function getDays(month, year){
	if (1 == month){
		return ((0 == year % 4) && (0 != (year % 100))) ||(0 == year % 400) ? 29 : 28;
	}else{
		return daysInMonth[month];
	}
}

function getToday(){
	this.now = new Date();
	this.year = this.now.getFullYear();
	this.month = this.now.getMonth();
	this.day = this.now.getDate();
	this.xq = this.now.getDay();
}

function Calendar() {
	newCal = new Date(year,month,1); 	
	today = new getToday(); 
	var day = -1;
	var startDay = newCal.getDay();	//alert(startDay);

	if(startDay != 0) {
		startDay = startDay-1;
	}else {
		startDay = 6;
	}

	var endDay=getDays(newCal.getMonth(), newCal.getFullYear());	//alert(endDay);
	var daily = 0;
	
	if ((today.year == newCal.getFullYear()) &&(today.month == newCal.getMonth())){
		day = today.day;
	}
	var intDaysInMonth = getDays(newCal.getMonth(), newCal.getFullYear());	//alert(daily);
	
	var li = '';
	for (var intWeek = 0; intWeek < 6;intWeek++){
   
		for (var intDay = 0;intDay < 7;intDay++){

			var cell = $('#txtDay_'+intWeek+'_'+intDay);
			var montemp=(newCal.getMonth()+1)<10?("0"+(newCal.getMonth()+1)):(newCal.getMonth()+1);         

			if ((intDay == startDay) && (0 == daily)){ daily = 1;}
			
			if ((month+1)<10){
				mons="0"+(month+1);
			}else{
				mons=month+1;
			}
			
			dys = (daily<10)?("0"+daily):daily;
            mons = ((month+1)<10)?("0"+(month+1)):(month+1);

			var date2 = year + '-' + mons + '-' + dys;

			if ((daily > 0) && (daily <= intDaysInMonth)){

				li += '<li id="li_' + daily + '" class="day" title="'+date2+'">' + daily + '</li>';

				daily++;
				bStart = false;

			}else{
				li += '<li>&nbsp;</li>';
				if(bStart) {
					iCount++;
				}
			}//End If

		}//END FOR

	}//END FOR

	document.getElementById("ul_days").innerHTML = li;	//alert(li);
	if (today.year==year && today.month==month){
		oLastDay = $(document.getElementById("ul_days").getElementsByTagName("li")[today.day + iCount - 1]);
	}else{
		oLastDay = $(document.getElementById("ul_days").getElementsByTagName("li")[1]);
	}

	document.getElementById('cal_month_en').innerHTML = monthEn[month]+"-"+year;
}


function checkMemInDay(pyear, pmonth){

	var days = getDays(pmonth, pyear);
	$.ajax({
		type: "GET",
		url: "getcount.php?year=" + year + "&month=" + (month+1) + '&days=' + days,
		dateType: "json",
		success: function(result){
			dayMemsArray = eval(result);
			Calendar();
			initCalEvent(sDay);
		},
		error: function(){
			//alert('ajax_error');
		}
	});
	
}//End Function

function checkCountent(DateValue){
	$('#neirong').html('<img src="themes/bby/images/themes/ico_loading.gif" >');
	rands=Math.random();

	$.ajax({
		type: "GET",
		url: "getcontent.php?DateValue=" + DateValue +"&rands="+rands,
		dateType: "html",
		success: function(html){
			if(html != ''){
				$('#neirong').html(html);
			}
			set_thumbs('.calendar-search .cm-content dd .pic', 100, 86);
		},
		error: function(){
			//alert('ajax_error');
		}
	});
	
}//End Function


function initCalEvent(day){
	var daysInMonth = getDays(month, year);

	for(var j=0;j<daysInMonth;j++){

		$('#li_'+(j+1)).attr('class', '');
		if( parseInt(dayMemsArray[j]) > 0 ){
			$('#li_'+(j+1)).attr('class', 'day');
			$('#li_'+(j+1)).css('cursor', 'pointer');
			$('#li_'+(j+1)).mouseover( function(){ buttonOver(this) } );
			$('#li_'+(j+1)).mouseout( function(){ buttonOut(this) } );
			$('#li_'+(j+1)).click( function(){ reSetCalEvent(this) } );
		}else{
			$('#li_'+(j+1)).html((j+1));	
		}
	}

	if( isFirst == 1 ){
		$("#li_"+day).attr('class', 'day-now');
		$('#li_'+day).css('cursor', '');
	}
}//End Function


function setWeek(){
	var li = '';
	if(today.xq == 0)	today.xq = 7;
	for(var d=1;d<8;d++){
		if(today.xq != d){
			li += "<li onMouseOver='btOver(this)' onMouseOut='btOut(this)' id='txtXq_" + d + "'>" + days[d-1] + "</li>";
		}else{
			li += "<li class='cur-week'>" + days[d-1] + "</li>";
		}
	}
	document.getElementById("ul_xq").innerHTML = li;
}

function btOver(obj){
	$(obj).attr('class', 'cur-week');
	$(obj).css("cursor", "pointer");
}

function btOut(obj){
	$(obj).attr('class', '');
	$(obj).css("cursor", "pointer");
}

function reSetCalEvent(oCurrent){
	checkCountent(oCurrent.title);
	oCurrent = $(oCurrent);

	if (oLastDay==null){
		oCurrent.attr("class", "day-now");
		oLastDay = oCurrent;
	}else if(oCurrent.html() != oLastDay.html()) {
		oCurrent.attr("class", "day-now");
		oLastDay.css("cursor", "pointer");
		oLastDay.attr("class", "day");
		oLastDay = oCurrent;
	}
	
	isFirst = 0;
}

function setMonthMenu(){
	
	var tempPreMonth,tempPreYear;
	var tempNextMonth,tempNextYear;

	if ((month-1)<0){
		tempPreMonth=11;
		tempPreYear=year-1;
	}else{
		tempPreMonth=month-1;
		tempPreYear = year;
	}

	if((month+1)>11){
		tempNextMonth=0;
		tempNextYear=year+1;
	}else{
		tempNextMonth=month+1;
		tempNextYear = year;
	}

	$('#nextMonth').html(monthEn[tempNextMonth] + ',' + tempNextYear);	
	$('#nextMonth').click(function(){ getNextMonthCal(tempNextYear, tempNextMonth) });

	$('#currMonth').html(monthEn[month]+','+year);
	$('#currMonth').click(function(){ getCurrMonthCal(todayYear, todayMonth) });

}

function buttonOver(obj){
	$(obj).attr('class', 'day-now');
}

function buttonOut(obj){
	if (oLastDay.html()==null){
		$(obj).attr('class', 'day');
	}else{
		if($(obj).html() == oLastDay.html()){
			$(obj).attr('class', 'day-now');
		}
		else{
			$(obj).attr('class', 'day');
		}
	}
	//if (oLastDay==null){
//		$(obj).attr('class', 'day-now');
//	}else{
//		if($(obj).html() == oLastDay.html())
//			$(obj).attr('class', 'day');
//		else
//			$(obj).attr('class', 'day-now');
//	}
}

function initCal(todays,years,months){
	setWeek();
	setMonthMenu();
	if (todays!=null && years!=null && months!=null){
		year = years;
		month = months-1;
		sDay=todays;
		checkMemInDay(year, month);
	}else{
		checkMemInDay(year, month);
	}
}

function getNextMonthCal(pyear, pmonth){
	$('#headtopa').css('display', 'none');
	year = pyear;
	month = pmonth;
	checkMemInDay(year, month);

	isFirst = 0;
}

function getCurrMonthCal(pyear, pmonth){
	$('#headtopa').css('display', 'none');
	year = pyear;
	month = pmonth;
	checkMemInDay(year, month);

	isFirst = 0;
}
