/*
 * Functions for search module behaviour.
 * 
 */
	
	
	function displayWaiting() {
	    jQuery("#loadingimagediv").show();
		jQuery("#button_search").hide();
	}

	

					
	function displayFieldForGeolocalize()  {
		var ticked = document.getElementById("localize_tick_box").checked
		if ( ticked ) {
		    jQuery("#address_tb").show();		
			jQuery("#distance_cb").show();	
		}
		else {
			jQuery("#address_tb").hide();	
			jQuery("#distance_cb").hide();	
		}                          
	}
	
	function displaymoreparticipants() {
		var ticked = document.getElementById("moreparticipants_tick_box").checked
		if ( ticked ) {
		    jQuery("#number_of_participants").show();		
			jQuery("#persons_select").attr("disabled",true);	
			jQuery("#persons_select").css("background-color","#d0ccc6");
		}
		else {
			jQuery("#number_of_participants").hide();	
			jQuery("#persons_select").removeAttr("disabled");	
			jQuery("#persons_select").css("background-color","#ffffff");
		}  
	}
	
	
	function check_value_in_panel(value,panel) {

		var input = jQuery("#tabs-by_"+panel+" :input[value='"+value+"']")	
		var ticked = input.attr('checked');
		
		if (ticked)
			input.removeAttr('checked');			
		else
			input.attr('checked', true);
			
		update_criterias_rail(panel);
	}
	
	function uncheck_all (panel) {
		if (!jQuery("#reinit_criterias_"+panel).attr('checked')) {
			jQuery("#tabs-by_"+panel+" :input").removeAttr('checked');
			jQuery("#reinit_criterias_"+panel).attr('checked', true);
			update_criterias_rail(panel);
		}
	}
	
	function reload_choices_after_search (){
		
		if (jQuery("#search_choice_order").val() != "") {
			choosen_search_criterias_rail = jQuery("#search_choice_order").val().split(",");
			global_list = ""
			for (j=0;j<choosen_search_criterias_rail.length;j++) {
				list_selected = get_checked_values_in_panel(choosen_search_criterias_rail[j]); 
				global_list = global_list + list_selected
				if (choosen_search_criterias_rail[j] == "area") {
					// in case this is area panel we verify if there is something to hide
					all_used_categories = jQuery("#area_categories_list").val().split(",")
					for (k=0;k<all_used_categories.length;k++){
						if (list_selected.indexOf(all_used_categories[k]) != -1){
							//Category is checked =>  hide all related areas
							areas_with_category = jQuery("td[category='"+all_used_categories[k]+"'] > *")			
							for (l = 0; l < areas_with_category.length; l++) {
								areas_with_category[l].hide();
							}
						}
					}
					
				}
			}

			if (choosen_search_criterias_rail.length != 0) {
				// we display the divttm
				html_to_display = display_choosen_criterias_rail(choosen_search_criterias_rail)
				if (html_to_display != "") {
					jQuery("#choosen_criterias").html(display_choosen_criterias_rail(choosen_search_criterias_rail));
					jQuery("#choosen_criterias").show();
				}
				else {
					jQuery("#choosen_criterias").hide();
				}	
			}
			else {
				// hide it
				jQuery("#choosen_criterias").hide();
			}
			
			
		}
	}
	
	function setup_calendar(min_date,max_date) {
		
		jQuery("#booking_criterias_date").datepicker(
		    jQuery.extend({}, 
			jQuery.datepicker.regional["fr"], 
			{ 
			    firstDay: 1,
				showStatus: true, 
			    showOn: "both", 
				dateFormat: "dd/mm/yy",
				minDate: min_date, 
				maxDate: max_date,
			    buttonImage: "/images/calendar.png", 
			    buttonImageOnly: true 
			}));	
	}

	function array_index (array,val) {
	  for(var i = 0; i < array.length; i++) {
	    if(array[i] == val) return i;
	  }
	  return null;
	}

	function display_choosen_criterias_rail(steps) {
    
	    rail_to_display = "";
		for (i=0;i<steps.length;i++){
	    	criteria = steps[i];
			if (eval("choosen_" + criteria) != "") {
				if (i != 0) 
					rail_to_display = rail_to_display + " <span class='criteria_separator'>|</span> "
				rail_to_display = rail_to_display + eval("choosen_" + criteria);
			}
		}
		return rail_to_display;
  	}

	function switch_area_category (elt) {

		// if selected area is an area category
		if (elt.attr("name") == "area_category_ids[]") {			
			if (elt.attr('checked')) {
				//Category is checked =>  hide all related areas
				areas_with_category = jQuery("td[category='"+elt.attr('id')+"'] > *")	
						
				for (i = 0; i < areas_with_category.length; i++) {					
					if (areas_with_category[i].id != null && areas_with_category[i].id != "")
						jQuery("#"+areas_with_category[i].id).hide();
				}
			}
			else {
				//Category is checked =>  hide all related areas
				areas_with_category = jQuery("td[category='"+elt.attr('id')+"'] > *")				
				for (i = 0; i < areas_with_category.length; i++) {
					if (areas_with_category[i].id != null && areas_with_category[i].id != "")
						jQuery("#"+areas_with_category[i].id).show();					
				}
			}
		}

	}
	
	// uncheck all checkboxed in the specified panel0
	function uncheck_removed_criterias (panel) {
		jQuery("#tabs-by_"+panel+" :input[checked='true']").removeAttr("checked");
		eval("choosen_"+panel+"=''")
		eval("checked_"+panel+"=''")
	}

	function get_checked_values_in_panel(panel) {
		// retrieve checked values and set them in the choosen and checked variable 
		list_selected = ""
		list_checked = ""

		checkedvalues = jQuery("#tabs-by_"+panel+" :input[checked='true'][class!='reinit_criteria_chbx']")
		for (i=0;i<checkedvalues.length;i++) {
			list_selected = list_selected + checkedvalues.get(i).title;
			list_checked = list_checked + checkedvalues.get(i).value;
			if (i < checkedvalues.length-1) {
				list_selected = list_selected +", ";
				list_checked = list_checked +", ";
			}			
		}
		
		if (list_selected!="")
			// at least one selected criteria
			jQuery("#reinit_criterias_"+panel).removeAttr('checked');
		else
			// no criterias selected anymore
			jQuery("#reinit_criterias_"+panel).attr('checked', true);
		
		// update global variables
		eval('choosen_'+panel+'="'+list_selected+'"')
		eval('checked_'+panel+'="'+list_checked+'"')
		
		return list_selected
	}

	function update_criterias_rail(tabmodified) {
	
		
		// retrieve checked values and set them in the choosen and checked variable 
		list_selected = get_checked_values_in_panel(tabmodified)
		

				
		// if last element is "more_xxx", we remove it
		if (choosen_search_criterias_rail.length >=1 && choosen_search_criterias_rail[choosen_search_criterias_rail.length-1].indexOf("more") != -1) {
			choosen_search_criterias_rail.pop();
		}
		
		index = array_index(choosen_search_criterias_rail,tabmodified);
				
		if (index == null){			
			// need to be added at last position
			choosen_search_criterias_rail.push(tabmodified);			
		}
		else {
			// already choosen criteria. We remove all criterias after
			elt_to_remove = new Array();
			// calculate the size now because of pop which removes elements
			size = choosen_search_criterias_rail.length
			for (i=index+1;i<size;i++) {
				elt_to_remove.push(choosen_search_criterias_rail.pop())
				// uncheck all previously selected values for "after" criterias			
				uncheck_removed_criterias(elt_to_remove[elt_to_remove.length-1])			
			}
		
			// in case nothing is selected anymore in this tab. we remove it from the list
			if (list_selected=="") {				
				elt_to_remove.push(choosen_search_criterias_rail.pop())
			}
			
			if (index+1 < size) {
				// this was not the last element of choices, we add a "more_xxx"
				if (elt_to_remove.length >= 2) {
					// there were more than 2 choices after this tab. We display "more criterias ?"				
					choosen_search_criterias_rail.push("more_criterias")
				}
				else {
					// only one criteria after. We display "criteria ?"
					choosen_search_criterias_rail.push("more_"+elt_to_remove.pop());
				}
			} 	
		}

		
		// reinitialise the panel that have been refreshed (all of them must be recomputed)
		allready_refreshed_panels = new Array();
		
		if (choosen_search_criterias_rail.length != 0) {
			// we display the div
			jQuery("#choosen_criterias").html(display_choosen_criterias_rail(choosen_search_criterias_rail));
			jQuery("#search_choice_order").val(choosen_search_criterias_rail.join(","));
			jQuery("#choosen_criterias").show();
		}
		else {
			// hide it
			jQuery("#choosen_criterias").hide();
		}
			
		
		
	}

	

	function update_criterias(panel_id) {
			// retrieve id of panel without "tabs-by_"
			current_short_name = panel_id.substr(8,panel_id.length-1);
			// get index in the choice order
			index_currentselection = array_index(choosen_search_criterias_rail,current_short_name)
				
			// refresh is needed only if selected tab is new (null) or if has been selected after the previous tab
			// and if it has not already been refreshed
			can_be_refreshed = (index_currentselection == null)
			has_been_refreshed = jQuery.inArray(panel_id,allready_refreshed_panels)

									 
			if (can_be_refreshed && has_been_refreshed == -1) {

				// add the panel has "refreshed"
				allready_refreshed_panels.push(panel_id)
				
				// display loading image
				jQuery("#"+panel_id).html("<div id='refresh_criterias'><img id='loadingimage' src='/images/index/refresh_criterias.gif'/></div>");
			
				new Ajax.Updater(panel_id, "/website/search/get_remaining_search_criterias?tab=" + panel_id +
				"&checked_cooking=" + checked_cooking +
				"&checked_atmo=" + checked_atmo +
				"&checked_area=" + checked_area +
				"&checked_areacategories=" + checked_areacategories +
				"&checked_price=" + checked_price, 
				{
					asynchronous: true,
					evalScripts: true
				});
				
			}
			
	}
	
	function append_element_to_array(array,html,visibility) {
		var object = {
			   html: html,
			   visible: visibility
			 };
		array.push(object);
	}
	
	function get_first_available(array) {
		for (var i = 0, it_obj; it_obj = array[i]; i++) {
		   if (it_obj.visible == false) {
		   		it_obj.visible = true;
				return it_obj.html;
		   }		   	 
		}
		return "";
	}
	
	function remove_from_hmi(array,id) {
		it_obj = array[id];
		it_obj.visible = false;
		jQuery('#new_part_waiting_'+id).remove();
	}
	