

// permet de supprimer un groupe entier (selection de l'utilisateur)
function delete_seat_per_group(ar_groups){
	for(i=0; i<(ar_groups.length); i++){
		//console.log(ar_groups[i]);
		// on switch la selection
		switch_cell_selection($(ar_groups[i]));
	}
	// on cache la fleche si jamais elle était visible
	$('error_seat_arrow').setStyle({
			display: 'none'
	});
	// on recharge la function de validation
	validate_user_choices(room, ar_seat_to_sold, array_price_association);
}
function in_array(needle, haystack, argStrict) {
    var key = '', strict = !!argStrict;

    if (strict) {
        for (key in haystack) {
            if (haystack[key] === needle) {
                return true;
            }
        }
    } else {
        for (key in haystack) {
            if (haystack[key] == needle) {
                return true;
            }
        }
    }

    return false;
}

// I am looking for X seat(s) contiguous
// return Bolean
function looking_for_contiguous_seat(price_category, nbr_seat){
	var i_recherche = 1;
	var fct_return = false;
	// on boucle les sièges selectable 
	$$('#table_grid .selectable').each(function(element) {
		seat_id = Element.readAttribute(element, 'id');
		var elem = seat_id.split('|');
		seat_id_y = elem[0];
		seat_id_x = elem[1];
		
		var new_seat_id = seat_id_y + '|' + (parseInt(seat_id_x)+1);
		
		if(typeof($(new_seat_id)) != 'undefined' && $(new_seat_id) != null && $(new_seat_id).hasClassName('selectable') && $(new_seat_id).hasClassName(price_category) && $(seat_id).hasClassName('selected') == false && $(new_seat_id).hasClassName('selected') == false){
			i_recherche++;
		}
		else {
			i_recherche = 1;
		}
		if(nbr_seat == i_recherche){
			fct_return = true;
		}
	});
	return fct_return;
}

// Plus d'une place entre 2 groupes ? (obligatoire)
function more_th_one_seat_between_two_group(ar_groups, i_ar_choice_error, ar_choice_error){
	var fct_return = true;
	for(i=0; i<(ar_groups.length-1); i++){
		// Dernier id du premier groupe
		var last_seat_id = ar_groups[i].last();
		var elem = last_seat_id.split('|');
		var last_seat_id_y = parseInt(elem[0]);
		var last_seat_id_x = parseInt(elem[1]);
		// Premier id du second groupe
		var first_seat_id = ar_groups[i+1].first();
		var elem = first_seat_id.split('|');
		var first_seat_id_y = parseInt(elem[0]);
		var first_seat_id_x = parseInt(elem[1]);
		
		if(last_seat_id_x+2 == first_seat_id_x && last_seat_id_y == first_seat_id_y && $(last_seat_id_y + '|' + (last_seat_id_x+1)).hasClassName('selectable')){
			fct_return = false;
			//console.log(last_seat_id_x + ' - ' + first_seat_id_x);
			// si une erreur pour ce groupe n'est pas encore déclarée,
			if(typeof(ar_choice_error[i_ar_choice_error]) == "undefined"){
				ar_choice_error[i_ar_choice_error] = new Array();
			}
			ar_choice_error[i_ar_choice_error].push({ group_id: i, error_type: 'one_seat_between_group', ar_group: ar_groups[i] });
			i_ar_choice_error++;
		}
	}
	return fct_return;
}

// Validation of the user's choice
// return Array
function validate_user_choices(room, ar_seat_to_sold, array_price_association){
	// init
	ar_groups = new Array();			
	var ar_seat_already_declare = new Array();	
	i_groups = 0;
	// rangée en cours pour différencier les groupes
	var line_number = 0;
	
	/////////////////////// ///////////////////////////////
	// Separation des groupes
	/////////////////////// ///////////////////////////////
	// on boucle les siège selectionné					
	$$('#table_grid .selected').each(function(element) {
		// on récupere l'id du siége
		// et on incrémente x
		seat_id = Element.readAttribute(element, 'id');
		if(typeof(room['seat'][seat_id]) != "undefined" && typeof(ar_seat_already_declare[seat_id]) == "undefined"){
			//console.log(seat_id);
			var elem = seat_id.split('|');
			seat_id_y = elem[0];
			// on défini la rangée en cours
			line_number = seat_id_y;
			seat_id_x = elem[1];
			
			// ajout du siege en cours dans les tableau
				// si groupe pas encore déclaré,
				if(typeof(ar_groups[i_groups]) == "undefined"){
					ar_groups[i_groups] = new Array();
				}
				ar_groups[i_groups].push(seat_id);
					
				// entrée unique
				ar_groups[i_groups] = ar_groups[i_groups].uniq();
					
				// on ajoute au tableau des sièges déjà déclaré
				ar_seat_already_declare.push(seat_id);
			//										
		
			// on boucle sur le nombre de colonne
			// moins la position du siège
			for (i=1; i<=((room['params']['init_column'])-seat_id_x); i++){	
				// si il fait partie du tableau des sièges				
				// si il fait partie du tableau des sièges	en vente
				// si il est selecté 
				// si il fait partie de la même rangée										
				if(typeof(room['seat'][seat_id_y + '|' + (parseInt(seat_id_x)+1)]) != 'undefined' && typeof(ar_seat_to_sold[seat_id_y + '|' + (parseInt(seat_id_x)+1)]) != 'undefined' && $(seat_id_y + '|' + (parseInt(seat_id_x)+1)).hasClassName('selected') && seat_id_y == line_number){
					
					// on ajoute au tableau des sièges déjà déclaré
					ar_seat_already_declare.push(seat_id_y + '|' + (parseInt(seat_id_x)+1));
					
					// on ajoute au groupe
					ar_groups[i_groups].push(seat_id_y + '|' + (parseInt(seat_id_x)+1));
					
					// entrée unique
					ar_groups[i_groups] = ar_groups[i_groups].uniq();
					
				}
				else {
					i_groups++;
					break;
				}								
			}
			// si on est en bout de ligne, on incrémente le nombre de groupe
			if(room['params']['init_column'] == parseInt(seat_id_x)){
				i_groups++;
			}
		}
		// entrée unique
		ar_seat_already_declare = ar_seat_already_declare.uniq();
	});
	//console.log(ar_seat_already_declare);								
	
	/////////////////////// ///////////////////////////////
	// Application des contraintes
	/////////////////////// ///////////////////////////////
	// tableau de retour des erreurs
	var ar_choice_error = new Array();
	var i_ar_choice_error = 0;						
	
	// Dans le cas d'un abonnement
		if(if_abonnement_item == 1){
			// n'a pas sélectionné assez de place ou inversément		
			//console.log($$('#table_grid .selected').length + ' ' + if_abonnement_item_max_select);	
			// trop
			if($$('#table_grid .selected').length > if_abonnement_item_max_select){
				
				// si une erreur pour ce groupe n'est pas encore déclarée,
				if(typeof(ar_choice_error[i_ar_choice_error]) == "undefined"){
					ar_choice_error[i_ar_choice_error] = new Array();
				}
				ar_choice_error[i_ar_choice_error].push({ error_type: 'if_abonnement_item_max'});
				i_ar_choice_error++;
			}
			// trop peu
			if($$('#table_grid .selected').length < if_abonnement_item_max_select){
				
				// si une erreur pour ce groupe n'est pas encore déclarée,
				if(typeof(ar_choice_error[i_ar_choice_error]) == "undefined"){
					ar_choice_error[i_ar_choice_error] = new Array();
				}
				ar_choice_error[i_ar_choice_error].push({ error_type: 'if_abonnement_item_min'});
				i_ar_choice_error++;
			}
		}
	
	// N'a pas laissé un siège seul et disponible 
	for(i=0; i<ar_groups.length; i++){
	
		// Dans le cas ou le groupe est centrer dans un bloc
		// ex: |/|/|/|o|x|x|x|o|/|/|/|
		var if_first_left_available = false;		
		var if_first_right_available = false;	
		
		// on regarde à gauche et à droite si il reste un siège seul et disponible
		
		// Gauche
		var first_seat_id = ar_groups[i].first();
		var elem = first_seat_id.split('|');
		var first_seat_id_y = parseInt(elem[0]);
		var first_seat_id_x = parseInt(elem[1]);
		
		if(typeof(room['seat'][first_seat_id_y + '|' + (first_seat_id_x-1)]) != 'undefined' && typeof(ar_seat_to_sold[first_seat_id_y + '|' + (first_seat_id_x-1)]) != 'undefined' && (first_seat_id_x-1) != 0 && $(first_seat_id_y + '|' + (first_seat_id_x-1)).hasClassName('selectable')){
			// si le siege encore avant est également disponible
			if(typeof(room['seat'][first_seat_id_y + '|' + (first_seat_id_x-2)]) != 'undefined' && typeof(ar_seat_to_sold[first_seat_id_y + '|' + (first_seat_id_x-2)]) != 'undefined' && (first_seat_id_x-2) != 0 && $(first_seat_id_y + '|' + (first_seat_id_x-2)).hasClassName('selectable')){
				//console.log('valide');
			}
			else {
				// si ce groupe est possible ailleur, on le conseille
				if(looking_for_contiguous_seat('price_category_' + array_price_association[ar_groups[i][0]], (ar_groups[i].length + 2)) == true){									
					//console.log('non valide - left');
					// si une erreur pour ce groupe n'est pas encore déclarée,
					if(typeof(ar_choice_error[i_ar_choice_error]) == "undefined"){
						ar_choice_error[i_ar_choice_error] = new Array();
					}
					ar_choice_error[i_ar_choice_error].push({ group_id: i, error_type: 'left_seat_alone', ar_group: ar_groups[i] });
					i_ar_choice_error++;
				}
				if_first_left_available = true;
			}
		}
		
		// Droite
		var last_seat_id = ar_groups[i].last();
		var elem = last_seat_id.split('|');
		var last_seat_id_y = parseInt(elem[0]);
		var last_seat_id_x = parseInt(elem[1]);
		
		if(typeof(room['seat'][last_seat_id_y + '|' + (last_seat_id_x+1)]) != 'undefined' && typeof(ar_seat_to_sold[last_seat_id_y + '|' + (last_seat_id_x+1)]) != 'undefined' && (last_seat_id_x+1) != 0 && $(last_seat_id_y + '|' + (last_seat_id_x+1)).hasClassName('selectable')){
			// si le siege encore après est également disponible
			if(typeof(room['seat'][last_seat_id_y + '|' + (last_seat_id_x+2)]) != 'undefined' && typeof(ar_seat_to_sold[last_seat_id_y + '|' + (last_seat_id_x+2)]) != 'undefined' && (last_seat_id_x+2) != 0 && $(last_seat_id_y + '|' + (last_seat_id_x+2)).hasClassName('selectable')){
				//console.log('valide');
			}
			else {
				// si ce groupe est possible ailleur, on le conseille
				if(looking_for_contiguous_seat('price_category_' + array_price_association[ar_groups[i][0]], (ar_groups[i].length + 2)) == true){									
					//console.log('non valide- right');
					// si une erreur pour ce groupe n'est pas encore déclarée,
					if(typeof(ar_choice_error[i_ar_choice_error]) == "undefined"){
						ar_choice_error[i_ar_choice_error] = new Array();
					}
					ar_choice_error[i_ar_choice_error].push({ group_id: i, error_type: 'right_seat_alone', ar_group: ar_groups[i] });
					i_ar_choice_error++;
				}
				if_first_right_available = true;
			}
		}									
		// Dernière vérification
		// Dans le cas ou le groupe est centrer dans un bloc
		// ex: |/|/|/|o|x|x|x|o|/|/|/|
		if(if_first_left_available == true && if_first_right_available == true){
			//console.log('erreur centrage');
			// si une erreur pour ce groupe n'est pas encore déclarée,
			if(typeof(ar_choice_error[i_ar_choice_error]) == "undefined"){
				ar_choice_error[i_ar_choice_error] = new Array();
			}
			ar_choice_error[i_ar_choice_error].push({ group_id: i, error_type: 'group_centering_error', ar_group: ar_groups[i] });
			i_ar_choice_error++;
		}
	}
	more_th_one_seat_between_two_group(ar_groups, i_ar_choice_error, ar_choice_error);
	
	// on vide le div 
	Element.update($('user_choice'), '');
	
	// Si aucunne erreur, on affiche le panier
	//console.log(ar_groups);
	//console.log(ar_choice_error);
	if(ar_choice_error.length == 0){
		user_selection(i_groups, ar_groups, array_price_association);
	}
	else {
		// affichage des erreurs dans le div de la selection
		show_user_selection_error(ar_choice_error);
	}
	$('user_choice').up().scrollTo();
}
// affiche les erreurs de selection de l'utilisateur
function show_user_selection_error(ar_choice_error){

	var p_error_header = new Element('p', { className: 'header_general' }).insert(ar_error_code['header_general']);
	
	var table = new Element('table',{ id:'user_choice_tbl', cellpadding:0, cellspacing:0});
	var tbody = new Element('tbody');
	
	// on boucle le nombre d'erreur
	 	for(i=0; i<ar_choice_error.length; i++){
	 		var error = ar_choice_error[i];
	 		if(error[0].error_type == 'if_abonnement_item_max'){
	 		
				var table_td = new Element('td', {  });
				
				var p_error_header = new Element('p', { className: 'header_general' }).insert(ar_error_code[error[0].error_type]);  										
				
				//Element.insert(table_td, p_description);
		 	
				var table_tr = new Element('tr', { className: '', rel: '' });
				Element.insert(table_tr, table_td);								
			
				Element.insert(tbody, table_tr);
				
	 		} else if(error[0].error_type == 'if_abonnement_item_min'){
	 		
				var table_td = new Element('td', {  });
				
				var p_error_header = new Element('p', { className: 'header_general' }).insert(ar_error_code[error[0].error_type]);  										
				
				//Element.insert(table_td, p_description);
		 	
				var table_tr = new Element('tr', { className: '', rel: '' });
				Element.insert(table_tr, table_td);								
			
				Element.insert(tbody, table_tr);
				
	 		} else {
				
				// intitule
				// gestion du code erreur string
				var table_td = new Element('td', {  });
				var seat_id = false;
				
				// dans le cas d'un blanc entre 2 groupes, on selectionne le siège entre les 2
				if(error[0].error_type == 'one_seat_between_group' || error[0].error_type == 'right_seat_alone'){
				var seat_id = error[0].ar_group.last();
				var elem = seat_id.split('|');
				var seat_id_y = parseInt(elem[0]);
				var seat_id_x = parseInt(elem[1]);
				seat_id = seat_id_y + '|' + (seat_id_x + 1);
				}
				else if(error[0].error_type == 'group_centering_error' || error[0].error_type == 'left_seat_alone'){
				var seat_id = error[0].ar_group.first();
				var elem = seat_id.split('|');
				var seat_id_y = parseInt(elem[0]);
				var seat_id_x = parseInt(elem[1]);
				seat_id = seat_id_y + '|' + (seat_id_x - 1);
				}
				
				var p_description = new Element('p').insert(ar_error_code[error[0].error_type]);  										
				
				Element.insert(table_td, p_description);
		 	
				var table_tr = new Element('tr', { className: 'error_item', rel: seat_id });
				Element.insert(table_tr, table_td);
				
				// delete
				var table_td = new Element('td', { valign: 'top', style: 'width: 30px;' });
				var delete_icon = new Element('img', { src: '/sf/sf_admin/images/cancel.png' });  			
				var delete_link = new Element('a', { href: 'javascript:delete_seat_per_group(' + (error[0].ar_group).toJSON() + ')'}).insert(delete_icon);							
				
				Element.insert(table_td, delete_link);
				Element.insert(table_tr, table_td);  									
			
				Element.insert(tbody, table_tr);
			}
		}				
	
	Element.insert(table, tbody);
	Element.insert($('user_choice'), table);

		var p_error_footer = new Element('p', { className: 'footer_general' }).insert(ar_error_code['footer_general']);
	Element.insert($('user_choice'), p_error_footer);
	
	// dans le cas d'erreur abonnement, on indique directement 	
	Element.insert($('user_choice'), { top: p_error_header });
	
	// affichage de la fleche montrant l'erreur au passage de la souris sur l'intitulé de l'erreur
	$$('.error_item').each(function(element) {
		if(element.hasAttribute('rel')){
			var element_position = $(element.getAttribute('rel')).cumulativeOffset();
			element.observe('mouseover', function(event){
				//console.log(element_position);
				$('error_seat_arrow').setStyle({
						top: (element_position.top-10) + 'px',
						left: (element_position.left-30) + 'px',
						display: 'block'
				});
				Effect.Shake('error_seat_arrow', { distance: 10 });
			});
			element.observe('mouseout', function(event){
				$('error_seat_arrow').setStyle({
						display: 'none'
				});
			});
		}
     }); 
	
}

// affiche les informations pour l'ajout dans le panier
function user_selection(nbr_group, ar_groups, array_price_association){
	//console.log(nbr_group);
	
	var table = new Element('table',{ id:'user_choice_tbl', cellpadding:0, cellspacing:0});
	var tbody = new Element('tbody');
	
	// on boucle le nombre de groupe
	// on retrouve la rangée et le prix
	 	for(i=0; i<nbr_group; i++){
	 		if(i%2 == 0){
				var table_tr = new Element('tr',{ className: 'cart_item cart_item_0', rel: ar_groups[i].first() });
			} else {
				var table_tr = new Element('tr',{ className: 'cart_item cart_item_1', rel: ar_groups[i].first() });
			}  									
			
			// rangée  																		
			var table_td = new Element('td', { style: 'width: 100px;' }).insert(user_selection_row_label + ' ' + $(ar_groups[i].first()).up().down().innerHTML);
		Element.insert(table_tr, table_td);
			
			// Nbr sièges
			var table_td = new Element('td', { style: 'width: 150px;' }).insert(ar_groups[i].length + ' ' + user_selection_seat_label);
		Element.insert(table_tr, table_td);
			
			// valeur
			if(if_abonnement_item == 0){
				//console.log($('price_category_' + array_price_association[ar_groups[i][0]]));
				if(typeof(array_price_association[ar_groups[i][0]]) != 'undefined' && $('price_category_' + array_price_association[ar_groups[i][0]]) != null){
					var table_td = new Element('td', {}).insert(($('price_category_' + array_price_association[ar_groups[i][0]]).innerHTML * ar_groups[i].length) + '&#8364;');
				Element.insert(table_tr, table_td);
			}
		}
			
			// delete
			var table_td = new Element('td', { valign: 'top', style: 'width: 30px;' });
			var delete_icon = new Element('img', { src: '/sf/sf_admin/images/cancel.png' });  			
			var delete_link = new Element('a', { href: 'javascript:delete_seat_per_group(' + ar_groups[i].toJSON() + ')'}).insert(delete_icon);							
			
		Element.insert(table_td, delete_link);
		Element.insert(table_tr, table_td);  		
		
		Element.insert(tbody, table_tr);
		}				
	
	// dans le cas ou la selection est vide
	if(nbr_group == 0){
			var table_tr = new Element('tr');
			var table_td = new Element('td', { style: 'width: 400px;' }).insert(empty_cart);
		Element.insert(table_tr, table_td);	
		Element.insert(tbody, table_tr);								
	}
	else {
			var table_tr = new Element('tr');
			var link_to_fct_addtocart = new Element('a', { href: 'javascript:add_to_cart();', id: 'btn_add_to_cart' }).insert(str_cart_btn_add_to_cart);
			var table_td = new Element('td', { style: 'text-align: right;height: 40px;width: 400px;', colspan: 4 }).insert(link_to_fct_addtocart);
		Element.insert(table_tr, table_td);	
		Element.insert(tbody, table_tr);
	}
	
	Element.insert(table, tbody);
	Element.update($('user_choice'), table);
	
	// affichage de la fleche montrant le groupe au passage de la souris sur l'intitulé du groupe
	$$('.cart_item').each(function(element) {
		if(element.hasAttribute('rel')){
			var element_position = $(element.getAttribute('rel')).cumulativeOffset();
			element.observe('mouseover', function(event){
				//console.log(element_position);
				$('error_seat_arrow').setStyle({
						top: (element_position.top-10) + 'px',
						left: (element_position.left-30) + 'px',
						display: 'block'
				});
				Effect.Shake('error_seat_arrow', { distance: 10 });
			});
			element.observe('mouseout', function(event){
				$('error_seat_arrow').setStyle({
						display: 'none'
				});
			});
		}
     }); 
}

//
function init_map(representation_id, room_part_id, room_part_json, hall_part_price_placement_room, if_abonnement){
	Element.hide('info_salle_unnumbered');
	//console.log(representation_id + ' - ' + room_part_id + ' - ' + room_part_json + ' - ' + hall_part_price_placement_room);									

	var hall_part_price_placement_room = hall_part_price_placement_room.evalJSON();
	var hall_part_price_placement_room_id = hall_part_price_placement_room[room_part_id];
	//console.log(hall_part_price_placement_room_id);
	if(typeof(hall_part_price_placement_room_id) != "undefined"){		
		// loading de la fenetre
		var show_map_loading = new Element('div',{ id:'edit_map_loading', style: 'text-align: center' }).insert(ajax_loader + '<br/>' + loading_map_text);
		$('edit_map').insert(show_map_loading);		
			
		var show_map_loading_error = new Element('div',{ id:'edit_map_loading_error', style: 'text-align: center' }).insert(loading_map_text_error);
		Element.insert('edit_map', show_map_loading_error);								
		
		// on charge les informations en ajax
		var url = url_order_seattosell;
		new Ajax.Request(url, {
  			method: 'get',
  			parameters: { representation_id: representation_id, room_part_id: room_part_id, hall_part_price_placement_room_id: hall_part_price_placement_room_id },
  			onSuccess: function(transport) {
  				var json = transport.responseText;
    			
  				json = json.evalJSON();
  				// annule le message d'avertissement google chrome
  				loading_map_text_error = '';
  				
    			// renvoie le tableau des sièges mis en vente

    			// on crée la salle par rapport aux tableau de salle, au placement des prix et aux places déjà réservée
    			ar_seat_to_sold = json['ar_seat_to_sold'];
				room = room_part_json.evalJSON();

				// tableau des sièges déjà réservé
				var ar_reserved_seat = json['ar_reserved_seat'];
				//console.log(ar_reserved_seat);

				// on récupere les associations de categorie
				if(json['array_association'] != ''){
					array_price_association = json['array_association'];
				}
				else {
					array_price_association = '{}'.evalJSON();
				}

				//console.log(array_price_association);

				new Ajax.Updater('prices_categories_list', url_order_ShowListCategory, {
						method: 'get',
  					parameters: { price_placement_room_id: hall_part_price_placement_room_id },
  					onComplete: function(transport) {
  						//console.log($F('array_price_categories'));
  						if($F('array_price_categories') != ''){
							var array_price_categories = $F('array_price_categories').evalJSON();
						}
						else {
							var array_price_categories = '{}'.evalJSON();
						}
						if(if_abonnement == 1){
							//alert($F('abo_date_ar_price_category'));
							var abo_date_ar_price_category = $F('abo_date_ar_price_category').evalJSON();			
							//alert('essai8');							
						} else {
							var abo_date_ar_price_category = new Array();
						}

						// on charge la valeur enregistrée dans le cookie
						var slide_cookie = new Cookies();
						if(typeof(slide_cookie.get('zoom_slider_value')) != "undefined" && slide_cookie.get('zoom_slider_value') != ''){
							$('zoom_slider_value').value = slide_cookie.get('zoom_slider_value');
						}

						// on crée la salle en js
							var table = new Element('table',{ id:'table_grid', cellpadding:0, cellspacing:0});
							var tbody = new Element('tbody');

						// rangées
							var y_total = room['params']['init_line']+1;
	 							for(y=1; y<y_total; y++){

								var table_tr = new Element('tr',{});						

								// on ajoute l'entête de la ligne pour la numerotation
								var table_td = new Element('td', {}).insert(room['room_rows']['room_row_' + y]['value']);
								Element.writeAttribute(table_td, 'style', 'width: ' + $F('zoom_slider_value') + 'px; height: ' + $F('zoom_slider_value') + 'px;');
								Element.insert(table_tr, table_td);

								// colonnes
								var x_total = room['params']['init_column']+1;
								for(x=1; x<x_total; x++){

									var table_td = new Element('td', {});

									var room_type = room['seat'][y + '|' + x]['type'];

									var element_id = y + '|' + x;
									Element.writeAttribute(table_td, 'id', element_id);
									Element.writeAttribute(table_td, 'style', 'width: ' + $F('zoom_slider_value') + 'px; height: ' + $F('zoom_slider_value') + 'px;');

									if(room_type == 'seat'){
										var img_propriety = {src:'/images/room/icons/seat_mask_30_30_public_unavailable.png', width:$F('zoom_slider_value'), height:$F('zoom_slider_value')};
										Element.update(table_td, new Element("img", img_propriety));
										Element.addClassName(table_td, 'type_seat');
										
										//set_tooltip(table_td, room['seat'][y + '|' + x]['name']);
			
										//console.log(typeof(array_price_association[y + '|' + x]));
										// si ce siège est dans le tableau des associations de catégories de prix
										if(typeof(array_price_association[y + '|' + x]) != 'undefined'){
				
											// on associe la classe correspondante
												Element.addClassName(table_td, 'price_category_' + array_price_association[y + '|' + x]);
											// et la couleur
												table_td.setStyle({
													backgroundColor: '#' + array_price_categories[array_price_association[y + '|' + x]]
												});
				
											// on vérifie si il est dans le tableau des sièges déjà vendus											
											if(typeof(ar_reserved_seat[y + '|' + x]) != 'undefined'){
												// on associe la classe et l'image												
												var img_propriety = {src:'/images/room/icons/seat_mask_30_30_public_unavailable.png', width:$F('zoom_slider_value'), height:$F('zoom_slider_value')};
												Element.update(table_td, new Element("img", img_propriety));
												Element.addClassName(table_td, 'seat_solded');			
											}
											// dans le cas d'un abonnement on ne rend selectable que la catégorie disponible
											else if(if_abonnement == 1 && !in_array(array_price_association[y + '|' + x], abo_date_ar_price_category)){
												//console.log(ar_seat_to_sold[y + '|' + x]);
												// on associe la classe et l'image												
												var img_propriety = {src:'/images/room/icons/seat_mask_30_30_public_unavailable.png', width:$F('zoom_slider_value'), height:$F('zoom_slider_value')};
												Element.update(table_td, new Element("img", img_propriety));
											}
											// on vérifie si il est dans le tableau des sièges en vente
											else if(typeof(ar_seat_to_sold[y + '|' + x]) != 'undefined'){
												// on associe la classe et l'image												
												var img_propriety = {src:'/images/room/icons/seat_mask_30_30.png', width:$F('zoom_slider_value'), height:$F('zoom_slider_value')};
												Element.update(table_td, new Element("img", img_propriety));
												Element.addClassName(table_td, 'seat_to_sold');		
						
												// et la classe selectable
												Element.addClassName(table_td, 'selectable');			
												new Insertion.Bottom(table_td, new Element("div", { className: 'type_seat_name' }).insert(room['seat'][y + '|' + x]['name']));						
											}  					
											
										}		
									}
									else if(room_type == 'corridor'){
										Element.addClassName(table_td, 'type_corridor');
									}
									else if(room_type == 'column'){
										var img_propriety = {src:'/images/room/icons/column_mask_30_30.png', width:$F('zoom_slider_value'), height:$F('zoom_slider_value')};
										Element.update(table_td, new Element("img", img_propriety));
										Element.addClassName(table_td, 'type_column');
									}
									else if(room_type == 'rumble_seat'){
										var img_propriety = {src:'/images/room/icons/seat_rumble_mask_30_30.png', width:$F('zoom_slider_value'), height:$F('zoom_slider_value')};
										Element.update(table_td, new Element("img", img_propriety));
										Element.addClassName(table_td, 'type_rumble_seat');
										//console.log(typeof(array_price_association[y + '|' + x]));
										// si ce siège est dans le tableau des associations de catégories de prix
										if(typeof(array_price_association[y + '|' + x]) != 'undefined'){
											
											// on associe la classe correspondante
												Element.addClassName(table_td, 'price_category_' + array_price_association[y + '|' + x]);
											// et la couleur
												table_td.setStyle({
													backgroundColor: '#' + array_price_categories[array_price_association[y + '|' + x]]
												});
										}
									}
									else{
										Element.addClassName(table_td, 'type_nothing');
									}
									Element.insert(table_tr, table_td);
								}
								// on ajoute l'entête de la ligne pour la numerotation
								var table_td = new Element('td', {}).insert(room['room_rows']['room_row_' + y]['value']);
								Element.writeAttribute(table_td, 'style', 'width: ' + $F('zoom_slider_value') + 'px; height: ' + $F('zoom_slider_value') + 'px;text-align: right;');
								Element.insert(table_tr, table_td);

								// 
								Element.insert(tbody, table_tr);
							}

							Element.insert(table, tbody);
							Element.update($('edit_map'), table);			

							set_slide_value($F('zoom_slider_value'), room);	

								$$('#table_grid .selectable').each(function(element) {

									if(element.hasAttribute('id')){
      									Event.observe(element, 'click', function(event) {
												// changement d'icone pour le siège
      										switch_cell_selection(element);
      										// validation du choix de l'utilisateur
												validate_user_choices(room, ar_seat_to_sold, array_price_association);
      									});
      								}
     							}); 	
					
     						var zoom_slider = $('zoom_slider');

    						new Control.Slider(zoom_slider.down('.handle'), zoom_slider, {
      							range: $R(16, 20),
      							sliderValue: $F('zoom_slider_value'),
      							onSlide: function(value) {
	     								set_slide_value(value, room);
      							},
      							onChange: function(value) { 
      								set_slide_value(value, room);
      							}
    						});			
	
							// La selection est vide								
								var table = new Element('table',{ id:'user_choice_tbl', cellpadding:0, cellspacing:0});
								var tbody = new Element('tbody');
									var table_tr = new Element('tr');
									var table_td = new Element('td', { style: 'width: 400px;' }).insert(empty_cart);
								Element.insert(table_tr, table_td);	
								Element.insert(tbody, table_tr);		
	
								Element.insert(table, tbody);
								Element.update($('user_choice'), table);
							//
							
							
  						},
  					evalScripts: false
				});	

  			},
  			onFailure: function(transport, json){
  				alert(script_error_code_loading_part + ' \n ' + script_error_code + ': X001.001');
  			}
		});								
	}		
	
}			
	
// permet de mettre la tips sur les sièges
function set_tooltip(element, name_value){
	// Intitulé du siège pour informations
	var name_input = new Element('span').insert(name_value);
		// tips
		new Tip(element, name_input, {
			className: 'default',
			hook: { tip: 'topLeft', target: 'topRight' },
			fixed: true,
			offset: { x: -2, y: -2 }
		});
		
}						
// function de selection de cellule
function switch_cell_selection(element){

	// on cache les tips présentes
	// dans certains cas, elle reste ouverte lors du clic sur siège
	$$('.prototip').each(function(element) {
		element.hide();
	});

	// si la cellule est déja sélectionné
	if(Element.hasClassName(element, 'selected')){
		// on enleve cette classe
  		Element.removeClassName(element, 'selected');
  		
  		
		var img_propriety = {src:'/images/room/icons/seat_mask_30_30.png', width:$F('zoom_slider_value'), height:$F('zoom_slider_value')};
			
    	Element.update(element, new Element("img", img_propriety));
    	new Insertion.Bottom(element, new Element("div", { className: 'type_seat_name' }).insert(room['seat'][Element.readAttribute(element, 'id')]['name']));
  	}
  	// sinon on ajoute la classe selected
  	else {
    	Element.addClassName(element, 'selected');
    	
		var img_propriety = {src:'/images/room/icons/seat_mask_30_30_public_selected.png', width:$F('zoom_slider_value'), height:$F('zoom_slider_value')};
		
    	Element.update(element, new Element("img", img_propriety));
    	
    }
}

// fonction
// permet de (de-)zoomer la grille 
function set_slide_value(value, room){
	// on arrondi la valeur à l'entier
	var value = Math.round(value);
	
	// on défini la variable qui va nous permettre d'incrémenter le nombre de rangées (également pour le placement)
	var yvv = 1;
	// on boucle les rangées
   	$$('#table_grid tr').each(function(element) {
   		
   		// on regarde si on doit la décalée ou pas
		if(typeof(room['room_rows']['room_row_' + yvv]) != "undefined" && typeof(room['room_rows']['room_row_' + yvv]['shift']) != "undefined" && room['room_rows']['room_row_' + yvv]['shift'] == 1){
			Element.writeAttribute(element, 'style', 'margin-top: ' + $F('zoom_slider_value')*yvv + 'px;margin-left: 10px; position: absolute; height: ' + $F('zoom_slider_value') + 'px;');
		}
		else {
			Element.writeAttribute(element, 'style', 'margin-top: ' + $F('zoom_slider_value')*yvv + 'px; position: absolute;');
		}
		// on incrémente 
   		yvv = yvv + 1;
   	});
   	// on boucle les cellules
   	$$('#table_grid td').each(function(element) {
   		// si la cellule contient une image, on l'a réduit également à la taille du slider
   		if(element.hasChildNodes() && element.firstChild.nodeName == 'IMG'){
   			element.firstChild.setStyle({ width: value + 'px', height: value + 'px' });
   		}
   		element.setStyle({ width: value + 'px', height: value + 'px' });
   	});
   	// on défini également la hauteur du div container
   	var height_value = ($F('zoom_slider_value')*yvv)-80;
   	String(height_value);
   	// on gere le zoom dans le cas de petites salles
   	if(room['params']['init_column'] < 10){
   		var width_this = ((room['params']['init_column']*(value+20)) - 95);
   		if(width_this < 500){ width_this = 500; }
   		$('edit_map').setStyle({
			height: (height_value + 100) + 'px',
			width: width_this + 'px'
		});
   	}
   	else {
   		var width_this = ((room['params']['init_column']*(value+5)) - 95);
   		if(width_this < 500){ width_this = 500; }
   		$('edit_map').setStyle({
			height: (height_value + 100) + 'px',
			width: width_this + 'px'
		});
   	}
   	//console.log(((room['params']['init_column']*(value+4))));
   	
   	// on change la valeur du input correspondant au slider
   	$('zoom_slider_value').value = value;
   	// on ajoute un cookie avec le niveau de zoom de l'internaute
   	var slide_cookie = new Cookies();
	slide_cookie.set('zoom_slider_value', value);
}
