//greyBoxShow() : OUVERTURE GREYBOX
function greyBoxShow(mode, url, imgPath, width, height, overflow){
	//Si OVERLAY n'existe pas
	if(!$('overlay')){
		// On l'ajoute dynamiquement
		$$('body')[0].insert({bottom:'<div id="overlay" onclick="greyBoxHide();"></div>'});
	}
	//Si GREYBOX n'existe pas
	if(!$('greyBox')){
		// On l'ajoute dynamiquement
		// greyBox			==> Positionnement en absolute centré
		// greyBoxContent	==> Contenus de la box
		// greyBoxLoader		==> Gif annimé de chargement
		// close			==> Div de fermeture
		$$('body')[0].insert({bottom:'<div id="greyBox"><div class="content" id="greyBoxContent"></div><div onclick="greyBoxHide();" class="close"><img src="'+imgPath+'close.gif" alt="" /></div></div>'});
	}
	
	// On dimensionne la box suivant les paramètres passés
	$('greyBoxContent').setHTML = '<div id="greyBoxLoader"></div>';
	
	$('greyBoxContent').writeAttribute('style', '');
	$('greyBox').writeAttribute('style', '');
	
	if(typeof(overflow)!='undefined' && overflow==true){
		$('greyBox').setStyle({'width':width+'px'});
		$('greyBoxContent').setStyle({'height':height+'px'});
	}
	else{
		$('greyBox').setStyle({'width':width+'px'});
	}
	// On centre la box suivant les dimensions passés
	$('greyBox').setOpacity(1);
	$('greyBox').setStyle({'left':'-'+(Math.ceil(width/2))+'px'});
	
	// On dimensionne la taille de l'overlay sur IE6
	var testIE6 = Prototype.Browser.IE6=Prototype.Browser.IE && parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE")+5))==6;
	if(testIE6){
		windowHeightIeFix = document.viewport.getHeight();
		if(document.viewport.getHeight() == 0){
			var windowHeightIeFix = 670;
		}
		$('overlay').setStyle({'height':windowHeightIeFix+'px'});
		$$('select').each(function(el){el.setStyle({'display':'none'});});
	}
	
	// Mode de contenu à afficher
	switch(mode){
		// AJAX		==> Page ajax à charger
		case 'ajax':
			if(testIE6){
				new Ajax.Request(url, {
					onComplete: function(transport){
						$('overlay').setStyle({'display':'block'});
						$('greyBox').setStyle({'display':'block'});
						$('greyBoxContent').update(transport.responseText);
					}
				});
			}
			else{
				$('overlay').setOpacity(0);
				$('overlay').setStyle({'display':'block'});
				$('overlay').fade({
					duration:0.5,
					from:0,
					to:0.6,
					afterFinish:function(){
						$('greyBox').setStyle({'display':'block'});
						new Ajax.Request(url, {
							onComplete: function(transport){
								$('greyBoxContent').update(transport.responseText);
							}
						});
					}
				});
			}
			break;
		// IFRAME		==> Iframe ajax à afficher
		case 'iframe':
			var paddings = parseInt($('greyBoxContent').getStyle('paddingLeft')) + parseInt($('greyBoxContent').getStyle('paddingRight'));
			$('greyBoxContent').innerHTML = '<iframe src="'+url+'" border="0" frameborder="0" width="'+(width-paddings)+'" height="'+height+'" allowTransparency="true"></iframe>';
			if(testIE6){
				$('overlay').setStyle({'display':'block'});
				$('greyBox').setStyle({'display':'block'});
			}
			else{
				$('overlay').setOpacity(0);
				$('overlay').setStyle({'display':'block'});
				$('overlay').fade({
					duration:0.5,
					from:0,
					to:0.6,
					afterFinish:function(){
						$('greyBox').setStyle({'display':'block'});
					}
				});
			}
			break;
		// YOUTUBE	==> Code video youtube à afficher
		case 'youtube':
			var paddings = parseInt($('greyBoxContent').getStyle('paddingLeft')) + parseInt($('greyBoxContent').getStyle('paddingRight'));
			$('greyBoxContent').innerHTML = '<object width="'+(width-paddings)+'" height="'+height+'"><param name="movie" value="http://www.youtube.com/v/'+url+'&hl=fr_FR&fs=1&rel=0&color1=0x006699&color2=0x54abd6"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/'+url+'&hl=fr_FR&fs=1&rel=0&color1=0x006699&color2=0x54abd6" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="'+(width-paddings)+'" height="'+height+'"></embed></object>';
			if(testIE6){
				$('overlay').setStyle({'display':'block'});
				$('greyBox').setStyle({'display':'block'});
			}
			else{
				$('overlay').setOpacity(0);
				$('overlay').setStyle({'display':'block'});
				$('overlay').fade({
					duration:0.5,
					from:0,
					to:0.6,
					afterFinish:function(){
						$('greyBox').setStyle({'display':'block'});
					}
				});
			}
			break;
		// DAILYMOTION	==> Code video dailymotion à afficher
		case 'dailymotion':
			var paddings = parseInt($('greyBoxContent').getStyle('paddingLeft')) + parseInt($('greyBoxContent').getStyle('paddingRight'));
			$('greyBoxContent').innerHTML = '<object width="'+(width-paddings)+'" height="'+height+'"><param name="movie" value="http://www.dailymotion.com/swf/video/'+url+'"></param><param name="allowFullScreen" value="true"></param><param name="allowScriptAccess" value="always"></param><embed type="application/x-shockwave-flash" src="http://www.dailymotion.com/swf/video/'+url+'" width="'+(width-paddings)+'" height="'+height+'" allowfullscreen="true" allowscriptaccess="always"></embed></object>';
			if(testIE6){
				$('overlay').setStyle({'display':'block'});
				$('greyBox').setStyle({'display':'block'});
			}
			else{
				$('overlay').setOpacity(0);
				$('overlay').setStyle({'display':'block'});
				$('overlay').fade({
					duration:0.5,
					from:0,
					to:0.6,
					afterFinish:function(){
						$('greyBox').setStyle({'display':'block'});
					}
				});
			}
			break;
		// IMAGE		==> Image à afficher
		case 'image':
			$('greyBoxContent').innerHTML = '<img src="'+url+'" alt="" />';
			//$('greyBoxContent').setStyle({'width':width+'px', 'height':height+'px', 'overflow':'hidden'});
			var paddings = parseInt($('greyBoxContent').getStyle('paddingLeft')) + parseInt($('greyBoxContent').getStyle('paddingRight'));
			$('greyBox').setStyle({'width':(width+paddings)+'px', 'left':(parseInt($('greyBox').getStyle('left'))-(paddings/2))+'px'});
			if(testIE6){
				$('overlay').setStyle({'display':'block'});
				$('greyBox').setStyle({'display':'block'});
			}
			else{
				$('overlay').setOpacity(0);
				$('overlay').setStyle({'display':'block'});
				$('overlay').fade({
					duration:0.5,
					from:0,
					to:0.6,
					afterFinish:function(){
						$('greyBox').setStyle({'display':'block'});
					}
				});
			}
			break;
		default:
			break;
	}
	
	// On remonte le scroll en haut de page
	$$('body')[0].scrollTo();
	
	return false;
}

//greyBoxHide() : FERMETURE GREYBOX
function greyBoxHide(){
	var testIE6 = Prototype.Browser.IE6=Prototype.Browser.IE && parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE")+5))==6;
	if(testIE6){
		$('overlay').setStyle({'display':'none'});
		$('greyBox').setStyle({'display':'none'});
		$('greyBoxContent').innerHTML = '';
		$$('select').each(function(el){el.setStyle({'display':'block'});});
	}
	else{
		$('greyBox').fade({
			duration:0.4,
			from:1,
			to:0,
			afterFinish:function(){
				$('greyBoxContent').innerHTML = '';
				$('overlay').fade({
					duration:0.4,
					from:0.6,
					to:0
				});
			}
		});
	}
}
