var myGallery;
var showSearch;

function addLoadEvent(func) 
{
	var oldonload = window.onload;
	if (typeof window.onload != 'function')	window.onload = func;
	else window.onload = function() {
										oldonload();
										func();
									}	
}

function init() 
{
	/*
	if(window.webkit420){
		//alert("safari 3" + $$('body').getStyle('fontSize'));
		$$('body').setStyles({	fontSize:'70%' });
	}
	*/


	// init de la galerie
	if($('myGallery'))
		initGallery();	
		
	// init de la popup des manifestations
	if($('manifs_popup')){		
	    var myFx = new Fx.Style('manifs_popup', 'opacity').set(0); //display a transition from transparent to opaque.
		$('manifs_popup').addEvent('click', function() {showManifs();});
	}
	
	
	// getion du menu
	if($('fancymenu'))
		FancyExample = new SlideList($E('ul', 'fancymenu'), {transition: Fx.Transitions.quadOut, duration: 500, onClick: function(ev, item) { ev.stop(); }});
	
	
	// gestion des sous-navs
	if($('ss_nav_0'))
	{		
		for(var i = 0; i<3 ; i++)
		{
			if($("ss_nav_"+i))
			{
				
				$("ss_nav_"+i).getElements('li').each(function(item,j){		
												 
						item.addEvent('mouseover', function(event){ onMouseOver(event, item,j); }.bind(this));
						item.addEvent('mouseout', function(event){ onMouseOut(event, item,j);}.bind(this));
						item.addEvent('click', function(event){ onClick(event, item,j); }.bind(this));
						
					}.bind(this));	

				$("content_"+i).setStyles({	visibility:'hidden',
										 	display:'block'
										 });
			}
			if($("ss_menu"))
			{
				
				$("ss_menu").getElements('li').each(function(item,j){	
												 
						item.addEvent('mouseover', function(event){ onMouseOver(event, item,j); }.bind(this));
						item.addEvent('mouseout', function(event){ onMouseOut(event, item,j);}.bind(this));
						item.addEvent('click', function(event){ onClick(event, item,j); }.bind(this));
					}.bind(this));	
			}
		}	
		// affiche le premier content
		$("content_"+0).setStyles({	visibility:'visible'});		
	}
	
	
	
	
	// preload des background	
	preload_bg();	
	
	// hack pour firefox (bug carousel)
	//window.onresize = resize;
	
	
	
	// gestion des liens externes
	$each(document.links, function(item){
		item = new Element(item);
		if(item.hasClass('external'))
		{	
			item.setProperty('target', '_blank');
		}
		
	}, this);
	
	// affiche manifs_popup
	if($('manifs_popup'))
		$("manifs_popup").setStyles({	display:'block'});		
	
	
	
}


function showManifs(){	
	if($('manifs_popup').getStyle("opacity").toInt() >= 1 ){
		 var myFx = new Fx.Style('manifs_popup', 'opacity').start(1,0); //display a transition from transparent to opaque.
	}else{
		 var myFx = new Fx.Style('manifs_popup', 'opacity').start(0,1); //display a transition from transparent to opaque.
	}	
}


function resize()
{
 	location.href=location.href;
}

function preload_bg()
{
 	var bg_arr = 	['images/skin/bg-home.jpg',
					 'images/skin/bg-appart.jpg',
					 'images/skin/bg-contact.jpg',
					 'images/skin/bg-grange.jpg',
					 'images/skin/bg-liens.jpg',
					 'images/skin/bg-plan.jpg',
					 'images/skin/bg-pres.jpg',
					 'images/skin/bg-tarifs.jpg'
					 ];					 
					 
	new Asset.images(bg_arr, {
		onComplete: function(){
			//alert('all images loaded!');
			if($("loading"))
				$("loading").setStyle('visibility', 'hidden');
		},
		onProgress: function(){
			//alert('onProgress!');
		},
		onload: function(){
				//alert('onload!');
		}
	});	
}

function initGallery(){

	myGallery = new gallery($('myGallery'), {
						//timed: false
						timed: ($('carousel_container'))? false : true,
						useExternalCarousel: ($('carousel_container'))? true : false,
						carouselElement: ($('carousel_container'))? $('carousel_container') : false,
						useThumbGenerator: ($('carousel_container'))? true : false,
						carouselPreloader: ($('carousel_container'))? true : false,
						thumbCloseCarousel:false,
						showCarouselLabel:false,
						showCarousel:($('carousel_container'))? true : false,
						delay: 5000
					});
	
	if($('myGallery_100'))
		 $('myGallery_100').setStyle('display', 'none');
}


function onMouseOver(event, item){
	var src = item.getElements('a img').getProperty('src');
	src = sReplace(src.toString(),'_out','_over');
	item.getElements('a img').setProperty('src', src);
}
function onMouseOut(event, item){
	item = new Element(item);
	if(!item.hasClass('current'))
	{					 
		var src = item.getElements('a img').getProperty('src');
		src = sReplace(src.toString(),'_over','_out');
		item.getElements('a img').setProperty('src', src);
	}
}
function onClick(event, item,j){	
	var divContainer = $(item.parentNode.parentNode);
	var divNewContainer = $("content_"+j);
	if(divContainer!=divNewContainer)
	{
		// on masque le conteneur
		var myFx = new Fx.Style(divContainer, 'opacity').start(1,0); //display a transition from transparent to opaque.
		
		// on affiche le conteneur masqué
		//divNewContainer.setStyles("display:block;opacity:0;");
		
		var myFx = new Fx.Style(divNewContainer, 'opacity').start(0,1); //display a transition from transparent to opaque.	
	}
	
	/*
	item = new Element(item);
	var id = item.getElements('a img').getProperty('id');
	if(id=="manifs"){
		 showManifs();
	}	
	*/
}
function sReplace(expr,a,b){
	var i=0
	while (i!=-1) {
	 i=expr.indexOf(a,i);
	 if (i>=0) {
		expr=expr.substring(0,i)+b+expr.substring(i+a.length);
		i+=b.length;
	 }
	}
	return expr;				
}



window.addEvent('domready', init);

//addLoadEvent(init);


