/*---------------------------------------------------------------------------------------
configure variables
---------------------------------------------------------------------------------------*/
var cal_prevyear = "<<";
var cal_nextyear = ">>";
var cal_prevmonth = "<";
var cal_nextmonth = ">";
var cal_today = "今日";
var cal_close = "关闭";

var cal_list_month = new Array(12);
cal_list_month[0] = "一月";
cal_list_month[1] = "二月";
cal_list_month[2] = "三月";
cal_list_month[3] = "四月";
cal_list_month[4] = "五月";
cal_list_month[5] = "六月";
cal_list_month[6] = "七月";
cal_list_month[7] = "八月";
cal_list_month[8] = "九月";
cal_list_month[9] = "十月";
cal_list_month[10] = "十一月";
cal_list_month[11] = "十二月";

var cal_list_weekday = new Array(7);
cal_list_weekday[0] = "日";
cal_list_weekday[1] = "一";
cal_list_weekday[2] = "二";
cal_list_weekday[3] = "三";
cal_list_weekday[4] = "四";
cal_list_weekday[5] = "五";
cal_list_weekday[6] = "六";

var cal_width = "200px";

var cal_head_tablespacing = 1;

var cal_year_navwidth = "20%";
var cal_year_datewidth = "60%";
var cal_year_itemwidth = "25%";
var cal_year_tablespacing = 1;
var cal_year_itemcount = 4;
var cal_year_popup = null;

var cal_day_itemwidth = "14%";
var cal_day_navwidth = "11%";
var cal_day_datewidth = "55%";
var cal_day_tablespacing = 0;

/*---------------------------------------------------------------------------------------
public objects and functions
---------------------------------------------------------------------------------------*/
var cal_list_days = new Array(12);
cal_list_days[0] = 31;
cal_list_days[1] = 28;
cal_list_days[2] = 31;
cal_list_days[3] = 30;
cal_list_days[4] = 31;
cal_list_days[5] = 30;
cal_list_days[6] = 31;
cal_list_days[7] = 31;
cal_list_days[8] = 30;
cal_list_days[9] = 31;
cal_list_days[10] = 30;
cal_list_days[11] = 31;

var cal_clickobject = null;
var cal_popupobject = null;

function _setposition(clickobj, wrapper)
{
	var obj = clickobj;
	var objtop = obj.offsetTop;
	var objheight = obj.clientHeight;
	var objleft = obj.offsetLeft;
	while (obj = obj.offsetParent) {
		objtop += obj.offsetTop;
		objleft += obj.offsetLeft;
	}
	
	var calwidth = wrapper[0].clientWidth;
	var calheight = wrapper[0].clientHeight;

	var docheight = document.body.clientHeight;
	var docwidth = document.body.clientWidth;
	var docleft = document.body.scrollLeft;
	var doctop = document.body.scrollTop;

	if ( docheight + doctop - objtop - objheight > calheight )
		wrapper.css("top", objtop + objheight);
	else
		wrapper.css("top", objtop - doctop < calheight ? objtop + objheight : objtop - calheight);
	
	if ( docwidth + docleft - objleft >= calwidth )
		wrapper.css("left", objleft);
	else
		wrapper.css("left", docwidth >= calwidth ? docwidth - calwidth + docleft : docleft);
}

function _setnavhover(obj)
{
	$(obj).addClass("calendar_nav_over");
}

function _removenavhover(obj)
{
	$(obj).removeClass("calendar_nav_over");
}

function _setitemhover(obj)
{
	$(obj).addClass("calendar_item_over");
}

function _removeitemhover(obj)
{
	$(obj).removeClass("calendar_item_over");
}

/*---------------------------------------------------------------------------------------
functions for month calendar
---------------------------------------------------------------------------------------*/
function _createmonthcalendar(wrapper)
{
	wrapper.append( 
		$("<div/>")
		.addClass("calendar_head")
		.append(
			$("<table/>")
			.attr("cellspacing", cal_head_tablespacing)
			.append(
				$("<tr/>")
				.append(
					$("<td/>")
					.addClass("calendar_headnav")
					.attr("width",cal_year_navwidth)
					.text(cal_prevyear)
					.click(function() { year_prevyear(this); } )
				).append( 
					$("<td/>")
					.addClass("calendar_headdate")
					.attr("width",cal_year_datewidth)
				).append(
					$("<td/>")
					.addClass("calendar_headnav")
					.attr("width",cal_year_navwidth)
					.text(cal_nextyear)
					.click(function() { year_nextyear(this); } )
				)
			)
		)
	).append(
		$("<div/>")
		.addClass("calendar_body")
		.append(
			$("<table/>")
			.attr("cellspacing", cal_year_tablespacing)
			.append(
				$("<tbody/>")
				.each(function() {
					var table = $(this);
					for (var i = 0; i < (12 / cal_year_itemcount); i++)
					{
						var row = $("<tr/>").appendTo(table);
						for (var j = 0; j < cal_year_itemcount; j++)
						{
							$("<td/>")
							.addClass("calendar_item")
							.attr("width", cal_year_itemwidth)
							.text(cal_list_month[ i * cal_year_itemcount + j])
							.appendTo(row);
						}
						row.appendTo(table);
					}
				})
			)
		)
	).append(
		$("<div/>")
		.addClass("calendar_foot")
		.append(
			$("<table/>")
			.attr("cellspacing", cal_head_tablespacing)
			.append(
				$("<tr/>")
				.each(function() {
					$("<td/>")
					.addClass("calendar_headnav")
					.text(cal_today)
					.click(function() {
						var today = new Date();
						_setmonthcalendar(wrapper, today.getFullYear(), today.getMonth() + 1);
						_eventmonthcalendar(wrapper);
					})
					.appendTo($(this))
					
					if (!wrapper.attr("popup")) return;
					$("<td/>")
					.addClass("calendar_headnav")
					.text(cal_close)
					.click(function() {
						cal_popupobject = null;
						wrapper.remove();
					})
					.appendTo($(this))
				})
			)
		)
	)
	
	wrapper.find(".calendar_headnav")
		.mouseover( function() { _setnavhover(this); } )
		.mouseout( function() { _removenavhover(this); } )
	wrapper.find(".calendar_item")
		.mouseover( function() { _setitemhover(this); } )
		.mouseout( function() { _removeitemhover(this); } )
		.click( function() { _clickmonthcalendar(this); } )
}

function _setmonthcalendar(wrapper, year, month)
{
	wrapper.attr("year", year);
	wrapper.attr("curyear", year);
	wrapper.attr("curmonth", month);
	
	wrapper.find(".calendar_headdate").text(year);
	wrapper.find(".calendar_item").removeClass("calendar_item_select");
	wrapper.find(".calendar_item").each(function() {
		if ($(this).text() == cal_list_month[month - 1])
		{
			$(this).addClass("calendar_item_select");
			return false;
		}
	});
}

function _eventmonthcalendar(wrapper)
{
	var date = wrapper.attr("curyear") + "-" + wrapper.attr("curmonth");
	
	// if a text div is set, set date to div's text
	if (wrapper.attr("textdiv"))
	{
		$("#" + wrapper.attr("textdiv")).text(date)
	}

	// remove calendar if popup, otherwise set display style
	if (wrapper.attr("popup"))
	{
		wrapper.remove();
		cal_popupobject = null;
	}

	// call if a call back is exits
	if (wrapper.attr("callback"))
	{
		var callback = wrapper.attr("callback") + "('" + date + "')";
		eval(callback);
	}
}

function _clickmonthcalendar(obj)
{
	var month = $(obj).text();
	for (var i = 0; i < 12; i++)
	{
		if (month == cal_list_month[i])
		{
			month = i + 1;
			break;
		}
	}

	var calobj = $(obj).parents().filter(".calendar_frame");
	_setmonthcalendar(calobj, calobj.attr("year"), month);
	_eventmonthcalendar(calobj);
}

function selectMonthCalendar(divid, year, month)
{
	var target = $("#" + divid);
	_setmonthcalendar(target, year, month);
}

function writeMonthCalendar(divid, callback)
{
	var target = $("#" + divid);
	target
		.addClass("calendar_frame")
		.attr("callback", callback)

	_createmonthcalendar(target);
		
	var today = new Date();
	selectMonthCalendar(divid, today.getFullYear(), today.getMonth() + 1);
}

function popupMonthCalendar(textdiv, clickdiv, callback)
{
	var textobj = $("#" + textdiv);

	$("#" + clickdiv)
		.addClass("calendar_icon")
		.click(function() {
			// 获取 textobj 值, 并设置日历
			var year = 0, month = 0;
			var date = textobj.text();
			var index = date.indexOf("-");
			if (index > 0)
			{
				year = parseInt(date.substring(0,index));
				month = parseInt(date.substr(index + 1));
			}
			if (year <= 0)
			{
				date = new Date();
				year = date.getFullYear();
				month = date.getMonth() + 1;
			}

			// if opened, close and return
			if (cal_popupobject)
			{
				cal_popupobject.remove();
				cal_popupobject = null;
				if (cal_clickobject == clickdiv)
				{
					return;
				}
			}
			
			// create new popup calendar
			cal_clickobject = clickdiv;
			cal_popupobject = $("<div/>");
			cal_popupobject
				.attr("popup", true)
				.attr("callback", callback)
				.attr("textdiv", textdiv)
				.addClass("calendar_frame")
				.css("position","absolute")
				.css("width", cal_width)
				.appendTo($("body"))
				
			// set up event for calendar
			_createmonthcalendar(cal_popupobject);
				
			// 获取 textobj 值, 并设置日历
			_setmonthcalendar(cal_popupobject, year, month);
				
			// set position
			_setposition(this, cal_popupobject);
		});
}

function year_prevyear(navobj)
{
	var calobj = $(navobj).parents().filter(".calendar_frame");
	var year = parseInt(calobj.attr("year")) - 1;
	calobj.attr("year", year);
	calobj.find(".calendar_headdate").text(year);
	
	calobj.find(".calendar_item").removeClass("calendar_item_select");
	var month = cal_list_month[parseInt(calobj.attr("curmonth")) - 1];
	if (year == calobj.attr("curyear"))
	{
		calobj.find(".calendar_item").each(function() {
			if ($(this).text() == month) $(this).addClass("calendar_item_select");
		});
	}
	_checknavcall(navobj);	
}

function year_nextyear(navobj)
{
	var calobj = $(navobj).parents().filter(".calendar_frame");
	var year = parseInt(calobj.attr("year")) + 1;
	calobj.attr("year", year);
	calobj.find(".calendar_headdate").text(year);
	
	calobj.find(".calendar_item").removeClass("calendar_item_select");
	var month = cal_list_month[parseInt(calobj.attr("curmonth")) - 1];
	if (year == calobj.attr("curyear"))
	{
		calobj.find(".calendar_item").each(function() {
			if ($(this).text() == month) $(this).addClass("calendar_item_select");
		});
	}
	_checknavcall(navobj);	
}

/*---------------------------------------------------------------------------------------
functions for week & day calendar
---------------------------------------------------------------------------------------*/
function _refreshcalendar(wrapper)
{
	var year = wrapper.attr("year");
	var month = wrapper.attr("month");
	
	// 更新标题
	wrapper.find(".calendar_headdate").text(year + "-" + month);
	
	// 刷新日历
	wrapper.find(".calendar_body table tbody").remove();
	wrapper.find(".calendar_body table")
		.append(
			$("<tbody/>")
			.each(function () {

				// 计算本月天数及第一天的 weekday
				var weekday = new Date(year, month - 1, 1).getDay();
				var days = cal_list_days[month - 1];
				if (month == 2)
				{
					if (year%4==0 && (year%100!=0 || year%400==0))
					{
						days = 29;
					}
				}
				
				// 第一周补足
				var rows = (weekday + days + 1) / 7;
				//var rows = 6;
				for (var i = 0; i < rows; i++)
				{
					var row = $("<tr" + (i % 2 == 1 ? " class='arow'" : '')+ "/>");
					
					for (var j = 0; j < 7; j++)
					{
						var index = i * 7 + j;
						if (index < weekday || index >= (weekday + days))
						{
							$("<td/>")
							.text(" ")
							.appendTo(row)
						}
						else
						{
							$("<td/>")
							.addClass("calendar_item")
							.text(index - weekday + 1)
							.appendTo(row)
						}
					}
					
					row.appendTo($(this));
				}
			})
		)
	
	// 如果当前月,选中
	var day = wrapper.attr("curday");
	if (wrapper.attr("mode") == "w")
	{
		var selectdate = new Date(wrapper.attr("curyear"), wrapper.attr("curmonth") - 1, day);
		selectdate.setDate(selectdate.getDate() - selectdate.getDay());
		wrapper.find(".calendar_item").each(function() {
			var thisdate = new Date(year, month - 1, $(this).text());
			thisdate.setDate(thisdate.getDate() - thisdate.getDay());
			if (selectdate.getFullYear() == thisdate.getFullYear() 
				&& selectdate.getMonth() == thisdate.getMonth()
				&& selectdate.getDate() == thisdate.getDate())
			{
				$(this).parent().addClass("calendar_item_select");
				return false;
			}
		});
	}
	if (wrapper.attr("mode") == "d")
	{
		if (year == wrapper.attr("curyear") && month == wrapper.attr("curmonth"))
		{
			wrapper.find(".calendar_item").each(function() {
				if ($(this).text() == day)
				{
					$(this).addClass("calendar_item_select");
				}
			});
		}
	}
	
	// 设置事件//20111026增加统计
	if (wrapper.attr("mode") == "d")
	{
		wrapper.find(".calendar_headnav")
			.mouseover( function() { _setnavhover(this); } )
			.mouseout( function() { _removenavhover(this); } )
		wrapper.find(".calendar_item")
			.mouseover( function() { _setitemhover(this); } )
			.mouseout( function() { _removeitemhover(this); } )
			.click( function() { write_log("85","股市日历"); _clickdaycalendar(this); } )
	}
	else
	{
		wrapper.find(".calendar_headnav")
			.mouseover( function() { _setnavhover(this); } )
			.mouseout( function() { _removenavhover(this); } )
		wrapper.find(".calendar_body tbody tr")
			.mouseover( function() { _setitemhover(this); } )
			.mouseout( function() { _removeitemhover(this); } )
			.click( function() { _clickweekcalendar(this); } )
	}
}

function _writedaycalendar(wrapper)
{
	wrapper.append( 
		$("<div/>")
		.addClass("calendar_head")
		.append(
			$("<table/>")
			.attr("cellspacing", cal_head_tablespacing)
			.append(
				$("<tr/>")
				.append(
					$("<td/>")
					.addClass("calendar_headnav")
					.attr("width",cal_day_navwidth)
					.text(cal_prevyear)
					.click(function() { month_prevyear(this); } )
				).append(
					$("<td/>")
					.addClass("calendar_headnav")
					.attr("width",cal_day_navwidth)
					.text(cal_prevmonth)
					.click(function() { month_prevmonth(this); } )
				).append( 
					$("<td/>")
					.addClass("calendar_headdate")
					.attr("width",cal_day_datewidth)
				).append(
					$("<td/>")
					.addClass("calendar_headnav")
					.attr("width",cal_day_navwidth)
					.text(cal_nextmonth)
					.click(function() { month_nextmonth(this); } )
				).append(
					$("<td/>")
					.addClass("calendar_headnav")
					.attr("width",cal_day_navwidth)
					.text(cal_nextyear)
					.click(function() { month_nextyear(this); } )
				)
			)
		)
	).append(
		$("<div/>")
		.addClass("calendar_body")
		.append(
			$("<table/>")
			.attr("cellspacing", cal_day_tablespacing)
			.append(
				$("<thead/>")
				.append(
					$("<tr class='title'/>")
					.each(function() {
						for (var i = 0; i < 7; i++)
						{
							$("<td/>")
							.css("width", cal_day_itemwidth)
							.text(cal_list_weekday[i])
							.appendTo($(this))
						}
					})
				)
			)
		)
	)/*.append(
		$("<div/>")
		.addClass("calendar_foot")
		.append(
			$("<table/>")
			.attr("cellspacing", cal_head_tablespacing)
			.append(
				$("<tr/>")
				.each(function() {
					$("<td/>")
					.addClass("calendar_headnav")
					.text(cal_today)
					.click(function() {
						var today = new Date();
						_setdaycalendar(wrapper, today.getFullYear(), today.getMonth() + 1, today.getDate());
						_eventdaycalendar(wrapper);
					})
					.appendTo($(this))
					
					if (!wrapper.attr("popup")) return;
					$("<td/>")
					.addClass("calendar_headnav")
					.text(cal_close)
					.click(function() {
						cal_popupobject = null;
						wrapper.remove();
					})
					.appendTo($(this))
				})
			)
		)
	)*/
}

function _setdaycalendar(wrapper, year, month, day)
{
	wrapper.attr("year",year);
	wrapper.attr("curyear",year);
	wrapper.attr("month",month);
	wrapper.attr("curmonth",month);
	wrapper.attr("curday",day);
	
	_refreshcalendar(wrapper);
}

function _eventdaycalendar(wrapper, day)
{
	var date = wrapper.attr("year") + "-" + wrapper.attr("month") + "-" + day;

	if (wrapper.attr("popup"))
	{
		wrapper.remove();
		cal_popupobject = null;
	}
	/*
	if (wrapper.attr("textdiv"))
	{
		$("#" + wrapper.attr("textdiv")).text(date)
	}
	*/	

	// call if a call back is exits
	if (wrapper.attr("callback"))
	{
		var callback = wrapper.attr("callback") + "('" + date + "')";
		eval(callback);
	}
}

function _clickdaycalendar(obj)
{
	var day = $(obj).text();
	var calobj = $(obj).parents().filter(".calendar_frame");
	
	//_setdaycalendar(calobj, calobj.attr("year"), calobj.attr("month"), day)	
	_eventdaycalendar(calobj, day);
}

function _clickweekcalendar(obj)
{
	var day;
	$(obj).find("td").each(function() {
		var value = $(this).text();
		if (value && value != " ")
		{
			day = parseInt(value);
			return false;
		}
	});
	if (!day) return;
	
	var calobj = $(obj).parents().filter(".calendar_frame");

	_setdaycalendar(calobj, calobj.attr("year"), calobj.attr("month"), day)	
	_eventdaycalendar(calobj);
}

function selectDayCalendar(divid, year, month, day)
{
	var target = $("#" + divid);
	_setdaycalendar(target, year, month, day);
}

function _writeCalendar(phase, divid, callback, navcallback)
{
	var target = $("#" + divid);
	target
		.addClass("calendar_frame")
		.attr("mode", phase)
		.attr("callback", callback)
		.attr("navcallback", navcallback)
	_writedaycalendar(target);
	
	// 设置当前日期
	var date = new Date();
	_setdaycalendar(target, date.getFullYear(), date.getMonth() + 1, date.getDate());
}

function writeDayCalendar(divid, callback, navcallback)
{
	_writeCalendar("d", divid, callback, navcallback);
}

function writeWeekCalendar(divid, callback, navcallback)
{
	_writeCalendar("w", divid, callback, navcallback);
}

function _popupCalendar(phase, textdiv, clickdiv, callback)
{
	var textobj = $("#" + textdiv);

	$("#" + clickdiv)
		.addClass("calendar_icon")
		.click(function() {
			// 获取 textobj 值, 并设置日历
			var year = 0, month = 0, day = 0;
			var date = textobj.text();
			var index = date.indexOf("-");
			if (index > 0)
			{
				year = parseInt(date.substring(0,index));
				month = date.substr(index + 1);
				index = month.indexOf("-");
				if (index > 0)
				{
					day = parseInt(month.substr(index + 1));
					month = parseInt(month.substring(0,index));
				}
			}
			
			if (day <= 0)
			{
				date = new Date();
				year = date.getFullYear();
				month = date.getMonth() + 1;
				day = date.getDate();
			}

			// if opened, close and return
			if (cal_popupobject)
			{
				cal_popupobject.remove();
				cal_popupobject = null;
				if (cal_clickobject == clickdiv)
				{
					return;
				}
			}
			
			// create new popup calendar
			cal_clickobject = clickdiv;
			cal_popupobject = $("<div/>");
			cal_popupobject
				.attr("mode", phase)
				.attr("callback", callback)
				.attr("textdiv", textdiv)
				.attr("popup", true)
				.addClass("calendar_frame")
				.css("position","absolute")
				.css("width", cal_width)
				.appendTo($("body"))
				
			// set up event for calendar
			_writedaycalendar(cal_popupobject);
				
			// 获取 textobj 值, 并设置日历
			_setdaycalendar(cal_popupobject, year, month, day);
			
			// set position
			_setposition(this, cal_popupobject);
		});
}

function popupDayCalendar(textdiv, clickdiv, callback)
{
	_popupCalendar("d", textdiv, clickdiv, callback);
}

function popupWeekCalendar(textdiv, clickdiv, callback)
{
	_popupCalendar("w", textdiv, clickdiv, callback);
}

function month_nextyear(navobj)
{
	var calobj = $(navobj).parents().filter(".calendar_frame");
	var year = calobj.attr("year");
	year = parseInt(year) + 1;
	
	calobj.attr("year", year);
	_refreshcalendar(calobj);
	_checknavcall(navobj);	
}

function month_prevyear(navobj)
{
	var calobj = $(navobj).parents().filter(".calendar_frame");
	var year = calobj.attr("year");
	year = parseInt(year) - 1;
	
	calobj.attr("year", year);
	_refreshcalendar(calobj);
	_checknavcall(navobj);	
}

function month_nextmonth(navobj)
{
	var calobj = $(navobj).parents().filter(".calendar_frame");
	var year = calobj.attr("year");
	var month = calobj.attr("month");
	month = parseInt(month) + 1;
	if (month > 12)
	{
		year = parseInt(year) + 1;
		month = 1;
	}
	
	calobj.attr("year", year);
	calobj.attr("month", month);
	_refreshcalendar(calobj);
	_checknavcall(navobj);	
}

function month_prevmonth(navobj)
{
	var calobj = $(navobj).parents().filter(".calendar_frame");
	var year = calobj.attr("year");
	var month = calobj.attr("month");
	month = parseInt(month) - 1;
	if (month < 1)
	{
		year = parseInt(year) - 1;
		month = 12;
	}
	
	calobj.attr("year", year);
	calobj.attr("month", month);
	_refreshcalendar(calobj);
	_checknavcall(navobj);	
}

function _checknavcall(navobj)
{
	var calobj = $(navobj).parents().filter(".calendar_frame");
	var callback = $(calobj).attr('navcallback');
	if (callback == null || callback == "") return;
	eval(callback + "()");
}

