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

var radio = function(url) {
  win_ntradio = window.open(url, 'ntradio', 'scrollbars=no,resizable=no,status=yes,width=620,height=210');
  win_ntradio.focus();
}

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

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

var fixCustomRadio = function(rowId) {
}

var selectRadio = function(event) {
	var e = event.element();
	
	if(e.tagName == 'INPUT') {
		var input = e;
		var span = input.up('span.radio');
		var link = span.down('span.radio-texte a');

	} else {
		var link = e;
		var span = link.up('span.radio');
		var input = span.down('input');
		var isFromLink = true;
	}

	span.addClassName('selected');
	if(isFromLink) input.checked = !input.checked;

	input.up('span.radio-groupe').childElements().each(function(outSpan) {
		var outInput = outSpan.down('input');
		if(!outInput.checked) outSpan.removeClassName('selected');
	});

	var row = span.up('p') || span.up('li');
	fixCustomRadio(row.id);
	
	if(isFromLink) {
		link.blur();
		event.stop();
	}
}

var selectCheckbox = function(event) {
	var e = event.element();

	if(e.tagName == 'INPUT') {
		var input = e;
		var span = input.up('span.checkbox');
		var link = span.down('span.checkbox-texte a');

	} else {
		var link = e;
		var span = link.up('span.checkbox');
		var input = span.down('input');
		var isFromLink = true;
	}
	
	if(input.up('span.checkbox-groupe')) {
		if(!input.checked) {
			span.addClassName('selected');
			if(isFromLink) input.checked = true;
	
		} else {
			span.removeClassName('selected');
			if(isFromLink) input.checked = false;
		}
	
		input.up('span.checkbox-groupe').childElements().each(function(outSpan) {
			outInput = outSpan.down('input');
			if(!outInput.checked) outSpan.removeClassName('selected');
		});
	}
	
	if(isFromLink) {
		link.blur();
		event.stop();
	}
}

var fixCustomTextInput = function(rowId) {
}

var changeTextInput = function(input) {
	var row = input.up('p');
	fixCustomTextInput(row.id);
}

var setupSlideshowOld = function(e) {
	if(e.childElements().size() > 1) {
		e.childElements().each(function(div, index) {
			var linkDiv = new Element('div', { 'class': 'btn' });
			var img = div.down('img');
			
			//imgWidth = img.width;
			//imgHeight = img.height;
			imgWidth = 300;
			imgHeight = 260;
			
			linkDiv.setStyle({ width: imgWidth + 'px' });
			linkDiv.setStyle({ height: imgHeight + 'px' });
			
			if(index < e.childElements().size()-1) {
				var nextLink = new Element('a', { 'class': 'btn-next', href: '' }).update('<span>&raquo</span>');
				if(index > 0) nextLink.setStyle({ width: imgWidth / 2 - 1 + 'px' })
				else nextLink.setStyle({ width: imgWidth + 'px' });
				nextLink.setStyle({ height: imgHeight + 'px' });
			
				Event.observe(nextLink, 'click', function(event) {
					var link = event.element();
					var parentDiv = link.up('div.pic');
					parentDiv.hide();
					parentDiv.next('div.pic').show();
					event.stop();
				});
				
				linkDiv.insert({ top: nextLink });
			}

			if(index > 0) {
				var prevLink = new Element('a', { 'class': 'btn-prev', href: '' }).update('<span>&laquo</span>');
				if(index < e.childElements().size()-1) prevLink.setStyle({ width: imgWidth / 2 - 1 + 'px' })
				else prevLink.setStyle({ width: imgWidth + 'px' });
				prevLink.setStyle({ height: imgHeight + 'px' });

				Event.observe(prevLink, 'click', function(event) {
					var link = event.element();
					var parentDiv = link.up('div.pic');
					parentDiv.hide();
					parentDiv.previous('div.pic').show();
					event.stop();
				});

				linkDiv.insert({ top: prevLink });
			}

			div.insert({ top: linkDiv });
		});
	}
}

var setupSlideshow = function(e) {
	if(e.childElements().size() > 1) {
		var selectDiv = new Element('div', { 'class': 'select' });

		e.childElements().each(function(div, index) {
			if(index == 0) e.selectedDiv = div;
			var link = new Element('a', { href: '' }).update(index + 1);
			link.associatedDiv = div;
			div.associatedLink = link;

			Event.observe(link, 'mouseover', function(event) {
				var link = event.element();
				link.blur();
				var slideshow = link.up('div.pic-slideshow');
				slideshow.selectedDiv.associatedLink.removeClassName('sel');
				slideshow.selectedDiv.hide();
				slideshow.selectedDiv = link.associatedDiv;
				slideshow.selectedDiv.show();
				slideshow.selectedDiv.associatedLink.className = 'sel';
				event.stop();
			});
			
			Event.observe(link, 'click', function(event) {
				var link = event.element();
				link.blur();
				event.stop();
			});
			
			selectDiv.insert({ bottom: link });
		});
		
		e.selectedDiv.associatedLink.className = 'sel';
		e.insert({ bottom: selectDiv });
	}
}

var activerRepresList = function(event) {
	$$('div.repres-list').each(function(e) {
		Effect.BlindDown(e, { duration: 0.1, queue: 'end' });
	});
	
	$('activer-repres-list').hide();
	$('desactiver-repres-list').show();
}

var desactiverRepresList = function(event) {
	$$('div.repres-list').each(function(e) {
		Effect.BlindUp(e, { duration: 0.1, queue: 'end' });
	});
	
	$('desactiver-repres-list').hide();
	$('activer-repres-list').show();
}

var initLaDanse = function(event) {
	$$('div.nav-drop').each(function(e) {
		var base = e.down('div.base');
		base.innerHTML = '<a href="#">' + base.innerHTML + '</a>';
		var link = base.down('a');
		
		Event.observe(link, 'click', function(event) {
			var link = event.element();
			link.blur();
			dropSelect = link.up('div.nav-drop').down('ul.select');
			dropSelect.show();
			event.stop();
		});

		Event.observe(window, 'click', function(event) {
			if(dropSelect) dropSelect.hide();
			dropSelect = null;
		}, false);
	});

	$$('div.pic-slideshow').each(setupSlideshow);
	
	if($('activer-repres-list')) {
		Event.observe($('activer-repres-list'), 'click', activerRepresList);
		Event.observe($('desactiver-repres-list'), 'click', desactiverRepresList);
	}

	$$('span.radio span.radio-texte').each(function(e) {
		e.update('<a href="">' + e.innerHTML + '</a>');
		var span = e.up('span.radio');
		var input = span.down('input');
		if(input.checked) span.addClassName('selected');
		var link = e.down('a');
		Event.observe(e, 'click', selectRadio, false);
	});

	$$('span.radio input').each(function(e) {
		Event.observe(e, 'click', selectRadio, false);
	});

	$$('span.checkbox span.checkbox-texte').each(function(e) {
		e.update('<a href="">' + e.innerHTML + '</a>');
		var span = e.up('span.checkbox');
		var input = span.down('input');
		if(input.checked) span.addClassName('selected');
		var link = e.down('a');
		Event.observe(e, 'click', selectCheckbox, false);
	});

	$$('span.checkbox input').each(function(e) {
		Event.observe(e, 'click', selectCheckbox, false);
	});
	
	$$('input[type="text"]').each(function(e) {
		new Form.Element.Observer(e, 0.2, changeTextInput);
	});
}

var dropSelect;

Event.observe(window, 'load', initLaDanse, false);
