// JavaScript Document

/*!
 * Metisclub JavaScript Fonction
 *
 *
 * Date: 2010 05 20
 */
 
 
 
 /****************************************************************************
 *     
 *    function: envoyerCourrier
 *
 *	  fonction récupération contenu ckeditor et formulaire envoi de mail
 ****************************************************************************/

 
 
function envoyerCourrier()
{ //alert('ddd') ;
	
	// Get the editor instance that we want to interact with.
	//var oEditor = CKEDITOR.instances.contenu;
//	var contenu = oEditor.getData();
//	var objet = document.forms['formMail'].objet.value;
//	var dest = document.forms['formMail'].dest.value;


	var contenu = document.forms['formMail'].contenu.value;
	var objet = document.forms['formMail'].objet.value;
	var dest = document.forms['formMail'].dest.value;

	
	xajax_envoyerCourrier(objet,contenu,dest );

}



		
 /****************************************************************************
 *     
 *    function: limitChars
 *
 *	  fonction limitation de caractères
 ****************************************************************************/		
		
function limitChars(textid, limit/*, infodiv*/)
		{
			var text = $('#'+textid).val();	
			var textlength = text.length;
			if(textlength > limit)
			{
				/*$('#' + infodiv).html('You cannot write more then '+limit+' characters!');*/
				$('#'+textid).val(text.substr(0,limit));
				return false;
			}
			else
			{
				/*$('#' + infodiv).html('You have '+ (limit - textlength) +' characters left.');*/
				return true;
			}
		}
		
		$(function(){
			$('#fpresentation').keyup(function(){
			/*	limitChars('fpresentation', 2000, 'charlimitinfo');*/
				limitChars('fpresentation', 2000);
			});
			

			
		});		
		

 /****************************************************************************
 *     
 *    function: check
 *
 *	  fonction tout cocher / tout decocher
 ****************************************************************************/	
var checkflag = "false";

function check(field) {
	
if (checkflag == "false") {

  for (i = 0; i < field.length; i++) {

  field[i].checked = true;}

  checkflag = "true";

  return "Tout décocher"; }

else {

  for (i = 0; i < field.length; i++) {

  field[i].checked = false; }

  checkflag = "false";

  return "Tout cocher"; }

}

 /****************************************************************************
 *     
 *    function: favorisAdd
 *
 *	  fonction traitement favoris
 ****************************************************************************/	

    function favorisAdd(ref){
		
		$('a.poplight2[href^=#]').popOpen();
	   xajax_getTraitementCoeur(ref);
		  
	  
   }
   
   
 /****************************************************************************
 *     
 *    function: favorisAdd
 *
 *	  fonction traitement favoris
 ****************************************************************************/	

    function mesFavoris(op){
		//alert(op+'mesFavoris');
	   xajax_mesFavoris(op);
		  
	  
   }
 
 
 /****************************************************************************
 *     
 *    function: flashMessage
 *
 *	  fonction traitement favoris
 ****************************************************************************/	
   
    function flashMessage(ref,formFlash){
		$('a.poplight2[href^=#]').popOpen();
		xajax_getTraitementFlash(ref,formFlash);

	  
   }
   
   
 /****************************************************************************
 *     
 *    function: Recherche Recherche
 *
 *	  
 ****************************************************************************/

 
   function recherche(mod)
	{
		
		switch(mod)
			{
				
			case 'mfavoris':
			/****************************** Recherche Mes favoris **********************************/
			  xajax_Recherche('',mod);
			break;
			
			case 'efavoris':
			/****************************** Recherche je suis dans favoris **********************************/
			  xajax_Recherche('',mod);
			break;
			
			case 'visite':
			/****************************** Recherche visite reçu **********************************/
			  xajax_Recherche('',mod);
			break;
			
			case 'autre1':
			/****************************** Autre Recherche  **********************************/
			//alert('autre');
			  xajax_Recherche(xajax.getFormValues('autre1'),mod);
			break;
			
			case 'autre2':
			/****************************** Autre Recherche  **********************************/
			//alert('autre');
			  xajax_Recherche(xajax.getFormValues('autre2'),mod);
			break;
			
			case 'autre3':
			/****************************** Autre Recherche  **********************************/
			//alert('autre');
			  xajax_Recherche(xajax.getFormValues('autre3'),mod);
			break;
			
			case 'autre4':
			/****************************** Autre Recherche  **********************************/
			//alert('autre');
			  xajax_Recherche(xajax.getFormValues('autre4'),'autre1');
			break;
			
			case 'online':
			/****************************** online Recherche  **********************************/
			  xajax_Recherche('',mod);
			break;
			
			case 'inscrit':
			/****************************** derniers inscrits Recherche  **********************************/
			  xajax_Recherche('',mod);
			break;
			
			case 'rapide':
			/****************************** Recherche Rapide **********************************/
			  xajax_Recherche(xajax.getFormValues('rapide'),mod);
			break;
			
			default:
			/****************************** Recherche Par default  **********************************/
			 xajax_Recherche('',mod);
			}
	
	}


 /****************************************************************************
 *     
 *    function: Recherche Recherche
 *
 *	  
 ****************************************************************************/

 
   function rechercheAccueil(mod)
	{
		
		switch(mod)
			{
			default:
			/****************************** Recherche Par default  **********************************/
			 xajax_rechercheAccueil(xajax.getFormValues('rapide'),mod);
			}
	
	}




 /****************************************************************************
 *     
 *    Gestion footer panel : chat +alert
 *
 *	  
 ****************************************************************************/

$(document).ready(function(){

	//Adjust panel height
	$.fn.adjustPanel = function(){ 
		$(this).find("ul, .subpanel").css({ 'height' : 'auto'}); //Reset subpanel and ul height
		
		var windowHeight = $(window).height(); //Get the height of the browser viewport
		var panelsub = $(this).find(".subpanel").height(); //Get the height of subpanel	
		var panelAdjust = windowHeight - 100; //Viewport height - 100px (Sets max height of subpanel)
		var ulAdjust =  panelAdjust - 25; //Calculate ul size after adjusting sub-panel (27px is the height of the base panel)
		
		if ( panelsub >= panelAdjust ) {	 //If subpanel is taller than max height...
			$(this).find(".subpanel").css({ 'height' : panelAdjust }); //Adjust subpanel to max height
			$(this).find("ul").css({ 'height' : ulAdjust}); //Adjust subpanel ul to new size
		}
		else if ( panelsub < panelAdjust ) { //If subpanel is smaller than max height...
			$(this).find("ul").css({ 'height' : 'auto'}); //Set subpanel ul to auto (default size)
		}
	};
	
	//Execute function on load
	$("#chatpanel").adjustPanel(); //Run the adjustPanel function on #chatpanel
	$("#alertpanel").adjustPanel(); //Run the adjustPanel function on #alertpanel
	
	//Each time the viewport is adjusted/resized, execute the function
	$(window).resize(function () { 
		$("#chatpanel").adjustPanel();
		$("#alertpanel").adjustPanel();
	});
	
	//Click event on Chat Panel + Alert Panel	
	$("#chatpanel a:first, #alertpanel a:first").click(function() { //If clicked on the first link of #chatpanel and #alertpanel...
		if($(this).next(".subpanel").is(':visible')){ //If subpanel is already active...
			$(this).next(".subpanel").hide(); //Hide active subpanel
			$("#footpanel li a").removeClass('active'); //Remove active class on the subpanel trigger
			
		}
		else { 
		//alert($(this)) ;//if subpanel is not active...
			$(".subpanel").hide(); //Hide all subpanels
			$(this).next(".subpanel").toggle(); //Toggle the subpanel to make active
			$("#footpanel li a").removeClass('active'); //Remove active class on all subpanel trigger
			$(this).toggleClass('active'); //Toggle the active class on the subpanel trigger
		}
		return false; //Prevent browser jump to link anchor
	});
	
	//Click event outside of subpanel
	/*$(document).click(function() { //Click anywhere and...
		$(".subpanel").hide(); //hide subpanel
		$("#footpanel li a").removeClass('active'); //remove active class on subpanel trigger
	});*/
	$('.subpanel ul').click(function(e) { 
		e.stopPropagation(); //Prevents the subpanel ul from closing on click
	});
	
	//Delete icons on Alert Panel
	$("#alertpanel li").hover(function() {
		$(this).find("a.delete").css({'visibility': 'visible'}); //Show delete icon on hover
	},function() {
		$(this).find("a.delete").css({'visibility': 'hidden'}); //Hide delete icon on hover out
	});





	
});

/**************************************************************************
**  fonction AlertePanel : ajoute une alerte et affiche l'alerte dans  "#alertsubpanel ul" footer.tpl
*****************************************************************************/

function AlertPanel(message){
   vardiv = "#alertpanel a:first" ;
 
/*  $("#alertpanel a:first").next(".subpanel").show(); 
  //$("#alertpanel a:first").next(".subpanel").is(':visible');
// alert($("#alertpanel a:first").next(".subpanel").is(':visible')) ;
   */
   if(message != ""){

	   if($("#alertsubpanel ul li").html() != message){
			$("#alertsubpanel ul").html(  "<li class=\"view\">"+message+"</li>" + $("#alertsubpanel ul").html() );
			
			$("#alertpanel a:first").next(".subpanel").show(); 
			$("#alertpanel").adjustPanel();
			
	   }
   }
		
}

/**************************************************************************
**  fonction FadeoutFadeIn : cache une Div et affiche une autre Div en fadein jquéry
*****************************************************************************/
 
	function FadeoutFadeIn(hide,fadeIn)
	{  //alert(hide) ;
	    $('#'+hide).hide(); //Hide all tab content
		$('#'+fadeIn).fadeIn(); //Fade in the active content
		

		return false;
	
	}
	
	
/**************************************************************
	 recharge lapage apres l'ajout d'une photo
*****************************************************************/
	function ReloadVignette(){
			// alert("dddd") ;
			xajax_reloadVignette(); 
	 	//document.location.href="/home/index.php?mod=annonce&op=monprofil";
	}





function readCookie(name) {

	var cookiename = name + "=";

	var ca = document.cookie.split(';');

	for(var i=0;i < ca.length;i++)
	{

		var c = ca[i];

		while (c.charAt(0)==' ') c = c.substring(1,c.length);

		if (c.indexOf(cookiename) == 0) return c.substring(cookiename.length,c.length);

	}

	return null;
}

 //deja_venu = readCookie("jablog");


 
var vars = [];
var hash = [];
//var userNameg = readCookie("jablog");
//var passwordg = readCookie("jabpw");

var userNameg = readCookie("jablog")+'@demov3.metisclub.fr';
var passwordg = readCookie("jabpw");
var hashes =
window.location.href.slice(window.location.href.indexOf('?') +
1).split('&');
for(var i = 0; i < hashes.length; i++)
{
hash = hashes[i].split('=');
if (hash[0] == 'username'){userNameg = hash[1];}
else if(hash[0] == 'password'){passwordg = hash[1];}
}

   function loginjab()
    {
        iJab.login(userNameg,passwordg);
    }
    function wait_for_script_load(look_for, callback) {
          var interval = setInterval(function() {
          if (eval("typeof " + look_for) != 'undefined') {
            clearInterval(interval);
            callback();
          }
        }, 3000); //better use 3 seconds
    }
    function onloadInit()
    {
        wait_for_script_load( 'iJab.login', loginjab );
    }

/**************************************************************************
**  fonction inscription 
*****************************************************************************/

function inscription(){
    xajax_inscription(xajax.getFormValues('formInscription')) ;
	
}

/**************************************************************************
**  fonction login
*****************************************************************************/

function login(){
    xajax_login(xajax.getFormValues('formLogin')) ;
	
}

/**************************************************************************
**  fonction oublie
*****************************************************************************/

function oublie(){
    xajax_alertEmailOublie(xajax.getFormValues('formEmailOublie')) ;
	
}

/**************************************************************************
**  fonction desinscription
*****************************************************************************/

function desinscription(){
    xajax_desinscription(xajax.getFormValues('formDesinscription')) ;
	
}

/**************************************************************************
**  fonction Postdescription : Ref :Id de formulaire
*****************************************************************************/
 
	function Postdescription(Ref)
	{ 
		
		$('#waiting'+Ref).show(); //aff picto waiting
		

	 	 //Hide waiting
		
 		
		switch(Ref)
		{
			/*chargement alerte next(photo)*/
			case 'AlertePresentation':
			xajax_getTraitementEssentiel(xajax.getFormValues('categorieformPresentation'));
			xajax_alerteSite('photo');
			break;
			
			/*chargement alerte next(presentation)*/
			case 'AlerteEssentiel':
			xajax_getTraitementEssentiel(xajax.getFormValues('categorieform4'));
			xajax_alerteSite('presentation');
			break;
			
			/*fin alerte inscription*/
			case 'AlerteFin':
			xajax_finAlerteSite();
			break;
			
			case 'AlerteFacebook':
			xajax_updateInscriptionFacebook(xajax.getFormValues('formInscriptionFacebook'))
			break;
			
			default:
			xajax_getTraitementEssentiel(xajax.getFormValues('categorieform'+Ref));
			$('#waiting'+Ref).hide();
			$('#succes'+Ref).fadeIn(); //aff succes;
		}
		
 		return false;
	
	}
	
	/**************************************************************************
**  fonction PostFormInscriptionMobile : Ref :Id de formulaire
*****************************************************************************/
 
	function PostFormInscriptionMobile()
	{ 
		
		$('#waitingForm').show(); //aff picto waiting

			xajax_inscriptionMobile(xajax.getFormValues('formInscriptionMobile'));;
			//$('#waitingForm').hide();
			//$('#succesForm').fadeIn(); //aff succes;
		
 		return false;
	
	}
	
/********************************************************************
**		PopPup
	
Fond noir => $('a.poplight[href^=#]').popOpen();
Fond transparent => $('a.poplight2[href^=#]').popOpen();
********************************************************************/
	
	
	$(document).ready(function(){



	$.fn.popOpen = function(op){
		
		//popID = "AlerteSite"//$(this).attr('rel'); //Get Popup Name
		popID = $(this).attr('rel'); //Get Popup Name
		popURL = $(this).attr('href'); //Get Popup href to define size
		
		//Pull Query & Variables from href URL
		query= popURL.split('?');
		dim= query[1].split('&');
		popWidth = dim[0].split('=')[1]; //Gets the first query string value
		//alert(popID) ;
		
		//Fade in the Popup and add close button
		//$('#' + popID).fadeIn().css({ 'width': Number( popWidth ) }).prepend('<a href="#" class="close"><img src="close_pop.png" class="btn_close" title="Close Window" alt="Close" /></a>');
		$('#' + popID).fadeIn().css({ 'width': Number( popWidth ) });
		
		//Define margin for center alignment (vertical + horizontal) - we add 80 to the height/width to accomodate for the padding + border width defined in the css
		var popMargTop = ($('#' + popID).height() + 80) / 2;
		//var popMargTop = 20;
		var popMargLeft = ($('#' + popID).width() + 80) / 2;
		
		
		//alert(popMargTop);
		
		//Apply Margin to Popup
		$('#' + popID).css({ 
			'margin-top' : -popMargTop,
			'margin-left' : -popMargLeft
		});
		
		//fond noir grise
		if(op == '1')
		{
		//Fade in Background
		$('body').append('<div id="fadePopup" style=" opacity : 0.8; -moz-opacity : 0.8;-ms-filter: "alpha(opacity=80)";filter : alpha(opacity=80);"></div>'); //Add the fadePopup layer to bottom of the body tag.
		$('#fadePopup').css({'filter' : 'alpha(opacity=80)'}).fadeIn(); //Fade in the fade layer 
		//Close Popups and Fade Layer
	$('a.close, #fadePopup').live('click', function() { //When clicking on the close or fade layer...
	  	$('#fadePopup , .popup_block').fadeOut(); //fade them both out
		$('#fadePopup').remove();
		return false;
	});

		}
		
		//fond noir pas de click possible
		if(op == '2')
		{
			$('body').append('<div id="fadePopup" style=" opacity : 0.8; -moz-opacity : 0.8;-ms-filter: "alpha(opacity=80)";filter : alpha(opacity=80);"></div>'); //Add the fadePopup layer to bottom of the body tag.
		$('#fadePopup').css({'filter' : 'alpha(opacity=80)'}).fadeIn(); //Fade in the fade layer 
	
		}
		
		//fond transparent
		else
		{
			$('body').append('<div id="fadePopup" style=" opacity : 0; -moz-opacity : 0;-ms-filter: "alpha(opacity=0)";filter : alpha(opacity=0);"></div>'); //Add the fadePopup layer to bottom of the body tag.
		$('#fadePopup').css({'filter' : 'alpha(opacity=0)'}).fadeIn(); //Fade in the fade layer 
		//Close Popups and Fade Layer
	$('a.close, #fadePopup').live('click', function() { //When clicking on the close or fade layer...
	  	$('#fadePopup , .popup_block').fadeOut(); //fade them both out
		$('#fadePopup').remove();
		return false;
	});

		}
			
		
		
	};

	//When you click on a link with class of poplight and the href starts with a # 
	$('a.poplight[href^=#]').click(function() {
		$(this).popOpen('1'); //Run popOpen function on click
		return false;
	});
	
	$('a.poplight2[href^=#]').click(function() {
		$(this).popOpen('2'); //Run popOpen function on click
		return false;
	});
	
//$('a.poplight[href^=#]').popOpen(); //Run popOpen function once on load
	
	//Close Popups and Fade Layer
	/*$('a.close, #fadePopup').live('click', function() { //When clicking on the close or fade layer...
	  	$('#fadePopup , .popup_block').fadeOut(); //fade them both out
		$('#fadePopup').remove();
		return false;
	});*/


	//popOpen
	
});
/**************************************************************************
** fonction permettant d'activer un element et de desactiver tous les autres
*****************************************************************************/	


// cache un element et fais apparaitre un autre
function hideFadeIn(fadeIn,classActive)
	{
		$("div").find('.'+classActive).hide();
		$(fadeIn).show(); //Fade in the active content
		$(fadeIn).addClass(classActive);
		return false;
	
	}


// efface le contenu d'une div
function flush(div)
{
	$(div).html('');
}
	
/****************************** accueil *********************************/

/*initialisation des flèches cachées bloc formulaire */

$(document).ready(function() {

	//Default Action
	$('#flecheage').hide(); //Hide all content
	$('#flechelog').hide();
	$('#flechepw').hide();
	$('#flechenais').hide();
	$('#flecheemail').hide();
	$('#flechepostal').hide();
	$('#flechesexe').addClass('activf');

});

/**************************************************************
	ajoute une classe a l'element
*****************************************************************/
function addClass(element,classElement)
{
		$(element).addClass(classElement);
}


/**************************************************************
	ovre une fenetre de chat
*****************************************************************/
function tchatAdd(reference)
{
	//alert ('--'+ reference );
	 xajax_Tchat(reference);
		//$(element).addClass(classElement);
}

/**************************************************************
	ouvre une fenetre de chat
*****************************************************************/
function slideMail(reference)
{
	//alert ('--'+ reference );
	 xajax_SlideMail(reference);
		//$(element).addClass(classElement);
}


$(document).ready(function(){
	
	//fonction permettant de couper les info trop volumineuses vignette.tpl
	
	$(".info-vignette").each(function(){
		var char = $(this).text();
  		if($(this).text().length>13)
		$(this).html($(this).text().substring(0,12));
		
	});

	//fonction permettant de couper les info trop volumineuses profil.presentation.tpl
	
	$("#presentationProfil").each(function(){
		var char = $(this).text();
  		if($(this).text().length>280)
		$(this).html($(this).text().substring(0,270)+' ... ');
		
	});
	
});

/**************************************************************************
**  fonction popUpfenetre
*****************************************************************************/

function OpenPopUp(lien){
	
	window.open(lien,'_blank','toolbar=0, location=0, directories=0, status=0, scrollbars=0, resizable=0, copyhistory=0, menuBar=0, width=680, height=610');
	
}

