/***************************************************************************************
	JavaScript Calendar - Digital Christian Design
	//Script featured on and available at JavaScript Kit: http://www.javascriptkit.com
	// Functions
		changedate(): Moves to next or previous month or year, or current month depending on the button clicked.
		createCalendar(): Renders the calander into the page with links for each to fill the date form filds above.
			
***************************************************************************************/
var globalevents=new Array();
var thisDate = 1;							// Tracks current date being written in calendar
if (typeof(wordMonth)=='undefined' || wordMonth==''){
	var wordMonth = new Array("Janvier","F&eacute;vrier","Mars","Avril","Mai","Juin","Juillet","Aout","Sept.","Oct.","Nov.","Dec.");
}
var today = new Date();							// Date object to store the current date
var todaysDay = today.getDay() + 1;					// Stores the current day number 1-7
var todaysDate = today.getDate();					// Stores the current numeric date within the month
var todaysMonth = today.getUTCMonth() + 1;				// Stores the current month 1-12
var todaysYear = today.getFullYear();					// Stores the current year
var monthNum = todaysMonth;	
var monthNum2chiffres= todaysMonth;				// Tracks the current month being displayed
var yearNum = todaysYear;						// Tracks the current year being displayed
var firstDate = new Date(String(monthNum)+"/1/"+String(yearNum));	// Object Storing the first day of the current month
var firstDay = firstDate.getUTCDay();					// Tracks the day number 1-7 of the first day of the current month
var lastDate = new Date(String(monthNum+1)+"/0/"+String(yearNum));	// Tracks the last date of the current month
var numbDays = 0;
var calendarString = "";
var eastermonth = 0;
var easterday = 0;


function changedate(buttonpressed, events,scope) {
	if (null == events){
		events=globalevents;				
	}else{
		globalevents=events;
	}


	if (buttonpressed == "prevyr") yearNum--;
	else if (buttonpressed == "nextyr") yearNum++;
	else if (buttonpressed == "prevmo") monthNum--;
	else if (buttonpressed == "nextmo") monthNum++;
	else  if (buttonpressed == "return") { 
		monthNum = todaysMonth;
		yearNum = todaysYear;
	}

	

	if (monthNum == 0) {
		monthNum = 12;
		yearNum--;
	}
	else if (monthNum == 13) {
		monthNum = 1;
		yearNum++;
	}

	


	if (typeof(monthNum)!=='undefined' && String(monthNum).length==1)	
		monthNum2chiffres='0'+monthNum;
	else
		monthNum2chiffres=monthNum;
	

	lastDate = new Date(String(yearNum),monthNum,1);
	numbDays = 31;
		if(monthNum==4 ||monthNum==6 || monthNum==9 || monthNum==11 ) --numbDays ;
		if(monthNum==2) {
		 numbDays = numbDays - 3;
		if(yearNum%4==0) numbDays ++;
		 if(yearNum%100==0) numbDays --;
		if(yearNum%400==0) numbDays ++;
		}


	//numbDays = lastDate.getDate();
	firstDate = new Date(String(monthNum)+"/1/"+String(yearNum));
	firstDay = firstDate.getDay() + 1;

	if (typeof(scope)=='undefined'){
		scope='mois';
	}	
	loadevents(1,monthNum ,yearNum,scope);
	createCalendar(events,null,null);
	return;
}




function loadSpecificMonth(monthNum,yearNum, events){
	
	if (null == events){
		events=globalevents;
		
		
	}else{
		globalevents=events;
	}


	if (typeof(monthNum)!=='undefined' && String(monthNum).length==1)	
		monthNum2chiffres='0'+monthNum;
	else
		monthNum2chiffres=monthNum;
	if (monthNum=='01')monthNum=1;
	if (monthNum=='02')monthNum=2;
	if (monthNum=='03')monthNum=3;
	if (monthNum=='04')monthNum=4;
	if (monthNum=='05')monthNum=5;
	if (monthNum=='06')monthNum=6;
	if (monthNum=='07')monthNum=7;
	if (monthNum=='08')monthNum=8;
	if (monthNum=='09')monthNum=9;
	if (monthNum=='10')monthNum=10;
	if (monthNum=='11')monthNum=11;
	if (monthNum=='12')monthNum=12;
	

	lastDate = new Date(String(yearNum),monthNum,1);
	numbDays = 31;
		if(monthNum==4 ||monthNum==6 || monthNum==9 || monthNum==11 ) --numbDays ;
		if(monthNum==2) {
		 numbDays = numbDays - 3;
		if(yearNum%4==0) numbDays ++;
		 if(yearNum%100==0) numbDays --;
		if(yearNum%400==0) numbDays ++;
		}

	firstDate = new Date(String(monthNum)+"/1/"+String(yearNum));
	firstDay = firstDate.getDay() + 1;

	//alert(firstDate+'-'+lastDate );
	//numbDays++;
	createCalendar(events,monthNum,yearNum);
	return;

}

function easter(year) {
// feed in the year it returns the month and day of Easter using two GLOBAL variables: eastermonth and easterday
var a = year % 19;
var b = Math.floor(year/100);
var c = year % 100;
var d = Math.floor(b/4);
var e = b % 4;
var f = Math.floor((b+8) / 25);
var g = Math.floor((b-f+1) / 3);
var h = (19*a + b - d - g + 15) % 30;
var i = Math.floor(c/4);
var j = c % 4;
var k = (32 + 2*e + 2*i - h - j) % 7;
var m = Math.floor((a + 11*h + 22*k) / 451);
var month = Math.floor((h + k - 7*m + 114) / 31);
var day = ((h + k - 7*m +114) % 31) + 1;
eastermonth = month;
easterday = day;
}


function createCalendar(events,m,d) {

	if (m !==null){
		monthNum=m;
		
	}

	if (d !==null){
		yearNum =d;
	}




	calendarString = '';
	var daycounter = 1;
	calendarString += '<table><thead>';
	calendarString += '<tr class="headrow">';
	calendarString += '<td class="button nav"><a href=\"javascript:;\" title=\"Ann&eacute;e pr&eacute;c&eacute;dente\" onClick=\"changedate(\'prevyr\',null)\"><<<\/a><\/td>';
	calendarString += '<td class="button nav"><a href=\"javascript:;\" title=\"Mois pr&eacute;c&eacute;dent\" onClick=\"changedate(\'prevmo\',null)\"><<\/a><\/td>';
	calendarString += '<td class="button nav" colspan=\"3\"><span class="moiscomplet" date="01-'+ monthNum2chiffres +'-'+ yearNum +'"  href=\"#\" title=\"Voir le calendrier du mois complet\" onclick=\"javascript:loadevents(01,' + monthNum + ',' + yearNum + ',\'mois\');">' + wordMonth[monthNum-1] + '&nbsp;&nbsp;' + yearNum + '</span><\/td>';
	calendarString += '<td class="button nav"><a href=\"javascript:;\" title=\"Mois suivant\" onClick=\"changedate(\'nextmo\',null)\">><\/a><\/td>';
	calendarString += '<td class="button nav"><a href=\"javascript:;\" title=\"Ann&eacute;e suivante\" onClick=\"changedate(\'nextyr\',null)\">>><\/a><\/td>';
	calendarString += '<\/tr>';
	calendarString += '<tr class="daynames">';	
	calendarString += '<td class="day name">Lun<\/td>';
	calendarString += '<td class="day name">Mar<\/td>';
	calendarString += '<td class="day name">Mer<\/td>';
	calendarString += '<td class="day name">Jeu<\/td>';
	calendarString += '<td class="day name">Ven<\/td>';
	calendarString += '<td class="day name weekend">Sam<\/td>';
	calendarString += '<td class="day name weekend">Dim<\/td>';
	calendarString += '<\/tr></thead>';

	thisDate == 1;
	/************Resolution du bug = si le 1er jour du mois tombe un dimanche****************/
	if(firstDay==1)
		thisDate=-6;
	
	
	for (var i = 1; i <= 6; i++) {
		calendarString += '<tr class="daysrow">';
		for (var x = 0; x <= 6; x++) {
			var classwe='';
			if (x==5 || x==6){
				classwe=' weekend';
			}
			daycounter = (thisDate - firstDay)+2;
			thisDate++;
			if ((daycounter > numbDays) || (daycounter < 1)) {
				calendarString += '<td class="emptycell">&nbsp;<\/td>';
			} else {
			
				if (checkevents(events,daycounter,monthNum,yearNum,i,x)){
				//if (checkevents(events,daycounter,monthNum,yearNum,i,x) || ((todaysDay == x) && (todaysDate == daycounter) && (todaysMonth == monthNum))){				
					if ((todaysDay == x) && (todaysDate == daycounter) && (todaysMonth == monthNum)) {
						calendarString += '<td class="day false today '+classwe+'"><a href=\"javascript:;\" title=\"Voir les manifestations d\'aujourd\'hui" href=\"#\" onclick=\"javascript:loadevents(' + daycounter + ',' + monthNum + ',' + yearNum + ',\'jour\');" onmouseover=\"javascript:showevents(' + daycounter + ',' + monthNum + ',' + yearNum + ',' + i + ',' + x + ')\">' + daycounter + '<\/a><\/td>';
					}
 					else	calendarString += '<td class="'+classwe+' false day"><a href=\"javascript:;\" title=\"Voir les manifestations du '+daycounter +' '+ wordMonth[monthNum-1] + '\" href=\"#\" onclick=\"javascript:loadevents(' + daycounter + ',' + monthNum + ',' + yearNum + ',\'jour\');"  onmouseover=\"javascript:showevents(' + daycounter + ',' + monthNum + ',' + yearNum + ',' + i + ',' + x + ')\">' + daycounter + '<\/a><\/td>';
				} else {
					if ((todaysDay == x) && (todaysDate == daycounter) && (todaysMonth == monthNum)) {
						calendarString += '<td class="today day disabled">' + daycounter + '<\/td>';
					}else{
						calendarString += '<td class="day disabled">' + daycounter + '<\/td>';
					}
				}
			}
		}
		calendarString += '<\/tr>';
	}

	calendarString += '<tfoot><tr class="footrow"><td class="ttip" colspan="7"><\/td><\/tr></tfoot><\/table>';


	
	$(".calendar").html(calendarString);
	

	thisDate = 1;
}
function loadeventsdiv(day,month,year,idpage,url,scope){
	
	if (day<10){day='0'+day};
	day=day+'';
	day=day.replace("00", "0");
	if (month<10){month='0'+month};	
	$("#datemanifdebut").val(day+'-'+month+'-'+year);
	$("#scope").val(scope);
	var date=day+'-'+month+'-'+year;
	
	$("#afficheEvents").load(url+"/index.php/calendrierhebdo/rechargeAgenda?typecontenu=agenda&datemanifdebut="+date+"&scope="+scope+"&crit1=&crit2=&idpage="+idpage);	

	//$("#afficheEvents").load("index.php/calendrierhebdo/chargementAgendaHebdo&typecontenu=agenda&datemanifdebut="+date+"&scope=jour&idpage="+idpage);	
}

// fonction sp�ciale crit�re
function loadeventspage(day,month,year,url,scope,idpage,liste1,liste2,liste3,liste4){
	
	if (day<10){day='0'+day};
	day=day+'';//cast to str
	day=day.replace("00", "0");
	if (month<10){month='0'+month};	
	$("#datemanifdebut").val(day+'-'+month+'-'+year);
	$("#scope").val(scope);
	var date=day+'-'+month+'-'+year;
	if (typeof(positioncontenu)=='undefined' || typeof(positioncontenu)=='object')positioncontenu='A1';
	$('.page'+idpage).load(url+'index.php/calendrierhebdo/chargementAgendaHebdoCritere?idpage='+idpage+'&typecontenu=aveccritere&datemanifdebut='+date+'&scope='+scope+'&liste1='+liste1+'&liste2='+liste2+'&liste3='+liste3+'&liste4='+liste4+'&iddossiercontenu=e&positioncontenu='+positioncontenu);

	$('.colcontenu #titre_contenu_'+positioncontenu).hide();
	
	urlc=encodeURIComponent(url+'index.php/calendrierhebdo/chargementAgendaHebdoCritere?idpage='+idpage+'&typecontenu=aveccritere&datemanifdebut='+date+'&scope='+scope+'&liste1='+liste1+'&liste2='+liste2+'&liste3='+liste3+'&liste4='+liste4+'&iddossiercontenu=e&positioncontenu='+positioncontenu);
	urlrss=encodeURIComponent('&idpage='+idpage+'&typecontenu=aveccritere&datemanifdebut='+date+'&scope='+scope+'&liste1='+liste1+'&liste2='+liste2+'&liste3='+liste3+'&liste4='+liste4+'&iddossiercontenu=e&positioncontenu='+positioncontenu);

	$("body").append('<div style="display:none" id="tempbarreoutils"></div>');
	$("#tempbarreoutils").load(url+"index.php/outils/reloadBarreOutils?url="+urlc+"&urlrss="+urlrss+"&idpage="+idpage,
		function() { 
			$("#barreoutils").empty().html($("#tempbarreoutils div").html());
			$("#tempbarreoutils").remove();
		}
	);

	
	
	
}

function loadeventsautrepage(day,month,year,url,scope,idpage,liste1,liste2,liste3,liste4){
	if (day<10){day='0'+day};
	day=day+'';//cast to str
	day=day.replace("00", "0");
	if (month<10){month='0'+month};	
	$("#datemanifdebut").val(day+'-'+month+'-'+year);
	$("#scope").val(scope);
	var date=day+'-'+month+'-'+year;
	if (typeof(positioncontenu)=='undefined' || typeof(positioncontenu)=='object')positioncontenu='A1';
	window.location=url+'index.php/calendrierhebdo/chargementAgendaHebdoCritere?idpage='+idpage+'&typecontenu=aveccritere&datemanifdebut='+date+'&scope='+scope+'&liste1='+liste1+'&liste2='+liste2+'&liste3='+liste3+'&liste4='+liste4+'&iddossiercontenu=e&positioncontenu='+positioncontenu;

	
	
	
}

function rechargeCritereParDate(day,month,year,url,scope,idpage,positioncontenu){	

	if (day<10){day='0'+day};
	day=day+'';
	day=day.replace("00", "0");
	if (month<10){month='0'+month};	
	$("#datemanifdebut").val(day+'-'+month+'-'+year);
	$("#scope").val(scope);
	var date=day+'-'+month+'-'+year;
	if (typeof(positioncontenu)=='undefined' || typeof(positioncontenu)=='object')positioncontenu='A1';
	$('#listecritere').load(url+'index.php/contenucritere/chargementCritereByDate?idpage='+idpage+'&datemanifdebut='+date+'&scope='+scope+'&positioncontenu='+positioncontenu+'&liste1='+liste1+'&liste2='+liste2);
	
		
}

function rechargeCriteresLies(day,month,year,url,scope, liste1, liste2, liste3, liste4){
	if (day<10){day='0'+day};
	day=day+'';
	day=day.replace("00", "0");
	
	if (month<10){month='0'+month};	
	$("#datemanifdebut").val(day+'-'+month+'-'+year);
	$("#scope").val(scope);
	var date=day+'-'+month+'-'+year;
	$('#listecritere').load(url+'index.php/contenucritere/chargementCritereLies?idpage='+idpage+'&datemanifdebut='+date+'&scope='+scope+'&positioncontenu='+positioncontenu+'&liste1='+liste1+'&liste2='+liste2+'&liste3='+liste3+'&liste4='+liste4);

		
}


function checkevents(events, day,month,year,week,dayofweek) {



var numevents = 0;
var floater = 0;
		
		for (var i = 0; i < events.length; i++) {

		
		if (events[i][0] == "W") {
			if ((events[i][2] == dayofweek)) numevents++;
		}
		else if (events[i][0] == "Y") {
			if ((events[i][2] == day) && (events[i][1] == month)) numevents++;
		}
		else if (events[i][0] == "F") {
			if ((events[i][1] == 3) && (events[i][2] == 0) && (events[i][3] == 0) ) {
				easter(year);
				if (easterday == day && eastermonth == month) numevents++;
			} else {
				floater = floatingholiday(year,events[i][1],events[i][2],events[i][3]);
				if ((month == 5) && (events[i][1] == 5) && (events[i][2] == 4) && (events[i][3] == 2)) {
					if ((floater + 7 <= 31) && (day == floater + 7)) {
						numevents++;
					} else if ((floater + 7 > 31) && (day == floater)) numevents++;
				} else if ((events[i][1] == month) && (floater == day)) numevents++;
			}
		}

		else if ((events[i][2] == day) && (events[i][1] == month) && (events[i][3] == year)) {
			numevents++;
		}
	}

	if (numevents == 0) {
		return false;
	} else {
		
		return true;
	}
}
/*function loadevents(day,month,year){
	if (day<10){day='0'+day};
	if (month<10){month='0'+month};	
	$("#datemanifdebut").val(day+'-'+month+'-'+year);
	$("#scope").val('jour');
	var date=day+'-'+month+'-'+year;
	$("#afficheEvents").load("index.php/Defaut/rechargeAgenda?datemanifdebut="+date+"&scope=jour&crit1=&crit2=");	
}*/

function showeventsdiv(day,month,year,week,dayofweek) {
		$("#events").hide();
var theevent = "";
var floater = 0;
var titrearticle="";


	for (var i = 0; i < events.length; i++) {
		// First we'll process recurring events (if any):
		if (events[i][0] != "") {
			
			if (events[i][0] == "Y") {
				if ((events[i][2] == day) && (events[i][1] == month)) {
				theevent += titrearticle+ day +'/'+ month +'/'+ year + ' </b><br />';
				theevent += '<br />'+events[i][6] ;
			
				$("#listeevents").html(theevent);
				}
			}			
		
		}
		// Now we'll process any One Time events happening on the matching month, day, year:
		else if ((events[i][2] == day) && (events[i][1] == month) && (events[i][3] == year)) {
			titrearticle = '<b>Le ' + day +'/'+ month +'/'+ year + ' </b><br />';
			theevent += '-' +events[i][6] + ' <br />';
			
		}
	}
	if (theevent == "") 
		$("#listeevents").html("Pas de manifestation ce jour");
	else{
		$("#listeevents").html(titrearticle + theevent);
	}
	$("#events").show();
}


function floatingholiday(targetyr,targetmo,cardinaloccurrence,targetday) {
// Floating holidays/events of the events.js file uses:
//	the Month field for the Month (here it becomes the targetmo field)
//	the Day field as the Cardinal Occurrence  (here it becomes the cardinaloccurrence field)
//		1=1st, 2=2nd, 3=3rd, 4=4th, 5=5th, 6=6th occurrence of the day listed next
//	the Year field as the Day of the week the event/holiday falls on  (here it becomes the targetday field)
//		1=Sunday, 2=Monday, 3=Tuesday, 4=Wednesday, 5=Thurday, 6=Friday, 7=Saturday
//	example: "F",	"1",	"3",	"2", = Floating holiday in January on the 3rd Monday of that month.
//
// In our code below:
// 	targetyr is the active year
// 	targetmo is the active month (1-12)
// 	cardinaloccurrence is the xth occurrence of the targetday (1-6)
// 	targetday is the day of the week the floating holiday is on
//		0=Sun; 1=Mon; 2=Tue; 3=Wed; 4=Thu; 5=Fri; 6=Sat
//		Note: subtract 1 from the targetday field if the info comes from the events.js file
//
// Note:
//	If Memorial Day falls on the 22nd, 23rd, or 24th, then we add 7 to the dayofmonth to the result.
//
// Example: targetyr = 2052; targetmo = 5; cardinaloccurrence = 4; targetday = 1
//	This is the same as saying our floating holiday in the year 2052, is during May, on the 4th Monday
//
var firstdate = new Date(String(targetmo)+"/1/"+String(targetyr));	// Object Storing the first day of the current month.
var firstday = firstdate.getUTCDay();	// The first day (0-6) of the target month.
var dayofmonth = 0;	// zero out our calendar day variable.

	targetday = targetday - 1;

	if (targetday >= firstday) {
		cardinaloccurrence--;	// Subtract 1 from cardinal day.
		dayofmonth = (cardinaloccurrence * 7) + ((targetday - firstday)+1);
	} else {
		dayofmonth = (cardinaloccurrence * 7) + ((targetday - firstday)+1);
	}
return dayofmonth;
}