// JavaScript Document

function id(a)
{
	return document.getElementById(a);
}

// Plan d'accès

// Fondu-enchaîné des deux plans
var timer1, timer2;
function fadein(n)
{
	var img = id('img-plan' + n);
	img.style.opacity = (1 - (id('img-plan' + (img == id('img-plan1') ? 2 : 1)).style.opacity));
	img.style.filter = 'alpha(opacity=' + (img.style.opacity * 100) + ')';
	if(img.style.opacity >= 1)
	{
		clearInterval(timer1);
		img.style.opacity = 1;

	}
}

function fadeout(n)
{
	var img = id('img-plan' + n);
	img.style.opacity -= 0.025;
	img.style.filter = 'alpha(opacity=' + (img.style.opacity * 100) + ')';
	if(img.style.opacity <= 0)
	{
		clearInterval(timer2);
		img.style.opacity = 0;
	}
}

function swplan()
{ //alert(id('img-plan1').style.opacity + ' : ' + id('img-plan2').style.opacity);
	if(id('img-plan1').style.opacity == 0 || id('img-plan1').style.opacity == 1)
	{
		timer1 = setInterval('fadein(' + (id('img-plan1').style.opacity < 0.5 ? 1 : 2) + ')', 30);
		timer2 = setInterval('fadeout(' + (id('img-plan1').style.opacity < 0.5 ? 2 : 1) + ')', 30);
	}
}

function vmail(email) 
{
	var reg = /^[a-z0-9._-]+@[a-z0-9.-]{2,}[.][a-z]{2,4}$/
	return (reg.exec(email) != null)
}

function contactverifmail(a)
{
	a.value = a.value.toLowerCase();
	if(!vmail(a.value))
	{
		a.style.color = "white";
		a.style.background = 'red';
	}
	else
	{
		a.style.color = "black";
		a.style.background = 'white';

		id('contact-submit').disabled = false;
	}
}

function rq(){
    var rq = null; 
	if(window.XMLHttpRequest) // Firefox et autres
		rq = new XMLHttpRequest(); 
	else if(window.ActiveXObject){ // Internet Explorer
	   try {
			rq = new ActiveXObject("Msxml2.XMLHTTP");
	    } catch (e) {
			rq = new ActiveXObject("Microsoft.XMLHTTP");
		}
	} else { // XMLHttpRequest non supporté par le navigateur 
		alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest..."); 
		rq = false; 
	} 
    return rq;
}

//Rotation des photos sur pages "Ventes" et "Nos Belles Demeures"
function rollphoto(n)
{
	if(id('photo' + n).src)
	{
		var photo = id('photo' + n).src;
		var r = rq();
		r.open('GET', 'ventes.php?action=rollphoto&n=' + n + '&photo=' + encodeURI(photo), false);
		r.send(null);
		if(r.readyState == 4 && r.status == 200)
		{
			var s = r.responseText.split('*');
			if(s[0])
				id('photo' + n).src = s[0];
		}
		r.abort();
	}
	else
		alert('Erreur lors du chargement de la photo.');
}

// Grande photo en pop-up pour les pages "Ventes" et "Nos Belles Demeures"
function grandephoto(img)
{
	img = img.substr(0, img.lastIndexOf('/') + 1) + img.substr(img.lastIndexOf('/') + 2);
	var d = window.open('', '_blank', 'toolbar=0,location=0,directories=0,menuBar=0,scrollbars=0,resizable=1');
	d.document.open();
	d.document.write("<html><head><title>Agence des Picantins - Compi&egrave;gne</title></head><body style='margin:0px;padding:0px;text-align:center;'><img src='" + img + "' onload='window.resizeTo(this.width + 10, this.height + 30)' /></body></html>");
	d.document.close();
}

// Scroll pour la description des annonces
function setscroll(i, pas)
{
	i = id(i);
	 	i.scrollTop += pas;
}

function upscroll(i)
{
	timer = setInterval('setscroll("'+i+'", -1)', 3);
}

function downscroll(i)
{ 
	timer = setInterval('setscroll("'+i+'", 1)', 3);
}

function stopscroll()
{
	if(timer)	
		clearInterval(timer);
}

function getCN(t)
{
	var c = document.getElementsByTagName("*");
	if(!c.length && document.all)
		c.document.all;

	var res = [], re = new RegExp("(^| )" + t + "( |$)");
	
	for(var i = 0, el; (el = c[i]); ++i)
	{ 
		if(el.className && re.test(el.className))
		{	
			res[res.length] = el;
		} // fin if
	} // fin for

	return res; 
} // fin function getElementsByClassName(t)

window.onload = function()
{
	var scrolls = getCN('div-scroll-annonces');
	var upscrolls = getCN('upscroll');
	var downscrolls = getCN('downscroll');
	for(var i = 0; i < scrolls.length; i++)
	{
		if(scrolls[i].offsetHeight < scrolls[i].childNodes[0].offsetHeight)
		{ 
			upscrolls[i].style.display = 'inline';	
			downscrolls[i].style.display = 'inline';	
		}
	}

	if(id('decouvrir-scroll') && id('decouvrir-scroll-intra'))
	{
		if(id('decouvrir-scroll').offsetHeight < id('decouvrir-scroll-intra').offsetHeight)
			id('decouvrir-scroll-fleches').style.display = 'inline';
	}
}
