var arrBoxMonth = new Array(); 
var calenderLayer="";
var date = SRCDate;
var win="";
var mois1, mois2, mois3;
var spacing = "";
var cont = "";
var DateRec;
var DateAnRec;
var daysOfShow = new Array("6"); /* Dimanche = 0 ; Lundi = 1; etc. Ne pas omettre guillemets, lorsque une seule journee */
var timeShow = "1730";
var showName = "/CBFT/5sur5";
var calendarNote = "Veuillez noter que les émissions sont conservées pour une période de deux mois après leur diffusion.";
var startDate = "20070914"; /* Date de debut des emissions */

function initCalendrier(NumberOfMonth, objLay) {
	NumberOfMonth = 1
	
	if (sRequest.sQueryString("r") == 2){NumberOfMonth = 2}
	if (sRequest.sQueryString("r") == 3){NumberOfMonth = 3}
	
	win = new RastaMartWindow();
	calenderLayer = new RastaMartLay(objLay);
	
	for (y=1;y<=NumberOfMonth;y++) {

		if ((y == 2) && (NumberOfMonth == 3)) {y = 3}
		
		if (y == 1) {
			//alert(1)
			setNewBoxMonth("mois1", date.getDate(), date.getMonth(), date.getFullYear(), date.getHours(), date.getMinutes());
			if (NumberOfMonth == 1){
				cont += getContModele(BoxMonth("mois1"));
				cont += spacing;
			}
		} 
		
		if (y == 2) {
			
			if (NumberOfMonth == 2){
			//alert(2)
				setNewBoxMonth("mois"+y, BoxMonth("mois"+eval(y+"-1")).date, BoxMonth("mois"+eval(y+"-1")).getPrevMonth(), BoxMonth("mois"+eval(y+"-1")).getPrevMonthYear(1), date.getHours(), date.getMinutes());
				
				cont += getContModele(BoxMonth("mois"+y));
				cont += spacing;
				}
		}
		
		if (y == 3) {	
			
			if (NumberOfMonth == 3){
			
				DateRec = date.getMonth() - 2;
				DateAnRec = date.getFullYear();
				
				if(DateRec==-1) {
					DateRec=11;
					DateAnRec = DateAnRec - 1;
				}
				if(DateRec==-2) {
					DateRec=10;
					DateAnRec = DateAnRec - 1;
				}
				
				
				setNewBoxMonth("mois3", date.getDate(), DateRec, DateAnRec, date.getHours(), date.getMinutes());
				if (NumberOfMonth == 3){
					cont += getContModele(BoxMonth("mois3"));
					cont += spacing;
				}
			}
		}
	}

	var layCalCont = "";
	layCalCont+='<table width="100%" border=0 cellspacing="0" cellpadding="1">';
	layCalCont+='<tr valign=top>';
	layCalCont+='<td align="center">';	
	layCalCont+='<table width="100%" border=0 cellspacing="0" cellpadding="2" class="calCouleur">';
	layCalCont+='<tr valign=top>';
	layCalCont+='<td align="center"><img src=Img/spacer.gif border=0 width=1 height=1>';	
	layCalCont+='</td></tr><tr valign=top>';
	layCalCont+='<td align="center">';	
	layCalCont+='';
	layCalCont+=(cont);
	layCalCont+='</td>';
	layCalCont+='</tr></table> ';
	layCalCont+='</td>';
	layCalCont+='</tr></table> ';
	
	calenderLayer.setCont(layCalCont);
}





function getContModele(objCurrDate) {

	var strry
	var strrm
	var strrd		
	var strrdayof		
	strry = date.getFullYear()
	strrm = date.getMonth() + 1
	strrd = date.getDate()
	strrdayof = date.getDay()
		
	FirstWeek = true;
	cont = "";
	cont += "<table border='0' width='100%' cellspacing='0' cellpadding='0'><tr>";
	if (objCurrDate.mois==date.getMonth()&&objCurrDate.annee==date.getFullYear()) cont += "<td valign='bottom'></td>";	
	cont += "<td><table " + paramContentOfTable + "><tr>";
	cont += "<td colspan='7'><img src=Img/spacer.gif border=0 width=1 height=1></td></tr><tr><td colspan='7'" + paramContentOfTdTitre + "  ><div align=center class=calMois>"

	var rett 
	
	rett = 1
	
	if (sRequest.sQueryString("r") == 2){rett = 2}
	if (sRequest.sQueryString("r") == 3){rett = 3}
	

	if (rett == 1){
		cont += "<a href='" + location.pathname + "?r=2'>";	
	}
	if (rett == 2){
		cont += "<a href='" + location.pathname + "?r=3'>";	
	}
	cont += "««</a> ";	
	cont += ArrMois[objCurrDate.mois];
	cont += "&nbsp;";	
	if (rett == 3){
		cont += "<a href='" + location.pathname + "?r=2'>";	
	}
	if (rett == 2){
		cont += "<a href='" + location.pathname + "'>";	
	}
	cont += "»»</a></div></td></tr><table>";
	cont += "<table " + paramContentOfTable3 + "><tr>";
	for(i=0;i<7;i++) {
		if ((strrdayof == i) && (objCurrDate.mois==date.getMonth())){
			cont += "<td><div align=center class=cadreJour>" + ArrTitleColumns[i] + "</div></td>";
		}else{
			cont += "<td><div align=center class=cadreJour>" + ArrTitleColumns[i] + "</div></td>";
		}

	}
	cont += "</tr><tr><td><img src=Img/spacer.gif border=0 width=1 height=5></td></tr></table><table " + paramContentOfTable2 + ">";
	cont += paramSpacingEntreTitreEtBoite;
	cont += "<tr>";
	nbrD = 0;
	
	var AffLien = 0;
	
	for(i=1;i<=ArrlastDayOfMonth[objCurrDate.mois];i++) {
		if (i==1) {	
			for(x=1;x<=7;x++) {	
				if (x<=objCurrDate.firstDayduMois) {
					cont += paramEmptyCell;
					nbrD ++;
				}
			}
		}
		cont += "<td " + paramContentOfTdFull + " >";
		var tempStrMois = eval(objCurrDate.mois+"+"+1);
		if (tempStrMois<10) tempStrMois = "0"+tempStrMois;
		tempStrMois = tempStrMois + "";
		var tempStrDate = i;
		if (tempStrDate<10) tempStrDate = "0"+tempStrDate;
		tempStrDate = tempStrDate + "";
		
		strLien = '';		
		//strLien = ZapTJ(objCurrDate.annee + tempStrMois + tempStrDate);	
		strLien = myCalendar(objCurrDate.annee + tempStrMois + tempStrDate);

		AffLien = 0
		
		if (!checkForExeption(objCurrDate.annee, tempStrMois, tempStrDate)) {
			if (objCurrDate.mois!=date.getMonth()||objCurrDate.annee!=date.getFullYear()) {
				cont += strLien;
				if ((nbrD != 0) && (nbrD != 6)){AffLien = 1}
			} else { 
				if (i<objCurrDate.date) {
					cont += strLien;
					if ((nbrD != 0) && (nbrD != 6)){AffLien = 1}
				} else if (i==objCurrDate.date) {
					if (objCurrDate.heure>DispoHeure) {
						cont += strLien;
						if ((nbrD != 0) && (nbrD != 6)){AffLien = 1}
					} else if (objCurrDate.heure==DispoHeure) {
						if (objCurrDate.minute>DispoMinute) {
							cont += strLien;
							if ((nbrD != 0) && (nbrD != 6)){AffLien = 1}
						}
					}
				}
			}
		} else {
			cont += "<a href='javascript:;' onClick='showErreur();' >";
		}

		
		if (AffLien == 0){
			cont += "<div align=center class=cadreCalendrier>" + i + "</div></a>"
		}else{
			if ((strry == objCurrDate.annee)&&(strrm == tempStrMois)&&(strrd == i)){
				cont += "<div align=center class=dateChoisi>" + i + "</div></a>"
			}else{
				cont += "<div align=center class=cadreCalendrier>" + i + "</div></a>"
			}
		}
		
		
		cont += "</td>";
		nbrD ++;
		if (nbrD >= 7) {
			nbrD = 0;
			cont += "</tr><tr>";
		}
	}
	for(nbrD=nbrD;nbrD<7;nbrD++) {
		cont += "<td></td>";
	}
	cont += "</tr></table><table width=160 align=center  border='0' cellpadding=1 cellspacing=0 background='img/trans.gif'><tr><td class=note><br>" + calendarNote +"</td></tr></table></td>";

	if (objCurrDate.mois==date.getMonth()&&objCurrDate.annee==date.getFullYear()) cont += "<td valign='top'></td>";
	cont += "</tr></table> ";
	
	return cont;
}

/*function ZapTJ(sDate) {
	if (nbrD == 0){
		return "<a href='javascript:;' onClick=\"window.open('http://www.radio-canada.ca/util/zapmedia/version10/detecte.asp?menu=1&lien=/medianet/RDI2/telejournal" + sDate + "2100.asx&promo=ZAPmedia_Telejournal','fenetreInter','toolbar=no,directories=no,status=no,menubar=no,scrollbars=yes,history=no,resizable=yes,width=1,height=1');\" class='CalTJLien'>";
			}else{
		return "<a href='javascript:;' onClick=\"window.open('http://www.radio-canada.ca/util/zapmedia/version10/detecte.asp?menu=1&lien=/medianet/CBFT/telejournal" + sDate + "2200.asx&promo=ZAPmedia_Telejournal','fenetreInter','toolbar=no,directories=no,status=no,menubar=no,scrollbars=yes,history=no,resizable=yes,width=1,height=1');\" class='CalTJLien'>";
		}
}*/

/*function ZapTJ(sDate) {
	var sAnnee = sDate.substr(0,4);
	var sChaine = "";
	var sHeure = "";
	if (nbrD == 0){
		sChaine = "RDI2";
		sHeure = "2100";
	} else {
		sChaine = "CBFT";
		sHeure = "2200";
	}
	if (sDate <= '20060322') {
		return "<a href='javascript:;' onClick=\"window.open('http://www.radio-canada.ca/util/zapmedia/version10/detecte.asp?menu=1&lien=/medianet/" + sChaine + "/telejournal" + sDate + sHeure + ".asx&promo=ZAPmedia_Telejournal','fenetreInter','toolbar=no,directories=no,status=no,menubar=no,scrollbars=yes,history=no,resizable=yes,width=1,height=1');\" class='CalTJLien'>";
	} else {
		return "<a href='javascript:;' onClick=\"window.open('http://www.radio-canada.ca/util/zapmedia/version10/detecte.asp?menu=1&lien=/medianet/" + sAnnee + "/" + sChaine + "/telejournal" + sDate + sHeure + ".asx&promo=ZAPmedia_Telejournal','fenetreInter','toolbar=no,directories=no,status=no,menubar=no,scrollbars=yes,history=no,resizable=yes,width=1,height=1');\" class='CalTJLien'>";
	}
}*/

/* Fonction qui genere les liens (par date de calendrier) vers la console Audio-Video */
function myCalendar(sDate) {
	var sAnnee = sDate.substr(0,4);
	var sMois = sDate.substr(4,2) - 1;
	var sJour = sDate.substr(6,2);
	var sChaine = "";
	var sHeure = "";
	/*if (nbrD == 0){
		sChaine = "RDI2";
		sHeure = "2100";
	} else {
		sChaine = "CBFT";
		sHeure = "2200";
	}*/
	if (sDate > startDate) {
		var thisDate = new Date(sAnnee, sMois, sJour);
		var weekday = thisDate.getDay();
		for(var foo in daysOfShow) {
		if(sDate >= 20080412){
			return "";
		}else if(weekday == daysOfShow[foo]) {
				return "<a href='javascript:void(0);' onClick=\"window.open('/util/zapmedia/version10/detecte.asp?menu=1&lien=/medianet/" + sAnnee + showName + sDate + timeShow + ".asx','fenetreInter','toolbar=no,directories=no,status=no,menubar=no,scrollbars=yes,history=no,resizable=yes,width=1,height=1');\" class='calLien'>";
			}
		}
		return "";
	} else {
		return "";
	}
}


function showErreur() {
	alert(strLienErreur);
}


function checkForExeption(pAnnee, pMois, pJour) {
	for (var i=0; i < jourException.length; i++) {
		var tempDate = jourException[i].split("/");
		var tempAnnee = tempDate[0];
		var tempMois = tempDate[1];
		var tempJour = tempDate[2];
		if (tempAnnee == String(pAnnee) && tempMois == String(pMois) && tempJour == String(pJour)) {
			return true;
		}
	}
	return false;
}



function BoxMonth(ID) {
	var i;
	if ( typeof(ID) == "number")
		return arrBoxMonth[ID].content;
	for ( i = 0; i < arrBoxMonth.length; i++)
	{
		if(arrBoxMonth[i].idName == ID)
			return arrBoxMonth[i].content;
	}
}


function setNewBoxMonth(boxName,boxDate, boxMonth, boxYear, nowHours, nowMinutes) {
	arrBoxMonth[arrBoxMonth.length] = new ClassObjMois(boxDate, boxMonth, boxYear, nowHours, nowMinutes);
	arrBoxMonth[arrBoxMonth.length-1].idName = boxName;
	return BoxMonth(boxName);
}
function ClassObjMois(boxDate, boxMonth, boxYear, nowHours, nowMinutes) {
	this.content = new objMois(boxDate, boxMonth, boxYear, nowHours, nowMinutes);
	this.idName = "";
}
function objMois(date, month, year, hours, minutes) {
	this.date = date
	this.mois = month;
	this.annee = year;
	this.heure = hours;
	this.minute = minutes;
	
	firstDayOfMonth = new Date();
	firstDayOfMonth.setYear(year);
	firstDayOfMonth.setMonth(month);
	firstDayOfMonth.setDate(1);
	this.firstDayduMois = firstDayOfMonth.getDay();
	
	return this;
}

objMois.prototype.getPrevMonth = function() {
	prevMonth = this.mois-1; 
	if(prevMonth==-1) prevMonth=11; 
	return prevMonth;
}
objMois.prototype.getPrevMonthYear = function() {
	prevMonth = this.mois-1;
	prevMonthYear = this.annee;
	if(prevMonth==-1) prevMonthYear-=1;
	return prevMonthYear;
}
