// JavaScript Document
function myJqueryInit() {
  //Hauteur container colonne
	$j('.wuc_col').each(function() {  
    $j(this).equalHeight();
  });	
  //Hauteur comp ergo
  $j(this).ergoHeight();
  //Hauteur div fond 2 du body
  $j(this).bodyFond2Height();
}  
//Reinitialisation des hauteurs 
function wuCleanUpHeight() {  
	$j('.wuc_col,.wu_object,[id=wuRub],[id^=wuContent],[id^=wuColExt],[id^=wuColInt],[id^=wuHeader],[id^=wuFooter]').each(function() {
	  $j(this).css('min-height', '');
		$j(this).height('auto');
	});
}	

$j(document).ready(function(){  
  //Impression
  if($j("body").attr("id") == 'wuPrint') { 
    var wuContentPrint = $j("#divContentPrint").attr("class");
    var wuFlow = $j('#'+wuContentPrint, window.opener.document).html(); 
    $j("#divContentPrint").html(wuFlow);
    window.print();
  }   
  //Affichage masque + popup type ajax
  $j("*[id^=wuOpenPopup]").click(function () {
    var Tid = $j(this).attr("id").split("__");
    //var
    var wuID = Tid[2];
    var wuWidth = Tid[3];
    var wuHeight = Tid[4];
    var wuMasqueClass = Tid[5];
    var wuMasqueOpacity = Tid[6]; 
    //Tableau post pour ajax
    var wuParams = new Array();
    wuParams['phpFile'] = 'plugins/wuPlugin_ajaxPopup.php';
    wuParams['location'] = '1';
    wuParams['wuPopupID'] = wuID;
    //appel ajax
    wuContentAjax("#wuPopup_"+wuID, wuParams);
    //ouverture popup + masque
    wuOpenPopup("#wuPopup_"+wuID, wuWidth, wuHeight);
    wuOpenMasque("#masque", wuMasqueClass, wuMasqueOpacity);    
    return false;
  });  
  //Fermeture masque + popup list components
  $j("*[id^=wuClosePopup]").live("click", function(){
    var Tid = $j(this).attr("id").split("__");
    var wuID = Tid[2];
    $j("#wuPopup_"+wuID).fadeOut("fast"); 
    $j("#masque").fadeOut("slow"); 
  });
  //Validation formulaire popup type ajax + affichage
  $j("*[id^=wuSubmitFormAjax]").live("click", function(){
    var Tid = $j(this).attr("id").split("__");
    var wuID = Tid[2];
    var wuForm = Tid[3];
    var wuSubmitFct = Tid[4];
    var wuVerifFct = Tid[5];
    if(wuVerifFct != '') return eval(wuVerifFct)(wuSubmitFct, wuForm, wuID);
    else { eval(wuSubmitFct)(wuForm, wuID); }
  }); 	
});

//Ouverture popup
function wuOpenPopup(wuEl, wuWidth, wuHeight) {
  $j(wuEl).width(wuWidth+'px');
  $j(wuEl).height(wuHeight+'px');
  wuPosPopup(wuEl, wuWidth, wuHeight);
  $j(wuEl).fadeIn("fast");   
}
//Positionnement popup
function wuPosPopup(wuEl, wuWidth, wuHeight) {
  $j(wuEl).css("left", ($j(window).width() - $j(wuEl).width()) / 2);  
  if($j(document).height() > $j(wuEl).height()) {
    $j(wuEl).css("top", (($j(window).height() - $j(wuEl).height()) / 2) + $j(document).scrollTop()); 
  }
}
//Contenu ajax
function wuContentAjax(wuEl, wuParams) {
  //Construction obj json pour post
  var params = '';  
  for(key in wuParams) {
    if(params != '') params += ", ";
    params += key+':"'+wuParams[key]+'"'; 
  }
  var params = '{'+params+'}';
  //ajax
  $j.post('wuAjax.php', eval('('+params+')'), function (jData) {
    var response = $j.evalJSON(jData);
    $j(wuEl).html(response.flow);
		if (response.functionCall) {			 
		  for(var i=0; i < response.functionCall.length; i++) {
        eval(response.functionCall[i]);
      }       
		}
		$j('.wuc_col').each(function() {  
	    $j(this).equalHeight();
	  });			
  }); 
}
//Ouverture du masque gris
function wuOpenMasque(wuEl, wuClass, wuOpacity) {
  $j(wuEl).addClass(wuClass);
  $j(wuEl).css("opacity", wuOpacity/100);
  $j(wuEl).css("filter", "alpha(opacity="+wuOpacity+")");
  $j(wuEl).height($j(document).height());  
  $j(wuEl).width("100%");    
  $j(wuEl).fadeIn("slow"); 
}
//Verification champ sendFriend
function wuVerifSendFriend(wuSubmitFct, wuForm, wuID) {
  var filtreMail = /^[A-Za-z0-9.-_--]+@[A-Za-z0-9.-_--]+\.[A-Za-z]{2,5}$j/;  
  if($j("#ami").val() == '') {
    alert('Merci de renseigner votre nom.');
    $j("#ami").focus(); 
  } else {    
    if (filtreMail.test($j("#mailFrom").val()) != true) {
      alert('Merci de renseigner votre adresse E-mail ou de saisir une adresse E-mail valide.');
      $j("#mailFrom").focus(); 
    } else {
      if (filtreMail.test($j("#mailDest").val()) != true) {
        alert('Merci de renseigner l\'adresse E-mail de votre ami(e) ou de saisir une adresse E-mail valide.');
        $j("#mailDest").focus();
      } 
      else { return eval(wuSubmitFct)(wuForm, wuID); }
    }  
  }
}
//Submit du sendFriend
function wuSubmitSendFriend(wuForm, wuID) {
  var wuParams = new Array();
  wuParams['phpFile'] = 'plugins/wuPlugin_ajaxPopup.php';
  wuParams['location'] = '1';
  wuParams['wuPopupID'] = wuID;
  $j("form[name="+wuForm+"]").ajaxSubmit({ url: "wuAjax.php", type: 'post', success: function() { wuParams['wuMess'] = 'ok'; wuContentAjax("#wuPopup_"+wuID, wuParams); }, error: function() { wuParams['wuMess'] = 'nok'; wuContentAjax("#wuPopup_"+wuID, wuParams); }});
  return false;   
}
 
//Hauteur des containers et colonnes 
jQuery.fn.equalHeight = function () {   
  var height       = 0;
  var maxHeight    = 0;
	// recupere la hauteur maximale
  $j(this).siblings('.wuc_col').each(function() {
    height       = jQuery(this).outerHeight();    
    maxHeight    = (height > maxHeight) ? height : maxHeight;
    jQuery(this).setHeight(maxHeight);
  }); 
  $j(this).prevAll('.wuc_col').each(function() {  
    jQuery(this).setHeight(maxHeight);
  });
	if(maxHeight == 0) maxHeight = jQuery(this).outerHeight(); 
  // Attribue la hauteur maximale aux colonnes et containers
  $j(this).setHeight(maxHeight);
  $j(this).parents('.wu_object:first').setHeight(maxHeight);       
};

//Hauteur des comp d ergo
jQuery.fn.ergoHeight = function () {  
	var contHeight = $j("#wuContentExt").outerHeight();
	//contenu ext
	$j("[id^=wuColExt],[id=wuContentInt]").each(function() {
	   jQuery(this).setHeight(contHeight);
	});
	//contenu int
	if($j("#wuHeaderInt").size() > 0) { var headerHeight = $j("#wuHeaderInt").outerHeight(); }
	else var headerHeight = 0;
	if($j("#wuFooterInt").size() > 0) { var footerHeight = $j("#wuFooterInt").outerHeight(); }
	else var footerHeight = 0;
	$j("[id^=wuColInt],[id=wuRub]").each(function() {
	   jQuery(this).setHeight(Math.round(contHeight - headerHeight - footerHeight));
	});
};

//Hauteur de la div affichant le fond 2 du body
jQuery.fn.bodyFond2Height = function () {   
	bodyHeight = $j('body').height();
  windowHeight = $j(window).height();  
	if($j('#wuBodyFond2').hasClass('wuBackFixed')) {
	  if(windowHeight > bodyHeight) { $j('#wuBodyFond2').height(windowHeight); }
  	else { $j('#wuBodyFond2').height(bodyHeight); } 
	}
	else {
	  $j('#wuBodyFond2').height(bodyHeight); 
	} 
  
  
};   

//Set height
jQuery.fn.setHeight = function(maxHeight) {          
  var minHeight   = maxHeight - ($j(this).outerHeight() - $j(this).height()); 
  var property    = jQuery.browser.msie && jQuery.browser.version < 7 ? 'height' : 'min-height';
	$j(this).css(property, minHeight + 'px');
} 

//Waiting form
function wuWaitingForm() {	  
	$j("#masque").css("background", "#000");
  wuOpenPopup("#wuWaitingPopup", '140', '65');
  wuOpenMasque("#masque", '', '50');    
} 
