jQuery.fn.showEvent = function() {
	var args = arguments[0] || {};
	var thisevent = $(this).attr("rel");
	
	//alert($(this).attr("rel"));
	jQuery.post("/eventajax.php", {showevent: $(this).attr("rel")}, function(resp) {

		var today = $("#showDay").overlay({
			closeOnClick: false,
			api: true
		});
		
		$("#showDay h2").text($("#theheader", resp).text());
		$(".contentWrap").empty();
		
		if(args.returntime)
		{
			var thetime = args.returntime;
			var returnStr = "<br><br><a class='return_link' rel='" + thetime + "'><img src='/icon/back.gif'> Back</a>";
			$(".contentWrap").html($("#content", resp).html() + returnStr);
			$(".contentWrap .return_link").css("cursor", "pointer").click(function() {
				$("td[timestamp=" + $(this).attr("rel") + "]").click();
			});
		}
		else
		{
			$(".contentWrap").html($("#content", resp).html());
		}
		
		$(".notice_link").css("cursor", "pointer").click(function() {
			//alert($(this).attr("rel"));
			$(this).showNotice({returnevent: thisevent}); 
		});
		
		today.load();
	});
	//$("#showDay h2").text(thetime);
};




jQuery.fn.showNotice = function() {
	var args = arguments[0] || {};
	
	//alert($(this).attr("rel"));
	jQuery.post("/noticeajax.php", {shownotice: $(this).attr("rel")}, function(resp) {

		var today = $("#showDay").overlay({
			closeOnClick: false,
			api: true
		});
		
		$("#showDay h2").text($("#theheader", resp).text());
		$(".contentWrap").empty();
		
		if(args.returnevent != 0)
		{
			//alert(args.returnevent);
			var returnevent = args.returnevent;
			var returnStr = "<br><br><a class='return_link' rel='" + returnevent + "'><img src='/icon/back.gif'> Back</a>";
			$(".contentWrap").html($("#content", resp).html() + returnStr);
			$(".contentWrap .return_link").css("cursor", "pointer").click(function() {
				$(".return_link").showEvent();										     
				//$("td[timestamp=" + $(this).attr("rel") + "]").showEvent();
			});
		}
		else
		{
			$(".contentWrap").html($("#content", resp).html());
		}
		
		today.load();
	});
	//$("#showDay h2").text(thetime);
};







jQuery.fn.updateCal = function() {
	var args = arguments[0] || {};
	
	var today = $("#showDay").overlay({
		closeOnClick: false,
		api: true
	});
	
	jQuery.post("/eventajax.php", {getdates: $("#theCal").attr("thismonth"), rand: Math.floor(Math.random()*101)}, function(rec) {
		//alert(rec);
		$("#theCal").attr({lastmonth: $("#new_lastmonth", rec).attr("string")});
		$("#theCal").attr({nextmonth: $("#new_nextmonth", rec).attr("string")});
		//alert($("#theCal").attr("lastmonth"));
	});
	
	jQuery.post("/eventajax.php", {showcal: $("#cal_select :selected").attr('value'), start: $("#theCal").attr("thismonth")}, function(data) {
		//alert(data);
		$("#theCal").empty();
		$("#theCal").append(data);
		
		//alert($("#lastmonth", data).attr("string"));
		
		$("#cal_next, #cal_back").css("cursor", "pointer");
		$("#cal_next").click(function() {
			//$("#theCal").attr({thismonth: $("#nextmonth").attr("string")});
			$("#theCal").attr({thismonth: $("#theCal").attr("nextmonth")});
			$(this).updateCal();
		});
		$("#cal_back").click(function() {
			//$("#theCal").attr({thismonth: $("#lastmonth").attr("string")});
			$("#theCal").attr({thismonth: $("#theCal").attr("lastmonth")});
			$(this).updateCal();
		});
		
		/*START SINGLE DAY VIEW*/
		$("td[timestamp]").css("cursor", "pointer").click(function () {
			//alert($(this).attr("timestamp"));
			var thetime = $(this).attr("timestamp");
			
			jQuery.post("/eventajax.php", {showday: $(this).attr("timestamp")}, function(ret) {
				$("#showDay h2").text($("#thedate", ret).text());
				$(".contentWrap").empty();
				$(".anevent", ret).each(function () {										
					$(".contentWrap").append("<span><a class='event_link' rel='" + $(this).attr("eventid") + "'>" + $(this).text() + "</a></span><br>");
				});
				
				/*START SINGLE EVENT VIEW*/
				$(".event_link", ".contentWrap").css("cursor", "pointer").click(function() {
					$(this).showEvent({returntime: thetime});
				});
				/*END SINGLE EVENT VIEW*/
				
				today.load();
			});
			//alert($(this).attr("timestamp"));
			//$(this).fadeOut("slow");
		});
		/*END SINGLE DAY VIEW*/
		
		$("td[outofmonth]").fadeTo(1, 0.33);
		
		$("td[title]").hover(
			function () {
				$(this).append("<span class='innertip' style='margin: 5px; text-align: center;'>" + $(this).attr("title") + "</span>");
				$(this).find(".innertip").hide();
				var ah = $(this).find(".innertip").width();
				var ph = $(this).find(".innertip").parent().width();
				var mh = (ph - ah) / 2;
				$(this).find(".innertip").css('margin-right', mh);
				$(this).find(".innertip").fadeIn(600);
				//$(this).text($(this).attr("title")).show("slow");
			},
			function () {
				$(this).find(".innertip").remove();
			}
		);

	});
};





