siteTools = Class.create(communTools,{
	bPasse : false,
	
	initialize : function () {
		this.getIEVersion();
			
		if(this.ie_version < 9){
			$('backgroundPage').show();
			this.setBackgroundSize();
			Event.observe(window, 'resize', function() { this.bPasse = false;this.setBackgroundSize();}.bind(this));
		}
	},
	
	setBackgroundSize : function () {
		if(!this.bPasse){
			var rRatio = 1;
			var iWidth = 0;
			var iHeight = 0;
			var iTop = 0;
			var iLeft = 0;
	
			if(document.documentElement.clientWidth <= document.documentElement.clientHeight){
				if($('backgroundPage')){
					rRatio = parseInt($('backgroundPage').style.width,10)/parseInt($('backgroundPage').style.height, 10);
				}
				iHeight = parseInt(document.documentElement.clientHeight,10)+20;
				iWidth = (parseInt(document.documentElement.clientWidth,10)+20)/(rRatio);
				iLeft = ((iWidth-document.documentElement.clientWidth)/2);
			}else{
				if($('backgroundPage')){
					rRatio = parseInt($('backgroundPage').style.height,10)/parseInt($('backgroundPage').style.width, 10);
				}
				iWidth = parseInt(document.documentElement.clientWidth,10)+20;
				iHeight = (parseInt(document.documentElement.clientHeight,10)+20)/(rRatio);
				iTop = ((iHeight-document.documentElement.clientHeight)/2);
			}
			
			if($('backgroundPage')){
				$('backgroundPage').setAttribute("width", iWidth);
				$('backgroundPage').setAttribute("height", iHeight);
				$('backgroundPage').style.width = iWidth+"px";
				$('backgroundPage').style.height = iHeight+"px";
				$('backgroundPage').style.top = "-"+Math.abs(iTop)+"px";
				$('backgroundPage').style.left = "-"+Math.abs(iLeft)+"px";
			}
		}
	},

	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)
		);
	},
	
	toogleDiv : function (sFirstDiv, sSecondDiv) {
		if($(sFirstDiv)){
			$(sFirstDiv).show();
		}
		
		if($(sSecondDiv)){
			$(sSecondDiv).hide();
		}
		
		$$('.toogle').each(
			function (oDiv) {
				oDiv.stopObserving("click");
				oDiv.observe("click", 
					function (){
						var sActiveDiv = oDiv.id.replace("select_", "");
						$$('.toogle').each(function(oOldDiv){var sOldDiv = oOldDiv.id.replace("select_", "");oOldDiv.removeClassName("select");$(sOldDiv).hide();});
						if($(sActiveDiv)){
							oDiv.addClassName("select");
							$(sActiveDiv).show();
						}
						
					}.bind(this)
				);
			}.bind(this)
		);

	},
	
	toogleMenus : function () {
		$$('.accordion_toggle').each(
			function (oCategories) {
				var aInfos = oCategories.id.split("_");
				oCategories.stopObserving("click");
				oCategories.observe("click", 
					function () {
						if(!oCategories.hasClassName("accordion_toggle_active")){
							$$('.accordion_content').each(
								function(oContent){
									var aPlats = oContent.id.split("_");
									if($("categorie_"+aPlats[1]).hasClassName("accordion_toggle_active")){
										$("categorie_"+aPlats[1]).removeClassName("accordion_toggle_active");
										new Effect.BlindUp(oContent, { duration: 0.3});
									}
								}
							);

							oCategories.addClassName("accordion_toggle_active");
							new Effect.BlindDown($('plat_'+aInfos[1]), { duration: 0.5});
						}
					}
				);
					
				$('plat_'+aInfos[1]).hide();
			}
		);
	},
	
	toogleForm : function (sFirstForm, sSecondForm) {
		var oOptions = {};
		 if(arguments.length > 2){
			oOptions = arguments[2] || {};
		}

		if($(sFirstForm)){
			$(sFirstForm).show();
			this.listenForm(sFirstForm, {sFieldEmpty:oOptions.sFieldEmpty,sFieldError:oOptions.sFieldError});
		}
		
		if($(sSecondForm)){
			$(sSecondForm).hide();
		}
		
		$$('.formSelect').each(
			function (oSelectForm) {
				oSelectForm.stopObserving("click");
				oSelectForm.observe("click", 
					function (){
						var sForm = oSelectForm.id.replace("select_", "");
						$$('.formSelect').each(function(oOldForm){oOldForm.removeClassName("select");});
						$$('.formToogle').each(function(oOldForm){if(sForm!=oOldForm.id){$(oOldForm.id+"Msg").update("");$(oOldForm.id+"Msg").hide();oOldForm.hide();}});
						if($(sForm)){
							$(sForm).show();
							$(sForm+"Msg").show();
							oSelectForm.addClassName("select");
							this.listenForm(sForm, {sFieldEmpty:oOptions.sFieldEmpty,sFieldError:oOptions.sFieldError});
						}
						
					}.bind(this)
				);
			}.bind(this)
		);

	},

	listenForm : function (sForm) {
		var oOptions = {};
		 if(arguments.length > 1){
			oOptions = 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", oOptions.sFieldEmpty, {sLabel:this.getLabel(sProperty),sField:sProperty});
								return false;
							}else{
								if(!this.checkField(sProperty)){
									this.bWait = false;
									this.setMessage(sForm+"Msg", "error", oOptions.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", oOptions.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));
					}
				}
				
				if(this.bWait){
					//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);oTools.bWait=false;"
					);
				}
			}
		}.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)
		);
	}
});

