////////////////////////////////////////////////////////////////////////////////
//                            INITS                                           //
////////////////////////////////////////////////////////////////////////////////
//alert(navigator.appName+"   "+navigator.appVersion);

var window_h = null;
var window_w = null;
var illus = new Array();

var horizontalCurtains = false;
var background = null;

var xm = null;
var ym = null;

var home_page = false;

initElements = function(){
	
    //Element.hide('content');
    //Element.hide('background');
	//animCurtains = true;
	
}

onload = function(){
    background = document.getElementById("background").src;
    correctPNG();
    resizeBackground();
    
    //if (animCurtains) initCurtains();
    
}


////////////////////////////////////////////////////////////////////////////////
//                            FONCTIONS   image de fond                       //
////////////////////////////////////////////////////////////////////////////////

onresize = resizeBackground;

function resizeBackground(){ 
	
	if( typeof( window.innerWidth ) == 'number' ) {	//Non-IE
		window_h = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) { //IE 6+ in 'standards compliant mode'
		window_h = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {	//IE 4 compatible
		window_h = document.body.clientHeight;
	}
	window_w = document.getElementById("background").offsetWidth;
	
	//window_h = $('background').getHeight();
	//window_w = $('background').getWidth();
	
	if ($('background_pict')){
		bg_img_w = $('background_pict').getWidth();
		bg_img_h = $('background_pict').getHeight();
		//bg_img_w = document.getElementById("background").width;
		//bg_img_h = document.getElementById("background").height;
		delta = bg_img_w/bg_img_h;
		

		if ( document.getElementById("background_pict").style.height!= null ){
			if ((window_w/window_h)>delta) Element.setStyle("background_pict", { width: px(window_w), height: px(window_w/delta) } );
			else                           Element.setStyle("background_pict", { width: px(window_h*delta), height: px(window_h) } );
		}
		Element.show('background_pict');  
	}    
    if ($('content_links') && $('content_main')){
    	if ($('content_main').getHeight()<(window_h)) $('content_main').setStyle({height: px(window_h-80) });
    }
}


////////////////////////////////////////////////////////////////////////////////
//                            FONCTIONS   actualité                           //
////////////////////////////////////////////////////////////////////////////////


var actualities = null;
var ydelta = 72;
var actuTimeout = null;
var actuOpened = false;

function initActuality(){
    if ( browser.isDOM1 
    && !( browser.isMac && browser.isIE ) 
    && !( browser.isOpera && browser.versionMajor < 7 )
    && !( browser.isIE && browser.versionMajor < 5 ) ) {
       
       if (!home_page) addAnEvent(document.getElementById('actuality_zone'),'mouseover',showActuality);
       //addAnEvent(document.getElementById('actuality_zone'),'mouseout',actuSurvey);
        var actuality = document.getElementById('actuality'); // the root element
        actualities = actuality.getElementsByTagName('li'); // all the li
        for ( var i=0; i<4; i++ ){
			if ( actualities.item(i).getElementsByTagName('span').length > 0 ) { 
				if (!home_page) addAnEvent(actualities.item(i).getElementsByTagName('span')[0],'mouseover',actuSurvey);
				if (!home_page) addAnEvent(actualities.item(i).getElementsByTagName('span')[0],'mouseout',actuSurvey);
				//actualities.item(i).getElementsByTagName('span')[0].hide();
				Element.hide(actualities.item(i).getElementsByTagName('span')[0]);
			}
		}
    }
}

function showActuality(){
	if (!actuOpened){
		actuOpened = true;
		for ( var i=0; i<4; i++ ){
			if ( actualities.item(i).getElementsByTagName('span').length > 0 ) {   
				tps = (3-i)*0.5;
				new Effect.Move(actualities.item(i), {delay:tps, duration: .5, y:ydelta, mode: 'relative'});
				new Effect.Appear(actualities.item(i).getElementsByTagName('span')[0], {delay:tps+0.4, duration: .5 } );
				//new Effect.Grow(actualities.item(i).getElementsByTagName('span')[0], {delay:1.5, duration: 1, direction: 'bottom-left'});
			}
		}
		if (!home_page) actuTimeout=setTimeout("actuSurvey()",4000);
		//setTimeout("showActuTitles()",2000);
	}
}

function hideActuality(){
	for ( var i=0; i<4; i++ ){
		if ( actualities.item(i).getElementsByTagName('span').length > 0 ) {
			tps = i*0.5;
			new Effect.Fade(actualities.item(i).getElementsByTagName('span')[0], {delay:tps, duration: .5 } );
			new Effect.Move(actualities.item(i), {delay:tps+0.4, duration: .5, y:-ydelta, mode: 'relative'});
		}
	}
	setTimeout("actuOpened = false;", 4000);
	
}

function actuSurvey(){
	clearTimeout(actuTimeout);
	actuTimeout=setTimeout("hideActuality()",4000);
}



////////////////////////////////////////////////////////////////////////////////
//                            FONCTIONS   menu dynamique                      //
////////////////////////////////////////////////////////////////////////////////

var timeout = 300;

for( var i = 0; i < 100; i++ ){
    eval("var timeoutli" + i + " = false;");
}

function initMenu(){
    if ( browser.isDOM1 
    && !( browser.isMac && browser.isIE ) 
    && !( browser.isOpera && browser.versionMajor < 7 )
    && !( browser.isIE && browser.versionMajor < 5 ) ) {
        var menu = document.getElementById('menu'); // the root element
        var lis = menu.getElementsByTagName('li'); // all the li
        
        menu.className='menu';
        
        for ( var i=0; i<lis.length; i++ ){
            if ( lis.item(i).getElementsByTagName('ul').length > 0 ) {        
                if ( browser.isIE ){
                    addAnEvent(lis.item(i),'keyup',show);
                }
                addAnEvent(lis.item(i),'mouseover',show);
                addAnEvent(lis.item(i),'mouseout',timeoutHide);
                addAnEvent(lis.item(i),'blur',timeoutHide);
                addAnEvent(lis.item(i),'focus',show);
                
                // add an id to list item
                lis.item(i).setAttribute( 'id', "li"+i );
            }
        }
    }
}

function addAnEvent( target, eventName, functionName ){
    if ( browser.isIE ){
        //attachEvent dont work properly with this
        eval('target.on'+eventName+'=functionName');
    }
    else    {
        target.addEventListener( eventName , functionName , true ); // true is important for Opera7
    }
}
    
function timeoutHide(){
    eval( "timeout" + this.id + " = window.setTimeout('hideUlUnder( \"" + this.id + "\" )', " + timeout + " );");
}

function hideUlUnder( id ){   
    document.getElementById(id).getElementsByTagName('ul')[0].style['visibility'] = 'hidden';
    var as = document.getElementById(id).getElementsByTagName('a');
    for ( var i=0; i<as.length; i++ ){
        as.item(i).className="";
    }
}

function show(){
    this.getElementsByTagName('ul')[0].style['visibility'] = 'visible';
    /*
    lis = this.getElementsByTagName('ul')[0].getElementsByTagName('li');
   for ( var i=0; i<lis.length; i++ ){
	    lis.item(i).hide();
	    new Effect.Appear(lis.item(i), {delay:0.05*i, duration: .5 } );
    }
    */
    var currentNode=this;
    while(currentNode)   {
            if( currentNode.nodeName=='LI'){
                currentNode.getElementsByTagName('a')[0].className = 'linkOver';
            }
            currentNode=currentNode.parentNode;
    }
    eval ( "clearTimeout( timeout"+ this.id +");" );
    hideAllOthersUls( this );
}

function hideAllOthersUls( currentLi ){
    var lis = currentLi.parentNode;
    for ( var i=0; i<lis.childNodes.length; i++ )   {
        if ( lis.childNodes[i].nodeName=='LI' && lis.childNodes[i].id != currentLi.id ){
            hideUlUnderLi( lis.childNodes[i] );
        }
    }
}

function hideUlUnderLi( li ){
    var as = li.getElementsByTagName('a');
    for ( var i=0; i<as.length; i++ ){
        as.item(i).className="";
    }
    var uls = li.getElementsByTagName('ul');
    for ( var i=0; i<uls.length; i++ ){
        uls.item(i).style['visibility'] = 'hidden';
    }
} 


////////////////////////////////////////////////////////////////////////////////
//                            FONCTIONS   souris et liens                     //
////////////////////////////////////////////////////////////////////////////////


function openWindow(url,nom,w,h){
        x = (screen.width-w)/2;
        y = (screen.height-h)/2;
        window.open(url, '_blank', "left="+x+",top="+y+", height="+h+",width="+w+",status=no,toolbar=no,menubar=no,location=no, resizable=yes,scrollbars=yes");
}


function openMedia(id_media){
    lien = document.createElement("a");
    lien.href = "/show_media/"+id_media;
    lien.className = 'resizespeed_5 blocksize_512x384';

    //initModalbox();
    myModalbox = new Modalbox();
    myModalbox.start(lien);
}

function openImage(url){
    var lien = document.createElement("a");
	lien.setAttribute("rel","lightbox");
	lien.setAttribute("href",url);
    myLightbox = new Lightbox();
    myLightbox.start(lien);
}

function px(x){
        return ''.concat(Math.round(x), 'px');
}






