// JavaScript Document


astro_infos = new Object ; // pour recevoir infos de _get_astro

astro_show(); // génére le div & elements icon  si astro.js appellé  par class Form


function astro_show() // Affiche d'icon astro, si asked dans la class  FormInscription => recup le html de base 
{

		if ( $("#astro").length ==  0 )  { return false ; } 		
		if(  $("#astro").val() == 'hide_icon' ){ return false ; }  // calcul astro mais pas d'affichage des icons , juste champs cachés
		
		var form = $("#astro").parents("form") ;
		
		var page =  '/lab/ajax/_html_stuff' ;
		info_html = new Object ;
	    info_html.ask_for =  'icon_astro' ; 
		
		$.post(  page ,
				info_html ,
				function(astro_html)  
				{ 		  
					$('#astro').after("<div id='astro_icon' class='astro'></div>"); 
					$("#astro_icon").html(astro_html);
					getAstro() ;
				} 
		, "html"
				);
}



function getAstro() //  check les valeur astro selon infos naissance => call Ajax ; // set les input hidden
{	

	// si le input astro a été affiché par la class  FormInscription
	if ( $("#astro").length ==  0 )  { return false ; } // doit pas arriver , triggé normalement par astro_show

	
	
	info_birth = new Object ;
	
	info_birth.birth_date  = $('#birth_date ').val();
	
	if( typeof($('#birth_city_id').val()) != 'undefined'  ){ info_birth.birth_city_id  = $('#birth_city_id').val();}
	if( typeof($('#birth_time').val()) != 'undefined'  ){ info_birth.birth_time  = $('#birth_time').val();}

	
	
	// si birth_date not ok, => clean tout astro 
	if(typeof(info_birth.birth_date) == 'undefined' || info_birth.birth_date == '')
	{	
	
	$("input[class='astro_tag']").val(''); // clean hidden
	$("div[id='text_sign']").text('...') ; 
	
	$.each( $('#icon_sign img') , function(key, value) // clean img
								   {
										var img = value ;
										var source = $(img).attr("src");
										pos =  source.lastIndexOf('/')+1 ;
										path =  source.substr(0,pos) ;
										new_img = path + '0.png' ;
										$(img).attr("src" , new_img);   
								   });

	return false ; 
	}
		
	// si au moins birth_date ok ,  => calcul  _get_astro
	
	
	var page =  '/lab/ajax/_get_astro' ;	 
	   $.post( page ,     
			   info_birth  ,  	  
			  function(astro_response)  
			  { 		 	
				 astro_infos = astro_response ;
				
				// set les hidden astro	
				$('#sign_id').val(astro_infos.zodiac_id) ;
				$('#chinese_sign_id').val(astro_infos.chinese_id_sign) ;
				$('#decan').val(astro_infos.decan) ;

				 if(astro_infos.ascendant_id==false) {astro_infos.ascendant_id ='' ; astro_infos.ascendant_sign =''; }
				 $('#ascendant_id').val(astro_infos.ascendant_id) ;
				 
				 
				 
				 // function trigger au changement de value astro : ex  astro_bebe_1.js
				 if(  typeof( trigger_astro_response) == 'function' )
				 {
					// alert('trigger_astro_response');
					trigger_astro_response( astro_response)
				 }
				 
				
				 astro_icon() ;
				
			  } 
			 , "json"
		  );
}





function astro_icon()
{
	 
	if ( $("#astro_icon").length == 0 )  {return false ; } // si affichage des icons n'a pas été demandé
	 
    icon_collection = $("#astro_icon div[id='sign']")   ;  // toutes les icon sign  a checker  
	
	$(icon_collection).each(function(){
	 
	 astro_type =  $(this).attr('alt'); 
	 pos =  astro_type.lastIndexOf('_') ;
	 astro_type =  astro_type.substr(0,pos) ;
	 	
	
 // cherche l'image a afficher
 
	icon_value = '';
	if(astro_type=='zodiac') 
	{ 
		icon_value =  astro_infos.zodiac_id  ;
		txt_value =   astro_infos.zodiac_sign  ;
		extension = 	   '.png' ; 	 
	}
		
	
	if(astro_type=='decan') 
	{
		icon_value =  astro_infos.decan  ;
		if(astro_infos.decan == '1') {conjug = 'er ' ;} else {conjug = 'ème ' ;} 
		txt_value =  astro_infos.decan  + conjug + ' décan' ;  
	    extension = 	   '.png' ; 
	}
	
	 if(astro_type=='ascendant') 
	 { 
		 icon_value =  astro_infos.ascendant_id   ;  
	 	 txt_value =  astro_infos.ascendant_sign  ; 
		 extension = 	   '.png' ; 
	 }
	
	if(astro_type=='chinese') 
	{ 
		icon_value =  astro_infos.chinese_id_sign   ; 
		txt_value =   astro_infos.chinese_sign  ;   
		extension = 	   '.png' ; 
	}

   //  si pas de result ajax , pas assez d'infos pour calculer un sign, ascendant etc..
   if(typeof(icon_value) =='undefined' || icon_value =='' )  
	{ 
	 icon_value = '0' ;
	 txt_value =  '...'; 
	 extension = 	   '.png' ; 
	} 
	
	
	icon_value  +=  extension  ;
	

   // si nouvelle image a afficher,  remplacement image et text, sinon  pass
  
   actual_img = $("div[id='astro_icon'] div[alt='" + astro_type + "_thumb'] #icon_sign img").attr('src') ;  
   pos =  actual_img.lastIndexOf('/')+1 ;
   path =  actual_img.substr(0,pos) ;
   actual_img = actual_img.substr(pos) ; 
   
   
   if(  icon_value != actual_img  )  
	{

		// change icon
		var img_to_change = $("div[id='astro_icon'] div[alt='" + astro_type + "_thumb'] #icon_sign img") ;
		
		
		if($.browser.msie == true) // IE bug animate opacity bug  :( 
		{
		  img_to_change.hide().attr('src', path + icon_value).show(); 
		} else {
		 // img_to_change.css("opacity",0).attr('src', path + icon_value).animate({opacity:1},1000); 
		 img_to_change.css("opacity",0).attr('src', path + icon_value).fadeTo(850,1);
		}
		
		// change text
		var zodiac_sign = 	  $('#zodiac_sign_astro').val() ;
		txt_value =  ucfirst( txt_value ) ;		
		element_txt =   $("div[id='astro_icon'] div[alt='" + astro_type + "_thumb'] div[id='text_sign']") ;	
		$(element_txt).css("opacity",0).text(txt_value).animate({ opacity: 1 },1000) ;	
	}  
	
	
 }); // end of loop $(icon_collection).each

	
}
