// JavaScript Document

function Calendar(calevents){
	for (var i=0;i<calevent.length;i++){
		alert("Hello");
	}
}

function DaysOfMonth(month,year){
	if ((month==4)||(month==6)||(month==9)||(month==11)){
		return 30;
	}else if (month!=2){
		return 31;
	}else if (((year%4==0)&&(year%100 != 0) ) || (year%400==0)){
		return 29;
	}else{
		return 28;
	}
}

function GetFullMonth(index){
	//alert(index);
	montharray=new Array("មករា","កម្ភៈ","មីនា","មេសា","ឧសភា","មិថុនា","កក្កដា","សីហា","កញ្ញា","តុលា","វិច្ឆិកា","ធ្នូ");
	return montharray[index-1];
}

function Convert2Digit(num){
	if (num<10){
		return "0"+num;
	}
	return num;
}



function DisplayCalendar(month,year){
	var premonth=month-1;
	var preyear=year;
	var nextmonth=month+1;
	var nextyear=year;
	if (premonth==0){
		premonth=12;
		preyear=year-1;
	}
	
	if (nextmonth>12){
		nextmonth=1;
		nextyear=year+1;
	}
	
	var nbday=DaysOfMonth(month,year);
	
	var CalHTML="<table cellspacing='0' class='calendarleft'><tr><td colspan='7' class='tabhead'><div class='premonth'><a href='javascript:DisplayCalendar("+premonth+","+preyear+")'><img src='images/leftcal.gif' border='0' width='21' height='20' /></a></div><div class='labelmonth'>"+GetFullMonth(month)+" "+year+"</div><div class='nextmonth'><a href='javascript:DisplayCalendar("+nextmonth+","+nextyear+")'><img src='images/rightcal.gif' width='21' height='20' border='0' /></a></div></td></tr>";
	// style='float:left; margin-left:3px;'(Premonth)        style='float:left; font-size:12px; width:120px;'(label)           style='float:left;'(nerxtmonth)
	// style='background:#C7D0DB; padding-bottom:0px; padding-top:3px; text-align:center;'(tabhead)
	CalHTML+="<tr><th>ចន្ទ</th><th>អង្គារ</th><th>ពុធ</th><th>ព្រហ</th><th>សុក្រ</th><th>សៅរ៏</th><th>អាទិ</th></tr>";
	
	var temdate=new Date(year+"/"+month+"/1");
	var startday=temdate.getDay();
	var nbdaypre=DaysOfMonth(premonth,preyear);
	
	//alert(year+" : "+month+" : "+temdate.getDay());
	CalHTML+="<tr>";
	
	var newrow=1;
	for (var i=(startday-1);i>=1;i--){
		CalHTML+="<td style='color:#999;'>"+(nbdaypre-i+1)+"</td>";
		newrow++;
	}
	
	var datenow=new Date();
	var daynow=datenow.getDate();
	var monthnow=datenow.getMonth()+1;
	var yearnow=datenow.getFullYear();
	var edate="";
	for (var i=1;i<=nbday;i++){
		edate=year+"-"+Convert2Digit(month)+"-"+Convert2Digit(i);
		if (newrow==8){
			CalHTML+="<tr>";
			newrow=1;
		}
		if (daynow==i && monthnow==month && yearnow==year){
			CalHTML+="<td";
			if (events[edate]!=undefined){
				CalHTML+=" class='date_has_event'";
			}else{
				CalHTML+=" class='today'";
			}
			CalHTML+=">"+i+"";
			// class="date_has_event"
			if (events[edate]!=undefined){
				CalHTML+=events[edate];
			}
			CalHTML+="</td>";
		}else{
			//alert(edate+" : "+events['"'+edate+'"']);
			CalHTML+="<td";
			if (events[edate]!=undefined){
				//alert("Hello");
				CalHTML+=" class='date_has_event' onmouseover=\"mouseoverevent();\"";
				// onmouseover=\"mouseoverevent('Hello')\"
			}
			CalHTML+=">"+i+"";
			if (events[edate]){
				//alert("sdfasdfadf");
				CalHTML+=events[edate];
			}
			CalHTML+="</td>";
		}
		
		if (newrow==8){
			CalHTML+="</tr>";
		}
		newrow++;
	}
	
	var count=1;
	for (i=newrow;i<8;i++){
		CalHTML+="<td style='color:#999;'>"+count+"</td>";
		if (newrow==8){
			CalHTML+="</tr>";
		}
		count++;
	}
	CalHTML+="</table>";
	//document.write(CalHTML);
	document.getElementById("leftcalendar").innerHTML=CalHTML;
}


function mouseoverevent(){
	$('.date_has_event').each(function () {
		// options
		var distance = 10;//10;
		var time = 0;//250;
		var hideDelay = 0;//500;

		var hideDelayTimer = null;

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

		var trigger = $(this);
		var popup = $('.events .title', 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 winH = $(window).height()/2+$(id).height;
				//var winW = $(window).width()/2-$(id).width;
				// reset position of popup box
				popup.css({					
					top: -57,							
					right: -1.7,					
					position: 'absolute',
					background: '#900',					
					display: 'block' // brings the popup back in to view
					
				})
				.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);
		});
	});
}




function DisplayCalendar1(month,year){
	var premonth=month-1;
	var preyear=year;
	var nextmonth=month+1;
	var nextyear=year;
	if (premonth==0){
		premonth=12;
		preyear=year-1;
	}
	
	if (nextmonth>12){
		nextmonth=1;
		nextyear=year+1;
	}
	
	var nbday=DaysOfMonth(month,year);
	
	var CalHTML="<table cellspacing='0' class='calendarleft'><tr><td colspan='7' class='tabhead'><div class='premonth'><a href='javascript:DisplayCalendar("+premonth+","+preyear+")'><img src='images/leftcal.gif' border='0' width='21' height='20' /></a></div><div class='labelmonth'>"+GetFullMonth(month)+" "+year+"</div><div class='nextmonth'><a href='javascript:DisplayCalendar("+nextmonth+","+nextyear+")'><img src='images/rightcal.gif' width='21' height='20' border='0' /></a></div></td></tr>";
	// style='float:left; margin-left:3px;'(Premonth)        style='float:left; font-size:12px; width:120px;'(label)           style='float:left;'(nerxtmonth)
	// style='background:#C7D0DB; padding-bottom:0px; padding-top:3px; text-align:center;'(tabhead)
	CalHTML+="<tr><th>ចន្ទ</th><th>អង្គារ</th><th>ពុធ</th><th>ព្រហ</th><th>សុក្រ</th><th>សៅរ៏</th><th>អាទិ</th></tr>";
	
	var temdate=new Date(year+"/"+month+"/1");
	var startday=temdate.getDay();
	var nbdaypre=DaysOfMonth(premonth,preyear);
	
	//alert(year+" : "+month+" : "+temdate.getDay());
	CalHTML+="<tr>";
	
	var newrow=1;
	for (var i=(startday-1);i>=1;i--){
		CalHTML+="<td style='color:#999;'>"+(nbdaypre-i+1)+"</td>";
		newrow++;
	}
	
	var datenow=new Date();
	var daynow=datenow.getDate();
	var monthnow=datenow.getMonth()+1;
	var yearnow=datenow.getFullYear();
	var edate="";
	for (var i=1;i<=nbday;i++){
		edate=year+"-"+Convert2Digit(month)+"-"+Convert2Digit(i);
		if (newrow==8){
			CalHTML+="<tr>";
			newrow=1;
		}
		if (daynow==i && monthnow==month && yearnow==year){
			CalHTML+="<td";
			if (events[edate]!=undefined){
				CalHTML+=" class='date_has_event'";
			}else{
				CalHTML+=" class='today'";
			}
			CalHTML+=">"+i+"";
			// class="date_has_event"
			if (events[edate]!=undefined){
				CalHTML+=events[edate];
			}
			CalHTML+="</td>";
		}else{
			//alert(edate+" : "+events['"'+edate+'"']);
			CalHTML+="<td";
			if (events[edate]!=undefined){
				//alert("Hello");
				CalHTML+=" class='date_has_event' onmouseover=\"mouseoverevent1();\"";
				// onmouseover=\"mouseoverevent1('Hello')\"
			}
			CalHTML+=">"+i+"";
			if (events[edate]){
				//alert("sdfasdfadf");
				CalHTML+=events[edate];
			}
			CalHTML+="</td>";
		}
		
		if (newrow==8){
			CalHTML+="</tr>";
		}
		newrow++;
	}
	
	var count=1;
	for (i=newrow;i<8;i++){
		CalHTML+="<td style='color:#999;'>"+count+"</td>";
		if (newrow==8){
			CalHTML+="</tr>";
		}
		count++;
	}
	CalHTML+="</table>";
	//document.write(CalHTML);
	document.getElementById("leftcalendar").innerHTML=CalHTML;
}


function mouseoverevent1(){
	$('.date_has_event').each(function () {
		// options
		var distance = 15;//10;
		var time = 0;//250;
		var hideDelay = 0;//500;

		var hideDelayTimer = null;

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

		var trigger = $(this);
		var popup = $('.events .title', 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 winH = $(window).height()/2+$(id).height;
				//var winW = $(window).width()/2-$(id).width;
				//reset position of popup box
				popup.css({					
					top: -57,
					right: -1.7,
					width: 157,
					height: 31,
					position: 'absolute',
					background: '#900',
					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);
		});
	});
}
