(function(){
var currentPicture;
var sidMatcher = /tpid=([0-9]*)/;
var imageMatcher = /moyPicture=(\S*)\s*originalPicture=(\S*)/
var static_site = {};

	function prepare(){

		var c = $('contenu_scroll');
		currentPicture = $("currentPicture");
		if(c)
			plgscroll(c, {height:199, width:0, top:0, thumbHeight:0});
			$('bulle_emailbtn').addEvent("mouseover",show);
			$('bulle_emailbtn').addEvent("mouseout",hide);

				$('bulle_impressionbtn').addEvent("mouseover",show);
			$('bulle_impressionbtn').addEvent("mouseout",hide);
			$('bulle_impressionbtn').addEvent("click", printResidence);
			$$(".smallPicture").addEvent("click",changePicture);
			$$('.tpContent').each(function(e){e.addEvent("mouseenter",prepareShowDetail)});
			$('closeBtn').addEvent("click",hideDetail);

	}//prepare

	function prepareShowDetail(){
			$$(".tpContent.disabled").each(function(site){site.removeClass("disabled")});
			this.addClass('disabled');
			var sids = this.className.match(sidMatcher);
			if(!sids)
				return;
			var sid = sids[1];
			 if(static_site[sid]){
				 	showDetail(static_site[sid],true);
				 	return;
			 }
			var residencesRequest = new Request({url:"/index.php?do=ajax/popup&lid="+Lang.lid,method:"post",onComplete:function(text){showDetail(text,false,sid) }});

			residencesRequest.send('tip='+sid);

	}//prepareShowDetail

	function showDetail(text,noCache,sid){
			if(sid && !noCache)
				static_site[sid] = text;

			var updateDiv = $("popupContent");
			if(!updateDiv)
				return;

			$("popup").removeClass('hidden');

			updateDiv.set('html', text);

	}//showDetail

	function hideDetail(){
			$("popup").addClass('hidden');
				$$(".tpContent.disabled").each(function(site){site.removeClass("disabled")});

	}//hideDetail

		function changePicture(e)
		{
			e.stop();
			
			var imagesUrls = this.className.match(imageMatcher);
			if(!imagesUrls)
				return;
			var urlMoy = imagesUrls[1];
			var original = imagesUrls[2];

			currentPicture.alt = this.alt;
			
			currentPicture.src = urlMoy;
			currentPicture.getParent().href =original;
		}

		function show(){

			var elm = this.id.replace("btn","");
			$(elm).removeClass('hidden');
		}//show

		function hide(){
			var elm = this.id.replace("btn","");
			$(elm).addClass('hidden');
		}//hide

		function printResidence(e) {
			e.stop()
			window.print();
		}

window.addEvent('domready', prepare);



})()
