var timer;
var timerActus;
var timerCC;
var timerCCSmall;
var BASE_URL = "index.html";

Event.observe(window, 'load', function() {
        //alert($('service-list'));
        if($('service-list')){
                $('service-list').observe('click', function(event){
                        event.stopPropagation();
                        if(event.target.tagName == 'LI'){
                                toggleInfo(event.target.id);
                        }
                });
                timer = setTimeout('toggleInfo("sl_1")',6000);
        }
        
        if($("coupdecoeur-small")) {
            timerCCSmall = setTimeout('scrollSmallCCRight(1)',7500);

        }

        if($('small-actu-content')) {
            $('small-actu-content').innerHTML = news[0].title + news[0].content ;
            $('small-actu-content').observe('click',function(){
                window.location=BASE_URL+'actus/'+news[0].id;
            });
            timerActus = setTimeout('newsTransition(1)',7000);

        }

        if($("floating-bubble-simul") && $("IR")) {
            //Element.clonePosition($("floating-bubble-simul"), $("IR") , {setWidth:false,setHeight:false, offsetTop:17, offsetLeft:-85});
            var pos = Element.viewportOffset( $("IR"));
            $("floating-bubble-simul").style.left = pos[0]-85 + 'px';
            $("floating-bubble-simul").style.top = pos[1]+19 + 'px';
            setTimeout(function() {Effect.toggle("floating-bubble-simul","appear")},1500);
        }
        
        var leftCol = $$('.left-main');
        if(leftCol.length == 1) {
            var height = leftCol[0].getHeight();
            if(height>500) {
                var rightCol = $$('.right-main');
                if (rightCol.length>0) rightCol[0].style.height=(height-10)+"px";
            }
        }
});

var toggleInfo = function(el_id) {
    if(timer) clearTimeout(timer);
    var id = el_id.split("_");
    moveInfoArrowTo(id[1]);
    $('service-info').select("div").each(function(e){
        e.hide();
    });
    $('sld_'+id[1]).show();
    var next = parseInt(id[1]) == 5 ? 0 : parseInt(id[1]) + 1 ;
    timer = setTimeout('toggleInfo("sl_'+ next +'")',6000);
};

var moveInfoArrowTo = function(index) {
    new Effect.Move('sl-selected',{x: 5, y: (25*parseInt(index)+4), mode: 'absolute',duration:0.3});
}

var newsTransition = function(index) {
    var newIndex = index;
    $('small-actu-content').stopObserving('click');
    $('small-actu-content').observe('click',function(){
                window.location=BASE_URL+'actus/'+news[newIndex].id;
            });
    new Effect.BlindUp('small-actu-content',{duration:0.3,afterFinish:function(){
          $('small-actu-content').innerHTML = news[index].title + news[index].content ;
          
        new Effect.BlindDown('small-actu-content',{duration:0.3});
        
        index ++;
        if(index==news.length) index = 0;

        timerActus = setTimeout('newsTransition('+index+')',6000);
    }});
    
}

var scrollCCRight= function(index) {
    new Effect.Move('cc-scrolling', {x: -440, y: 0, mode: 'relative' ,afterFinish:function(){
          if ($('cc-scrolling').offsetLeft-440 == -$('cc-scrolling').getWidth()) {
              $('cc-scrolling').style.left = 0;
          }
    }
    });
    timerCC = setTimeout('scrollCCRight()',7500);
}

var scrollCCLeft= function(index) {
    new Effect.Move('cc-scrolling', {x: 940, y: 0, mode: 'relative' ,afterFinish:function(){
          if ($('cc-scrolling').offsetLeft == 0) {
              $('cc-scrolling').style.left = -$('cc-scrolling').getWidth()+940;
          }
    }
    });
    timerCC = setTimeout('scrollCCRight()',7500);
}

var pauseCCScroll = function() {
    timerCC = null;
}

var startCCScroll = function(){
     timerCC = setTimeout('scrollCCRight()',7500);
}

var scrollSmallCCRight= function(index) {
    new Effect.Move('cc-scrolling-small', {x: -190, y: 0, mode: 'relative' ,afterFinish:function(){
          if ($('cc-scrolling-small').offsetLeft-190 == -$('cc-scrolling-small').getWidth()) {
              $('cc-scrolling-small').style.left = 0;
          }
    }
    });
    timerCCSmall = setTimeout('scrollSmallCCRight()',7500);
}

//TODO pause/start small cc 
