// JavaScript Document
// $(document).ready(function(){
	// city();
	// pcity();
// });

function city(form_name){ // loading et Affichage du module city_birth avec auto_complete
	var form_element = $('form[name='+form_name+']').length>0 ? $('form[name='+form_name+']') : $('form[id='+form_name+']');
	var city_text = $("input[type='text'][name$='city'][name!='pbirth_city']",form_element); 
	if(city_text.length == 0) return false; 
 
	// Auto Complete proposition ville
	city_text.keyup(function(event){ 
		var input_txt_city = $(this);
		var text = $(this).val();

		$('#birth_city_id ',form_element).val('');
		$("#city_complement").remove();
		input_txt_city.css("color","#7E7E7E");

		// timer on keyup , déclenche le call ajax uniquement si pause de 700 ms entre 2 frappes
		$(this).stopTime('timer_keyup'); // kill le precedent timer 		
		$(this).oneTime(500, 'timer_keyup', function() { city_suggest(text, $(this), form_element); });  
	});
	city_text.focusin(function () { $(this).val('');});

	// sur Blur , si aucune ville selectionné par le user , checke si une ville dans la liste correspond 100 % avec ville tapée
	// si oui => completeCityBirth()
	city_text.focusout(function () {
		var info_city_selected = false; // default
		if(typeof(global_city_response)!= 'undefined'){ 				 	 
			var city_asked = $(this).val().toLowerCase(); 				 	 
			$.each(global_city_response.city_result, function(key, value){
				var city_name_elem = split_city_name_propo(value.city_name_propo);
				var city_name_elem_1 = city_name_elem[0];
				var city_name_elem_2 = city_name_elem[1];
				var city_name_compare = city_name_elem_1.toLowerCase();
				if(city_name_compare == city_asked){ 	
					completeCityBirth (city_name_elem_1 , city_name_elem_2, form_element);
					info_city_selected = global_city_response.city_result[key];			 		
				} 
			}); 					 	 
		} 
		attribute_city_values(info_city_selected, form_element);
		hide_city_list();
	}); // end city_text.blur(function ..
}

function city_suggest(txt , caller, form_element) {
	delete global_city_response;
	
	if(txt.length > 1){
		var page = '/common/lab/ajax/_get_city';
		post_value = new Object;
		post_value.city_to_complete = txt;
		var city_to_complete = txt;
		$.post(page, post_value, function(city_response){ 		 		
			var pos_input_city = caller.offset();

			if($.browser!= 'light'){
				var decal_top = 20;
				var decal_left = -5;
			} else { // iphone
				var decal_top = 20;
				var decal_left = 100;
			}

			var top_pos_city_list = pos_input_city.top + decal_top;
			var left_pos_city_list = pos_input_city.left + decal_left;
			var page = '/common/lab/ajax/_html_stuff';
			info_html = new Object;
			info_html.ask_for = 'div_city_list';
			info_html.element_name = 'city_list_cont';

			if(city_response.statut == true){ // on a une liste a afficher 
				// Set la liste sur objet global accéssible depuis autre partie du script
				global_city_response = new Object;
				global_city_response = city_response;

				$.post(page, info_html, function(div_city_html){ 		 	
					if($('#'+ info_html.element_name).length == 0){
						$('body').append(div_city_html);
						$('#'+ info_html.element_name).addClass('autocomp_list_cont');
						if($.browser.msie == true){
							$('#'+ info_html.element_name).hide().css({'top':top_pos_city_list , 'left':left_pos_city_list , 'position':'absolute','z-index':'1999'}).show();
						} else {
							$('#'+ info_html.element_name).css({'top':top_pos_city_list , 'left':left_pos_city_list , 'opacity':0 , 'position':'absolute','z-index':'1999'});
							$('#'+ info_html.element_name).animate({ opacity: 1 },350);
						}
					} else {
						$('#'+ info_html.element_name + ' #list_propo').remove(); // refresh de la liste proposée
					}

					var city_list = city_response.city_result;
					var ind = 1;
					$.each(city_list, function(key, value){ 
						var city_info = city_list[key];
						var city_propo = city_info.city_name_propo;
						city_propo = boldMe(city_propo , txt); 

						// Add la Row City à la Liste
						city_propo = "<div id='list_propo' value='"+ key +"' class='tag_city_propo'> <span>" + city_propo + " </span> </div>";
						$('#'+ info_html.element_name +' #main').append(city_propo);

						// Alternate color row 1/2 
						if(is_int(ind/2))
							$("div[id='list_propo'][value='"+ key +"']").addClass('classWhiteFond'); 
						ind ++;

						// Bind le Behavior Click, passe en param l'object city_info, contient lat/long etc ..
						$("div[id='list_propo'][value='"+ key +"']").bind("click", { info:city_info, 'form_element':form_element } , clickCity);

					}); // end each
				}, "html");
			} else { // end if(city_response.statut == true)				
				$('#'+ info_html.element_name).remove(); 
			}
		}, "json");
	} // end Else
} // end propo_ville func 

// note for debug IE : clickCity pas appellé si pas matching 100% 
function clickCity(event){
	var info = event.data.info;
	var form_element = event.data.form_element;
	var name_city = info.city_name_propo;
	var city_name_elem = split_city_name_propo(name_city);
	var city_name_elem_1 = city_name_elem[0];
	var city_name_elem_2 = city_name_elem[1];
	completeCityBirth (city_name_elem_1 , city_name_elem_2, form_element);
	hide_city_list();
	var value = $(this).attr('value');
	var info_city_selected = global_city_response.city_result[value]; 
	attribute_city_values(info_city_selected, form_element);
}

function completeCityBirth (city_name_elem_1 , city_name_elem_2, form_element){
	var city_style_default = new Array();
	city_style_default['selected_city_color'] = new Array();
	city_style_default['selected_city_color']['color'] = '#6E6420';
	if(typeof(option_city_style) == 'undefined') 
		city_style = city_style_default; 
	else 
		city_style = option_city_style;

	$("#birth_city",form_element).val(city_name_elem_1);
	bcity = city_name_elem_1;
	$("#birth_city",form_element).css(city_style['selected_city_color']); 
	$('#city_complement').remove(); 
	var city_complement = "<label id='city_complement'>" + city_name_elem_2 + "</label>";

	if($.browser != 'light'){
		$("#birth_city",form_element).after(city_complement);
		$('#city_complement').css({ opacity: 0 }).addClass('city_complement').animate({ opacity: 1 },200);
	} 
}
 
function hide_city_list(){
	var cont = "#city_list_cont";
	if($.browser.msie == true) 
		$(cont).oneTime(400, function() {$(cont).remove()});  
	else 
		$(cont).animate({ opacity: 0 },400,function(){$(cont).remove();}); // fade out and destroy
}

function split_city_name_propo(city_name_propo){
	var pos_split = stripos(city_name_propo , '(');
	var city_name_elem_1 = trim(city_name_propo.substr(0, pos_split-1));
	var city_name_elem_2 = trim(city_name_propo.substr(pos_split));
	var result = new Array(city_name_elem_1 , city_name_elem_2);
	return result;
}
 
function attribute_city_values(city_object, form_element){ 	 
	if(typeof(city_object) == 'undefined' || city_object === false) {
		$('#birth_city_id ',form_element).val('');
		bcityid = '';
	} else {  
		var birth_city_id = city_object.id;
		$('#birth_city_id',form_element).val(birth_city_id);
		bcityid = birth_city_id;
		
		// set a callback on global sur script appellant => exemple : game appellant le champ birth_city
		if(typeof(attribute_city_values_callback) == 'function') 
			attribute_city_values_callback ();
	} 

	// si astro appellé par class Form, sette les values zodiac , chines , decan , ascendant sur selection de ville
	if(typeof(getAstro)=='function')
		getAstro();
}
 
function pcity(form_name){ // loading et Affichage du module pcity_birth avec auto_complete
	var form_element = $('form[name='+form_name+']').length>0 ? $('form[name='+form_name+']') : $('form[id='+form_name+']');
	var pcity_text = $("input[type='text'][name='pbirth_city']",form_element); 
	if(pcity_text.length == 0) 
		return false; 

	// Auto Complete proposition ville
	pcity_text.keyup(function(event){
		var input_txt_pcity = $(this);
		var text = $(this).val();
		$('#pbirth_city_id',form_element).val('');
		$("#pcity_complement").remove();
		input_txt_pcity.css("color","#7E7E7E");

		// timer on keyup , déclenche le call ajax uniquement si pause de 700 ms entre 2 frappes
		$(this).stopTime('timer_keyup'); // kill le precedent timer 		
		$(this).oneTime(500, 'timer_keyup', function() { pcity_suggest(text , $(this), form_element); });  
	});

	pcity_text.focusin(function () { $(this).val('');});

	// sur Blur , si aucune ville selectionné par le user , checke si une ville dans la liste correspond 100 % avec ville tapée
	// si oui => completepcityBirth()
	pcity_text.focusout(function () {

		var info_pcity_selected = false; // default
		if(typeof(global_pcity_response)!= 'undefined'){ 				 	 
			var pcity_asked = $(this).val().toLowerCase(); 				 	 
			$.each(global_pcity_response.city_result, function(key, value){
				var pcity_name_elem = split_pcity_name_propo(value.city_name_propo);
				var pcity_name_elem_1 = pcity_name_elem[0];
				var pcity_name_elem_2 = pcity_name_elem[1];
				var pcity_name_compare = pcity_name_elem_1.toLowerCase();

				if(pcity_name_compare == pcity_asked){ 	
					completepcityBirth (pcity_name_elem_1 , pcity_name_elem_2, form_element);
					info_pcity_selected = global_pcity_response.city_result[key];			 		
				} 
			}); 					 	 
		} 

		attribute_pcity_values(info_pcity_selected, form_element);
		hide_pcity_list();
	}); // end pcity_text.blur(function ..
}

function pcity_suggest(txt , caller, form_element){
	delete global_pcity_response;
	if(txt.length > 1){
		var page = '/common/lab/ajax/_get_city';
		post_value = new Object;
		post_value.city_to_complete = txt;
		var pcity_to_complete = txt;
		$.post(page, post_value, function(pcity_response){ 		 		
			var pos_input_pcity = caller.offset();

			if($.browser!= 'light'){
				var decal_top = 20;
				var decal_left = -5;
			} else { // iphone
				var decal_top = 20;
				var decal_left = 100;
			}
			var top_pos_pcity_list = pos_input_pcity.top + decal_top;
			var left_pos_pcity_list = pos_input_pcity.left + decal_left;
			
			var page = '/common/lab/ajax/_html_stuff';
			info_html = new Object;
			info_html.ask_for = 'div_city_list';
			info_html.element_name = 'city_list_cont';

			if(pcity_response.statut == true){ // on a une liste a afficher 
				// Set la liste sur objet global accéssible depuis autre partie du script
				global_pcity_response = new Object;
				global_pcity_response = pcity_response;
				$.post(page, info_html, function(div_pcity_html){ 		 	
					if($('#'+ info_html.element_name).length == 0){
						$('body').append(div_pcity_html);
						$('#'+ info_html.element_name).addClass('autocomp_list_cont');
						if($.browser.msie == true)
							$('#'+ info_html.element_name).hide().css({'top':top_pos_pcity_list , 'left':left_pos_pcity_list , 'position':'absolute','z-index':'1999'}).show();
						else {
							$('#'+ info_html.element_name).css({'top':top_pos_pcity_list , 'left':left_pos_pcity_list , 'opapcity':0 , 'position':'absolute','z-index':'1999'});
							$('#'+ info_html.element_name).animate({ opapcity: 1 },350);
						}
					} else
						$('#'+ info_html.element_name + ' #list_propo').remove(); // refresh de la liste proposée

					var pcity_list = pcity_response.city_result;
					var ind = 1;
					$.each(pcity_list, function(key, value){
						var pcity_info = pcity_list[key];
						var pcity_propo = pcity_info.city_name_propo;
						pcity_propo = boldMe(pcity_propo , txt); 

						// Add la Row pcity à la Liste
						pcity_propo = "<div id='list_propo' value='"+ key +"' class='tag_pcity_propo'> <span>" + pcity_propo + " </span> </div>";
						$('#'+ info_html.element_name +' #main').append(pcity_propo);

						// Alternate color row 1/2 
						if(is_int(ind/2))
							$("div[id='list_propo'][value='"+ key +"']").addClass('classWhiteFond');
						ind ++;

						// Bind le Behavior Click, passe en param l'object pcity_info, contient lat/long etc ..
						$("div[id='list_propo'][value='"+ key +"']").bind("click", { info:pcity_info, 'form_element':form_element } , clickpcity);
					}); // end each
				}, "html");
			} else { // end if(pcity_response.statut == true)				
				$('#'+ info_html.element_name).remove(); 
			}
		}, "json");
	} // end Else
} // end propo_ville func 

// note for debug IE : clickpcity pas appellé si pas matching 100% 
function clickpcity(event){
	var info = event.data.info;
	var form_element = event.data.form_element;
	var name_pcity = info.city_name_propo;
	var pcity_name_elem = split_pcity_name_propo(name_pcity);
	var pcity_name_elem_1 = pcity_name_elem[0];
	var pcity_name_elem_2 = pcity_name_elem[1];

	completepcityBirth (pcity_name_elem_1 , pcity_name_elem_2);
	hide_pcity_list();
	var value = $(this).attr('value');
	var info_pcity_selected = global_pcity_response.city_result[value]; 
	attribute_pcity_values(info_pcity_selected);
}

function completepcityBirth (pcity_name_elem_1 , pcity_name_elem_2, form_element){
	var pcity_style_default = new Array();
	pcity_style_default['selected_pcity_color'] = new Array();
	pcity_style_default['selected_pcity_color']['color'] = '#6E6420';

	if(typeof(option_pcity_style) == 'undefined') 
		pcity_style = pcity_style_default; 
	else 
		pcity_style = option_pcity_style;

	$("#pbirth_city",form_element).val(pcity_name_elem_1);
	$("#pbirth_city",form_element).css(pcity_style['selected_pcity_color']); 

	$('#pcity_complement').remove(); 
	var pcity_complement = "<label id='pcity_complement'>" + pcity_name_elem_2 + "</label>";

	if($.browser != 'light'){
		$("#pbirth_city",form_element).after(pcity_complement);
		$('#pcity_complement').css({ opapcity: 0 }).addClass('city_complement').animate({ opapcity: 1 },200);
	}
}

function hide_pcity_list(){
	var cont = "#city_list_cont";
	if($.browser.msie == true)
		$(cont).oneTime(400, function() {$(cont).remove()});  
	else 
		$(cont).animate({ opapcity: 0 },400,function(){$(cont).remove();}); // fade out and destroy
}

function split_pcity_name_propo(pcity_name_propo){
	var pos_split = stripos(pcity_name_propo , '(');
	var pcity_name_elem_1 = trim(pcity_name_propo.substr(0, pos_split-1));
	var pcity_name_elem_2 = trim(pcity_name_propo.substr(pos_split));
	var result = new Array(pcity_name_elem_1 , pcity_name_elem_2);
	return result;
}
 
function attribute_pcity_values(pcity_object, form_element){ 	 
	if(typeof(pcity_object) == 'undefined' || pcity_object === false)
		$('#pbirth_city_id ',form_element).val('');
	else {  
		var pbirth_city_id = pcity_object.id;
		$('#pbirth_city_id',form_element).val(pbirth_city_id);

		// set a callback on global sur script appellant => exemple : game appellant le champ birth_pcity
		if(typeof(attribute_pcity_values_callback) == 'function') 
			attribute_pcity_values_callback ();
	} 
	// si astro appellé par class Form, sette les values zodiac , chines , decan , ascendant sur selection de ville
	if(typeof(getAstro)=='function') 
		getAstro();
}

