// JavaScript Document
var regExpBeginning = /^\s+/;
var regExpEnd       = /\s+$/;

var requete = null;
function creerRequete() {
    try {
        requete = new XMLHttpRequest(); /* On essaye de créer un objet XmlHTTPRequest */
    } catch (microsoft) {
        /* Microsoft utilise une autre technique, on tente de créer un objet ActiveX */
        try {
            requete = new ActiveXObject('Msxml2.XMLHTTP');
        } catch(autremicrosoft) {
            /* On en teste une autre méthode si la première n'a pas marché */
            try {
                requete = new ActiveXObject('Microsoft.XMLHTTP');
            } catch(echec) {
                /* Si aucune méthode ne fonctionne, il ne reste plus qu'à mettre à jour le navigateur ! */
                requete = null;
            }
        }
    }
    if (requete == null) {
        alert('Votre navigateur ne semble pas supporter les objets XMLHttpRequest.');
    }
}

function getVideo(lien) {
	creerRequete();
	var url = 'ajax/video_humour.php?vid='+lien;
	requete.open('GET', url, true);
	requete.onreadystatechange = function() {
		if(requete.readyState == 4) {
			if(requete.status == 200) {
				document.getElementById('humour-video-content').innerHTML = requete.responseText;
			}
		}
	};
	requete.send(null);
}

function fillVideo() {
	document.getElementById('video').checked = true;
}



function floatWin(theURL,winName,winWidth,winHeight,lang) {
	var Left= (window.screen.width/2)-winWidth;
	var Top= (window.screen.height/2)-winHeight;
	window.open(theURL,winName,'left='+Left+',top='+Top+',width='+winWidth+',height='+winHeight);
	hauteur(winName);
}

function MM_openBrWindow(theURL,winName,features) {
	window.open(theURL,winName,features);
}

// Supprime les espaces inutiles en d�but et fin de la cha�ne pass�e en param�tre.
function trim(aString) {
    return aString.replace(regExpBeginning, "").replace(regExpEnd, "");
}
// Supprime les espaces inutiles en d�but de la cha�ne pass�e en param�tre.
function ltrim(aString) {
    return aString.replace(regExpBeginning, "");
}
// Supprime les espaces inutiles en fin de la cha�ne pass�e en param�tre.
function rtrim(aString) {
    return aString.replace(regExpEnd, "");
} 

// echappe les caract�re sp�ciaux pour l'url
function escapeURI(La){
	if(encodeURIComponent) {
		return encodeURIComponent(La);
	}
	if(escape) {
		return escape(La);
	}
}

//test si le champ du formulaire est vide
function test_vide(field) {
	
	if(trim($('#' + field).val()) != "") {
		return "";  
	} else {
		$('#' + field).css("color","red");
		return "x";
	}	
}

function navDetect() {
//	d�tecte le navigateur & la plateforme
	var OS = navigator.platform;
	NS6 = false;
	IE4 = (document.all);
	if (!IE4) {NS6=(document.getElementById);}
	NS4 = (document.layers);
	if ((IE4) && (OS.toLowerCase() == "macppc")) {
//		alert("IE on a Mac");
		convertPopup = false;
	} else {
//		alert("ok");
		convertPopup = true;
	}
}

function getPageOffset() {
	navDetect();
	if (convertPopup) {
		if ((NS4) || (NS6)) {
			horizontalOffset = this.parent.window.pageXOffset + 10;
			verticalOffset = this.parent.window.pageYOffset + 10;
			windowWidth = this.parent.window.innerWidth;
			windowHeight = this.parent.window.innerHeight;
		} else {
			horizontalOffset = this.parent.document.body.scrollLeft + 10;
			verticalOffset = this.parent.document.body.scrollTop + 10;
			windowWidth = this.parent.document.body.offsetWidth;
			windowHeight = this.parent.document.body.offsetHeight;
		}
	} else {
		horizontalOffset = 10;
		verticalOffset = 10;
	}
}

function resetPageOffset() {
	this.parent.window.scrollTo(0,0);
}

function centerWin(theURL,winName,winWidth,winHeight) {
	getPageOffset();
	Xwin = horizontalOffset-10-(winWidth/2)+(windowWidth/2);
	Ywin = verticalOffset-10-(winHeight/2)+(windowHeight/2);
	window.open(theURL,winName,'left='+Xwin+',top='+Ywin+',width='+winWidth+',height='+winHeight);
}

function getWindowHeight() {
	var windowHeight = 0;
	if (typeof(window.innerHeight) == 'number') {
		windowHeight = window.innerHeight;
	}
	else {
		if (document.documentElement && document.documentElement.clientHeight) {
			windowHeight = document.documentElement.clientHeight;
		}
		else {
			if (document.body && document.body.clientHeight) {
				windowHeight = document.body.clientHeight;
			}
		}
	}
	return windowHeight;
}
function setFooter() {
	var windowHeight = getWindowHeight();
	if (windowHeight > 0) {
		var contentHeight = $('#contenu')[0].offsetHeight + $('#left')[0].offsetHeight - 5;
		var footerElement = $('#footer')[0];
		var footerHeight  = footerElement.offsetHeight;
		if (windowHeight - (contentHeight + footerHeight) >= 0) {
			footerElement.style.position = 'relative';
			footerElement.style.top = (windowHeight - (contentHeight + footerHeight)) + 'px';
		}
		else {
			footerElement.style.position = 'static';
		}
	}
}




$(document).ready(function(){
	
	$('img[name$=photoannon][src$=.png]').ifixpng();
		
	$("#footer").removeClass().addClass('footer_class');
	//setFooter();
	
	$('select').change(function(){
		$("#footer").removeClass().addClass('footer_class');
	});

});

window.onresize = function() {
	//setFooter();
	$("#footer").removeClass().addClass('footer_class');
}


function legal(theURL,winName,features) {
  window.open(theURL,winName,features);
}



sfHover = function() {
	var sfEls = document.getElementById("mainmenu").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);


function EcrireCookie(nom, valeur){
	var argv=EcrireCookie.arguments;
	var argc=EcrireCookie.arguments.length;
	var expires=(argc > 2) ? argv[2] : null;
	var path=(argc > 3) ? argv[3] : null;
	var domain=(argc > 4) ? argv[4] : null;
	var secure=(argc > 5) ? argv[5] : false;
	document.cookie=nom+"="+escape(valeur)+
	((expires==null) ? "" : ("; expires="+expires.toGMTString()))+
	((path==null) ? "" : ("; path="+path))+
	((domain==null) ? "" : ("; domain="+domain))+
	((secure==true) ? "; secure" : "");
}

function getCookieVal(offset){
	var endstr=document.cookie.indexOf (";", offset);
	if (endstr==-1) endstr=document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}

function LireCookie(nom){
	var arg=nom+"=";
	var alen=arg.length;
	var clen=document.cookie.length;
	var i=0;
	while (i<clen){
		var j=i+alen;
		if (document.cookie.substring(i, j)==arg) return getCookieVal(j);
		i=document.cookie.indexOf(" ",i)+1;
		if (i==0) break;
	}
	return null;
}


$(document).ready(function(){
	
	$("#reg_promos").click(function(){
		$("#promos-display").fadeIn(1000);
		$("#promosdisplay_form").fadeIn(1000);
		$("#promos-display").animate( { top:"120px" }, 1500 );
		return false;
	});
	
	$(".reg_promos").click(function(){
		$("#promos-display").fadeIn(1000);
		$("#promosdisplay_form").fadeIn(1000);
		$("#promos-display").animate( { top:"120px" }, 1500 );
		return false;
	});
	
	var_cookie = LireCookie("Promos");
    if (var_cookie == null) {
		$("#promos-display").animate( { top:"120px" }, 1500 );
	}
	// $("#promos-display").animate( { top:"120px" }, 1500 );
	
	//alert(var_cookie);
	
	$("#promosdisplay_submit").click(function(){					   				   
		$(".error").hide();
		var hasError = false;
		var idclientVal = $("#promosdisplay_idclient").val();
		var langVal = $("#promosdisplay_lang").val();
		
		var gsmVal = $("#promosdisplay_gsm").val();
		var exp = new RegExp("^[0-9|\.|/|\+]*$");
		
		if((gsmVal == '') || (exp.test(gsmVal) == false)) {
			if (langVal == 'fr') {
				$("#promosdisplay_gsm").after("<span class=\"error\">*</span>");
			} else {
				$("#promosdisplay_gsm").after("<span class=\"error\">*</span>");
			}
			hasError = true;
		}
		
		var emailVal = $("#promosdisplay_email").val();
		var exp = new RegExp("^([a-zA-Z0-9]+(([\.\-\_]?[a-zA-Z0-9]+)+)?)\@(([a-zA-Z0-9]+[\.\-\_])+[a-zA-Z]{2,4})$")
		if((emailVal == '') || (exp.test(emailVal) == false)) {
			if (langVal == 'fr') {
				$("#promosdisplay_email").after("<span class=\"error\">*</span>");
			} else {
				$("#promosdisplay_email").after("<span class=\"error\">*</span>");
			}
			hasError = true;
		}
		
		var provinceVal = $("#promosdisplay_province").val();
		if(provinceVal == '0') {
			if (langVal == 'fr') {
				$("#promosdisplay_province").after("<span class=\"error\">*</span>");
			} else {
				$("#promosdisplay_province").after("<span class=\"error\">*</span>");
			}
			hasError = true;
		}
		
		if(hasError == false) {
			// $(this).hide();
			
			// $("#promosdisplay_form").append('<div align="center"><img src="pictures/loading.gif" alt="Loading" id="loading" /></div>');
			
			$.post("ajax/sendpromosdisplay.php",
   				{ gsm: gsmVal, email: emailVal, province: provinceVal, idclient: idclientVal },
   					function(data){
						$("#promosdisplay_form").slideUp("normal", function() {
							$("#promos-display").fadeOut(2000);
						});
   					}
				 );
		}
		
		return false;
	});
	
	$("#promosdisplayno").click(function(){	
		$("#promos-display").fadeOut(1000);
		return false;
	});
	
	duree_cookie = 3650;         // durée de vie du cookie en jours
    expiration = new Date();    // date et heure courante en format texte
    expiration.setTime(expiration.getTime() + (duree_cookie * 24*60*60*1000)); // => on peut utiliser la variable "expiration"
	EcrireCookie("Promos", "ok", expiration);
	
});


/* Permettre l'affichage des sous-menus sous IE6 */
sfHover = function() {
	var sfEls = document.getElementById("menu").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}

if (window.attachEvent) {
	window.attachEvent("onload", sfHover);
}


// Afficher une popup centrée
function popupcentree(page,largeur,hauteur,optionss) {
 var top = (screen.height-hauteur)/2;
 var left = (screen.width-largeur)/2;
 window.open(page,"","top="+top+",left="+left+",width="+largeur+",height="+hauteur+","+optionss);
}








// Fonction permettant de faire défiler les photos des annonces à la une
function au_diaporama() {
	photocount = 0;
	mynbrphotos = $("input[@name=au_nbrphotos]").val();
	if (mynbrphotos > 1) {	
		$('#au_mainphoto').everyTime(9000,function() {
			photocount = photocount + 1;
			// Changer la photo
			$(this).fadeOut("slow", function () {
				$(this).attr({ 
					src: $("#au_miniphotohide_" + photocount).attr("src")
					
				});
				$(this).fadeIn("slow");
			});
			// Changer la rubrique
			$("#annoncesune_txt h3").fadeOut("slow", function () {
				$(this).html($("li#au_photoselect h4").html());
				$(this).fadeIn("slow");
			});
			// Changer le titre
			$("#annoncesune_txt h4").fadeOut("slow", function () {
				$(this).html($("li#au_photoselect h5").html());
				$(this).fadeIn("slow");
			});
			// Changer la description
			$("#annoncesune_annonce p").fadeOut("slow", function () {
				$(this).html($("li#au_photoselect p").html());
				$(this).fadeIn("slow");
			});
			if (photocount == mynbrphotos ) {
				photocount = 0;
			}
			$("li#au_photoselect").removeAttr('id');
			$("#annoncesune_liste ul li:eq(" + photocount + ")").attr("id","au_photoselect");
		});
	}
}






$(document).ready(function() {

	/********************
	* Annonces à la une *
	********************/
	photocount = 0;
	au_diaporama();
		
	// Effet quand on passe sur les vignettes des photos
	$('[@title^=vign_annonceune_]').mouseover(function(){
		_idselected = $(this).attr("title");
		_strlength = _idselected.length;
		_idselected = _idselected.substring(16, _strlength);	
		$('#au_mainphoto').stopTime();
		$('#au_mainphoto').attr({ 
        	src: $("#au_miniphotohide_" + _idselected).attr("src")
        });
        // Changer la rubrique
		$("#annoncesune_txt h3").html($("li[title='vign_annonceune_" + _idselected + "'] h4").html());
		// Changer le titre
		$("#annoncesune_txt h4").html($("li[title='vign_annonceune_" + _idselected + "'] h5").html());
		// Changer la description
		$("#annoncesune_annonce p").html($("li[title='vign_annonceune_" + _idselected + "'] p").html());
		// Changer le lien
		$("#au_mainlink").attr({
			href: $("li[title='vign_annonceune_" + _idselected + "'] h5 a").attr("href")
		});
		// Enlever la classe d'hover à celui qui l'a
		$('li#au_photoselect').removeAttr("id");
		// Ajouter la classe d'hover à celui qui doit l'avoir
		$(this).attr("id","au_photoselect");
	});

	$('[@title^=vign_annonceune_]').mouseout(function(){
		au_diaporama();
	});	

	$('#au_mainphoto').mouseover(function(){
		$('#au_mainphoto').stopTime();
	});

	$('#au_mainphoto').mouseout(function(){
		au_diaporama();
	});


});





