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 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 video = function(event) {
	var e = event.element();
	e.blur();
	var btn = e.up('ul.video-btn');
	var ecran = btn.next('div.video-ecran');
	
	var videoHttp = ecran.down('.input-http').value;
	var videoWidth = parseInt(ecran.down('.input-width').value);
	var videoHeight = parseInt(ecran.down('.input-height').value);
	
	var videoObject = new Element('object', {
		classid:  'clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B',
		codebase: 'http://www.apple.com/qtactivex/qtplugin.cab',
		width:    videoWidth,
		height:   (videoHeight + 16)
	});
	
	videoObject.insert({ bottom: new Element('param', { name: 'src', value: videoHttp }) });
	videoObject.insert({ bottom: new Element('param', { name: 'autoplay', value: 'true' }) });
	videoObject.insert({ bottom: new Element('param', { name: 'controller', value: 'true' }) });
	videoObject.insert({ bottom: new Element('param', { name: 'loop', value: 'false' }) });

	videoObject.insert({
		bottom: new Element('embed', {
			src:      videoHttp,
			width:    videoWidth,
			height:   (videoHeight + 16),
			autoplay: 'true',
			controller: 'true',
			loop:     'false',
			pluginspage: 'http://www.apple.com/quicktime/',
		})
	});
	
	ecran.update(videoObject);
	btn.hide();
	ecran.show();
	
	event.stop();
}

var setupVideoEcran = function(e) {
	var btn = e.down('li');
	btn.update('<a href="">' + btn.innerHTML + '</a>');
	var link = btn.down('a');
	Event.observe(link, 'click', video, false);
}

var setupPairImpair = function(ul) {
	ul.childElements().each(function(li, index) {
		if(((index+1) % 2) > 0) li.addClassName('impair');
	});
}

var jwRotator = function(playerUrl, playlistUrl, width, height) {
	var so = new SWFObject(playerUrl, 'rotator', width, height, '7');
	so.addVariable('file', playlistUrl);
	so.addVariable('height',height);
	so.addVariable('width',width);
	so.addVariable('transition','slowfade');
	so.addVariable('shuffle','true');
	so.addVariable('backcolor','0x000000');
	so.addVariable('rotatetime','5');
	so.addVariable('kenburns','true');
	so.write('jwrotator');
}

var initSiteDomLoaded = function(event) {
	$$('div.pic-slideshow').each(setupSlideshow);
	$$('ul.video-btn').each(setupVideoEcran);
	$$('div.invites ul').each(setupPairImpair);
}

var initSiteAllLoaded = function(event) {
	isAllLoaded = true;

	if(!$$('body')[0].hasClassName('popup')) {
		initRadio();
	}
}

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

var isAllLoaded = false;
