document.observe("dom:loaded", function() {
    // chargements des produits
    loadProducts();
    // Diaporama
    $$('#diaporama .elements')[0].style.width=(($$('#diaporama .element').length*980)+10)+'px';
    var be=$$('#diaporama .element');
	var bn=$('diaporama_navigation');
	be.each(function(e){
        var sp=new Element('span')
        if (ill=as=e.select('img.illustration')[0]){
            var src=ill.src.split('?')[0]+'?min=45&c=45,45,eaeaea';
        } else {
            src='/app/img/empty.gif';
        }
        var ne=new Element('img',{'src':src})
        ne.onclick=function(ev){
            if(!ev) var ev=window.event;var tg=(window.event)?ev.srcElement:ev.target;
            var idxn=$$('#diaporama_navigation img').indexOf(tg);
            selectionDiaporamaElement(idxn);
        }
        sp.insert(ne);
		bn.insert(sp);
	});
    if (tmp=$$('#diaporama_navigation span')[0])tmp.addClassName('current');
    if (be.length>1) window.setTimeout(function(){selectionDiaporamaElement()},5000);
    // détails produits
    $$('a.product').each(function(e){
        Event.observe(e, 'click', function(event) {
            productsDetailsOpen(e);
        });
    })
    // actualités
    for(i=0; i<$$('.newsElement').length ; i++){
        var a=new Element('a',{'class':'pin','href':'#'});
        a.onclick=function(){newsDisplay(this); return false;}
        $$('#news p.pins')[0].insert(a);
        if (i==0) a.addClassName('current');
        a.insert(new Element('img',{'src':'/app/img/empty.gif'}))
    }
});
selectionDiaporamaElement=function(idxn){
    window.clearTimeout(wtoBE);
    if ($$('.dpOpened').length==0){
        if(typeof(idxn)=='undefined'){
            var tmp=$$('#diaporama .element').indexOf($$('#diaporama .current')[0])
            if ((tmp+selectionDiaporamaElementSens>= $$('#diaporama .element').length) || (tmp+selectionDiaporamaElementSens<0)) selectionDiaporamaElementSens=-selectionDiaporamaElementSens;
            idxn=tmp+selectionDiaporamaElementSens;
    	}
    	var ben=$$('#diaporama .element')[idxn];
        var l1=parseInt($$('#diaporama .element')[idxn].offsetLeft);
        $$('#diaporama .element').invoke('removeClassName','current');
        $$('#diaporama_navigation span').invoke('removeClassName','current');
        new Effect.Morph($$('#diaporama .elements')[0], {
    		duration:.5,
            style:{'left':(-l1+980)+'px'},
    		afterFinish: function(){
                $$('#diaporama .element')[idxn].addClassName('current')
                $$('#diaporama_navigation span')[idxn].addClassName('current')
            }
        });
    }
    wtoBE=window.setTimeout(function(){selectionDiaporamaElement()},5000); 
    return
}
var wtoBE;
var selectionDiaporamaElementSens=1;
selectionDiaporamaElementEnabled=true;

loadProducts=function(){
    url='/pages/homepage/ajax/loadProducts.php';
    new Ajax.Request(url, {
    method: 'get',
    parameters:'lg='+LANG,
        onSuccess: function(r) { //debug.print(r.responseText);// return
            p=r.responseJSON;
            for(var x in r.responseJSON){
                if (ul=$$('.'+p[x].cat +' ul')[0]) {
                    var li=new Element('li',{'class':'p'});
                    ul.insert(li);
                    if (p[x].img != '') li.setStyle({'backgroundImage':'url(/msmedias/produits/img/'+p[x].img+'?max=50)'})
                    li.insert(new Element('a',{'href':p[x].url}).update(p[x].title))
                    li.insert(new Element('input',{'type':'hidden','value':p[x].ind}))
                }
            }
            $$('.productsDetails li').each(function(e){
                var industries=e.select('input')[0].value;
                industries.split(',').each(function(ind){
                    if ((ind !='') && (ul=$$('.'+ind +' ul')[0])) ul.insert(e.cloneNode(true));
                })
            });
            $$('.productsDetails ul').each(function(e){
                var c=e.select('li').length;
                var rang=parseInt(c/3);
                e.style.width=Math.max(832,((rang+1)*160)+20)+'px'
            })      
        }
    });    
}
productsDetailsOpen=function(e){
    var idx=$$('#productsNav a').indexOf($(e));
    if (dpOpened=$$('.dpOpened')[0]){
        productsDetailsClose($$('.dpOpened')[0]);
        if (dpOpened== $$('.productsDetails')[idx]) return;
    }
    if (d=$$('.productsDetails')[idx]){
        slideshowEnabled=false;
        d.addClassName('dpOpened');
        d.setOpacity(0);
        new Effect.Morph(d, {
            style:'top:5px; height:315px; opacity:1',
            duration:.75
    	});
        selectionDiaporamaElementEnabled=false;
    }
}
productsDetailsClose=function(opened){
    $(opened).removeClassName('dpOpened');
    new Effect.Morph($(opened), {
        style:'top:350px;  height:0; opacity:0',
        duration:.50
    });  
    selectionDiaporamaElementEnabled=true;
}
productsDetailsMove=function(e){
    var a=$(e);
    var p=a.parentNode
    var ul=p.select('ul')[0]
    var sens=-1;if (a.hasClassName('mvLeft')) sens=+1
    var x=parseInt(ul.style.left);
    if (((sens==1) && (x==0)) || ((sens==-1)&&(800-x>ul.offsetWidth))) {
        new Effect.Shake(ul,{distance:2, duration:.2});
        return false
    }
    var step=ul.select('li')[0].offsetWidth+12;
    var newX=x+(step*sens)
    new Effect.Morph(ul, { style:'left:'+newX+'px', duration:.3});
    return true;
}

// actualites 
newsDisplay=function(e){
    if (newsDisplaying) return;
    var crntLink=$$('#news p.pins a.current')[0];
    var crntIdx=$$('#news p.pins a').indexOf(crntLink);
    var crntNews=$$('#news .newsElement')[crntIdx];
    var newsLink=$(e);
    var newsIdx=$$('#news p.pins a').indexOf($(e));
    if (typeof ($$('#news .newsElement')[newsIdx])=='undefined') return
    var newNews=$$('#news .newsElement')[newsIdx];
    //debug.print(newNews.offsetTop ); 
    newsDisplaying=true
    crntLink.removeClassName('current');
    newsLink.addClassName('current');
    new Effect.Morph($('newsElements'), {
        style: 'top:-'+newNews.offsetTop+'px', 
        duration: 0.5, 
        afterFinish: function(){
             newsDisplaying=false;
        }
    });
}
var newsDisplaying=false
newsInit=function(){
	var bd=$('newsElements');
	bd.select('div.newsElement').each(function(e){
		e.onmouseover=function(){
			newsEffectCancelled=true
		}
		e.onmouseout=function(){
			if (newsEffectCancelled) {
				newsEffectCancelled=false
			}
		}
	})
	bd.style.top='0px';
	bandeauWTI=window.setTimeout(newsMove,5);
}


newsMove=function(){
	if (newsEffectCancelled==false){
        var bd=$('newsElements');
		var el=$$('#newsElements div.newsElement')[0];
        bd.style.top=(parseInt(bd.style.top)-2)+'px'
    	if (debug) debug.print(el.offsetTop)
		if (Math.abs(bd.offsetTop) > el.offsetHeight){
			var e=new Element('div',{'class':'newsElement'});
			e.innerHTML=el.innerHTML;
			bd.style.top=(parseInt(bd.style.top)+el.offsetHeight)+'px'
			el.remove();
			$('newsElements').insert(e);
		}
	}
	window.setTimeout(newsMove,70)	
}
var newsEffectCancelled=false
