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

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

var plus = function(url) {
  win_smcq = window.open(url, 'smcq', 'scrollbars=yes,resizable=yes,status=yes,width=500,height=550');
  win_smcq.focus();
}

var mini = function(url) {
  win_smcq = window.open(url, 'smcq', 'scrollbars=yes,resizable=yes,status=yes,width=350,height=250');
  win_smcq.focus();
}

var pop = function(url, w, h) {
	w = w + 20;
	h = h + 20;
  win_smcq = window.open(url, 'smcq', 'scrollbars=yes,resizable=yes,status=yes,width='+w+',height='+h);
  win_smcq.focus();
}

var video = function(url, w, h) {
	w = w + 50;
	h = h + 120;
  win_smcq = window.open(url, 'smcq', 'scrollbars=yes,resizable=yes,status=yes,width='+w+',height='+h);
  win_smcq.focus();
}

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

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

var initLieuDetails = function() {
	$$('a.lieu').each(function(link) {
		var lieuDetail = link.next('div.lieu-detail');
		lieuDetail.hide();
		
		Element.observe(link, 'click', function(event) {
			link.blur();
		
			if(selectLieuDetail == lieuDetail) {
				Effect.SlideUp(selectLieuDetail, { duration: 0.2, queue: 'end' });
				selectLieuDetail = null;
		
			} else {
				if(selectLieuDetail) Effect.SlideUp(selectLieuDetail, { duration: 0.1, queue: 'end' });
				Effect.SlideDown(lieuDetail, { duration: 0.2, queue: 'end' });
				selectLieuDetail = lieuDetail;
			}
		
			event.stop();
		});
	});
}

var initNavTopPtr = function(li) {
	li.relativize();
	li.insert({ bottom: '<div class="nav-ptr"></div>' });
}

var fixNavOffset = function(ul) {
	if(ul.down('a.here1')) {
		var ulHeight = ul.getHeight();

		var here = ul.down('a.here1').up('li');
		var hereHeight = here.getHeight();
		var hereOffset = here.offsetTop - ul.offsetTop;
		
		if((hereOffset + hereHeight) > (ul.scrollTop + ulHeight)) {
			ul.scrollTop = hereOffset - (ulHeight / 2) + (hereHeight / 2);
		}
	}
}

var initNavSaison = function(navSaison) {
	navSaison.show();
	var selection = navSaison.down('div.selection');
	var root = navSaison.down('a.root');

	/*
	root.replace('<a href="#" class="root" title="' + root.title + '">' + root.innerHTML + '</a>');
	root = navSaison.down('a.root');
	*/
	
	Event.observe(root, 'click', function(event) {
		if(!navSaisonStatus) {
			clearTimeout(navSaisonTimer);
			selection.show();
			navSaisonStatus = true;
			navSaison.addClassName('actif');
		}

		event.element().blur();
		event.stop();
	});
	
	Event.observe(navSaison, 'mouseout', function(event) {
		Event.observe(window, 'click', function(event) {
			if(navSaisonStatus && (!event.findElement('#nav-saison'))) {
				selection.hide();
				navSaisonStatus = false;
				navSaison.removeClassName('actif');
			}
		});
	});
	
	Event.observe(selection.down('a'), 'click', function(event) {
		navSaisonStatus = false;
		navSaison.removeClassName('actif');
		selection.hide();
	});
}

var initUtilListe = function(utilListe) {
	Event.observe(utilListe.down('input.vide'), 'focus', function(event) {
		var inp = event.element();
		inp.removeClassName('vide');
		vide[inp.identify()] = inp.value;
		inp.value = '';
	});

	Event.observe(utilListe.down('input.vide'), 'blur', function(event) {
		var inp = event.element();
		
		if(inp.value == '') {
			inp.addClassName('vide');
			inp.value = vide[inp.identify()];
		}
	});
	
	Event.observe(utilListe.down('div.btn input'), 'click', function(event) {
		var inp = event.element().up('#util-liste').down('input.vide');
		if(inp.hasClassName('vide')) event.stop();
	});
}

var qualifyImage = function(image) {
	var result = '';
	if(image.width > image.height) result = 'horizontal'
	else if(image.width < image.height) result = 'vertical'
	else result = 'carre';
	return result;
}

var initPhoto = function(photo) {
	if(photo.down('img')) photo.addClassName(qualifyImage(photo.down('img')));
}

var initDiaporama = function(diaporama) {
	var getNext = function(index) {
		var newIndex = index+1;
		if(newIndex >= photoArray.size()) newIndex = 0;
		return newIndex;
	}

	var getPrev = function(index) {
		var newIndex = index-1;
		if(newIndex <= -1 ) newIndex = photoArray.size()-1;
		return newIndex;
	}

	var getTimeout = function(photo) {
		var l = photo.down('div.desc').innerHTML.stripTags().length;
		var timeout = l * 50;
		if(timeout < 4000) timeout = 4000;
		return timeout;
	}
	
	var showPhoto = function(n) {
		Effect.Fade(photoArray[diaporama.actif], { from: 1, to: 0, duration: 0.5 });
		Effect.Appear(photoArray[n], { from: 0, to: 1, duration: 0.5 });

		clearTimeout(diaporama.timer);
		diaporama.timer = setTimeout(nextPhoto, getTimeout(photoArray[diaporama.actif]));
		diaporama.actif = n;
	}

	var nextPhoto = function() {
		showPhoto(getNext(diaporama.actif));
	}
	
	var addNextPrevBtn = function(photo, prevActif, nextActif) {
		if(photoArray.size() > 2) {
			var prevImage = photoArray[prevActif].down('div.image img');
			photo.insert({ bottom: '<div class="btn-prev"><a href="#"></a></div>' });
			photo.down('div.btn-prev a').insert({ bottom: new Element('img', { 'src': prevImage.src, 'class': qualifyImage(prevImage) }) });
			
			Event.observe(photo.down('div.btn-prev a'), 'click', function(event) {
				event.element().blur();
				var photo = event.element().up('div.photo');
				showPhoto(getPrev(photo.index));
				event.stop();
			});
		}

		var nextImage = photoArray[nextActif].down('div.image img');
		photo.insert({ bottom: '<div class="btn-next"><a href="#"></a></div>' });
		photo.down('div.btn-next a').insert({ bottom: new Element('img', { 'src': nextImage.src, 'class': qualifyImage(nextImage) }) });

		Event.observe(photo.down('div.btn-next a'), 'click', function(event) {
			event.element().blur();
			var photo = event.element().up('div.photo');
			showPhoto(getNext(photo.index));
			event.stop();
		});
	}
	
	photoArray = diaporama.select('div.photo');

	if(photoArray.size() > 1) {
		if(diaporama.hasClassName('aleatoire')) {
			photoArray[0].hide();
		
			photoArray = photoArray.sortBy(function(s) { 
				return Math.floor(Math.random()*10000);
			});
		}
		
		var isMinHeight = diaporama.hasClassName('hauteur-min');
		var maxHeight = 0;
		var minHeight = 999999;
	
		photoArray.each(function(photo, index) {
			photo.index = index;
			photo.show();
			if(photo.getHeight() > maxHeight) maxHeight = photo.getHeight();
			if((photo.getHeight() >= 180) && (photo.getHeight() < minHeight)) minHeight = photo.getHeight();
			addNextPrevBtn(photo, getPrev(index), getNext(index));
			photo.absolutize();
			if(((index+1) % 2) == 0) photo.addClassName('pair');
			if(index > 0) photo.hide()
			else photo.show();
		});
		
		if(minHeight == 999999) minHeight = 180;
		var fixedHeight = (isMinHeight? minHeight: maxHeight);
		
		diaporama.setStyle({ 'height': fixedHeight + 'px' });
		diaporama.actif = 0;
		diaporama.timer = setTimeout(nextPhoto, getTimeout(photoArray[diaporama.actif]));

		photoArray.each(function(photo, index) {
			photo.setStyle({ 'height': fixedHeight + 'px' });
			photo.down('div.image').setStyle({ 'height': fixedHeight + 'px' });
		});
	}
}

var initEvenImgIE = function(img) {
	Event.observe(img, 'click', function(event) {
		link = event.element().up('a');
		location.href = link.href;
	});
}

var initSiteDomLoaded = function(event) {
	if($$('#nav-top-saison li.path1, #nav-top-aussi li.path1')) {
		$$('#nav-top-saison li.path1, #nav-top-aussi li.path1').each(initNavTopPtr);
	}

	if($('nav-saison')) {
		initNavSaison($('nav-saison'));
	}

	if($('util-liste')) {
		initUtilListe($('util-liste'));
	}

	if($('radio-poste')) {
		initRadio();
	}
	
	if($$('div.video-ecran')) {
		initVideo();
	}
	
	if($$('a.lieu')) {
		initLieuDetails();
	}
	
	if(Prototype.Browser.IE) {
		if($$('ul.even-list a span.photo img')) {
			$$('ul.even-list a span.photo img').each(initEvenImgIE);
		}
	}
	
	Event.observe(document, 'mousemove', function(event) {
		if($('tip') && $('tip').isActif) {
			$('tip').setStyle({
				'left': (Event.pointerX(event) - ($('tip').getWidth() / 2)) + 'px',
				'top': (Event.pointerY(event) - $('tip').getHeight() - 12 ) + 'px'
			});
		}
	});
}

var initSiteAllLoaded = function(event) {
	if($$('div.photo-diaporama')) {
		$$('div.photo-diaporama').each(initDiaporama);
	}
	
	if($$('span.photo')) {
		$$('span.photo').each(initPhoto);
	}
	
	if($$('ul.nav-contexte-concerts')) {
		$$('ul.nav-contexte-concerts').each(fixNavOffset);
	}
}

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

var vide = new Array();
var navSaisonStatus = false;
var navSaisonTimer;
var tipTimer;
var selectLieuDetail;
