/*
 * THIS IS FREE SCRIPT BUT LEAVE THIS COMMENT IF
 * YOU WANT USE THIS CODE ON YOUR SITE
 * 
 * Made by Wilq32, wilq32@gmail.com, Wroclaw, Poland, 01.2009
 * 
 */
/*
Description:

This is an final product of a Wilq32.PhotoEffect Snippet. Actually you can
 use this simple and tiny script to get effect of rotated images directly 
 from client side (for ex. user generated content), and animate them using
 own functions. 


Notices:

Include script after including main jQuery. Whole plugin uses jQuery
namespace and should be compatible with older version (unchecked). 
To use it in IE you need those two lines to be added after <body> tag:

 

<!-- Include the VML behavior -->
<style>v\: * { behavior:url(#default#VML); display:inline-block }</style>
<!-- Declare the VML namespace -->
<xml:namespace ns="urn:schemas-microsoft-com:vml" prefix="v" />
 

If someone know working workaround for doing this only in javascript please
let me know. Right now i tried using createStyleSheets but IE doesnt recognise
 "v\: *". Adding namespace with document.namespace.add is also messy. If
 someone know this problem and a solution just please let me know.




Usage:

jQuery(imgElement).rotate(angleValue)
jQuery(imgElement).rotate(parameters)
jQuery(imgElement).rotateAnimation(parameters)
jQuery(imgElement).rotateAnimation(parameters)



Returns:

jQueryRotateElement - !!! NOTICE !!! function return rotateElement
instance to help connect events with actually created 'rotation' element.

Parameters:

    ({angle:angleValue,
     [animateAngle:animateAngleValue],
     [maxAngle:maxAngleValue],
     [minAngle:minAngleValue],
     [callback:callbackFunction],
     [bind:[{event: function},{event:function} ] })
jQuery(imgElement).rotateAnimation

Where:

- angleValue - clockwise rotation given in degrees,
- [animateAngleValue] - optional parameter, animate rotating into this value,
- [maxAngleValue] - optional parameter, maximum angle possible for animation,
- [minAngleValue] - optional parameter, minimum angle possible for animation,
- [callbackFunction] - optional function to run after animation is done
- [bind: [ {event: function}...] -optional parameter, list of events binded
  to newly created rotateable object

Examples:

		$(document).ready(function()
		{
			$('#image').rotate(-25);			
		});

		$(document).ready(function()
		{
			$('#image2').rotate({angle:5});	
		});

		$(document).ready(function()
		{
			var rot=$('#image3').rotate({maxAngle:25,minAngle:-55,
			bind:
				[
					{"mouseover":function(){rot[0].rotateAnimation(85);}},
					{"mouseout":function(){rot[0].rotateAnimation(-35);}}
				]
			});
		});
*/


jQuery.fn.extend({
ImageRotate:function(parameters)
{	
	if (this.Wilq32&&this.Wilq32.PhotoEffect) return;
	return (new Wilq32.PhotoEffect(this.get(0),parameters))._temp;
},
rotate:function(parameters)
{
	if (this.length===0) return;
	if (typeof parameters=="undefined") return;
	if (typeof parameters=="number") parameters={angle:parameters};
	var returned=[];
	for (var i=0,i0=this.length;i<i0;i++)
	{
	    var element=this.get(i);	
		if (typeof element.Wilq32 == "undefined") 
			returned.push($($(element).ImageRotate(parameters)));
		else 
		{
			element.Wilq32.PhotoEffect._rotate(parameters.angle);
		}
	}
	return returned;
},

rotateAnimation:function(parameters)
{
	if (this.length===0) return;
	if (typeof parameters=="undefined") return;
	if (typeof parameters=="number") parameters={angle:parameters};
	var returned=[];
	for (var i=0,i0=this.length;i<i0;i++)
	{	
	    var element=this.get(i);
		if (typeof element.Wilq32 == "undefined") 
			returned.push($($(element).ImageRotate(parameters)));
		else 
		{
			element.Wilq32.PhotoEffect._parameters.animateAngle = parameters.angle;
			element.Wilq32.PhotoEffect._parameters.callback = parameters.callback ||
			function()
			{
			};
			element.Wilq32.PhotoEffect._animateStart();
		}
	}
	return returned;
}

});

Wilq32={};

Wilq32.PhotoEffect=function(img,parameters)
{
			this._IEfix=img;
			this._parameters=parameters;
			this._parameters.className=img.className;
			this._parameters.id=img.getAttribute('id');
			
			if (!parameters) this._parameters={};
			this._angle=0;
			if (!parameters.angle) this._parameters.angle=0;
			this._temp=document.createElement('span');
			this._temp.Wilq32 = 
				{
					PhotoEffect: this
				};			
			var image=img.src;
			img.parentNode.insertBefore(this._temp,img);
			this._img= new Image();
			this._img.src=image;
			this._img._ref=this;
			jQuery(this._img).bind("load", function()
			{
				this._ref._Loader.call(this._ref);
			});
			if (jQuery.browser.msie) if (this._img.complete) this._Loader();
}

Wilq32.PhotoEffect.prototype._Loader=
(function()
{
	if (jQuery.browser.msie)
	return function()
	{
		var src=this._IEfix.src;
		this._IEfix.parentNode.removeChild(this._IEfix);
		this._temp.setAttribute('id',this._parameters.id);
		this._temp.className=this._parameters.className;
		var width=this._img.width;
		var height=this._img.height;
		
		this._img._widthMax=this._img._heightMax=Math.sqrt((height)*(height) + (width) * (width));
		this._img._heightMax=Math.sqrt((height)*(height) + (width) * (width));
		
		this._vimage = document.createElement('v:image');
		this._vimage._ref=this;
		this._vimage.style.height=height;
		this._vimage.style.width=width;
		this._vimage.style.position="relative";
		this._temp.style.display="inline-block";
		this._temp.style.width=this._temp.style.height=this._img._heightMax;
		this._vimage.src=src;
		this._vimage.rotate=0;
		this._temp.appendChild(this._vimage);
		
		var self = this;
		this._parameters.animateAngle=0;
		if (this._parameters.bind) 
		{
			for (var a in this._parameters.bind) if (this._parameters.bind.hasOwnProperty(a)) 
			for (var b in this._parameters.bind[a]) if (this._parameters.bind[a].hasOwnProperty(b)) 
			jQuery(this._temp).bind(b,this._parameters.bind[a][b]);
		}
		this._rotate(this._parameters.angle);		
		
	}
	else
	return function ()
	{
		this._IEfix.parentNode.removeChild(this._IEfix);
		this._temp.setAttribute('id',this._parameters.id);
		this._temp.className=this._parameters.className;
		var width=this._img.width;
		var height=this._img.height;
		
		this._img._widthMax=this._img._heightMax=Math.sqrt((height)*(height) + (width) * (width));

		this._canvas=document.createElement('canvas');
		this._canvas._ref=this;
		this._canvas.height=height;
		this._canvas.width=width;

		this._canvas.setAttribute('width',width);

		this._temp.appendChild(this._canvas);

		var self = this;
		this._parameters.animateAngle=0;
		if (this._parameters.bind) 
		{
			for (var a in this._parameters.bind) if (this._parameters.bind.hasOwnProperty(a)) 
			for (var b in this._parameters.bind[a]) if (this._parameters.bind[a].hasOwnProperty(b)) 
			jQuery(this._canvas).bind(b,this._parameters.bind[a][b]);
		}
		this._cnv=this._canvas.getContext('2d');
		this._rotate(this._parameters.angle);
	}

})();

Wilq32.PhotoEffect.prototype._animateStart=function()
{	
	if (this._timer) clearTimeout(this._timer);
	this._animate();
}
Wilq32.PhotoEffect.prototype._animate=function()
{	
    var temp=this._angle;
	if (typeof this._parameters.animateAngle!="undefined") this._angle-=(this._angle-this._parameters.animateAngle)*0.1;
	if (typeof this._parameters.minAngle!="undefined") if (this._angle<this._parameters.minAngle) this._angle=this._parameters.minAngle;
	if (typeof this._parameters.maxAngle!="undefined") if (this._angle>this._parameters.maxAngle) this._angle=this._parameters.maxAngle; 

	if (Math.round(this._angle * 100 - temp * 100) == 0 && this._timer) 
		{
			clearTimeout(this._timer);
			if (this._parameters.callback) 
				this._parameters.callback();
		}
		else 
		{
			this._rotate(this._angle);
			var self = this;
			this._timer = setTimeout(function()
			{
				self._animate.call(self);
			}, 10);
		}
}

Wilq32.PhotoEffect.prototype._rotate = (function()
{
	if (jQuery.browser.msie)
	return function(angle)
	{
		this._vimage.style.rotation=angle;
		var radians=angle*Math.PI/180;
		this._vimage.style.top=  (this._img._heightMax - this._img.height)/2- (this._vimage.offsetHeight-this._img.height)/2 +"px";
		this._vimage.style.left= (this._img._widthMax - this._img.width)/2- (this._vimage.offsetWidth-this._img.width)/2 +"px";
	}
	else
	return function(angle)

	{

		if (!this._img.width) return;
		if (typeof angle!="number") return;
		angle=(angle%360)* Math.PI / 180;
		var width=this._img.width;
		var height=this._img.height;
		var widthAdd = this._img._widthMax - width;
		var heightAdd = this._img._heightMax - height;
		// clear canvas	
		this._canvas.width = width+widthAdd;
		this._canvas.height = height+heightAdd;

		//this._cnv.scale(0.8,0.8); // SCALE - if needed ;)
		
		// REMEMBER: all drawings are read from backwards.. so first function is translate, then rotate, then translate, translate..
		this._cnv.save();
		this._cnv.translate(widthAdd/2,heightAdd/2); // at least center image on screen
		this._cnv.translate(width/2,height/2);		  // we move image back to its orginal 
		this._cnv.rotate(angle);					  // rotate image
		this._cnv.translate(-width/2,-height/2);	  // move image to its center, so we can rotate around its center
		this._cnv.drawImage(this._img, 0, 0);		  // First - we draw image
		this._cnv.restore();
	}


})();

 // STILL DONT WORK ON IE8...
 /*
if (jQuery.browser.msie)
{
  document.createStyleSheet().addRule('v\\:image', "behavior: url (#default#VML);display:inline-block"); 
  document.namespaces.add('v', 'urn:schemas-microsoft-com:vml'); 
  if(document.documentMode && document.documentMode>=8) { 
	document.writeln('<?import namespace="v" implementation="#default#VML" ?>'); 
  }
}*/

function bookmarksite(title, url)
{
    if (document.all)
		window.external.AddFavorite(url, title);
    else if (window.sidebar)
		window.sidebar.addPanel(title, url, "");
}

function openPopup(url, titre, largeur, hauteur, scroll) {
	window.open(url, titre, 'width=' + largeur + ', height=' + hauteur + ', scrollbars=' + scroll);
}

function favoris(){
	browserName = navigator.appName;
	browserVer = parseInt(navigator.appVersion);
	if (browserName == "Microsoft Internet Explorer" & browserVer >= 4) {
		window.external.AddFavorite('http:webby.free.fr/', 'Webby.free.fr');
	}
}

function alerteEmail()
{
	if($('#emailValeur').val()!='')
	{
		if(verifierEMail($('#emailValeur').val())==false)
		{
			alert('Votre adresse e-mail n\'est pas correcte');
		}
		else
		{
			var parametreLien="";
			
			al=document.forms['formRecherche'].elements['al'].value;
			typeOffre=document.forms['formRecherche'].elements['to'].value;
			budgetMin=document.forms['formRecherche'].elements['budgetmin'].value;
			budgetMax=document.forms['formRecherche'].elements['budgetmax'].value;
			surfaceMin=document.forms['formRecherche'].elements['surfacemin'].value;
			surfaceMax=document.forms['formRecherche'].elements['surfacemax'].value;
			divers1=document.forms['formRecherche'].elements['divers1'].value;
			piece=document.forms['formRecherche'].elements['piece'].value;
			chambre=document.forms['formRecherche'].elements['chambre'].value;
			email=$('#emailValeur').val();					
			
			parametreLien=parametreLien+"email="+escape(email);

			if(al!="")
			{
				parametreLien=parametreLien+"&bien="+escape(al);
			}
			if(typeOffre!="")
			{
				parametreLien=parametreLien+"&type="+escape(typeOffre);
			}
			if(budgetMin!="")
			{
				parametreLien=parametreLien+"&bmin="+escape(budgetMin);
			}
			if(budgetMax!="")
			{
				parametreLien=parametreLien+"&bmax="+escape(budgetMax);
			}
			if(surfaceMin!="")
			{
				parametreLien=parametreLien+"&smin="+escape(surfaceMin);
			}
			if(surfaceMax!="")
			{
				parametreLien=parametreLien+"&smax="+escape(surfaceMax);
			}
			if(piece!="")
			{
				parametreLien=parametreLien+"&pi="+escape(piece);
			}
			if(chambre!="")
			{
				parametreLien=parametreLien+"&ch="+escape(chambre);
			}
			if(divers1!="")
			{
				parametreLien=parametreLien+"&divers1="+escape(divers1);
			}

			file('../phidias/stockage-email.php?'+parametreLien);

			alert('Votre demande a bien \351t\351 prise en compte');
		}
	}
}


function alerteEmailNoRes()
{
	if($('#emailValeurNoRes').val()!='')
	{
		if(verifierEMail($('#emailValeurNoRes').val())==false)
		{
			alert('Votre adresse e-mail n\'est pas correcte');
		}
		else
		{
			var parametreLien="";
			
			al=document.forms['formRechercheNoRes'].elements['alNoRes'].value;
			typeOffre=document.forms['formRechercheNoRes'].elements['toNoRes'].value;
			budgetMin=document.forms['formRechercheNoRes'].elements['budgetminNoRes'].value;
			budgetMax=document.forms['formRechercheNoRes'].elements['budgetmaxNoRes'].value;
			surfaceMin=document.forms['formRechercheNoRes'].elements['surfaceminNoRes'].value;
			surfaceMax=document.forms['formRechercheNoRes'].elements['surfacemaxNoRes'].value;
			divers1=document.forms['formRechercheNoRes'].elements['divers1NoRes'].value;
			piece=document.forms['formRechercheNoRes'].elements['pieceNoRes'].value;
			chambre=document.forms['formRechercheNoRes'].elements['chambreNoRes'].value;
			email=$('#emailValeurNoRes').val();
			
			parametreLien=parametreLien+"email="+escape(email);

			if(al!="")
			{
				parametreLien=parametreLien+"&bien="+escape(al);
			}
			if(typeOffre!="")
			{
				parametreLien=parametreLien+"&type="+escape(typeOffre);
			}
			if(budgetMin!="")
			{
				parametreLien=parametreLien+"&bmin="+escape(budgetMin);
			}
			if(budgetMax!="")
			{
				parametreLien=parametreLien+"&bmax="+escape(budgetMax);
			}
			if(surfaceMin!="")
			{
				parametreLien=parametreLien+"&smin="+escape(surfaceMin);
			}
			if(surfaceMax!="")
			{
				parametreLien=parametreLien+"&smax="+escape(surfaceMax);
			}
			if(piece!="")
			{
				parametreLien=parametreLien+"&pi="+escape(piece);
			}
			if(chambre!="")
			{
				parametreLien=parametreLien+"&ch="+escape(chambre);
			}
			if(divers1!="")
			{
				parametreLien=parametreLien+"&divers1="+escape(divers1);
			}
			
			file('../phidias/stockage-email.php?'+parametreLien);

			alert('Votre demande a bien \351t\351 prise en compte');
		}
	}
}


/*==================================================================================================
Fonction pour chager les pages de requêtes  (AJAX)
==================================================================================================*/
function file(fichier)
{
	if (window.XMLHttpRequest) // FIREFOX
	{
		requeteEmail=new XMLHttpRequest();
	}
	 else if (window.ActiveXObject) // IE
	{
		requeteEmail=new ActiveXObject("Microsoft.XMLHTTP");
	}
	else
	{
		return false;
	}

	requeteEmail.open("GET", fichier, false);
	requeteEmail.send(null);
	if(requeteEmail.readyState == 4)
	{
		return requeteEmail.responseText;
	}
	else
	{
		return false;
	}
}

function visibilite(thingId) {
	var targetElement;
	targetElement = document.getElementById(thingId) ;
	if (targetElement.style.display == "none" ) {
		targetElement.style.display = "" ;
	} else {
		targetElement.style.display = "none" ;
	}
}

function openPopup(url,name,w,h,o){
	window.open(url,name,'width='+w+',height='+h+',resizable=0,'+o);
}

function flash_fermer(){
	document.getElementById('pubflashtrans').style.display="none";
}

function activerCC(id){
	document.getElementById('popup'+id).style.display="block";
}

function desactiverCC(id){
	document.getElementById('popup'+id).style.display="none";
}

function correctPNG() // correctly handle PNG transparency in Win IE 5.5 or higher.
{
	var arVersion = navigator.appVersion.split("MSIE")
	var version = parseFloat(arVersion[1])

	if ((version<7) && (document.body.filters))
	{
		for(var i=0; i<document.images.length; i++)
		{
			var img = document.images[i]
			var imgName = img.src.toUpperCase()
			if (imgName.substring(imgName.length-3, imgName.length) == "PNG" )
			{
				var imgID = (img.id) ? "id='" + img.id + "' " : ""
				var imgClass = (img.className) ? "class='" + img.className + "' " : ""
				var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
				var imgStyle = "display:inline-block;" + img.style.cssText
				if (img.align == "left" ) imgStyle = "float:left;" + imgStyle
				if (img.align == "right" ) imgStyle = "float:right;" + imgStyle
				if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
				var strNewHTML = "<span " + imgID + imgClass + imgTitle
				+ " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
				+ "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
				+ "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>"
				img.outerHTML = strNewHTML
				i = i-1
			}
		}
	}
}

var arVersion = navigator.appVersion.split("MSIE")
var version = parseFloat(arVersion[1])

if ((version<7) && (document.body.filters))
{
	window.attachEvent("onload", correctPNG);
}

function getWindowWidth() 
{
    var w = 0;
    if (typeof(window.innerWidth) == 'number') { // Netscape
        w = window.innerWidth;
    } else if (document.documentElement && document.documentElement.clientWidth) {
        w = document.documentElement.clientWidth;
    } else if (document.body && document.body.offsetWidth) { //client
        w = document.body.offsetWidth;
    }
    return w;
}

function afficherFlashAccueil()
{	
	/*hauteurFenetre=getWindowHeight();
	
	alert(hauteurFenetre);*/
	
	if(document.getElementById('fondflashaccueil').style.visibility=='visible')
	{
		document.getElementById('fondflashaccueil').style.visibility='hidden';
	}
	else
	{
		document.getElementById('fondflashaccueil').style.visibility='visible';
		
		/*hauteurMap=document.getElementById('flashaccueil').offsetHeight;
		hauteurInt=hauteurFenetre-hauteurMap;
		mTop=hauteurInt/2;
		document.getElementById('flashaccueil').style.top=mTop+'px';
		*/
		
		//Pour IE6
		var arVersion = navigator.appVersion.split("MSIE")
		var version = parseFloat(arVersion[1])

		if (version==6)
		{
			largeurFenetre=getWindowWidth();
			placementLargeur=((largeurFenetre/2)-300);			
			document.getElementById('fondflashaccueil').style.width=largeurFenetre+'px';
			document.getElementById('flashaccueil').style.marginLeft=placementLargeur+'px';
			document.getElementById('fondflashaccueil').style.height='100%';
			document.getElementById('fondflashaccueil').style.paddingBottom='500px';
		}
		
	}	
}

function getWindowHeight() {
    /*var h = 0;
	if (typeof(window.innerHeight) == 'number') { // Netscape
        h = window.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) {
        h = document.documentElement.clientHeight;
    } else if (document.body && document.body.offsetHeight) { //client
        h = document.body.offsetHeight;
    }*/
	return new Array((document.documentElement && document.documentElement.scrollWidth) ? document.documentElement.scrollWidth : (document.body.scrollWidth > document.body.offsetWidth) ? document.body.scrollWidth : document.body.offsetWidth,(document.documentElement && document.documentElement.scrollHeight) ? document.documentElement.scrollHeight : (document.body.scrollHeight > document.body.offsetHeight) ? document.body.scrollHeight : document.body.offsetHeight);
}


function ajusterFondBlanc()
{
	var hauteur=getWindowHeight();
	hauteur=hauteur[1];
	document.getElementById('fondBlanc').style.height=hauteur+'px';
	document.getElementById('colonneGauche').style.height=(hauteur-180)+'px';
	document.getElementById('colonneDroite').style.height=(hauteur-180)+'px';
	document.getElementById('lightbox').style.height=hauteur+'px';
}

function ajusterFondBlancRepliage(hauteurSuppr)
{
	var hauteur=getWindowHeight();
	hauteur=hauteur[1];
	
	document.getElementById('fondBlanc').style.height=(hauteur-hauteurSuppr)+'px';
	document.getElementById('colonneGauche').style.height=(hauteur-180-hauteurSuppr)+'px';
	document.getElementById('colonneDroite').style.height=(hauteur-180-hauteurSuppr)+'px';
	document.getElementById('lightbox').style.height=hauteur+'px';
	
	ajusterFondBlanc()
}


$(function()
{
	$('.cgItemPt').click(function()
	{
		etat=$('.cgContenuCache', this).css('display');
		if(etat=='none')
		{
			$('.cgPuceBleu img', this).attr('src', '/images/puce-bleu-bas.png');
			hauteurDiv=$('.cgContenuCache', this).height();
			$('.cgContenuCache', this).css('height', '0');
			$('.cgContenuCache', this).animate({height:hauteurDiv}, 300, function()
			{
				ajusterFondBlanc();
			});
		}
		else
		{
			hauteurSuppr=$('.cgContenuCache', this).height();
			$('.cgPuceBleu img', this).attr('src', '/images/puce-bleu-inv.png');
			$('.cgContenuCache', this).hide(function()
			{				
				ajusterFondBlancRepliage(hauteurSuppr);
			});
		}
	});
	
	$('#choixType').click(function()
	{
		hauteurDiv=$('#listeType').height();
		etat=$('#choixTypeCentre').height();		
		if(etat==20)
		{
			$('.flecheGauche img', this).attr('src', '/images/ico-fleche-bas.png');
			//$('#choixTypeItem').animate({height:200}, 300);
			$('#choixTypeCentre').animate({height:(hauteurDiv+45)}, 300);
		}
		else
		{
			$('.flecheGauche img', this).attr('src', '/images/ico-fleche-gauche.png');
			//$('#choixTypeItem').animate({height:22}, 300);
			$('#choixTypeCentre').animate({height:20}, 300, function()
			{
				$('#choixTypeCentre').css('overflow', 'hidden');
			});			
			
		}
	});	
	
	$('.listeOptionType').click(function()
	{
		$('#choixType').html('<div class="flecheGauche"><img src="/images/ico-fleche-gauche.png" alt="" /></div>'+$(this).text());
		$('#choixTypeCentre').animate({height:20}, 300, function()
		{
			$('#choixTypeCentre').css('overflow', 'hidden');
		});
		$('#formRechercheTo').val($(this).text());
	});
	
	$('#villeValeur').keyup(function()
	{
		$('.flecheGauche2 img').attr('src', '/images/ico-fleche-bas.png');
		//valVille=$('#villeValeur').val();
		valVille='';
		$('#formRechercheDivers1').val($('#villeValeur').val());
		$.ajax({
				type: "POST",
				url: "spip.php?page=ajax-liste-ville",
				data:"valVille="+valVille,
				success: function(msg){
					$("#listeVille").html(msg);
					hauteurDiv=($('#listeVille').height());
					if(hauteurDiv==0)
					{
						$("#choixVilleCentre").css('height', 20);
						$("#choixVilleCentre").css('overflow', 'hidden');
					}
					else
					{
						$("#choixVilleCentre").css('height', (hauteurDiv+20));
					}
				}
		});
	});
	
	/*$('#leChoixVille').click(function()
	{
		$('.flecheGauche2 img', this).attr('src', '/images/ico-fleche-bas.png');
		valVille=$('#villeValeur').val();
		$('#formRechercheDivers1').val($('#villeValeur').val());
		$.ajax({
				type: "POST",
				url: "spip.php?page=ajax-liste-ville",
				data:"valVille="+valVille,
				success: function(msg){
					$("#listeVille").html(msg);
					hauteurDiv=($('#listeVille').height());
					if(hauteurDiv==0)
					{
						$("#choixVilleCentre").css('height', 20);
						$("#choixVilleCentre").css('overflow', 'hidden');
					}
					else
					{
						$("#choixVilleCentre").css('height', (hauteurDiv+20));
					}
				}
		});
	});*/
	
	$('#clickVille').click(function()
	{
		var etat=$('#choixVilleCentre').height();
		if(etat<=20)
		{
			$('.flecheGauche2 img').attr('src', '/images/ico-fleche-bas.png');
			valVille=$('#villeValeur').val();
			if(valVille=='choisissez la ville')
			{
				valVille='';
			}
			$.ajax({
					type: "POST",
					url: "spip.php?page=ajax-liste-ville",
					data:"valVille="+valVille,
					success: function(msg){
						$("#listeVille").html(msg);
						hauteurDiv=($('#listeVille').height());
						if(hauteurDiv==0)
						{
							$("#choixVilleCentre").css('height', 20);
							$("#choixVilleCentre").css('overflow', 'hidden');
						}
						else
						{
							$("#choixVilleCentre").css('height', (hauteurDiv+20));
						}
					}
			});
		}
		else
		{
			$('.flecheGauche2 img').attr('src', '/images/ico-fleche-gauche.png');
			$("#choixVilleCentre").css('height', 20);
			$("#choixVilleCentre").css('overflow', 'hidden');
		}
	});
	
	$('.flecheGauche2').click(function()
	{
		var etat=$('#choixVilleCentre').height();
		if(etat<=20)
		{
			$('.flecheGauche2 img').attr('src', '/images/ico-fleche-bas.png');
			valVille=$('#villeValeur').val();
			if(valVille=='choisissez la ville')
			{
				valVille='';
			}
			$.ajax({
					type: "POST",
					url: "spip.php?page=ajax-liste-ville",
					data:"valVille="+valVille,
					success: function(msg){
						$("#listeVille").html(msg);
						hauteurDiv=($('#listeVille').height());
						if(hauteurDiv==0)
						{
							$("#choixVilleCentre").css('height', 20);
							$("#choixVilleCentre").css('overflow', 'hidden');
						}
						else
						{
							$("#choixVilleCentre").css('height', (hauteurDiv+20));
						}
					}
			});
		}
		else
		{
			$('.flecheGauche2 img').attr('src', '/images/ico-fleche-gauche.png');
			$("#choixVilleCentre").css('height', 20);
			$("#choixVilleCentre").css('overflow', 'hidden');
		}
	});
	
	$('.afficherFormulaireContact').click(function()
	{
		var nasp=$(this).attr('id');
		var nbCarac=nasp.length;
		if(nbCarac==8)
		{
			nasp=nasp;
		}
		else
		{
			nasp='';
		}		
		
		$.ajax({
				type: "POST",
				url: "spip.php?page=ajax-formulaire",
				data: "nasp="+nasp,
				success: function(msg){
					$("#lightboxContenu").html(msg);
					$("#lightbox").show();
				}
		});
	});
	
	$('.afficherFormulaireTel').click(function()
	{
		$.ajax({
				type: "POST",
				url: "spip.php?page=ajax-telephone",
				success: function(msg){
					$("#lightboxContenu").html(msg);
					$("#lightbox").show();
				}
		});
	});
	
	$('.afficherFormulaireEstimation').click(function()
	{
		$.ajax({
				type: "POST",
				url: "spip.php?page=ajax-estimation",
				success: function(msg){
					$("#lightboxContenu").html(msg);
					$("#lightbox").show();
				}
		});
	});
	
	$('.afficherFormulaireAmi').click(function()
	{
		$.ajax({
				type: "POST",
				url: "spip.php?page=ajax-ami",
				data:"lurl="+$(this).attr('id'),
				success: function(msg){
					$("#lightboxContenu").html(msg);
					$("#lightbox").show();
				}
		});
	});
	
	$('.afficherCarte').click(function()
	{
		$.ajax({
			type: "POST",
			url: "spip.php?page=ajax-carte",
			success: function(msg){
				$("#lightboxContenu").html(msg);
				$("#lightbox").show();
				load();
			}
		});
	});	
	
	$('.afficherCalculette').click(function()
	{
		$.ajax({
			type: "POST",
			url: "spip.php?page=ajax-calculette",
			success: function(msg){
				$("#lightboxContenu").html(msg);
				$("#lightbox").show();
			}
		});
	});
	
	$('#lightboxInFermer').click(function()
	{
		$('#lightbox').hide();
	});
	
	$('.onglet').click(function()
	{
		$('.onglet').css('background-image', "url('/images/fond-onglet-vendeur.png')");
		$('.onglet').css('color', '#00234c');
		$('.onglet').css('font-weight', 'normal');
		$(this).css('background-image', 'none');
		$(this).css('background-color', '#19A8DD');
		$(this).css('color', '#FFFFFF');
		$(this).css('font-weight', 'bold');
		
		texteOnglet=$(this).text();		
		if(texteOnglet=='acheter')
		{
			if($('#formRechercheAl').val()!='acheter')
			{
				$('#formRechercheAl').val('acheter');
				min=parseInt(str_replace(' ', '', $('#sbmin').text()))*500;
				max=parseInt(str_replace(' ', '', $('#sbmax').text()))*500;
				minFormat=number_format(min, 0, ' ', ' ');
				maxFormat=number_format(max, 0, ' ', ' ');
				if(isNaN(max))
				{
					max=445000;
				}
				
				if(isNaN(min))
				{
					min=0;
				}
				
				if(max==445000)
				{
					maxImport='';
				}
				else
				{
					maxImport=max;
				}
				
				$('#formRechercheBudgetmin').val(min);
				$('#formRechercheBudgetmax').val(maxImport);
				
				if(min==0 && max!=445000)
				{
					$('#d1').html('jusqu\'\340 <span id="sbmax" style="color:#000000">'+maxFormat+' &euro;</span>');
				}
				else if(min!=0 && max==445000)
				{
					$('#d1').html('\340 partir de <span id="sbmin" style="color:#000000">'+minFormat+' &euro;</span>');			
				}
				else if(min==0 && max==445000)
				{
					document.getElementById('d1').innerHTML='Tous les budgets';
				}
				else
				{
					$('#d1').html('de <span id="sbmin" style="color:#000000">'+minFormat+' &euro;</span> \340 <span id="sbmax" style="color:#000000">'+maxFormat+' &euro;</span>');
				}
			}
		}
		else if(texteOnglet=='louer')
		{
			if($('#formRechercheAl').val()!='louer')
			{
				$('#formRechercheAl').val('louer');
				min=parseInt(str_replace(' ', '', $('#sbmin').text()))/500;
				max=parseInt(str_replace(' ', '', $('#sbmax').text()))/500;
				minFormat=number_format(min, 0, ' ', ' ');
				maxFormat=number_format(max, 0, ' ', ' ');
				if(isNaN(max))
				{
					max=1000;
				}
				
				if(isNaN(min))
				{
					min=0;
				}
				
				if(max==1000)
				{
					maxImport='';
				}
				else
				{
					maxImport=max;
				}
				
				$('#formRechercheBudgetmin').val(min);
				$('#formRechercheBudgetmax').val(maxImport);
				
				if(min==0 && max!=1000)
				{
					$('#d1').html('jusqu\'\340 <span id="sbmax" style="color:#000000">'+maxFormat+' &euro;</span>');
				}
				else if(min!=0 && max==1000)
				{
					$('#d1').html('\340 partir de <span id="sbmin" style="color:#000000">'+minFormat+' &euro;</span>');			
				}
				else if(min==0 && max==1000)
				{
					document.getElementById('d1').innerHTML='Tous les budgets';
				}
				else
				{
					$('#d1').html('de <span id="sbmin" style="color:#000000">'+minFormat+' &euro;</span> \340 <span id="sbmax" style="color:#000000">'+maxFormat+' &euro;</span>');
				}
			}
		}
		else
		{
			$('#formRechercheAl').val('acheter');
		}
	});
	
	$('.listeOptionType').mouseover(function()
	{
		$(this).css('background-image', 'url("/images/overlay.png")');
	});
	
	$('.listeOptionType').mouseout(function()
	{
		$(this).css('background-image', 'none');
	});
	
	$('.optionVille').mouseover(function()
	{
		$(this).css('background-image', 'url("/images/overlay.png")');
	});
	
	$('.optionVille').mouseout(function()
	{
		$(this).css('background-image', 'none');
	});
	
	$('#villeValeur').focus(function()
	{
		valeur=$(this).val();
		if(valeur=='choisissez la ville')
		{
			$(this).val('');
		}
		
		$('.flecheGauche2 img').attr('src', '/images/ico-fleche-bas.png');
		valVille=$('#villeValeur').val();
		$.ajax({
				type: "POST",
				url: "spip.php?page=ajax-liste-ville",
				data:"valVille="+valVille,
				success: function(msg){
					$("#listeVille").html(msg);
					hauteurDiv=($('#listeVille').height());
					if(hauteurDiv==0)
					{
						$("#choixVilleCentre").css('height', 20);
						$("#choixVilleCentre").css('overflow', 'hidden');
					}
					else
					{
						$("#choixVilleCentre").css('height', (hauteurDiv+20));
					}
				}
		});
		
	});	
	
	$('.optionPiece').click(function()
	{
	
		var srcVal=$('img', this).attr('src');
		if(srcVal=='/images/btn-checkbox-on.png')
		{
			$('img', this).attr('src', '/images/btn-checkbox-off.png');
			$('#formRecherchePiece').val(str_replace($(this).text(), '', $('#formRecherchePiece').val()));
		}
		else
		{
			$('img', this).attr('src', '/images/btn-checkbox-on.png');
			$('#formRecherchePiece').val($('#formRecherchePiece').val()+$(this).text());
		}
		
	});

	$('.optionChambre').click(function()
	{
		var srcVal=$('img', this).attr('src');
		if(srcVal=='/images/btn-checkbox-on.png')
		{
			$('img', this).attr('src', '/images/btn-checkbox-off.png');
			$('#formRechercheChambre').val(str_replace($(this).text(), '', $('#formRechercheChambre').val()));
		}
		else
		{
			$('img', this).attr('src', '/images/btn-checkbox-on.png');
			$('#formRechercheChambre').val($('#formRechercheChambre').val()+$(this).text());
		}
	});
	
	$('#lienRecherche').click(function()
	{
		var etat=$('#recherchePrecision').css('display');
		if(etat=='none')
		{
			var hauteur=$('#recherchePrecision').height();
			$('#recherchePrecision').css('height', '0px');
			$('#recherchePrecision').show();
			$('#recherchePrecision').animate({height:hauteur}, 300, function()
			{
				ajusterFondBlanc();
			});
			
		}
		else
		{
			var hauteur=$('#recherchePrecision').height();
			$('#recherchePrecision').hide(function()
			{
				ajusterFondBlancRepliage(hauteur);
			});
		}
	});
	
	$('#lienRechercheEmail').click(function()
	{
		var etat=$('#rechercheEmail').css('display');
		if(etat=='none')
		{
			var hauteur=$('#rechercheEmail').height();
			$('#rechercheEmail').css('height', '0px');
			$('#rechercheEmail').show();
			$('#rechercheEmail').animate({height:hauteur}, 100, function()
			{
				ajusterFondBlanc();
			});
		}
		else
		{
			var hauteur=$('#rechercheEmail').height();
			$('#rechercheEmail').hide(function()
			{
				ajusterFondBlancRepliage(hauteur);
			});		
		}
	});
	
	
});

function verifierEMail(email)
{
	var expr=/^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]{2,}[.][a-zA-Z]{2,3}$/;
	if(expr.exec(email))
	{
		return true;
	}
	else
	{
		return false;
	}
}

function verifFormulaire()
{
	var erreur=0;
	valFormulaire=document.forms['formulaireContact'].elements['formulaire_contact'].value;
	switch(valFormulaire)
	{
		case '1':
			if(document.forms['formulaireContact'].elements['nom'].value=='' || document.forms['formulaireContact'].elements['adr1'].value=='' || document.forms['formulaireContact'].elements['cp'].value=='' || document.forms['formulaireContact'].elements['ville'].value=='' || document.forms['formulaireContact'].elements['email'].value=='' || document.forms['formulaireContact'].elements['sujet'].value==''  || document.getElementById('comments').value=='')
			{
				erreur=1;
			}
			else if(verifierEMail(document.forms['formulaireContact'].elements['email'].value)==false)
			{
				erreur=2;
			}

			
			break;
		case '2':
			if(document.forms['formulaireContact'].elements['nom'].value=='' || document.forms['formulaireContact'].elements['tel'].value=='')
			{
				erreur=1;
			}
			break;
		case '3':
			if(document.forms['formulaireContact'].elements['nom'].value=='' || document.forms['formulaireContact'].elements['adr1'].value=='' || document.forms['formulaireContact'].elements['cp'].value=='' || document.forms['formulaireContact'].elements['ville'].value=='' || document.forms['formulaireContact'].elements['email'].value=='' || document.getElementById('comments').value=='' || document.forms['formulaireContact'].elements['adresse_vente'].value=='' || document.forms['formulaireContact'].elements['cp_vente'].value=='' || document.forms['formulaireContact'].elements['ville_vente'].value=='')
			{
				erreur=1;
			}
			else if(verifierEMail(document.forms['formulaireContact'].elements['email'].value)==false)
			{
				erreur=2;
			}
			break;
		case '4':
			if(document.forms['formulaireContact'].elements['nom'].value=='' || document.forms['formulaireContact'].elements['email'].value=='' || document.forms['formulaireContact'].elements['sujet'].value=='' || document.getElementById('comments').value=='' || document.forms['formulaireContact'].elements['email_ami'].value=='')
			{
				erreur=1;
			}
			else if(verifierEMail(document.forms['formulaireContact'].elements['email'].value)==false || verifierEMail(document.forms['formulaireContact'].elements['email_ami'].value)==false)
			{
				erreur=2;
			}
			break;
	}
	
	if(erreur==0)
	{
		return true;
	}
	else if (erreur==1)
	{
		alert('Merci de remplir tous les champs obligatoires');
		return false;
	}
	else if (erreur==2)
	{
		alert('Merci de v\351rifier la validit\351 de votre adresse e-mail');
		return false;
	}
}


/*======================================================================================
Mise a jhour du moteur de recherche
======================================================================================*/

/* 	function maj_onglet()
	{
	
		$(".onglet").each(function(i){
			
				if($(this).text() == 'louer' && $(this).css('background-image') == )
				{
						$('.onglet').css('background-image', "url('/images/fond-onglet-vendeur.png')");
						$('.onglet').css('color', '#00234c');
						$('.onglet').css('font-weight', 'normal');
						$(this).css('background-image', 'none');
						$(this).css('background-color', '#19A8DD');
						$(this).css('color', '#FFFFFF');
						$(this).css('font-weight', 'bold');
						
						$('#formRechercheAl').val('louer');
						min=parseInt(str_replace(' ', '', $('#sbmin').text()))/500;
						max=parseInt(str_replace(' ', '', $('#sbmax').text()))/500;
						minFormat=number_format(min, 0, ' ', ' ');
						maxFormat=number_format(max, 0, ' ', ' ');
						if(isNaN(max))
						{
							max=1000;
						}
						
						if(isNaN(min))
						{
							min=0;
						}
						
						if(max==1000)
						{
							maxImport='';
						}
						else
						{
							maxImport=max;
						}
						
						$('#formRechercheBudgetmin').val(min);
						$('#formRechercheBudgetmax').val(maxImport);
						}
			});
	
	
	} */


function reload_moteur(tab,louer_achat)
{

	 if(louer_achat== "louer")
	{
	
		$(".onglet").each(function(i){
		
			if($(this).text() == 'louer')
			{
					$('.onglet').css('background-image', "url('/images/fond-onglet-vendeur.png')");
					$('.onglet').css('color', '#00234c');
					$('.onglet').css('font-weight', 'normal');
					$(this).css('background-image', 'none');
					$(this).css('background-color', '#19A8DD');
					$(this).css('color', '#FFFFFF');
					$(this).css('font-weight', 'bold');
					
					$('#formRechercheAl').val('louer');
					document.forms['formRecherche'].elements['al'].value = 'louer';
					min=parseInt(str_replace(' ', '', $('#sbmin').text()))/500;
					max=parseInt(str_replace(' ', '', $('#sbmax').text()))/500;
					minFormat=number_format(min, 0, ' ', ' ');
					maxFormat=number_format(max, 0, ' ', ' ');
					if(isNaN(max))
					{
						max=1000;
					}
					
					if(isNaN(min))
					{
						min=0;
					}
					
					if(max==1000)
					{
						maxImport='';
					}
					else
					{
						maxImport=max;
					}
					
					$('#formRechercheBudgetmin').val(min);
					$('#formRechercheBudgetmax').val(maxImport);
				/*	
					if(min==0 && max!=1000)
					{
						$('#d1').html('jusqu\'\340 <span id="sbmax" style="color:#000000">'+maxFormat+' &euro;</span>');
					}
					else if(min!=0 && max==1000)
					{
						$('#d1').html('\340 partir de <span id="sbmin" style="color:#000000">'+minFormat+' &euro;</span>');			
					}
					else if(min==0 && max==1000)
					{
						document.getElementById('d1').innerHTML='Tous les budgets';
					}
					else
					{
						$('#d1').html('de <span id="sbmin" style="color:#000000">'+minFormat+' &euro;</span> \340 <span id="sbmax" style="color:#000000">'+maxFormat+' &euro;</span>');
					}*/
			}
		});
	} 
	//la ville
	if(tab["divers1"] != "")
	{
		$('#villeValeur').val(tab["divers1"]);
		$('#formRechercheDivers1').val(tab["divers1"]);
	}
	// le type de residence
	if(tab["to"] != "")
	{
		$('#choixType').html('<div class="flecheGauche"><img src="/images/ico-fleche-gauche.png" alt="" /></div>'+tab["to"]);
		$('#formRechercheTo').val(tab['to']);
	}
	//le nombre de pieces
	if(tab["piece"] != "")
	{
		$('#formRecherchePiece').val();
		
		 $(".optionPiece").each(function(i){
			var tampon = ($(this).html());
			tampon  = tampon.substr(0,1);
			if(tampon == "+")
			{
				tampon = "[+]";
			}
			
			if (tab["piece"].search(tampon) != -1)
			{
				$('img', this).attr('src', '/images/btn-checkbox-on.png');
			}

		});
		$('#formRecherchePiece').val(tab['piece']);
	}
	
	
	//le nombre de chambres
	if(tab["chambre"] != "")
	{
		$('#formRechercheChambre').val();
		
		 $(".optionChambre").each(function(i){
			var tampon = ($(this).html());
			tampon  = tampon.substr(0,1);
			if(tampon == "+")
			{
				tampon = "[+]";
			
			}
			
			if (tab["chambre"].search(tampon) != -1)
			{
				$('img', this).attr('src', '/images/btn-checkbox-on.png');
				$('#formRechercheChambre').val($('#formRecherchePiece').val()+$(this).text());
			}

		});
		$('#formRechercheChambre').val(tab['chambre']);
	}
		
	if(tab["chambre"] != "" || tab["piece"] != "" || $("#tampon_surface").val() == '1')
	{
	
		var hauteur=$('#recherchePrecision').height();
		$('#recherchePrecision').css('display', 'block');
		ajusterFondBlanc();

	
	}
}

function number_format(number,decimals,dec_point,thousands_sep)
{var n=number,prec=decimals;n=!isFinite(+n)?0:+n;prec=!isFinite(+prec)?0:Math.abs(prec);var sep=(typeof thousands_sep=="undefined")?',':thousands_sep;var dec=(typeof dec_point=="undefined")?'.':dec_point;var s=(prec>0)?n.toFixed(prec):Math.round(n).toFixed(prec);var abs=Math.abs(n).toFixed(prec);var _,i;if(abs>=1000){_=abs.split(/\D/);i=_[0].length%3||3;_[0]=s.slice(0,i+(n<0))+
_[0].slice(i).replace(/(\d{3})/g,sep+'$1');s=_.join(dec);}else{s=s.replace('.',dec);}
return s;}

function str_replace(search,replace,subject){var s=subject;var ra=r instanceof Array,sa=s instanceof Array;var f=[].concat(search);var r=[].concat(replace);var i=(s=[].concat(s)).length;var j=0;while(j=0,i--){if(s[i]){while(s[i]=(s[i]+'').split(f[j]).join(ra?r[j]||"":r[0]),++j in f){};}}
return sa?s:s[0];}


//Hauteur des div bleue et blanche
$(window).load(function(){ 
		// Variable globale pour récupérer la hauteur maximale
		var hauteurBleue = $('#colDroite').height();
		var hauteurBlanche = $('#colGauche').height();
		if(hauteurBleue > hauteurBlanche) {
			maxHeight = hauteurBleue;
			$('#colGauche').height(maxHeight);
			$('#colDroite').height(maxHeight);
		}else{
			maxHeight = hauteurBlanche;
			$('#colDroite').height(maxHeight);
			$('#colGauche').height(maxHeight);
		};
		//page.height(maxHeight+$hauteurLiens+10);
		//document.getElementById("#listingAgences").bottom = 10;
});