var lng = function(f, e) {
	return (lngId == 'f')? f: e;
}

var formHandler = function(form) {
  var URL = form.site.options[form.site.selectedIndex].value;
  if (URL != "") { location.href = URL; }
}

var radio = function(url) {
  win_stationradio = window.open(url, 'stationradio', 'scrollbars=no,resizable=no,status=yes,width=400,height=168');
  win_stationradio.focus();
}

var video = function(url, winw, winh) {
	winw = winw+50; winh = winh+120;
  win_stationvid = window.open(url, 'stationvid', 'scrollbars=no,resizable=yes,status=yes,width='+winw+',height='+winh);
  win_stationvid.focus();
}

var retour = function(url) {
  opener.location.href = url;
}

var pouf = function() {
  window.close();
}

var initCalList = function(table) {
	table.select('tr').each(function(tr) {
		tr.select('span.pays').each(function(pays) {
			tr.addClassName(pays.getAttribute('rel'));
		});
		
		tr.select('td.desc p').each(function(p) {
			p.select('span.pays').each(function(pays) {
				p.addClassName(pays.getAttribute('rel'));
			});
		});
	});
	
	var paysHash = new Hash();

	table.select('span.pays').each(function(pays) {
		paysHash.set(pays.getAttribute('rel'), pays.innerHTML);
	});
	
	if(paysHash.size() > 1) {
		var paysSelect = new Element('ul', { 'class': 'pays-select' });
		
		paysSelect.insert({
			bottom: new Element('li').insert({
				top: new Element('a', { 'class': 'here', 'rel': '_tout', 'href': '#pays--tout' }).update(lng('Tous les pays', 'All countries'))
			})
		});
		
		paysHash.sortBy(function(pair) { return pair.key; }).each(function(pair) {
			paysSelect.insert({ bottom: ' ' });
		
			paysSelect.insert({
				bottom: new Element('li').insert({
					top: new Element('a', { 'rel': pair.key, 'href': '#pays-' + pair.key }).update(pair.value)
				})
			});
		});
		
		paysSelect.select('a').each(function(link) {
			var paysId = link.getAttribute('rel');
			
			Event.observe(link, 'click', function(event) {
				var link = event.element();
				link.blur();
				paysSelect.down('a.here').removeClassName('here');
				link.addClassName('here');
	
				if(paysId == '_tout') {
					table.select('tr').each(function(tr) {
						tr.show();
					
						tr.select('td.desc p').each(function(p) {
							p.show();
						});
					});
	
				} else {
					table.select('tr').each(function(tr) {
						if(tr.hasClassName(paysId)) tr.show(); else tr.hide();
						
						tr.select('td.desc p').each(function(p) {
							if(p.hasClassName(paysId)) p.show(); else p.hide();
						});
					});
				}
				
				event.stop();
			});
		});
		
		paysSelect.insert({ bottom: new Element('div', { 'class': 'clear' }) });
		
		table.insert({ before: paysSelect });
	}
}

var initAccueilBioDeco = function(ul) {
	var n = ul.select('li').size();
	var reste = 6 - (n % 6);
	
	if(reste < 6) {
		for(i = 0; i < reste; i++) {
			var pos = Math.floor(Math.random()*n);
			ul.select('li')[pos].insert({ after: '<li class="vide"><span></span></li>' });
			n++;
		}
	}
}

var initSiteDomLoaded = function(event) {
	if($('accueil-bio-deco')) {
		initAccueilBioDeco($('accueil-bio-deco'));
	}

	$$('table.cal-list').each(function(table) {
		initCalList(table);
	});
}

var initSiteAllLoaded = function(event) {
}

Event.observe(document, 'dom:loaded', initSiteDomLoaded, false);
Event.observe(window, 'load', initSiteAllLoaded, false);
