var mTimer1;
var bTimerLock = false;

function grille_start_slide(id, sens) {
	mDiv = document.getElementById('liste_programmes_'+id);
	iDivSize = parseInt(mDiv.firstChild.style.width);
	iNbTranche = parseInt(iDivSize/600);
	iTrancheCourante = parseInt(document.getElementsByName('tranche_liste_programmes_'+id)[0].value);
	
	if (!bTimerLock) {
		window.clearInterval(mTimer1);
		bTimerLock = true;
		
		if (sens=='+') {
			if (iTrancheCourante>=iNbTranche) {
				bTimerLock = false;
				// Creneau horaire suivant
				if (sTrancheHoraireCourante==5) {
					if (sGrilleChaineUnique=='0') {
						if (sJourActif==sJourMed && sTrancheCourante==1) {
							iTmp = 1;
							changeGrilleProgrammes(2, '+', iTmp);
						} else if (sJourActif!=sJourMax) {
							iTmp = 1;
							changeGrilleProgrammes('', '+', iTmp);
						}
					}
				} else {
					iTmp = parseInt(sTrancheHoraireCourante)+1;
					changeGrilleProgrammes('', '+', iTmp);
				}
			} else {
				// Affichage des 3 programmes suivants
				//alert(parseInt(mDiv.style.left));
				iNewPosition = parseInt(mDiv.style.left) - 603;
				//alert(iNewPosition);
				mTimer1 = setInterval("grille_slide('"+id+"', '+', "+iNewPosition+")", 20);
				document.getElementsByName('tranche_liste_programmes_'+id)[0].value = iTrancheCourante+1;
			}
		} else if (sens=='-') {
			if (iTrancheCourante==1) {
				bTimerLock = false;
				// Creneau horaire precedent
				if (sTrancheHoraireCourante==1) {
					if (sGrilleChaineUnique=='0') {
						if (sJourActif==sJourMed && sTrancheCourante==2) {
							iTmp = 5;
							changeGrilleProgrammes(1, '-', iTmp);
						} else if (sJourActif!=sJourMin) {
							iTmp = 5;
							changeGrilleProgrammes('', '-', iTmp);
						}
					}
				} else {
					iTmp = parseInt(sTrancheHoraireCourante)-1;
					changeGrilleProgrammes('', '-', iTmp);
				}
			} else {
				// Affichage des 3 programmes precedents
				iNewPosition = parseInt(mDiv.style.left) + 603;
				mTimer1 = setInterval("grille_slide('"+id+"', '-', "+iNewPosition+")", 20);
				document.getElementsByName('tranche_liste_programmes_'+id)[0].value = iTrancheCourante-1;
			}
		}
	}
}

function grille_slide(id, sens, position) {
	mDiv = document.getElementById('liste_programmes_'+id);
	//alert(mDiv+':'+parseInt(mDiv.style.left));
	if (sens=='-') {
		if (parseInt(mDiv.style.left) >= position) {
			window.clearInterval(mTimer1);
			bTimerLock = false;
		} else {
			mDiv.style.left = parseInt(mDiv.style.left)+3+"px";
		}
	} else {
		if (parseInt(mDiv.style.left) <= position) {
			window.clearInterval(mTimer1);
			bTimerLock = false;
		} else {
			mDiv.style.left = parseInt(mDiv.style.left)-3+"px";
		}
	}
}

function changeGrilleProgrammes(tj, j, th) {
	if (j=='') {
		j = sJourActif;
	} else if (j=='-') {
		if (th=='5') {
			aTmp = sJourActif.split('-')
			oDate1 = new Date(aTmp[2], aTmp[1], aTmp[0]);
			iTmp = oDate1.valueOf()-(1000*60*60*24);
			oDate2 = new Date(iTmp);
			j = '';
			if (oDate2.getDate().toString().length==1) { j += '0'; }
			j += oDate2.getDate()+'-';
			if (oDate2.getMonth().toString().length==1) { j += '0'; }
			j += oDate2.getMonth()+'-';
			if (navigator.userAgent.indexOf("Gecko")==-1 && navigator.userAgent.indexOf("Opera")==-1) {
				j += oDate2.getYear()
			} else {
				j += 1900+oDate2.getYear()
			}
			sJourActif = j;
		} else {
			j = sJourActif;
		}
	} else if (j=='+') {
		if (th=='1') {
			aTmp = sJourActif.split('-')
			oDate1 = new Date(aTmp[2], aTmp[1], aTmp[0]);
			iTmp = oDate1.valueOf()+(1000*60*60*24);
			oDate2 = new Date(iTmp);
			j = '';
			if (oDate2.getDate().toString().length==1) { j += '0'; }
			j += oDate2.getDate()+'-';
			if (oDate2.getMonth().toString().length==1) { j += '0'; }
			j += oDate2.getMonth()+'-';
			if (navigator.userAgent.indexOf("Gecko")==-1 && navigator.userAgent.indexOf("Opera")==-1) {
				j += oDate2.getYear()
			} else {
				j += 1900+oDate2.getYear()
			}
			sJourActif = j;
		} else {
			j = sJourActif;
		}
	} else {
		sJourActif = j;
	}
	if (th=='') { th = sTrancheHoraireCourante; } else { sTrancheHoraireCourante = th; }
	if (tj=='') { tj = sTrancheCourante; bChangementTranche = 0;} else { sTrancheCourante = tj; bChangementTranche = 1; }
	
	if (typeof window.ActiveXObject != 'undefined' ) {
		xmlDoc = new ActiveXObject("Microsoft.XMLHTTP");
		xmlDoc.onreadystatechange = function() { displayGrilleProgrammes(xmlDoc); };
	} else {
		xmlDoc = new XMLHttpRequest();
		xmlDoc.onload = function() { displayGrilleProgrammes(xmlDoc); };
	}
	sUrl = "/processus/page/grille_v2/ajax_grille_programmes.php?";
	sUrl += "&j="+j;
	sUrl += "&th="+th;
	if (sGrilleChaineUnique) {
		sUrl += "&chaine="+sGrilleChaineUnique;
	}
	xmlDoc.open("GET", sUrl, false);
	xmlDoc.send(null);
	
	changeNavigationJours(sTrancheCourante, sJourActif, sTrancheHoraireCourante, bChangementTranche)
}

function displayGrilleProgrammes(xmlDoc) {
	var text = document.getElementById('grilles_programmes');
	if (xmlDoc.readyState == 4) {
		if (xmlDoc.status == 200) {
			aTmp = xmlDoc.responseText.split('######');
			text.innerHTML = aTmp[0];
			eval(aTmp[1]);
		} else {
			alert("There was a problem retrieving the data:\n"+xmlDoc.statusText);
		}
	}
}

function changeListeProgrammes(j, th, chaine) {	
	if (typeof window.ActiveXObject != 'undefined' ) {
		xmlDoc = new ActiveXObject("Microsoft.XMLHTTP");
		if (sGrilleChaineUnique) {
			xmlDoc.onreadystatechange = function() { displayListeProgrammes(xmlDoc, j); };
		} else {
			xmlDoc.onreadystatechange = function() { displayListeProgrammes(xmlDoc, chaine); };
		}
	} else {
		xmlDoc = new XMLHttpRequest();
		if (sGrilleChaineUnique) {
			xmlDoc.onload = function() { displayListeProgrammes(xmlDoc, j); };
		} else {
			xmlDoc.onload = function() { displayListeProgrammes(xmlDoc, chaine); };
		}
	}
	sUrl = "/processus/page/grille_v2/ajax_liste_programmes.php?";
	sUrl += "&j="+j;
	sUrl += "&th="+th;
	sUrl += "&chaine="+chaine;
	xmlDoc.open("GET", sUrl, false);
	xmlDoc.send(null);
}

function displayListeProgrammes(xmlDoc, param) {
	param = new String(param);
	param = param.replace(/\-/g, "");
	var text = document.getElementById('liste_programmes_'+param);
	if (xmlDoc.readyState == 4) {
		if (xmlDoc.status == 200) {
			text.innerHTML = xmlDoc.responseText;
		} else {
			alert("There was a problem retrieving the data:\n"+xmlDoc.statusText);
		}
	}
}

function changeNavigationJours(tj, j, th, chg) {	
	if (typeof window.ActiveXObject != 'undefined' ) {
		xmlDoc = new ActiveXObject("Microsoft.XMLHTTP");
		xmlDoc.onreadystatechange = function() { displayNavigationJours(xmlDoc); };
	} else {
		xmlDoc = new XMLHttpRequest();
		xmlDoc.onload = function() { displayNavigationJours(xmlDoc); };
	}
	if (sGrilleChaineUnique) {
		sUrl = "/processus/page/grille_v2/ajax_navigation_chaine.php?";
		sUrl += "&j="+j;
		sUrl += "&th="+th;
		sUrl += "&chaine="+sGrilleChaineUnique;
	} else {
		sUrl = "/processus/page/grille_v2/ajax_navigation_jours.php?";
		sUrl += "&j="+j;
		sUrl += "&th="+th;
		sUrl += "&tj="+tj;
		sUrl += "&chg="+chg;
	}
	xmlDoc.open("GET", sUrl, false);
	xmlDoc.send(null);
}

function displayNavigationJours(xmlDoc) {
	var text = document.getElementById('navigation_jours');
	if (xmlDoc.readyState == 4) {
		if (xmlDoc.status == 200) {
			//text.innerHTML = xmlDoc.responseText;
			aTmp = xmlDoc.responseText.split('######');
			text.innerHTML = aTmp[0];
			if (aTmp[1]) { sJourActif = aTmp[1]; }
		} else {
			alert("There was a problem retrieving the data:\n"+xmlDoc.statusText);
		}
	}
}

function switchGrilleChaine(chaine) {
	sGrilleChaineUnique = chaine;
	changeGrilleProgrammes('', '', '');
}

function changeAlertState(diffusionId,state,url){
	try{
	if(state==1){
		document.getElementById('diff_prog'+diffusionId).innerHTML='<img src="'+url+'/grille_v2/desactiver.gif">';
		var deactivate=true;
		document.getElementById('diff_prog'+diffusionId).onclick=function(){xajax_open_popup_alert_diffusion('alert_diffusion',diffusionId,deactivate,y);};
	}else{
		document.getElementById('diff_prog'+diffusionId).innerHTML='<img src="'+url+'/grille_v2/activer.gif">';
		document.getElementById('diff_prog'+diffusionId).onclick=function(){xajax_open_popup_alert_diffusion('alert_diffusion',diffusionId,0,y);};
	}
	}
	catch(err){
		
	}
	try{
		changeAlertState2(diffusionId,state,url);
	}
	catch(err){
		
	}
}

function changeAlertState2(diffusionId,state,url){
	if(state==1){
		document.getElementById('diff'+diffusionId).innerHTML='<img src="'+url+'/grille_v2/activee.gif"><br/><img src="'+url+'/grille_v2/desactiver.gif">';
		var deactivate=true;
		document.getElementById('diff'+diffusionId).onclick=function(){xajax_open_popup_alert_diffusion('alert_diffusion',diffusionId,deactivate,y);};
	}else{
		document.getElementById('diff'+diffusionId).innerHTML='<img src="'+url+'/grille_v2/activer.gif">';
		document.getElementById('diff'+diffusionId).onclick=function(){xajax_open_popup_alert_diffusion('alert_diffusion',diffusionId,0,y);};
	}
}


function position(e) {
 x = 0;
 y = 0;
 if (!e) var e = window.event;
 if (e.pageX || e.pageY){
  x = e.pageX;
  y = e.pageY;
 }
 else if (e.clientX || e.clientY){
  x = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
  y = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
 }
}

if (navigator.appName.substring(0,3) == "Net") document.captureEvents(Event.MOUSEMOVE);
document.onmousemove = position;


