var dialogOpen=false;


//MAJ = GLOBAL
$(document).ready(function(){//on attend que la page soit chargée
	

	/*//centrer navigation
	$('#navigation').width($('#navigation').width()+5);
	$('#navigation').css('float', 'none');*/
	
	
	/*
	//bon de commande
	$('.btn_submit_command').each(function(){
		$(this).mouseenter(function(){
			var index = $(this).index('.btn_submit_command');
			$(this).css('border-color', '#08AEED').css('background-image','url("files/design/nextC.png"');
			index++;
			$('.table_commande:eq('+index+')').css('border-color', '#08AEED');
		});
		$(this).mouseleave(function(){
			$(this).css('border-color', '#626262').css('background-image','url("files/design/next.png"');
			$('.table_commande').css('border-color', '#626262');
		});
	});
	
	$('.btn_submit_account_prev input').mouseenter(function(){
		$(this).css('border-color', '#08AEED').css('background-image','url("files/design/nextC.png"');
	});
	$('.btn_submit_account_prev input').mouseleave(function(){
		$(this).css('border-color', '#626262').css('background-image','url("files/design/next.png"');
	});
	$('.btn_submit_account_prev a').mouseenter(function(){
		$(this).css('border-color', '#08AEED').css('background-image','url("files/design/prevC.png"');
	});
	$('.btn_submit_account_prev a').mouseleave(function(){
		$(this).css('border-color', '#626262').css('background-image','url("files/design/prev.png"');
	});

	*/
	
	if($('#metaDescriptionEdit').length>0)setMetaDescription();
	$('#linkMeta').click(function(){setMetaDescription();});
	
	//FONCTION zzzGeneral
		arianne();
		
		bookmark();
		gdmAdmin();
		delSuggest();
		setModalSignaBugAdmin();
		setModalRespFiche();
		setModalCheckFiche();
		//recherche avancé
		searchAdvanced();
		suggestMouse();
		
		getBodyInfo();
		arianeSearch();
		arianeBrother();
		
		if(self.location.hash!="#validRegister" && $.trim(self.location.hash)!="" && $('#isConnect').length==0)selectInfo(self.location.hash.substr(1));
		faq();
		if($('.elm_listQuestion').length>0){
			$('.contentQuestion a').click(function(){faq();log('test');});
		}
	//FUNCTION zzzUserInfo
		setModalUserInfo();	
	//FUNCTION zzzEcommerce
		setEcommercePage();
		
		
	//FUNCTION zzzEdit
		setModalBoxSignal();
		setModalDelFiche();
		setModalBoxAdd();
		setModalBoxEdit();
		setModalDelBody();
		sortable();
		sortByName();
		
	//FUNCTION zzzConnect
		if(self.location.hash=="#validRegister" && $('#isConnect').length==0)modalLogin("noFade");
		if($('#maxLoginWait').length>0)modalLoginWait();
		connectHome();
		errorLogin();
	///FUNCTION zzzFunction
		if($('#myGMap').length>0){
		
			placeMarkers();	
		}
		
		tooltip($('.linkAdminTop'));
		if($('.tooltipBody').length>0)tooltipBody();
	
	//en dernière position, sinon le footer remonte en mode non connecté
		placeFooter();
		setResolution();
		$(window).resize(function(){placeFooter();});
		$(document).resize(function(){placeFooter();});
});


////////////////////////////////////////////
///////////////GENERAL//////////////////////
////////////////////////////////////////////

// => SET FOOTER BOTTOM positione footer en bas de page quand la page est plus petite que la fenetre
	function placeFooter(){
		if ($('body').height()>$(window).height()){
			$('#footer').css({position:'relative'});
		}else{
			$('#footer').css('position','absolute');
		}
	}

// => RESOLUTION <1000px de large on enlève quelque pixel au site pour éviter scrollbar horizontal
	function setResolution(){
		$(window).resize(function(){
			if(document.body.clientWidth<1000){
				$('#barre_menu,#footer').width(1000);
				$('#search').width(970);
				$('#blocArianne').width(940);
			}else{
				$('#barre_menu, #search, #blocArianne').css('width',"auto");
				$('#footer').css('width','100%');
			}
		});
		if(document.body.clientWidth<1000){
			$('#barre_menu,#footer').width(1000);
			$('#search').width(970);
			$('#blocArianne').width(940);
		}else{
			$('#barre_menu, #search, #blocArianne').css('width',"auto");
			$('#footer').css('width','100%');
		}
		if($('#barre_menu_left').length)var ref = $('#barre_menu_left').width();
		if($('#barre_menu_center').length)var ref = $('#barre_menu_center').width();
		
		$('#logout').width(ref - ($('#menu').width()+$('#chooseLanguage').width()+40));
		$('#connect').width(ref - ($('#menu').width()+$('#chooseLanguage').width()+40));
	}
	
/////////////////////////////////////////////////
///////////////////SEARCH////////////////////////
/////////////////////////////////////////////////

var CURRENT=-1;

$('*').click(function (evt){
	if(!$(evt.target).parents('#suggest').length)$('#suggest').hide();
	
});

// => SET SUGGEST ALL
	function suggest(e,elm){
		if($('#suggest').is(':hidden') && $('.elm_suggest').length)$('#suggest').show();
		//haut
		if(e==38 && $('.elm_suggest').length){
			$('.elm_suggest:eq('+CURRENT+')').removeClass('elm_suggest_hover');
			CURRENT--;
			log(CURRENT);
			if(CURRENT<-1){
				CURRENT=$('.elm_suggest').length-1;
			}
			
			//stock la valeur tapé dans le rel, si rel vide
			if(!$(elm).attr('rel'))$(elm).attr('rel',$(elm).attr('value'));
				
			if(CURRENT!=-1){
				$(elm).attr('value' , strip_tags($('.elm_suggest:eq('+CURRENT+')').html()));
			}else{
				$(elm).attr('value',$(elm).attr('rel'));
				$(elm).attr('rel',"");
			}
			$('.elm_suggest:eq('+CURRENT+')').addClass('elm_suggest_hover');
		//bas
		}else if(e==40 && $('.elm_suggest').length){
			$('.elm_suggest:eq('+CURRENT+')').removeClass('elm_suggest_hover');
			CURRENT++;
			if(CURRENT>$('.elm_suggest').length-1){
				CURRENT=-1;
			}
			
			//stock la valeur tapé dans le rel, si rel vide
			if(!$(elm).attr('rel'))$(elm).attr('rel',$(elm).attr('value'));
			if(CURRENT!=-1){
				$(elm).attr('value' , strip_tags($('.elm_suggest:eq('+CURRENT+')').html()));
			}else{
				$(elm).attr('value',$(elm).attr('rel'));
				$(elm).attr('rel',"");
			}
			$('.elm_suggest:eq('+CURRENT+')').addClass('elm_suggest_hover');
		}else{
			CURRENT=-1;
			var value = $(elm).attr('value');
			$.ajax({
				type: "GET",
				url: domain+"/do/other/gdm_ajax_suggest.php",
				data: "suggest="+value,
				success: function(msg){
					$('#suggest').html(msg);
					if($('#suggest').html()!=''){
						$('#suggest').show();
						suggestMouse(elm);
					}else{
						$('#suggest').fadeOut();	
					}
				}
		
			});
		}
		
	}
	
// => NAVIG TO SUGEGEST WITH MOUSE (des fois je parle anglais, enfin j'essaye!)
	function suggestMouse(elm){
		$('.elm_suggest').mouseenter(function(){
			$('.elm_suggest_hover').removeClass('elm_suggest_hover');
			$(this).addClass('elm_suggest_hover');
		});
		$('.elm_suggest').click(function(){
			$(elm).attr('value' , strip_tags($(this).html()));
			$('#search_form').submit();
		});
		
	}
// => SET ADVANCED ALL

	function searchAdvanced(){
			//clique sur recherhe avancé :
		if($('#home_search_link_first').length){
			elm = '#home_search_link_first';
			home = '#home_search';
		}else {
			elm = '#search_advanced';
			home = '#search';			
		}
		$(elm).click(function(){
			if($(home+'_more').css('display')=="none"){
				
				var tmp_rel = $(this).html();
				$(this).html($(this).attr('rel'));
				$(this).attr('rel', tmp_rel);
				
				$(home+' fieldset').height('auto');
				
				$(home+'_more').slideDown('normal', function(){
					placeFooter();
				});
			}else{
				var tmp_rel = $(this).html();
				$(this).html($(this).attr('rel'));
				$(this).attr('rel', tmp_rel);
				$(home+' fieldset').animate({height:'auto'});
				$(home+'_more').slideUp('normal', function(){
					placeFooter();
				});
				
			}
		});
	}

	

	
////////////////////////////////////////////////
////////////////BOOKMARK////////////////////////
////////////////////////////////////////////////

// => SET
	function bookmark(){
		if($('#boxBookmark').next('h2.bookmark_link:first').length==0){
			$('#noBookmark').fadeIn();
		}else{
			$('#noBookmark').hide();
		}
		//del bookmark
		$('.bookmark_elm').click(function(){
			delBookmark(this);
		});
		$('.bookmark').each(function(){
			addBookmark(this)
		});	
		
}

// => SAVE ADD
	function addBookmark(elm){
		$(elm).click(function(){
			$.ajax({
				type: "POST",
				url: domain+"/do/other/gdm_ajax_bookmark.php",
				data: 'id='+$(this).attr('rel')+'&language='+$('#globalLanguage').attr('value')+'&typeId='+$(elm).attr('name'),
				success: function(bk){
					$(elm).fadeOut();
					$('#boxBookmark').after(bk);	
					if($('#noBookmark').length>0)$('#noBookmark').fadeOut();
					$('#boxBookmark').next('h2.bookmark_link:first').children('.bookmark_elm').click(function(){delBookmark(this);});
				}
			});
		});
	
	}

// => SAVE DEL

	function delBookmark(elm){
		//sidebar
		if($(elm).attr('rev')=='true'){
			$(elm).parent().slideUp('low', function(){
				$.ajax({
					type: "POST",
					url: domain+"/do/other/gdm_ajax_bookmark.php",
					data: 'remove=true&id='+$(elm).attr('rel')+'&language='+$('#globalLanguage').attr('value')+'&typeId='+$(elm).attr('name'),
					success:function(){
						$(elm).parent().remove();
						if($('#boxBookmark').next('h2.bookmark_link:first').length==0){
							$('#noBookmark').fadeIn();
						}
					}
				});
	
			});
		//title fiche
		}
	}

///////////////////////////////////////////////////////////
/////////PAGE ADMINISTRATION SUGGESTION ///////////////////
///////////////////////////////////////////////////////////

// => CHOIX DES SUGGESTIONS EMPECHE D'EN COCHER PLUSIEURS
	function gdmAdmin(){
		$('.newValueInput').each(function(i,elm){
			$(elm).keyup(function(){
				if($.trim($(elm).attr('value'))!=""){
					$(elm).parent().nextAll().children('.validSuggest').attr('checked','checked');
					validSuggestChange($(elm).parent().nextAll().children('.validSuggest'));
				}else{
					$(elm).parent().nextAll().children('.validSuggest').attr('disabled', 'disabled');
					validSuggestChange($(elm).parent().nextAll().children('.validSuggest'));
				}
			});
			if($.trim($(elm).attr('value'))==""){
				$(elm).parent().nextAll().children('.validSuggest').attr('disabled', 'disabled');
				//validSuggestChange($(elm).parent().nextAll().children('.validSuggest'));
			}
		});
		$('.validSuggest').change(function(){
			validSuggestChange($(this));
		});
	}
// =>
	function validSuggestChange(checkbox){
		if(checkbox.is(':checked')){
				$('.validSuggest[value="'+checkbox.attr('value')+'"]').attr('checked',false).attr('disabled','disabled');
				checkbox.attr('checked',true).attr('disabled','');
		}else{
			$('.validSuggest[value="'+checkbox.attr('value')+'"]').attr('disabled','');	
		}
	}
	
// MET EN EVIDENCE L'INFO RECHERCHER
	function selectInfo(hash){
		$('#id_'+hash).css('text-transform', 'uppercase').css('color', "#AE1C1C").css('font-weight',"bold");
	}
	
//SUPPRIMER UNE SUGGESTION
function delSuggest(){
	$('.delSuggest').each(function(i,elm){
		$(elm).click(function(){
			$.ajax({
				type: "POST",
				url: domain+"/do/other/gdm_ajax_admin.php",
				data: "id="+$(elm).attr('rel')+"&date="+$(elm).attr('rev')+"&language="+$(elm).attr('lang'),
				success: function(msg){
					$(elm).parent().parent().hide();
				}
			});
		});
	});
	
}
///////////////////////////////////////////////////////////
/////////////////SIGNAL BUG MYOP///////////////////////////
///////////////////////////////////////////////////////////

// => SET LINK SIGNAL
	function setModalSignaBugAdmin(){
		$('#linkSignalBugAdmin').click(function(){
			$('#SignalBugAdmin').dialog({
				width: 600,
				resizable: false,
				disabled:true,
				draggable:false,
				buttons: {
					send: function() {
						$.ajax({
							type: "GET",
							url: $(this).attr('action'),
							data: $(this).serialize(),
							success: function(eeeee){
								
								$('#signalSend').dialog({
									width: 400,
									resizable: false,
									disabled:true,
									draggable:false,
									buttons: {
										ok: function() {
											$( this ).dialog('destroy');
											backOpac(false);
										}
									},
									open: function(event, ui) { 
										backOpac(true);
										$(this).css('height', '100%');
										$(this).dialog('option', 'position', 'center');
									},
									close: function(event, ui) { 
										backOpac(false);
										$( this ).dialog('destroy');
									}
								});
								$(".ui-dialog-titlebar").css('display','block'); 
								$("#SignalBugAdmin").dialog('destroy');
								
							}
						});
					}
				},
				open: function(event, ui) { 
					if($('.ui-widget-overlay').lenght>0){
						$('.ui-widget-overlay').show();
					}else{
						$('.ui-dialog').before('<div class="ui-widget-overlay" style="width:'+document.width+'px; height:'+document.height+'px; z-index: 1000;"></div>');
					}
				},
				close: function(event, ui) { 
					$('.ui-widget-overlay').hide();
					$( this ).dialog('destroy');
				}
			});
			$(".ui-dialog-titlebar").css('display','block'); 
			$('.signalInfo').fadeIn();
		});							   		
	}
	
	
///////////////////////////////////////////////////////////
////////////DESIGNER UN RESPONSABLE DE FICHE///////////////
///////////////////////////////////////////////////////////


// => SET LINK RESPONSEBLE FICHE
	function setModalRespFiche(data){
		$('#linkRespFiche').click(function(){
			modalRespFiche("getRespFiche=true&documentId="+$(this).attr('rel')+"&language="+$(this).attr('lang'));
		});
	}
// => OUVERTURE DE LA MODAL RESPONSABVLE DE FICHE
	function modalRespFiche(data){
		loading();
		$.ajax({
			type: "POST",
			url: domain+"/do/modal/gdm_modal_emailResp.php",
			data: data,
			success: function(modal){
				
				$('#modal').html(modal);
				if($('#respFiche').length==0){
					errorModal();
				}else{
					$('#respFiche').dialog({
						width: 600,
						position:'middle',
						resizable: false,
						disabled:true,
						draggable:false,

						buttons: {
							cancel: function() {
								closeModal($(this));
								backOpac(false);						
							},
							save: function() {
								var pass=require($('#respFiche'));
								if(pass==true){
									saveRespFiche($('#respFiche').serialize());
									closeModal($(this));
									backOpac(false);
								}
							}
						},
						open: function(event, ui) { 
							backOpac(true);
							$(this).css('height', '100%');
							$(this).dialog('option', 'position', 'center');
						
						},
						close: function(event, ui) { 
							closeModal($(this));
							backOpac(false);
						}
	
					});
					
					endLoading("tt");
				}
			}
		});
		
	}
	
	function saveRespFiche(data){
		loading();
		$.ajax({
			type: "POST",
			url: domain+"/do/modal/gdm_modal_emailResp.php",
			data: data,
			success: function(modal){
				$('#modal').html(modal);
				if($('#respFiche').length==0){
					errorModal();
				}else{
					$('#respFiche').dialog({
						width: 600,
						position:'middle',
						resizable: false,
						disabled:true,
						draggable:false,

						buttons: {
							ok: function() {
								closeModal($(this));
								backOpac(false);
							}
						},
						open: function(event, ui) { 
							backOpac(true);
							$(this).css('height', '100%');
							$(this).dialog('option', 'position', 'center');
						
						},
						close: function(event, ui) { 
							closeModal($(this));
							backOpac(false);
						}
	
					});
					
					endLoading('ddd');
				}
			}
		});
	}
	
	
///////////////////////////////////////////////////////////
/////////////////////VALIDER UNE FICHE/////////////////////
///////////////////////////////////////////////////////////

checkFicheValid ="";
checkFicheNoValid = "";
		

// => SET LINK CHECK FICHE
	function setModalCheckFiche(){
		//récupère le title pour le tooltip
		checkFicheValid =$('#linkCheckFiche').attr('title');
		//récupère le title pour le tooltip
		tooltip($('#linkCheckFiche'));
		
		$('#linkCheckFiche').click(function(){
			//FICHE PAS VALIDe
			if($('#linkCheckFiche').attr('rev')=="false"){
				modalCheckFiche("getCheckFiche=true&documentId="+$(this).attr('rel')+"&language="+$('#languageFiche').attr('value')+"&languageDoc="+$(this).attr('lang'));
				
			}else{
				modalCheckFiche("documentId="+$(this).attr('rel')+"&language="+$('#languageFiche').attr('value')+"&languageDoc="+$(this).attr('lang'));
			}
			if($(this).attr('rev')=="false"){
				$('#linkCheckFiche').attr('title', "");
				tooltip($('#linkCheckFiche'));
				$('#linkCheckFiche').attr('class', 'linkCheckValid');
				
			}
		});
		
	}
// => OUVERTURE DE LA MODAL RESPONSABVLE DE FICHE
	function modalCheckFiche(data){
		loading();
		$.ajax({
			type: "POST",
			url: domain+"/do/modal/gdm_modal_check_fiche.php",
			data: data,
			success: function(modal){
				
				$('#modal').html(modal);
				if($('#checkFiche').length==0){
					errorModal();
				}else{
					$('#checkFiche').dialog({
						width: 600,
						position:'middle',
						resizable: false,
						disabled:true,
						draggable:false,

						buttons: {
							ok: function() {
								closeModal($(this));
								backOpac(false);
							}
							
						},
						open: function(event, ui) { 
							tooltip($('.unchekFiche'));
							$('.unchekFiche').click(function(){
								closeModal($('#checkFiche'));
								modalCheckFiche("getCheckFiche="+$(this).attr('rev')+"&documentId="+$(this).attr('rel')+"&language="+$('#languageFiche').attr('value')+"&languageDoc="+$(this).attr('lang'));
								if($(this).attr('lang')==$('#languageFiche').attr('value')){
									$('#linkCheckFiche').attr('title',"");
									$('#linkCheckFiche').attr('rev', 'true');
									
									$('#linkCheckFiche').css('background-position', 'bottom');
								}
							});
							backOpac(true);
							$(this).css('height', '100%');
							$(this).dialog('option', 'position', 'center');
						
						},
						close: function(event, ui) { 
							closeModal($(this));
							backOpac(false);
						}
	
					});
					
					endLoading("ttt");
				}
			}
		});
		
	}
	

	
///////////////////////////////////////////////////////////
//////////////////////////FAQ//////////////////////////////
///////////////////////////////////////////////////////////


// => OUVRIR UNE QUESTION
	function faq(){
		if($('.elm_listQuestion').length>0){
			var hash = window.location.hash;
			if($.trim(hash)!=''){
				$('.contentQuestion').hide();
				$.scrollTo($('body'));
				$.scrollTo($(hash),500, {offset: {top:-50}, onAfter:function(){
					$(hash).next('.contentQuestion').show();	
				}});
				
			}
			$('.elm_listQuestion').click(function(){
				/*var hash = window.location.hash;
				var target = $(hash == "#" ? body : hash);
				
				if (target.length>0) {
					var id = target.attr("id");
					target.attr("id", "");
					window.location.hash = hash;
					target.attr("id", id);
				}*/
				elm = $(this);
				if(!elm.parent().next('.contentQuestion').is(':visible')){
					$('.contentQuestion').hide();
					$.scrollTo(elm,500, {offset: {top:-50}, onAfter:function(){
						elm.parent().next('.contentQuestion').show();
						placeFooter();
					}});
				
				}else{
					elm.parent().next('.contentQuestion').hide();
					placeFooter();
				}
				return false;
			});	
		}
	}
	
/////////////////////////////////////////////////////////////
/////////////////////TOOLTIP PERSONNE////////////////////////
/////////////////////////////////////////////////////////////

// => RECUPERE LES INFOS DE LA PERSONNE AU HOVER
	function getBodyInfo(){
		$('.linkBody').each(function(i,elm){
			tooltipBodyInfo(elm);
			
			$(elm).mouseenter(function(){
				if($(elm).attr('rev')!="set" && $(elm).attr('rev')!='no'){
					$('#tooltip').hide();
					$.ajax({
						type: "POST",
						url: domain+"/do/other/gdm_ajax_display_body.php",
						data: "persoId="+$(elm).attr('rel')+"&language="+$(elm).attr('lang'),
						success: function(info){
							
							if($.trim(info)!=""){
								$('#tooltip').show();
								$(elm).prev('.linkBodyTooltip').html(info);
								$('#tooltip .body').html(info);
								$(elm).attr('rev','set');
							}else{
								$(elm).attr('rev','no');	
							}
							
						}
					});
				}else if($(elm).attr('rev')=='no'){
					$('#tooltip').hide();
				}
			});
			
		});
	}
	function tooltipBodyInfo(elm){
		$(elm).tooltip({ 
			track: true, 
			delay: 0,
			showURL: false, 
			fade: 250,
			extraClass:'tooltipWidth',
			bodyHandler: function() { 
				return $(this).prev('.linkBodyTooltip').html(); 
			} 
		});	
	}
	
//////////////////////////////////////////////////////
///////////////MODIFIER META DESCRIPTION//////////////
//////////////////////////////////////////////////////
function setMetaDescription(){
	$('#metaDescription').dialog({
		width: 400,
		resizable: false,
		disabled:true,
		draggable:false,
		buttons: {
			cancel : function(){
				$(this).dialog('destroy');
				backOpac(false);
			},
			ok: function() {
				$.ajax({
					type: "POST",
					url: domain+"/do/other/gdm_ajax_metaDescription.php",
					data: $('#metaDescription').serialize(),
					success: function(msg){
						$('#metaDescription').dialog('destroy');
						backOpac(false);
		
					}
				});
			}
		},
		open: function(event, ui) { 
			backOpac(true);
			$(this).css('height', '100%');
			$(this).dialog('option', 'position', 'center');
		},
		close: function(event, ui) { 
			backOpac(false);
			$( this ).dialog('destroy');
		}
	});
	$(".ui-dialog-titlebar").css('display','block'); 
}

/*
///////////////////////////////////////////////////////
////////////////HOVER SUR LES BOUTONS//////////////////
///////////////////////////////////////////////////////
function hoverSubmit(){
	$('.btn_submit_account input, .btn_submit_account a').hover(
		function(){
			$(this).css('color', '#08AEED').css('border-color','#08AEED').css('background-position',"96% bottom");
		},function(){
			$(this).css('color', '#626262').css('border-color','#C0C0C0').css('background-position',"96% top");
		}
	);
}*/

