Event.observe(window, "load", initMain);

function initMain()
{
    setup_news();
    setup_customer();
    setup_tabs();
    bandeauResize(null);
    Event.observe(window,"resize",bandeauResize);
}

function setup_news() {
	if(!$("actu_stepshow"))
		return;
    var actuShow = new M4StepShow("actu_stepshow","actu_container","actu_step",true,1);
    actuShow.controller = true;
    var playtime = 4;
    var t;
    var itemNb = $$('#actu_controller a').length;

    $$("#actu_controller a").each(function(a){
        Event.observe(a,"click",stepHandler);
    });

    $('actu_controller').setStyle({"left":(439-(itemNb*21)/2)+"px"});

    playStep(null);

    Event.observe($("actu_container"),"mouseout",playStep);


    function playStep(evt) {
        t = new PeriodicalExecuter(function(t){
            actuShow.next();
        },playtime);

         Event.observe($('actu_container'),"mouseover",function(evt){
            t.stop();
        });
    }
    

    function stepHandler(evt) {
        Event.stop(evt);
        t.stop();
        playStep(null);
        var index = evt.target.id.substr(evt.target.id.length-1);
        actuShow.display(Number(index));
    }

}

function setup_customer() {
	if(!$("cust_stepshow"))
		return;
    var custStepshow = new M4StepShow('cust_stepshow','cust_container','cust_step',false,6);

    $$('#cust_controller a').each(function(a){
        Event.observe(a,"click",navHandler);
    });

    function navHandler(evt) {
        Event.stop(evt);
        var target = evt.target.nodeName == 'IMG' ? evt.target.parentNode : evt.target;
        switch(target.rel) {
            case "prev":
                custStepshow.previous();
                break;
            case "next":
                custStepshow.next();
                break;
            default:
                return;
                break;
        }
    }
}


function setup_tabs() {
	if(!$("tabcontent-second"))
		return;
   $('tabcontent-second').setStyle({"opacity":"0"});

   Event.observe($('first_tab'),"mouseover",tabHandler);
   Event.observe($('second_tab'),"mouseover",tabHandler);


    function tabHandler(evt) {
        if(evt.target.hasClassName('tab_on'))
            return;

        var target1, target2;

        switch(evt.target.id) {
            case "first_tab":
                target1 = $('tabcontent-first');
                target2 = $('tabcontent-second');
                $('first_tab').className = 'tab_on';
                $('second_tab').className = 'tab_off';
                break;
            default:
                target1 = $('tabcontent-second');
                target2 = $('tabcontent-first');
                $('first_tab').className = 'tab_off';
                $('second_tab').className = 'tab_on';
            break;
        }

        M4Tween.to(target2,.3,{"opacity":"0"}).onComplete(function(){
            M4Tween.to(target1,.3,{"opacity":"1"});
        });

    }
}

function bandeauResize(evt) {
    var screenWidth = document.viewport.getWidth();
    var custWidth = $('cust_controller').offsetWidth;
    $('bg_cust').setStyle({width:screenWidth+'px',left:-(screenWidth-custWidth)/2+'px'});
}

function isIPad(){
    return navigator.platform == "iPad";
}
