/* déclaration des variables */
var myAJAXContainerCount = 0,mySearchContainer,myContactContainer,myLastPost,myLastSearch;

/* Déclaration des fonctions */
function refreshList(idList,params)
{
	var url = '/listeSousCategorie.ajax';
	var pars = params;
	
	var myAjax = new Ajax.Updater(idList, url, {method: 'post', parameters: pars}); 
	
	return true;
}
function showDetail(bienID)
{
	
}
function viewPicturePrevious()
{
	var nombreTotal = document.getElementById("photoVisionneuseTotal").value;
	var photoActive = document.getElementById("photoVisionneuseActive").value;
	photoActive--;
	photoActive = (photoActive<1)?nombreTotal:photoActive;
	var sourcePhoto = document.getElementById("photoVisionneuse"+photoActive).value;
	document.getElementById("photoVisionneuse").src = sourcePhoto;
	document.getElementById("photoVisionneuseActive").value = photoActive;
	return false;
}
function viewPictureNext()
{
	var nombreTotal = document.getElementById("photoVisionneuseTotal").value;
	var photoActive = document.getElementById("photoVisionneuseActive").value;
	photoActive++;
	photoActive = (photoActive>nombreTotal)?1:photoActive;
	var sourcePhoto = document.getElementById("photoVisionneuse"+photoActive).value;
	document.getElementById("photoVisionneuse").src = sourcePhoto;
	document.getElementById("photoVisionneuseActive").value = photoActive;
	return false;
}
// FROM lightbox.js - LightBox V2
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
function getMyPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}
function myAJAXContainer(myHeight,myWidth)
{
	var myActiveContainer = this;
	this.myID = 'myContainerNo'+(myAJAXContainerCount+1);
	this.myPageSize = getMyPageSize();
	this.myMask = document.createElement("DIV");
	with(this.myMask)
	{
		className = 'myMaskStyle';
		with (style)
		{
			top = 0;
			left = 0;
			display = 'none';
			zIndex = 50+(20*myAJAXContainerCount);
			height = this.myPageSize[1]+'px';
		}
	}
	this.myMaskAnchor = document.createElement("A");
	with(this.myMaskAnchor)
	{
		name = 'hautDePageLB';
	}
	this.myMask.appendChild(this.myMaskAnchor);
	this.myContainer = document.createElement("DIV");
	with(this.myContainer)
	{
		className = 'myContainerStyle';
		with (style)
		{
			top = Math.ceil((100-myHeight)/2)+'%';
			left = Math.ceil((this.myPageSize[2]-myWidth)/2)+'px';
			display = 'none';
			zIndex = 50+(20*myAJAXContainerCount)+10;
			height = Math.ceil((myHeight/100)*this.myPageSize[3])+'px';
			width = myWidth+'px';
			backgroundColor = 'transparent';
		}
	}
	this.myContainerControl = document.createElement("DIV");
	with(this.myContainerControl)
	{
		className = 'myContainerControlStyle';
		with (style)
		{
			margin = '0';
			borderBottom = '1px solid Black';
			padding = '0';
			backgroundColor = 'White';
			textAlign = 'right';
		}
	}
	this.myContainerControlAnchor = document.createElement("A");
	with(this.myContainerControlAnchor)
	{
		href = '#';
		title = 'Fermer cette fenêtre';
	}
	this.myContainerControlAnchor.onclick = function () { return myActiveContainer.hide(); };
	this.myContainerControlImage = document.createElement("IMG");
	with(this.myContainerControlImage)
	{
		src = '/image/closelabel.gif';
		title = 'Fermer cette fenêtre';
		alt = 'Fermer';
		border = 0;
	}
	this.myContainerControlAnchor.appendChild(this.myContainerControlImage);
	this.myContainerControl.appendChild(this.myContainerControlAnchor);
	this.myContainer.appendChild(this.myContainerControl);
	
	this.myContainerInner = document.createElement("DIV");
	with(this.myContainerInner)
	{
		id = this.myID;
		className = 'myContainerInnerStyle';
		with (style)
		{
			margin = 0;
			border = 0;
			padding = '10px';
			height = (Math.ceil((myHeight/100)*this.myPageSize[3])-23-20)+'px';
			width = (myWidth-20)+'px';
			overflow = 'auto';
			backgroundColor = 'White';
		}
	}
	this.myContainer.appendChild(this.myContainerInner);
	this.myBodyElement = document.getElementsByTagName('BODY')[0];
	this.myBodyElement.appendChild(this.myMask);
	this.myBodyElement.appendChild(this.myContainer);
	myAJAXContainerCount++;
}
myAJAXContainer.prototype.show = function()
{
	this.myMask.style.display = 'block';
	window.scrollTo(0,0);
	//location.href = '#hautDePageLB';
	this.myContainer.style.display = 'block';
	return false;
}
myAJAXContainer.prototype.hide = function()
{
	this.myContainer.style.display = 'none';
	this.myMask.style.display = 'none';
	return false;
}
myAJAXContainer.prototype.load = function(myURL,myParams,myForm,myMethod)
{
	var myActiveContainer = this;
	this.hide();
	if (myForm!=undefined&&myForm!=false&&myForm!=0&&myForm!='')
	{
		/* On récupère les différents éléments du formulaire qui vont constituer les paramêtres */
		var myFormElements = myForm.elements,myParameters = '',activFormElement;
		for (var i=0;i<myFormElements.length;i++)
		{
			activFormElement = myFormElements[i];
			/* Si l'élément est une liste ou bien une entrée texte ou cachée */
			if ((activFormElement.type == 'text' || activFormElement.type == 'hidden' || activFormElement.type == 'select-one' || activFormElement.type == 'select-multiple' || ((activFormElement.type == 'checkbox' || activFormElement.type == 'radio') && activFormElement.checked))&&activFormElement.name!=undefined&&activFormElement.value!=undefined)
			{
				myParameters +=	(myParameters=='')
									?
									activFormElement.name+'='+activFormElement.value
									:
									'&'+activFormElement.name+'='+activFormElement.value
									;
			}
		}
		if (myURL=='/rechercher.ajax'||myURL=='rechercher.ajax')
			myLastSearch = myParameters;
		myLastPosted = myParameters;
	}
	else
		myParameters = '';
		
	myParams = (myParameters!=''&&myParams!=''&&myParams!=undefined)?myParams+'&'+myParameters:myParams+myParameters;
	myMethod = (myMethod!='post'&&myMethod!='get')?'post':myMethod;
	var myAJAXConnector = new Ajax.Updater(this.myID, myURL, {method: myMethod, parameters: myParams, evalScripts: true, onSuccess: myActiveContainer.show()});
	return false;
}
function myPageInit()
{
	/* Chargement du HUB CSS regroupant tous les CSS util au contenu chargé en AJAX */
	var documentHead = document.getElementsByTagName('HEAD')[0];
	var hubCSSAJAX = document.createElement('LINK');
	with(hubCSSAJAX)
	{
		rel = 'stylesheet';
		type = 'text/css';
		href = '/css/style-hubCSSAJAX.css';
	}
	documentHead.appendChild(hubCSSAJAX);
	
	/* mise en place des contenant AJAX */
	mySearchContainer = new myAJAXContainer(80,600);
	myContactContainer = new myAJAXContainer(60,500);
}
function searchWaitAlert(buttonToHide)
{
	var formToSubmit = buttonToHide.form;
	var buttonParentNode = buttonToHide.parentNode;
	
	var zoneWaitAlert = document.createElement('P');
	zoneWaitAlert.innerHTML = 'Nous recherchons les biens correspondant à votre recherche, veuillez patienter...';
	
	buttonParentNode.appendChild(zoneWaitAlert);
	buttonToHide.style.display = 'none';
	
	return true;
}
var plusCritere_etat;
function plusCritere()
{
	var element_liste,element_liste_nombre,element_encart;
	element_liste	=	$$('.plusCritere_invisible');
	element_liste_nombre	=	element_liste.length;
	element_encart	=	document.getElementById('encart_plusCritère');
	
	switch (plusCritere_etat)
	{
		case '+':
			for (i=0;i<element_liste_nombre;i++)
			{
				element_liste[i].hide();
			}
			plusCritere_etat	=	'-';
			element_encart.innerHTML	=	'+ Plus de critères';
			break;
		case '-':
			for (i=0;i<element_liste_nombre;i++)
			{
				element_liste[i].show();
			}
			plusCritere_etat	=	'+';
			element_encart.innerHTML	=	'- Moins de critères';
			break;
	}
	
	return false;
}