$(document).ready(function () {
	/*$('div.contenu_zone_onglets').hide();
	*/
	/*Contains (avec un C majuscule) utilisé dans l'autocomplete d'envoi des invitations*/
	jQuery.extend(jQuery.expr[':'], { 
    Contains : "jQuery(a).text().toUpperCase().indexOf(m[3].toUpperCase())>=0" 
  }); 
	$('.gros_bouton_acceuil').mouseover(function(e){
		$(this).parent('.div_gros_bouton_accueil').css('background-position','0px -70px');
	});
	$('.gros_bouton_acceuil').mouseout(function(e){
		$(this).parent('.div_gros_bouton_accueil').css('background-position','0px 0px');
	});
	$('#infobulle_barre').hide();
	$('.infobulle__barre_aide').mouseout(function(e){
		$('#barre_texte').hide();
	});
	$('.infobulle__barre_aide').mouseover(function(e){
		$('#barre_texte').html($(this).children('.cache').html());
		$('#barre_texte').show();
	});
	$('#infobulle').hide();
	$('.infobulle_aide').mousemove(function(e){
		$('#infobulle').css('left',(e.pageX + 10)+'px');
		$('#infobulle').css('top',(e.pageY - 5)+'px');
	});
	$('.infobulle_aide').mouseout(function(e){
		$('#infobulle').hide();
	});
	$('.infobulle_aide').mouseover(function(e){
		$('#infobulle').html($(this).children('.cache').html()).show();
	});
	$('#carnet_adresse').hide();
  var config_hover_invitation = {    
     sensitivity: 3, // number = sensitivity threshold (must be 1 or higher)    
     interval: 100, // number = milliseconds for onMouseOver polling interval    
     over: function(e){ // function = onMouseOver callback (REQUIRED) 
      var temp = $(this).attr('id').replace('invit_reseau_','');
      $(this).children('.invit_reseau_interieur').children('.invit_actions').slideDown(100);
     },    
     timeout: 0, // number = milliseconds delay before onMouseOut    
     out: function(e){// function = onMouseOut callback (REQUIRED) 
      var temp = $(this).attr('id').replace('invit_reseau_','');
      $(this).children('.invit_reseau_interieur').children('.invit_actions').hide();
     } 
  };
  $('.invit_reseau').hoverIntent(config_hover_invitation);
  $('.invit_reseau').mouseover(function(){
      var temp = $(this).attr('id').replace('invit_reseau_','');
      $(this).addClass('invit_reseau_survol');
      $(this).children('.invit_reseau_interieur').addClass('invit_reseau_interieur_survol');
    });
    $('.invit_reseau').mouseout(function(){
      $(this).removeClass('invit_reseau_survol');
      $(this).children('.invit_reseau_interieur').removeClass('invit_reseau_interieur_survol');
    });
	$('.ajout_in').click(function(e){
		e.preventDefault;
		var temp = $(this).attr('id').replace('ajout_','');
		$.ajax({
			type: 'POST',
			url: 'include/appel_ajax.php',
			data: 'objet=ajout_in&fi_id=' + temp + '&m=' + $('#champ_ajout_' + temp).val(),
			dataType: 'html',
			success: function(message) {
				$('#ligne_ajout_'+temp).before(message);
				$('#champ_ajout_' + temp).val('');
			}
		}); 
	});
	$('#nouv_mail').keyup(function(e){
		var temp = $('#nouv_mail').val();
		$('#nouv_mail').width(temp.length*8 + 15 + 'px');
	});
	$('#deroule_menu, .deroule_menu').click(function(e){
		e.preventDefault();
		$('#postit').animate({opacity: 'toggle'});
	});	
	$('#menu_options_affichage a').click(function(){
		$('#menu_options_affichage a').removeClass('option_menu_selected');
		$(this).addClass('option_menu_selected');
	});
	$('#invitations_options_affichage a').click(function(){
		$('#invitations_options_affichage a').removeClass('option_menu_selected');
		$(this).addClass('option_menu_selected');
	});
	
	$('.simpleModal_message').click(function (e) {
		e.preventDefault();
		$('#chargement_page').slideDown("fast");
		var valeur_param = 0;
		if(typeof($(this).attr('id')) !='undefined'){
      valeur_param = $(this).attr('id');
		}
		$.get("popup_message.php",
      {param: valeur_param},
      function(data){
			$(data).modal({
				close: true,
				overlayId: 'contactModalOverlay',
				containerId: 'contactModalContainer',
				onOpen: msg.open,
				onShow: msg.show,
				onClose: msg.close
			});
      $('#chargement_page').slideUp("fast");
		});
	});
	$('.simpleModal_connect').click(function (e) {
		e.preventDefault();
		$.get("identification.php", function(data){
			$(data).modal({
				close: true,
				overlayId: 'contactModalOverlay',
				containerId: 'contactModalContainer',
				onOpen: identification.open,
				onShow: identification.show,
				onClose: identification.close
			});
		});
	});
	$('.simpleModal_inscrire').click(function (e) {
		e.preventDefault();
		$.get("inscription.php", function(data){
			$(data).modal({
				close: true,
				overlayId: 'contactModalOverlay',
				containerId: 'contactModalContainer',
				onOpen: inscription.open,
				onShow: inscription.show,
				onClose: inscription.close
			});
		});
	});
	$('.simpleModal_activation').click(function (e) {
		e.preventDefault();
		$.get("activation.php", function(data){
			$(data).modal({
				close: true,
				overlayId: 'contactModalOverlay',
				containerId: 'contactModalContainer',
				onOpen: activation.open,
				onShow: activation.show,
				onClose: activation.close
			});
		});
	});
	$('.simpleModal_feedback').click(function (e) {
		e.preventDefault();
		$.get("feedback-invitation.php", function(data){
			$(data).modal({
				close: true,
				overlayId: 'contactModalOverlay',
				containerId: 'contactModalContainer',
				onOpen: feedback.open,
				onShow: feedback.show,
				onClose: feedback.close
			});
		});
	});
    $('.envoi_changer_mdp').click(function(e){
		var temp = $("#form_changer_mdp").serialize();
		e.preventDefault();
		
		if($('#form_changer_mdp').valid()){
			$.ajax({
				type: 'POST',
				url: 'include/appel_ajax.php',
				data: temp + '&objet=change_mdp',
				dataType : 'html',
				complete : function(xhr) {
					$('#td_changer_mdp').html(xhr.responseText);
				}
			}); 
		}
    });
    $('#chargement_page').hide();
});

var inscription = {
	message: null,
	open: function (dialog) {
		dialog.overlay.fadeIn(200, function () {
			dialog.container.fadeIn(200, function () {
				dialog.data.slideDown(200, function () {
					//$('#contactModalContainer #nom').focus();
				});
				// fix png's for IE 6
				if ($.browser.msie && $.browser.version < 7) {
					$('#contactModalContainer .send, #contactModalContainer .cancel').each(function () {
						if ($(this).css('backgroundImage').match(/^url[("']+(.*\.png)[)"']+$/i)) {
							var src = RegExp.$1;
							$(this).css({
								backgroundImage: 'none',
								filter: 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="' +  src + '", sizingMethod="scale")'
							});
						}
					});
				}
			});
		});
	},
	show: function (dialog) {
		$('#contactModalContainer .send').click(function (e) {
			e.preventDefault();
			// validate form
			
			if($('#formulaire_inscription').valid()){
				$('#contactModalContainer .message').hide();
				$('.loading').show();
				$.ajax({
					type: 'post',
					url: 'inscription.php',
					data: $('#contactModalContainer form').serialize() + '&action=envoi',
					dataType: 'html',
					complete: function (xhr) {
						$('.loading').hide();
						$('#contactModalContainer .message').hide().html(xhr.responseText).fadeIn(200);
					},
					error: inscription.error
				});
			}
		});
	},
	close: function (dialog) {
		dialog.data.fadeOut(200, function () {
			dialog.container.fadeOut(200, function () {
				dialog.overlay.fadeOut(200, function () {
					$.modal.close();
				});
			});
		});
	},
	error: function (xhr) {
		alert(xhr.statusText);
	}
};


var feedback = {
	message: null,
	open: function (dialog) {
		dialog.overlay.fadeIn(200, function () {
			dialog.container.fadeIn(200, function () {
				dialog.data.slideDown(200, function () {
					//$('#contactModalContainer #code').focus();
				});
				// fix png's for IE 6
				if ($.browser.msie && $.browser.version < 7) {
					$('#contactModalContainer .send, #contactModalContainer .cancel').each(function () {
						if ($(this).css('backgroundImage').match(/^url[("']+(.*\.png)[)"']+$/i)) {
							var src = RegExp.$1;
							$(this).css({
								backgroundImage: 'none',
								filter: 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="' +  src + '", sizingMethod="scale")'
							});
						}
					});
				}
			});
		});
	},
	show: function (dialog) {
		$('#contactModalContainer .send').click(function (e) {
			e.preventDefault();
			// validate form
			$('#contactModalContainer #feedback_formulaire').hide();
			$('.loading').show();
			$.ajax({
				type: 'post',
				url: 'feedback-invitation.php',
				data: $('#contactModalContainer form').serialize() + '&action=envoi',
				dataType: 'html',
				complete: function (xhr) {
					$('.loading').hide();
					$('#contactModalContainer .message').hide().html(xhr.responseText).fadeIn(200);
				},
				error: activation.error
			});
		});
	},
	close: function (dialog) {
		//dialog.data.fadeOut(200, function () {
			dialog.container.fadeOut(200, function () {
				dialog.overlay.fadeOut(200, function () {
					$.modal.close();
				});
			});
		//});
	},
	error: function (xhr) {
		alert(xhr.statusText);
	}
};

var msg = {
	message: null,
	open: function (dialog) {
		dialog.overlay.fadeIn(200, function () {
			dialog.container.fadeIn(200, function () {
				dialog.data.slideDown(200, function () {
					//$('#contactModalContainer #code').focus();
				});
				// fix png's for IE 6
				if ($.browser.msie && $.browser.version < 7) {
					$('#contactModalContainer .send, #contactModalContainer .cancel').each(function () {
						if ($(this).css('backgroundImage').match(/^url[("']+(.*\.png)[)"']+$/i)) {
							var src = RegExp.$1;
							$(this).css({
								backgroundImage: 'none',
								filter: 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="' +  src + '", sizingMethod="scale")'
							});
						}
					});
				}
			});
		});
	},
	show: function (dialog) {
    $('html,body').animate({ scrollTop: 0 }, 500);
    $('textarea#texte').autoResize({
      extraSpace : 35
    });
		$('#aff_liste').click(function(e){
			$('#liste_contacts').show();
			$('.recherchelistecontact').focus();
		});
		$('input#titre,textarea').focus(function(e){
			$('#liste_contacts').hide();
		});
		$('input.recherchelistecontact').keyup(function(e){
      e.preventDefault();
      $('#liste_contacts .ajout_dest').hide();
      var temp = $('.recherchelistecontact').val();
      var reg=new RegExp(temp,"i");
      $('#liste_contacts .ajout_dest').each(function(i){
        var id = $(this).attr('id').replace('dest_','');
        var reg2=new RegExp(id);
        var temp_valeur = $(this).text();
        if(temp_valeur.match(reg) && !($('input#pour').val().match(reg2))){
           $(this).show();
         }
      });
      //$('#liste_contacts .ajout_dest:contains('+temp+')').show();
      return false;
		});
		$('#liste_contacts a.ajout_dest').click(function(e){
			e.preventDefault();
			$('.recherchelistecontact').val('');
			var id = $(this).attr('id').replace('dest_','');
			var reg=new RegExp(id,"i");
      if(!$('input#pour').val().match(reg)){
        var nom = $(this).html();
        $('#pour_noms').append('<span class=\'adresse_mail\'>' + nom + '<a href="javascript:void(0);" id="dest_sel_'+id+'" onclick="$(\'input#pour\').val($(\'input#pour\').val().replace(\'::'+id+'\',\'\'));$(this).parent().remove();$(\'#dest_'+id+'\').show();">&nbsp;X&nbsp;</a></span>');
        $('input#pour').val($('input#pour').val() + '::' + id);
      }
      $('#liste_contacts .ajout_dest').hide();
      $('#liste_contacts .ajout_dest').each(function(i){
        var id = $(this).attr('id').replace('dest_','');
        var reg2=new RegExp(id);
        if(!($('input#pour').val().match(reg2))){
           $(this).show();
         }
      });
		});
		$('#contactModalContainer .send').click(function (e) {
			e.preventDefault();
			// validate form
			if($('#form_message').valid()){
        $('#contactModalContainer #form_message').hide();
        $('.loading').show();
        $.ajax({
          type: 'post',
          url: './include/inc_message_ajax.php',
          data: $('#contactModalContainer #form_message').serialize() + '&objet=envoi_message',
          dataType: 'html',
          success: function (reponse) {
            $('.loading').hide();
            if($.trim(reponse) == 1){
              $.modal.close();
              lancer_timer();
            }else {
              $('#contactModalContainer .message').hide().html(reponse).fadeIn(200);
            }
          },
          error: msg.error
        });
			}
		});

	},
	close: function (dialog) {
		//dialog.data.fadeOut(200, function () {
			dialog.container.fadeOut(200, function () {
				dialog.overlay.fadeOut(200, function () {
					$.modal.close();
				});
			});
		//});
	},
	error: function (xhr) {
		alert(xhr.statusText);
	}
};
var activation = {
	message: null,
	open: function (dialog) {
		dialog.overlay.fadeIn(200, function () {
			dialog.container.fadeIn(200, function () {
				dialog.data.slideDown(200, function () {
					//$('#contactModalContainer #code').focus();
				});
				// fix png's for IE 6
				if ($.browser.msie && $.browser.version < 7) {
					$('#contactModalContainer .send, #contactModalContainer .cancel').each(function () {
						if ($(this).css('backgroundImage').match(/^url[("']+(.*\.png)[)"']+$/i)) {
							var src = RegExp.$1;
							$(this).css({
								backgroundImage: 'none',
								filter: 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="' +  src + '", sizingMethod="scale")'
							});
						}
					});
				}
			});
		});
	},
	show: function (dialog) {
		$('#contactModalContainer .send').click(function (e) {
			e.preventDefault();
			// validate form
			if($('#activation').valid()){
				$('#contactModalContainer .message').hide();
				$('.loading').show();
				$.ajax({
					type: 'post',
					url: 'activation.php',
					data: $('#contactModalContainer form').serialize() + '&action=envoi',
					dataType: 'html',
					complete: function (xhr) {
						$('.loading').hide();
						$('#contactModalContainer .message').hide().html(xhr.responseText).fadeIn(200);
					},
					error: activation.error
				});
			}
		});
	},
	close: function (dialog) {
		//dialog.data.fadeOut(200, function () {
			dialog.container.fadeOut(200, function () {
				dialog.overlay.fadeOut(200, function () {
					$.modal.close();
				});
			});
		//});
	},
	error: function (xhr) {
		alert(xhr.statusText);
	}
};


var identification = {
	message: null,
	open: function (dialog) {
		dialog.overlay.fadeIn(200, function () {
			dialog.container.fadeIn(200, function () {
				dialog.data.slideDown(200, function () {
					$('#contactModalContainer #login').focus();
				});
				// fix png's for IE 6
				if ($.browser.msie && $.browser.version < 7) {
					$('#contactModalContainer .send, #contactModalContainer .cancel').each(function () {
						if ($(this).css('backgroundImage').match(/^url[("']+(.*\.png)[)"']+$/i)) {
							var src = RegExp.$1;
							$(this).css({
								backgroundImage: 'none',
								filter: 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="' +  src + '", sizingMethod="scale")'
							});
						}
					});
				}
			});
		});
	},
	show: function (dialog) {
		$('form#connection').submit(function (e) {
			e.preventDefault();
			// validate form
			if($('#connection').valid()){
				$('#contactModalContainer .message').hide();
				$('.loading').show();
				$.ajax({
					type: 'post',
					url: 'identification_ajax.php',
					data: $('#contactModalContainer form').serialize() + '&action=envoi',
					dataType: 'html',
					success: function (message) {
						$('#contactModalContainer .zone_message').html(message).fadeIn(200);
					},
					error: identification.error
				});
			}
			
		});
	},
	close: function (dialog) {
		//dialog.data.fadeOut(200, function () {
			dialog.container.fadeOut(200, function () {
				dialog.overlay.fadeOut(200, function () {
					$.modal.close();
				});
			});
		//});
	},
	error: function (xhr,chaine,objet) {
		alert(xhr.statusText + ' -- ' + chaine);
	}
};

var classique = {
	message: null,
	open: function (dialog) {
		dialog.overlay.fadeIn(200, function () {
			dialog.container.fadeIn(200, function () {
				dialog.data.slideDown(200, function () {
				});
				// fix png's for IE 6
				if ($.browser.msie && $.browser.version < 7) {
					$('#contactModalContainer .send, #contactModalContainer .cancel').each(function () {
						if ($(this).css('backgroundImage').match(/^url[("']+(.*\.png)[)"']+$/i)) {
							var src = RegExp.$1;
							$(this).css({
								backgroundImage: 'none',
								filter: 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="' +  src + '", sizingMethod="scale")'
							});
						}
					});
				}
			});
		});
	},
	show: function (dialog) {
        $('textarea#fi_id_texte_invitation').autoResize({
          extraSpace : 15
        });
	},
	close: function (dialog) {
    dialog.container.fadeOut(200, function () {
      dialog.overlay.fadeOut(200, function () {
        $.modal.close();
      });
    });
	},
	error: function (xhr,chaine,objet) {
		alert(xhr.statusText + ' -- ' + chaine);
	}
};