var masterident;
var lightJsWin;
var lightJsWinChroniqueuse;

var slideShow;
var aSliceActive;
var flashvars = {};
var params = {allowFullScreen:"false", scale:"noscale",wmode:"transparent"};
var attributes = {id:"background", name:"background"};


swfobject.embedSWF("ui/flash/Background.swf", "my_flash", "960", "600", "9", "expressInstall.swf", flashvars, params, attributes);
swffit.fit("background", 960, 600);

needToRedirect = false;

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

	changeLink();
	
	var url = document.location.href;
	
	if(url.indexOf('#')===-1){
		
		
		if(url.indexOf('/en/')!==-1){
			
			var ident = '/en/';
			var arr = url.split('/en/');
						
		}else{
			
			var ident = '/fr/';
			var arr = url.split('/fr/');

		}
		
		
		
		if(arr.length>1){
			ident +=arr[1];
		}else{
			
			var a = $H({fr:'/fr/accueil',en:'/en/home'});
			
			ident=a.get($j('html').attr('xml:lang').substring(0,2));
			
			
			
		}
		
		//on retrouve lurl de base
		var urlbase = url.replace(ident,'');
		
		
		//on lui ajoute le hash
		url = urlbase+(url.indexOf('#')===-1?'#':'')+ident;
		
		
		var oldurl = document.location.href;
		

		
		
		if(urlbase==oldurl){//si lurl de base est le meme la page ne sera pas reloader, alors on lance la requete ajax a la place
			//on enleve le # pour faire la requete ajax
			var urlaj = url.replace('/#','');
			aj(urlaj,'');
		}else{
			//on redirige, pour revenir a lurl de base avec un hash
			//document.location.href =url;
			needToRedirect = true;
			new Ajax.Request('flashsection?section='+ident,{onSuccess:function(rep){changeSection(rep.responseText);}});
		}
				
	}else{
		
		//on enleve le # pour faire la requete ajax
		var urlaj = url.replace('/#','');


		if(urlaj!=$('pageurl').innerHTML){
			aj(urlaj,'');
		}else{
			if(url.indexOf('/en/')!==-1){
		
				var ident = '/en/';
				var arr = url.split('/en/');
					
			}else{
		
				var ident = '/fr/';
				var arr = url.split('/fr/');

			}
	


			new Ajax.Request('flashsection?section='+ident+arr[1],{onSuccess:function(rep){changeSection(rep.responseText);}});

		}

		
	}

});


aj = function(url,identflash){


	
	
	
	/*if(slideShow){
		slideShow.stopAutoScroll();
		slideShow = null;
	}*/
	
	
	if(url.indexOf('/en/')!==-1){
		
		var ident = '/en/';
		var arr = url.split('/en/');
					
	}else{
		
		var ident = '/fr/';
		var arr = url.split('/fr/');

	}
	
	
	
	if(arr.length>1){
		ident +=arr[1];
	}else{
		ident='';
	}
	
	
	if(ident){	
		if(needToRedirect){


			jQuery.ajax(
				{
					
					url:'changesection?section='+ident,
					success:function(){
						document.location.href = arr[0]+'/#'+ident;	
					}
				}

			);

			return false;
		}else{
			pageTracker._trackPageview(ident);
			document.location.hash = ident;	
		}
	}
	
	
	
	
	
	
	el=$j('#body_content');
	
	el.fadeOut();

	
	
	
	if(!identflash){
		new Ajax.Request('flashsection?section='+ident,{onSuccess:function(rep){identflash = rep.responseText;changeSection(identflash);masterident=identflash;}});
	}else{
		changeSection(identflash);
	}

	if(identflash){
		masterident=identflash
	}
	
	
	
	el.load(url+'/',function(e){
/*
		if(identflash=='accueil'){
			new PeriodicalExecuter(function(pe){
				initSlideShow();
				pe.stop();
			},.2);	
			
		}else{
			if(slideShow){
				slideShow.stopAutoScroll();
			}
		}
*/
		changeLink();
		el.fadeIn('slow');
		document.title = $$('h1')[0].innerHTML;
		if($('linkRelFacebook')){
			$('linkRelFacebook').href = $j('#linkUrl').html();
		}
		if(!masterident){
			masterident='accueil';
		}
		$$('body')[0].className=masterident;

	});
	
}


changeLink = function(){
	
	$$('a.ajlink').each(function(el){
		var href = el.href;
		Event.observe(el,'click',function(e){aj(href,el.id.split('_')[0])});
		el.href='javascript:;';
		
	});
	
	
}



popupLook = function(lookurl){
	
	if(!lookurl){
		return false;
	}
	
	
	if(!lightJsWin){
		lightJsWin=new jsWin({cssSkin:'jsWinDefaultSkin2',width:350,height:500,modal:true,hideTagObjects:true,onCloseCallback:'showObjectTags'});
	}
	lightJsWin.setContentFromUrl(lookurl);

	
}

addVote = function(a,url){
	
	var aj = new Ajax.Request(url);
	aj.options.onSuccess = function(rep){
		
		$(a).replace(rep.responseText);
		
		
	}
	
}


initSlideShow = function(){	
			
		//slideShow = new k3Carousel('caroussel_ul', 700,{previous_button_content:'<img src="ui/img/boutons/btn_nav_g.png" alt="Précedent" />',next_button_content:'<img src="ui/img/boutons/btn_nav_d.png" alt="Suivant" />'});
		//slideShow.startAutoScroll(5);

}
/*
scrollCarousel = function(a,index){
	//aSliceActive =a;
//	slideShow.carousel.scrollTo(index);
}


Event.observe(document,'carousel:scroll:ended',function(){
	
	$$('#chiffres ul li a').invoke('removeClassName','selected');
	if(aSliceActive){
		var aLink = aSliceActive;
		aSliceActive=null
	}else{
		var aLink = $('carouselitem_'+Math.ceil(slideShow.carousel.currentIndex()));
	}
	
	aLink.addClassName('selected');
	
});
*/


popupChroniqueuse  = function(chrUrl){
	

	if(!lightJsWinChroniqueuse){
		
		lightJsWinChroniqueuse=new jsWin({cssSkin:'jsWinDefaultSkin3',width:350,height:454,modal:true,hideTagObjects:true,onCloseCallback:'showObjectTags'});
	}
	
	lightJsWinChroniqueuse.setContentFromUrl(chrUrl);

	
	
	
}




