/*--FUNCTIONS.JS--*/

function pngFix(){
	$$(".pngFix").each(function(el){
		$(el).firstChild.style.visibility = "hidden";
		$(el).style.display = "block";
		$(el).style.width = $(el).firstChild.width+"px";
		$(el).style.height = $(el).firstChild.height+"px";
		$(el).style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+$(el).firstChild.src+"', sizingMethod='crop');";
	});
}

//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').innerHTML = '<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.4,
					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.4,
					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.4,
					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.4,
					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.4,
					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.4,
					to:0
				});
			}
		});
	}
}

Event.observe(window,'load',function(){

	//Sliding pubs
	/*var pubItemsId = 0;
	
	new PeriodicalExecuter(function(){
		pubItemsId++;
		if(!$$('#pubArea .pubItem')[pubItemsId]){
			pubItemsId = 0;
		}
		$$('#pubArea .pubItem').each(function(el){
			$(el).removeClassName('active');
		});
		$$('#pubArea .pubItem')[pubItemsId].addClassName('active');
	}, 10);*/

	//Opacity on disabled pictures
	$$('.disable').each(function(el){
		el.setOpacity(0.2);
	});
	
	//Affichage du vainqueur
	/*var i = 0;
	var score1 = 0;
	var score2 = 0;
	$$('.block .scoreboard table .score_value').each(function(el){
		
		var cycle = Math.floor(i/3);
		
		if(cycle%2 == 0){
			if(el.innerHTML != ''){
				score1 = score1 + parseInt(el.innerHTML);
				//console.log(score1);
			}
		}
		else{
			if(el.innerHTML != ''){
				score2 = score2 + parseInt(el.innerHTML);
				//console.log(score2);
			}
		}
		
		i++;
		
		if(i%6 == 0){
			if(score1 > score2){
				//console.log(el.ancestors()[1].previous().immediateDescendants()[0]);
				el.ancestors()[1].previous().immediateDescendants()[0].insert({
					bottom: '<img src="/templates/1/medias/img/scoreboard_victory.gif" alt="" class="winner" />'
				});
			}
			else if(score2 > score1){
				el.ancestors()[1].immediateDescendants()[0].insert({
					bottom: '<img src="/templates/1/medias/img/scoreboard_victory.gif" alt="" class="winner" />'
				});
			}
			score1 = 0;
			score2 = 0;
		}
	});*/

	//newsHighLight gestion
	compteur = 1;
	$$('#newsSpotlight .content').each(function(el){
		el.id = 'content_'+compteur;
		compteur++;
	});

	compteur = 1;
	$$('#newsSpotlight ul li').each(function(el){
		el.id = 'tab_'+compteur;
		Event.observe(el, 'click', function(){
			if(!$(el).hasClassName('active')){
				$$('#newsSpotlight ul li').each(function(el){
					$(el).removeClassName('active');
				});
				$(el).addClassName('active');
				var stockContentName = $(el).id.substr(4,$(el).id.length);
				if($('content_'+stockContentName)){
					$$('#newsSpotlight .content').each(function(el){
						$(el).removeClassName('active');
					});
					$('content_'+stockContentName).addClassName('active');
				}
			}
		});
		compteur++;
	});
	
	if($("dailymotionContent")){
		$$('.video_home').each(function(el){
			Event.observe(el,'click',function(event){
				if(!$(el).hasClassName('active')){
					$$('.video_home').each(function(el2){
						$(el2).removeClassName('active');
					});
					$(el).addClassName('active');
				}
				new Ajax.Updater($('dailymotionContent'), '../../../templates/1/ajax/video_home.php', {
					method: 'post' , 
					evalScripts: true , 
					parameters:{id_video : $(el).id}
				});
				
				event.stop();
				return false;
			});
		});
	}
});
