siteTools = Class.create(communTools,{

	initLanguage : function () {
		$$(".languages img.btn").each(
			function (oElement) {
				oElement.observe("click",
					function (){
						var aInfos = oElement.id.split("|");
						this.ajaxRequest("init.php", "iAction=1&sAction="+aInfos[1]+"&iIdLanguage="+aInfos[2]);

					}.bindAsEventListener(this)
				);
			}.bindAsEventListener(this)
		);
	},

	listenForm : function (sForm) {
		var aOptions = {};
		 if(arguments.length > 1){
			aOptions = arguments[1] || {};
		}

		$(sForm).observe('submit', function(){
			if(!this.bWait){
				this.bWait = true;
				this.closeMessage(sForm+"Msg");
				var sQuery = "iAction="+parseInt($F('iAction_'+sForm),10);
				var oForm = $(sForm).serialize(true);
				var sProperty = null;
				for (sProperty in oForm) {
					if($(sProperty) && sProperty !== "iAction_"+sForm){
						if($(sProperty).hasClassName("mandatory")){
							if ($F(sProperty) === "") {
								this.bWait = false;
								this.setMessage(sForm+"Msg", "error", aOptions.sFieldEmpty, {sLabel:this.getLabel(sProperty),sField:sProperty});
								return false;
							}else{
								if(!this.checkField(sProperty)){
									this.bWait = false;
									this.setMessage(sForm+"Msg", "error", aOptions.sFieldError, {sLabel:this.getLabel(sProperty),sField:sProperty});
									return false;
								}
							}
							$(sProperty).removeClassName("fieldError");
						}else{
							if ($F(sProperty) !== "") {
								if(!this.checkField(sProperty)){
									this.bWait = false;
									this.setMessage(sForm+"Msg", "error", aOptions.sFieldError, {sLabel:this.getLabel(sProperty),sField:sProperty});
									return false;
								}
							}
							$(sProperty).removeClassName("fieldError");
						}

						if(sProperty.match("sPassword")){
							$(sProperty).value = SHA1($F(sProperty));
						}
						sQuery += "&"+sProperty+"="+encodeURIComponent($F(sProperty));
					}
				}
				this.bWait = false;
				var oRequest = this.ajaxRequest("init.php",
												sQuery,
												"var oTools = new siteTools();oTools.setMessage('"+sForm+"Msg', 'success', oResult.sText);$('"+sForm+"').reset();oTools.closeMessageAfter('"+sForm+"Msg', 5);oTools.bWait=false;",
												"var oTools = new siteTools();oTools.setMessage('"+sForm+"Msg', 'error', oResult.sText);"
				);
			}
		}.bind(this));
	},

	initLogout : function () {
		$('logout').observe("click",
			function (){
				this.ajaxRequest("init.php", "iAction=4", "document.location.href='index.php'");
			}.bindAsEventListener(this)
		);
	},

	initSlideNavigations : function () {
		$$('ul.slideNav li').each(
			function (oNavigation) {
				oNavigation.observe("mouseover",
					function () {
						$$('.slideNavHover').each(
							function (oCurrentNav){
								oCurrentNav.removeClassName("slideNavHover");
								//new Effect.Fade(oCurrentNav);
								oCurrentNav.hide();
							}
						);

						var aInfos = oNavigation.id.split("|");
						if($('subNav|'+aInfos[1])){
							$('subNav|'+aInfos[1]).addClassName("slideNavHover");
							$('subNav|'+aInfos[1]).clonePosition(oNavigation, {offsetTop:23, setWidth:false, setHeight:false});
							//new Effect.Appear($('subNav|'+aInfos[1]));
							$('subNav|'+aInfos[1]).show();
							$('subNav|'+aInfos[1]).observe("mouseout",
								function () {
									$('subNav|'+aInfos[1]).hide();
								}
							);
						}
					}
				);
			}
		);
	},

	initInterviews : function () {
		$$('li.LinkPodcast').each(
			function (oPodcast) {
				oPodcast.stopObserving("click");
				oPodcast.observe("click",
					function (){
						var aInfos = oPodcast.id.split("|");
						if($('interview|youtube|'+aInfos[2])){
							var sLink = $('interview|youtube|'+aInfos[2]).innerHTML;
							var sDate = $('interview|date|'+aInfos[2]).innerHTML;
							var sAuteur = $('interview|auteur|'+aInfos[2]).innerHTML;

							if($('interview|youtube')){
								$('interview|youtube').remove();
								var oObject = new Element('object', { id: 'interview|youtube', width: '320', height: '250', classid: 'clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' });
								var oParamMovie = new Element('param', { id: 'interview|youtube|movie', name: 'movie', value: sLink });
								var oParamFullScreen = new Element('param', { name: 'allowFullScreen', value: 'true' });
								var oParamScriptAccess = new Element('param', { name: 'allowscriptaccess', value: 'always' });
								var oEmbed = new Element('embed', { id: 'interview|youtube|src', src: sLink, type: 'application/x-shockwave-flash', allowscriptaccess: 'always', allowfullscreen: 'true', width: '320', height: '250'});

								oObject.insert(oParamMovie);
								oObject.insert(oParamFullScreen);
								oObject.insert(oParamScriptAccess);
								oObject.insert(oEmbed);
								$('showPodcastVideo').insert(oObject);
							}else{
								$('interview|youtube|src').remove();
								var oEmbed = new Element('embed', { id: 'interview|youtube|src', src: sLink, type: 'application/x-shockwave-flash', allowscriptaccess: 'always', allowfullscreen: 'true', width: '320', height: '250'});
								$('showPodcastVideo').insert(oEmbed);
							}

							$('interview|auteur').update(sAuteur);
							$('interview|date').update(sDate);
						}
					}
				);
			}
		);
	},

	initGroupsToogle : function (sDirectory, iId) {
		var sUrl = "modules/"+sDirectory+"/content.php";
		$$(".groupToogle .title").each(
			function (oElement) {
				oElement.stopObserving("click");
				oElement.observe("click",
					function () {
						$$(".groupToogle .content").each(
							function (oContent) {
								oContent.update("");
							}
						);

						var aInfos = oElement.id.split("|");
						this.ajaxUpdater("content|"+parseInt(aInfos[1],10), sUrl, "iGroupCode="+parseInt(aInfos[1],10));
					}.bindAsEventListener(this)
				);
			}.bind(this)
		);

		this.ajaxUpdater("content|"+parseInt(iId,10), sUrl, "iGroupCode="+parseInt(iId,10));
	},

	initPagesSelect : function (sMode) {
		$$(".go").each(
			function (oElement) {
				oElement.stopObserving("click");
				oElement.observe("click",
					function () {
						this.ajaxRequest("init.php", "iAction=5&sMode="+sMode+"&iPage="+parseInt(oElement.innerHTML,10));
					}.bindAsEventListener(this)
				);
			}.bind(this)
		);

		$$(".goPagination").each(
			function (oElement) {
				oElement.stopObserving("click");
				oElement.observe("click",
					function () {
						var aInfos = oElement.id.split("|");
						this.ajaxRequest("init.php", "iAction=5&sMode="+sMode+"&iPage="+parseInt(aInfos[1],10));
					}.bindAsEventListener(this)
				);
			}.bind(this)
		);
	}
});

