// JavaScript Document

function initPlanning() {
	tab_plages = new Array();
	tab_rollover = new Array();
	tab_plages_selection = new Array();
}

function initialisation_creneau(argDivPlage,argStatut,argSelection,argDateDebut,argHeureDebut,argDateFin,argHeureFin) {

	//alert(0);
	
	var id_plage = argDivPlage.substr(13,argDivPlage.length);
	
	//alert(argDivPlage+' = '+id_plage+';');
	
	if (!tab_plages[id_plage]) {
		//alert(0);
		tab_plages[id_plage] = new Array();
		tab_plages[id_plage]["div"] = argDivPlage;
		tab_plages[id_plage]["statut"] = argStatut; //vierge / dispo / pas_dispo
		tab_plages[id_plage]["selection"] = argSelection; //cliqué ou non
	}
	
	//eval(argDivPlage+' = '+argSelection+';');
	
	/*if (!tab_plages[argDivPlage]) {
		tab_plages[argDivPlage] = argStatut;
		eval(argDivPlage+'_reservation = '+argSelection);
	}*/

	if (tab_plages[id_plage]["selection"] === true) {
		tab_plages[id_plage]["selection"] = false;
		creation_plage('div_planning_'+id_plage,false);
	}

}

function suppression_selection(argDivPlage) {

	var nom_div_selection = argDivPlage+'_selection';
	
	var mynode = document.getElementById(nom_div_selection);
	var parent = document.getElementById(argDivPlage);
	
	parent.removeChild(mynode);
	
	var id_plage = argDivPlage.substr(13,argDivPlage.length);
	
	tab_plages[id_plage]["selection"] = false; //eval(argDivPlage+'_reservation = false;');
	//eval(argDivPlage+' = false;');

}

function maj_plage(argDivPlage,argDuree) {

	//alert(argDivPlage);
	//alert(argDuree);
	var hauteur = parseInt(argDuree)*30;
	
	document.getElementById(argDivPlage).style.height = hauteur+"px";
	
	//alert(hauteur+' - '+document.getElementById(argDivPlage).style.height);

}

function survolColonne(argPlanningReference,argIdJour,argAction) {
//alert(argPlanningReference+' - '+argIdJour);
	var liste_div = document.getElementsByTagName('div');
	
	var search_string = 'div_planning_'+argPlanningReference+'_'+argIdJour+'_';
	
	for (i=0; i<liste_div.length; i++){
		if (liste_div[i].id.substr(0,search_string.length) == search_string) {
			//alert(liste_div[i].id);
			//alert(liste_div[i].id.substr(search_string.length-6,6));
			var id_plage = liste_div[i].id.substr(13,liste_div[i].id.length);
			if (tab_plages[id_plage]) {
				if (tab_plages[id_plage]['statut'] == 'vierge' || liste_div[i].id.substr(liste_div[i].id.length-6,6) == "entete") {
					if (argAction == 'on') {
						document.getElementById(liste_div[i].id).style.background = '#e5faff';
					} else if (argAction == 'out') {
						document.getElementById(liste_div[i].id).style.background = '#FFFFFF';
					}
				}
			}
		}
	}
	//alert(liste.length);

}

function resetSelectionPlanning(argRefSelection) {

	if (argRefSelection) {

	} else {
	
		var l= tab_plages_selection.length;
		for (i=0; i<l; i++) {
			tab_plages[tab_plages_selection[i].substr(13,tab_plages_selection[i].length)]["selection"] = false;
		}
		
		var nom_div_selection = 'div_selection';
		
		var mynode = document.getElementById(nom_div_selection);
		var parent = document.getElementById(tab_plages_selection[0]);
		
		parent.removeChild(mynode);
		
		tab_plages_selection = new Array();
	
	}

}

function survolPlage(argAction,argDivPlage) {

	//var nombre_creneau_tarif = 6;
	
	if (argAction == 'out') {
	
		if (tab_rollover) {
			var l= tab_rollover.length;
			for (i=0; i<l; i++) {
				document.getElementById(tab_rollover[i]).style.background = '#a4e3f4';
			}
			tab_rollover = new Array();
		}
	
	} else if (tab_plages_selection.length == 0) {
	
		survolPlage('out','');
		
		if (nombre_creneau_tarif == 1) {
		
			tab_rollover.push(argDivPlage);
		
		} else {
		//alert(argDivPlage);
			var id_div_plage = contenu = argDivPlage.replace('div_planning_','');
			//Nombre de creneau dispo apres
			//var l= tab_plages.length;
			//alert(tab_plages.length);
			
			var element_plage = id_div_plage.split('_');
			var id_jour_plage = element_plage[1];
			
			var t = false;
			var tab_rollover_tmp_av = new Array();
			var tab_rollover_tmp_ap = new Array();
			
			for (var i in tab_plages) {
				if (tab_plages[i]['statut'] == 'dispo') {
					//alert(i);
					if (i == id_div_plage) t = true;
					if (!t) tab_rollover_tmp_av.push(i);
					if (t && tab_rollover_tmp_ap.length < nombre_creneau_tarif) {
						//meme jour
						var element_plage_tmp = i.split('_');
						var id_jour_plage_tmp = element_plage_tmp[1];
						if (id_jour_plage == id_jour_plage_tmp) {
							var plage_colle = false;
							//plage collée
							if (tab_rollover_tmp_ap.length > 0) {
								//alert(tab_rollover_tmp_ap.length-1);
								var div_prec = tab_rollover_tmp_ap[tab_rollover_tmp_ap.length-1];
								//prec
								var element_plage_prec_tmp = div_prec.split('_');
								var timestamp_fin_plage_prec_tmp = element_plage_prec_tmp[3];
								//actuel
								var timestamp_debut_plage_actuel_tmp = element_plage_tmp[2];
								if (timestamp_fin_plage_prec_tmp == timestamp_debut_plage_actuel_tmp) plage_colle = true;
							} else {
								plage_colle = true;
							}
							if (plage_colle) tab_rollover_tmp_ap.push(i);
						}
					}
				}
			}
			
			if (tab_rollover_tmp_ap.length < nombre_creneau_tarif && tab_rollover_tmp_av) {
			
				//Recuperation des creneau dispo avt
				tab_rollover_tmp_av.reverse();
				
				var l= tab_rollover_tmp_av.length;
				for (i=0; i<l; i++) {
					var div_prec = tab_rollover_tmp_ap[0];
					//prec
					var element_plage_prec_tmp = div_prec.split('_');
					//actuel
					var element_plage_actuel_tmp = tab_rollover_tmp_av[i].split('_');
					//plage collée
					//alert(element_plage_prec_tmp[2]+' - '+element_plage_actuel_tmp[3]);
					if (element_plage_prec_tmp[2] == element_plage_actuel_tmp[3] && tab_rollover_tmp_ap.length < nombre_creneau_tarif) {
						tab_rollover_tmp_ap.unshift(tab_rollover_tmp_av[i]);
					}
				}
			
			}
			
			var l= tab_rollover_tmp_ap.length;
			for (i=0; i<l; i++) {
				tab_rollover.push('div_planning_'+tab_rollover_tmp_ap[i]);
			}
		
		}
		
		var l= tab_rollover.length;
		if (l < nombre_creneau_tarif) {
			tab_rollover = new Array();
			tab_rollover_tmp_ap = new Array();
		} else {
			for (i=0; i<l; i++) {
				document.getElementById(tab_rollover[i]).style.background = '#f7f3f3';
			}
		}
	
	}
	//alert(argDivPlage);

}

function creation_plage(argDivPlage,argAfficheModalBox) {

	//alert(argDivPlage);
	//alert(style_div_selection);
	
	var continu = true;
	
	if (tab_rollover.length > 0) {
		
		var l= tab_rollover.length;
		for (i=0; i<l; i++) {
			if (tab_plages[tab_rollover[i].substr(13,tab_rollover[i].length)]["selection"] === true) continu = false;
		}
		
		if (continu) {
		
			//Premiere plage
			var id_plage_debut = tab_rollover[0];
			var element_plage = id_plage_debut.split('_');
			var timestamp_debut = element_plage[4];
			
			var d_debut = new Date();
			d_debut.setTime(timestamp_debut * 1000);
			var date_debut = d_debut.getDate();
			var h = d_debut.getHours();
			if (h<10) h = '0' + h;
			var m = d_debut.getMinutes();
			if (m<10) m = '0'+m;
			var heure_debut = h+':'+m;
			
			//Derniere plage
			var id_plage_fin = tab_rollover[tab_rollover.length-1];
			var element_plage = id_plage_fin.split('_');
			var timestamp_fin = element_plage[5];
			
			var d_fin = new Date();
			d_fin.setTime(timestamp_fin * 1000);
			var date_fin = d_fin.getDate();
			var h = d_fin.getHours();
			if (h<10) h = '0' + h;
			var m = d_fin.getMinutes();
			if (m<10) m = '0'+m;
			var heure_fin = h+':'+m;
			
			//alert(heure_debut+' - '+heure_fin);
			
			//Creation div selection
			var nom_div_selection = 'div_selection';
			
			maDiv = document.createElement(nom_div_selection);
			maDiv.id = nom_div_selection;
			maDiv.style.position = 'absolute';
			
			var contenu = style_div_selection;
			contenu = style_div_selection.replace('##NOM_DIV##',nom_div_selection+'_contenu');
			contenu = contenu.replace('##CONTENU_DIV##',heure_debut+' - '+heure_fin);
			
			maDiv.innerHTML = contenu;
			
			//Action clic
			/*
			var lien = lien_selection_planning;
			lien = lien.replace("[planning_nom_div]",argDivPlage);
			
			if (action_selection_planning == 'modalbox') {
				maDiv.onclick = function() {
					//JQueryModalboxOpen('../../../Wc8dc2af5a05b.htm',false,'','',0.1,'',false,'');
					JQueryModalboxOpen(lien,false,'','',0.1,'',false,'');
				};
			} else {
				maDiv.onclick = function() {
					eval(lien);
				};
			}*/
			
			//Ajout
			document.getElementById(tab_rollover[0]).appendChild(maDiv);
			
			//Hauteur
			var hauteur = 0;
			var l= tab_rollover.length;
			for (i=0; i<l; i++) {
				hauteur += parseInt(document.getElementById(tab_rollover[i]).style.height.replace('px','')) + 1;
			}
			
			maDiv.style.height = hauteur+'px';
			maDiv.style.lineHeight = hauteur+'px';
			document.getElementById(nom_div_selection+'_contenu').style.height = hauteur+'px';
			document.getElementById(nom_div_selection+'_contenu').style.lineHeight = hauteur+'px';
			
			var l= tab_rollover.length;
			for (i=0; i<l; i++) {
				tab_plages[tab_rollover[i].substr(13,tab_rollover[i].length)]["selection"] = true;
				tab_plages_selection.push(tab_rollover[i]); 
			}
			//tab_plages[id_plage]["selection"] = true;
			
			//Action selection
			var lien = lien_selection_planning;
			lien = lien.replace("[planning_nom_div]",argDivPlage);
			
			if (argAfficheModalBox) { //Action lors de la creation de la plage
				if (action_selection_planning == 'modalbox') {
					JQueryModalboxOpen(lien,false,'','',0.1,'',false,'');
				} else {
					ShowPageYN(action_selection_planning_ajax_div, lien_selection_planning+'&td='+timestamp_debut+'&tf='+timestamp_fin, 'GET', '');
				}
			}
		
		}
	
	}

}
/*
function creation_plage(argDivPlage,argAfficheModalBox) {

	//alert(argDivPlage);
	//alert(style_div_selection);
	
	var element_plage = argDivPlage.split('_');
	var id_jour = element_plage[3];
	var timestamp_debut = element_plage[4];
	var timestamp_fin = element_plage[5];
	
	var d_debut = new Date();
	d_debut.setTime(timestamp_debut * 1000);
	var date_debut = d_debut.getDate();
	var h = d_debut.getHours();
	if (h<10) { h = '0' + h }
	var m = d_debut.getMinutes();
	if (m<10) { m = '0'+m }
	var heure_debut = h+':'+m;
	
	var d_fin = new Date();
	d_fin.setTime(timestamp_fin * 1000);
	var date_fin = d_fin.getDate();
	var h = d_fin.getHours();
	if (h<10) { h = '0' + h }
	var m = d_fin.getMinutes();
	if (m<10) { m = '0'+m }
	var heure_fin = h+':'+m;
	
	var nom_div_selection = argDivPlage+"_selection";
	
	var id_plage = argDivPlage.substr(13,argDivPlage.length);
	
	//if (eval(argDivPlage) === false) {
	if (tab_plages[id_plage]["selection"] === false) {
	
		//eval(argDivPlage+'_reservation = true;');
		
		infoPopUp_endRollover();
		document.getElementById('info_PopUp').style.zIndex  = '2';
		
		//alert('*'+nom_div);
		
		maDiv = document.createElement(nom_div_selection);
		maDiv.id = nom_div_selection;
		maDiv.style.position = 'absolute';
		
		//maDiv.innerHTML ='<a href=\"javascript:supression_selection(\''+nom_div+'\');\">x</a>';
		//maDiv.innerHTML ='<a href=\"javascript:JQueryModalboxOpen(\'index.php?l_nr=page_custom.php&l_nr_c=5a2d09bf49ab76f2ecb5f19a404b7772&fichier=planning_modalbox&definir_width=false&afficher_body=false&affiche_contenu_uniquement=true\',false,\'\',\'\',0.1,\'\',false,\'\');void(0);\">Lien</a>';
		
		var contenu = style_div_selection;
		contenu = style_div_selection.replace('##NOM_DIV##',nom_div_selection+'_contenu');
		contenu = contenu.replace('##CONTENU_DIV##',heure_debut+' - '+heure_fin);
		//alert(contenu);
		//alert(nom_div_selection+'_contenu');
		maDiv.innerHTML = contenu;
		
		var lien = lien_selection_planning;
		lien = lien.replace("[planning_nom_div]",argDivPlage);
		
		if (action_selection_planning == 'modalbox') {
		maDiv.onclick = function() {
			//JQueryModalboxOpen('../../../Wc8dc2af5a05b.htm',false,'','',0.1,'',false,'');
			JQueryModalboxOpen(lien,false,'','',0.1,'',false,'');
		};
		} else {
			maDiv.onclick = function() {
				eval(lien);
			};
		}
		
		document.getElementById(argDivPlage).appendChild(maDiv);
		
		maDiv.style.height = document.getElementById(argDivPlage).style.height;
		document.getElementById(nom_div_selection+'_contenu').style.height = document.getElementById(argDivPlage).style.height;
		
		//eval(argDivPlage+' = true;');
		tab_plages[id_plage]["selection"] = true;
		//alert(id_plage+' - '+tab_plages[id_plage]["selection"]);
		
		//Action selection
		var lien = lien_selection_planning;
		lien = lien.replace("[planning_nom_div]",argDivPlage);
		
		if (argAfficheModalBox) { //Action lors de la creation de la plage
			if (action_selection_planning == 'modalbox') {
			JQueryModalboxOpen(lien,false,'','',0.1,'',false,'');
			} else {
				ShowPageYN(action_selection_planning_ajax_div, lien_selection_planning+'&td='+timestamp_debut+'&tf='+timestamp_fin, 'GET', '');
			}
		}
		
		//JQueryModalboxOpen('../../../Wc8dc2af5a05b.htm',false,'','',0.1,'',false,'');
	
	}

}
*/
function MonComptePrestationAfficheTarifs(argIdPlanningRessourceReservable,argPlanningRessourceReservablePrestation,argPrestationNom,argLienAjax) {

	var div_tarif = 'planning_forfait_prestation_form_tarif_'+argIdPlanningRessourceReservable;
	
	if (div_planning_form != div_tarif) {
		switchDiv('planning_forfait_prestation_form_tarif_'+argIdPlanningRessourceReservable,'',div_planning_form);
		div_planning_form = 'planning_forfait_prestation_form_tarif_'+argIdPlanningRessourceReservable;
	}
	
	document.getElementById('div_planning_gestion_tarif_statut_'+argIdPlanningRessourceReservable).innerHTML = argPrestationNom+' :';
	
	ShowPageYN('planning_forfait_prestation_form_tarif_contenu_'+argIdPlanningRessourceReservable, argLienAjax, 'GET', '');

}

function MonComptePrestationAfficheForm(argRefForm) {

	if (document.getElementById('mon_compte_planning_form_ajout_'+argRefForm).style.display == 'none') {
	
		if (argRefForm == 'ressource') {
			var autre_ref_form = 'groupe';
		} else {
			var autre_ref_form = 'ressource';
		}
		
		document.getElementById('mon_compte_planning_form_ajout_'+autre_ref_form).style.display = 'none';
		document.getElementById('mon_compte_planning_form_ajout_'+argRefForm).style.display = 'inline';
	
	}

}

