$.noConflict();
jQuery(document).ready(function($){
	//masquer/démasquer l'aide
/*	var enTete = $("#enTete");
	var displayHelp = $("#displayHelp");
	var needSomeHelp = $("#needSomeHelp");
	if(enTete && needSomeHelp){
		var thNeedHelp = enTete.find("th.help");
		displayHelp.change(function(){
			thNeedHelp.find("img").toggle();
			var divWindowHelp = thNeedHelp.find(".windowHelp");
			if(divWindowHelp.length == 0){
				thNeedHelp.each(function(i,e){
					// variables nécessaires pour construire la fenêtre d'aide
					//id du th
					var myAttrId = $(this).attr("id");
					myAttrId = myAttrId.split("_");
					//numéro figurant dans la valeur de l'id
					var myNumber = myAttrId[1];
					myAttrId = "#help_"+myNumber;
					//on reconstitue l'id correspondant. Cet id sera utilisé pour chercher le contenu html
					var divHelpId = "windowHelp_"+myNumber;
					// on affiche qu'une fois l'aide
					var helpAvailable = $(this).children("div");
					if(helpAvailable.length == 0){
						//construit le div comportant le message d'aide
						$(this).append("<div id='"+divHelpId+"' class='windowHelp'>"+$(myAttrId).html()+"</div>");
						$(this).bind("mouseenter",function(e){
							$(this).mousemove(function(e){
								if(displayHelp.is(':checked')){
									var x = e.pageX - 25;
									var y = e.pageY + 10;
									var position = "top:"+y+"px;left:"+x+"px;";
									$("#"+divHelpId).attr("style",position).show();
									}else{
									$("#"+divHelpId).hide();
									}
								});
							}).bind("mouseleave",function(e){
								$(this).find(".windowHelp").hide();
							});
						}
				 	});
			 	}
			})
		}*/

	//menu déroulant top
	var menuTopLi = $("#menuTop").find("li.level1");
	menuTopLi.find("span").click(function(){
		//on cache les autres menu de niveau 1 ouvert
		var ulVisible = menuTopLi.find("ul:visible");
		var formVisible = menuTopLi.find("form:visible");
		//ulVisible.append(menuTopLi.find("form"));
		//@todo: optimiser le test ci-dessous car je n'ai pas la doc jquery dans le train....
		if( (ulVisible && (ulVisible.parent().children(0).html()!=$(this).html())) ){
			ulVisible.hide();
			if($(this).next("ul").length!=0)
				formVisible.hide();
			}
		nextElement = $(this).next();
		if(nextElement){
			nextElement.toggle();
			nextElement.find("li").hover(function(){
				$(this).find("ul").show();
				},function(){
				$(this).find("ul").hide();
				});
			}
		});

	//tableaux des services
	var serviceTabConteneur = $("#serviceTabConteneur");
	if(serviceTabConteneur.length!=0){
		setTimeout("refresh()",600000);

		//affichage du div si pas loggué
		var serviceLogMsg = serviceTabConteneur.find("#logMsg");
		if(serviceLogMsg.length!=0){
			serviceLogMsg.show();
			serviceLogMsg.find("#closeLogMsg").bind("click",function(){
				$(this).parent().hide();
			});
		}


		//taille automatique du tableau
		var conteneurTab = serviceTabConteneur.find("#serviceTabContenu");
		var serviceTab = conteneurTab.find('#serviceTab');
		var widthStyle = 'width:'+serviceTab.width()+'px';
		var heightStyle = 'height:'+serviceTab.height()+'px';
		var style = widthStyle+';'+heightStyle;
		conteneurTab.attr('style',style);

		//scroll du tableau
		var serviceTabThFirst = $("#serviceTab").find('th.first:not(.code)');//conteneurTab.find('#serviceTab thead tr th.first:not(.code)');
		var heightCell = 0;
		serviceTabThFirst.each(function(i,e){
			heightCell+=$(this).innerHeight();
			});
		var heightStyleCell = 'height:'+heightCell+'px';
		//console.log(heightStyleCell);
		serviceTabConteneur.bind('scroll',function(e){
			var scrollPos = e.target.scrollLeft;
			if(scrollPos>1){
				var absolutePos = 'left:'+scrollPos+'px';
				$("#serviceTabCopy").find('thead tr:first th').attr('style',heightStyleCell);
				$("#serviceTabCopy").attr('style',absolutePos);
				$("#serviceTabCopy").show();
				}
			else
				$("#serviceTabCopy").hide();
		});
	}

	//tableau des prix physiques
	var physicalTabConteneur = $("#physicalTabConteneur");
	if(physicalTabConteneur.length!=0){
		//refresh automatique du tableau sayf celui des instruments (afin de ne pas renvoyer le formulaire automatiquement
		if(!$(this).has(".noAutoRefresh"))
			setTimeout("refresh()",600000);

		//affichage du div si pas loggué
		var physicalLogMsg = physicalTabConteneur.find("#logMsg");
		if(physicalLogMsg.length!=0){
			physicalLogMsg.show();
			physicalLogMsg.find("#closeLogMsg").bind("click",function(){
				$(this).parent().hide();
			});
		}
	}

	//javascript formulaire abonnement
	var subscriptionForm = $("#subscription");
	if(subscriptionForm.length!=0){
		var subscriptionRenewCb = subscriptionForm.find("#subscriptionRenew");
		var subscriptionRenewForm = subscriptionForm.find("#renewForm");
		if(subscriptionRenewCb.is(':checked'))
			subscriptionRenewForm.show();
		subscriptionRenewCb.click(function(){
			subscriptionRenewForm.slideToggle();
			});
		var subscriptionChangePwdCb = subscriptionForm.find("#subscriptionChangePwd");
		var subscriptionChangePwd = subscriptionForm.find("#changePwd");
		if(subscriptionChangePwdCb.is(':checked'))
			subscriptionChangePwd.show();
		subscriptionChangePwdCb.click(function(){
			subscriptionChangePwd.slideToggle();
			subscriptionForm.find("#subscriptionPassword").attr('value','');
			subscriptionForm.find("#subscriptionConfirmPassword").attr('value','');
			});
		}

	//paypal
	var paypalForm = $("#paypal_form");

	if (paypalForm.length!=0){
		paypalForm.submit();
	}

	//speedguide
	var speedGuide = $("#speedGuide");

	if (speedGuide.length!=0){
		//etat du user (si il est loggué ou non)
		//@todo: liens tjs apparent => a voir si on vire pas cela par la suite
		//var isUserLoged = speedGuide.find("#isUserLoged").attr("value");
		var isUserLoged = 1;

		//definit l'offset de pagination par défaut
		var sgInit = 0;
		// traitement des listes déroulantes
		var selectSpeedGuide = speedGuide.find("select");
		selectSpeedGuide.change(function(){
			//on mémorise l'id du select qui a changé ainsi que la valeur
			var changeSelectId = $(this).attr("id");
			//recupere l'offset de pagination: on soustrait 1 car la pagination commence à 1 et non 0
			sgInit = speedGuide.find(".sgInit").attr("value");

			var selectIds = new Array();
			var selectValues = new Array();
			//récupère les valeurs de chaque select: on inclut pas le select de la liste déroulante de pagination
			selectSpeedGuide.not(".sgInit").each(function(i,e){
				var idTemp = $(this).attr("id");
				selectIds[i] = idTemp;
				selectValues[i] = this.value;
				});
			//regénère les listes déroulantes
			selectSpeedGuide.not(".sgInit").each(function(i,e){
				var currentSelectId = $(this).attr("id");
				var currentSelectValue = this.value;
				//regénère la liste déroulante en fonction des valeurs trouvées
				$.post(
					"./ajax/speedGuideSearch.php",
					{ type: "setOptions", currentSelectId: ""+currentSelectId+"", currentSelectValue: ""+currentSelectValue+"",  selectIds: ""+selectIds+"", selectValues: ""+selectValues+"" },
					function(data){
						speedGuide.find("#"+currentSelectId).html(data);
		   				}
		   			);
		   		if(currentSelectId == changeSelectId){
					//RAZ de la pagination
					if(sgInit!=0)
						sgInit = 0;
		   			}
				});
			//détermine le nbre de résultats trouvés
			var sgNbResult = speedGuide.find("#sgNbResult");
			$.post(
				"./ajax/speedGuideSearch.php",
				{ type: "getNbResult", selectIds: ""+selectIds+"", selectValues: ""+selectValues+"", sgInit: ""+sgInit+"" },
				function(data){
					sgNbResult.html(data);
					sgNbResult.show();
					sgNbResult.find(".sgInit").change(function(){
						//affiche les résultats
						var sgListResult = speedGuide.find("#sgListResult");
						$.post(
							"./ajax/speedGuideSearch.php",
							{ type: "getSearchResult", selectIds: ""+selectIds+"", selectValues: ""+selectValues+"", isUserLoged: ""+isUserLoged+"", sgInit: ""+this.value+"" },
							function(data){
								sgListResult.html(data);
				   				}
				   			);
						});
	   				}
	   			);
			//affiche les résultats
			var sgListResult = speedGuide.find("#sgListResult");
			$.post(
				"./ajax/speedGuideSearch.php",
				{ type: "getSearchResult", selectIds: ""+selectIds+"", selectValues: ""+selectValues+"", isUserLoged: ""+isUserLoged+"", sgInit: ""+sgInit+"" },
				function(data){
					sgListResult.html(data);
	   				}
	   			);
			});
		// le bouton reset
		speedGuide.find("#sgReset").click(function(){
			var selectIds = new Array();
			var selectValues = new Array();
			//récupère les valeurs de chaque select
			selectSpeedGuide.each(function(i,e){
				selectIds[i] = $(this).attr("id");
				selectValues[i] = 0;
				});
			//mise à zéro des listes déroulantes
			selectSpeedGuide.each(function(i,e){
				var currentSelectId = $(this).attr("id");
				var currentSelectValue = 0;
				//regénère la liste déroulante en fonction des valeurs trouvées
				$.post(
					"./ajax/speedGuideSearch.php",
					{ type: "setOptions", currentSelectId: ""+currentSelectId+"", currentSelectValue: ""+currentSelectValue+"",  selectIds: ""+selectIds+"", selectValues: ""+selectValues+"" },
					function(data){
						speedGuide.find("#"+currentSelectId).html(data);
		   				}
		   			);
				});
			//reset du nbre de résultats trouvés
			var sgNbResult = speedGuide.find("#sgNbResult");
			$.post(
				"./ajax/speedGuideSearch.php",
				{ type: "getNbResult", selectIds: ""+selectIds+"", selectValues: ""+selectValues+"" },
				function(data){
					sgNbResult.html(data);
					sgNbResult.show();
	   				}
	   			);
			//affiche les résultats
			var sgListResult = speedGuide.find("#sgListResult");
			$.post(
				"./ajax/speedGuideSearch.php",
				{ type: "getSearchResult", selectIds: ""+selectIds+"", selectValues: ""+selectValues+"", isUserLoged: ""+isUserLoged+"", sgInit: "0" },
				function(data){
					sgListResult.html(data);
	   				}
	   			);
			});

			//affichage de la fenetre sur clique des faux liens
			speedGuide.find("a.linkLike").click(function(e){
				var sgLogMsg = speedGuide.find("#logMsg");
				var offsetTop = e.pageY-80;
				sgLogMsg.attr("style","top:"+offsetTop+"px");
				sgLogMsg.show();
				sgLogMsg.find("#closeLogMsg").bind("click",function(){
					$(this).parent().hide();
					});
				return false;
			});
		}

		//date
		$('.date-pick').datePicker();
		$('.date-pick-past').datePicker().dpSetStartDate('01/10/2010');
	});
function refresh(){
	window.location.reload( true );
	}
