function loadBien(el) {
	el = $(el);
	el.addClassName('active');
	el.siblings().invoke('removeClassName', 'active');
	redirectAjax('bien', el.down('a').href);
	$$('body')[0].scrollTo();
}

function updateBudgetMaxMenu(budget_min) {
	var budget_min = parseInt(budget_min);
	var budget_max_cur = $('budget_max').value;
	var el = $('budget_max');
	while(el.options.length) el.remove(0);
	var budgets = new Array('75000', '100000', '150000', '200000', '250000', '300000', '350000', '400000', '450000', '500000', '600000', '700000', '800000', '900000');
	for(i=0; i < budgets.length; i++) {
		var val = budgets[i];
		var caption = val.substring(0, val.length-3)+" 000 €";
		if (val > budget_min) {
			el.options[el.length] = new Option(caption, val);
			if (val == budget_max_cur) el.selectedIndex = el.length-1;
		}
	}
}

function loadResults() {
	$('results').innerHTML = '<img src="images/process-results.gif" alt="Recherche en cours..." />';
	$('results').addClassName('processing');
	new Ajax.Updater('results', 'results.ajax',
	{
		parameters: $('recherche_form').serialize(),
		evalScripts: true,
		onComplete: function (responder) {
			$(responder.request.container.success).removeClassName('processing');
		}
	});
}

function redirectAjax(module, thelink) {
	var qs = thelink.indexOf('?');
	thelink = thelink.substring(qs+1, thelink.length);
	$(module).innerHTML = '<img src="images/process-'+module+'.gif" alt="Recherche en cours..." />';
	$(module).addClassName('processing');
	
	new Ajax.Updater(module, module+'.ajax',
	{
		parameters: thelink,
		evalScripts: true,
		onComplete: function (responder) {
			$(responder.request.container.success).removeClassName('processing');
		}
	});
}

document.observe("dom:loaded", function() {
	if ($('bien')) {
		$$('head')[0].insert('<style type="text/css">#bien h2 span { visibility: hidden; }</style>');
		el = $$('#bien h2')[0];
		el.innerHTML = '<img src="sir/h2/'+encodeURIComponent(el.down('span').innerHTML)+'" />';
	}
});

function closePopup() {
	if (window.top && window.top.myLightWindow) window.top.myLightWindow.deactivate();
	else window.location = 'index.php';
}


var previousKenburnStyle = -1;
function Kenburn(el1) {
	var rounder = 50;
	var theImages = $$('#bigphoto img');
	if (theImages.length > 1) {
		el2 = theImages[0];
		for(var i=0; i<theImages.length; i++) {
			if (theImages[i] == el1) {
				if (i+1 < theImages.length) el2 = theImages[i+1];
			}
		}
		el0 = el1.previous();
		if (el0 == undefined || el0 == null)
			el0 = theImages[theImages.length-1];
		do {
			var kenburnStyle = Math.round(Math.random()*100000)%4;
		} while (kenburnStyle == previousKenburnStyle);
		console.log(el0);
		previousKenburnStyle = kenburnStyle;
		var targetLeft = (el1.up().getWidth()-el1.getWidth());
		var targetTop = (el1.up().getHeight()-el1.getHeight());
		switch (kenburnStyle) {
			case 0: // top left
				el1.style.left = targetLeft+'px';
				el1.style.top = targetTop+'px';
				var wayX = 1;
				var wayY = 1;
				break;
			case 1: // top right
				el1.style.left = 0;
				el1.style.top = targetTop+'px';
				var wayX = -1;
				var wayY = 1;
				break;
			case 2: // bottom left
				el1.style.left = targetLeft+'px';
				el1.style.top = 0;
				var wayX = 1;
				var wayY = -1;
				break;
			case 3: // bottom right
				el1.style.left = 0;
				el1.style.top = 0;
				var wayX = -1;
				var wayY = -1;
				break;
		}
		var moveByX = Math.round((el1.getWidth()-el1.up().getWidth())*wayX*Math.random()/rounder)*rounder;
		var moveByY = Math.round((el1.getHeight()-el1.up().getHeight())*wayY*Math.random()/rounder)*rounder;
		if (moveByX == 0 && moveByY == 0) {
			moveByX = rounder*wayX;
			moveByY = rounder*wayY;
		}
		console.log(el1);
		console.log(kenburnStyle+': '+moveByX+'/'+moveByY);
		/*new Effect.Parallel([
			new Effect.Move(el1,
			{
				x: moveByX, 
				y: moveByY,
				mode: 'relative',
				sync: true
			}),
			new Effect.Scale(el1,
			100+Math.round(Math.random()*100000)%20,
			{
				scaleFrom: 100,
				scaleFromCenter: false,
				sync: true
			})
		], */


		new Effect.Parallel([
			new Effect.Appear(el1, { sync: true }),
			new Effect.Fade(el0, { sync: true } )
		],
		{
			duration: 1,
			afterFinish: function (effect2) {
				new Effect.Move(el1,
				{
					x: moveByX, 
					y: moveByY,
					mode: 'relative',
					duration: 5,
					afterFinish: function (effect2) {
						Kenburn(el2);
					}
				}
				);				
			}
		})
	}
}

if (!window.console || !console.firebug) {
	var names = ["log", "debug", "info", "warn", "error", "assert", "dir", "dirxml",
	"group", "groupEnd", "time", "timeEnd", "count", "trace", "profile", "profileEnd"];
	window.console = {};
	for (var i = 0; i < names.length; ++i)
	window.console[names[i]] = function() {}
}