jQuery(document).ready(function(){
    var visualdiv=$$('.visual')[0];
    var rotpub=visualdiv.select('.mags');
    var pagelinks=visualdiv.select('.pages li');
    var pause=false;
    var current=0;
    for(var i=rotpub.size()-1;i>=0;i--){
        rotpub[i].cleanWhitespace();
        rotpub[i].observe("mouseover",function(){
            pause=true
            });
        rotpub[i].observe("mouseout",function(){
            pause=false
            });
        rotpub[i].setStyle({
            'zIndex':rotpub.size()-1
            });
        var pub=rotpub[i].remove();
        visualdiv.insert(pub)
        };

    pagelinks.each(function(li,index){
        li.observe("click",function(){
            pause=true;
            if(current!=index){
                current=index;
                var pub=rotpub[index].remove();
                pub.setOpacity(100);
                pub.setStyle({
                    'left':0+'px'
                });
                visualdiv.insert(pub);
                pagelinks.invoke('removeClassName','active');
                pagelinks[index].addClassName('active')
                }
            })
    });
var rotate=function(index){
    var pub=rotpub[index].remove();
    rotpub[index].setOpacity(0);
    rotpub[index].setStyle({
        'left':760+'px'
    });
    visualdiv.insert(pub);
    element=rotpub[index];
    new Effect.Parallel([new Effect.Opacity(element,{
        sync:true,
        from:0,
        to:1
    }),new Effect.Move(element,{
        sync:true,
        x:0,
        y:0,
        mode:'absolute'
    })],{
        transition:Effect.Transitions.sinoidal,
        duration:1,
        delay:0.5,
        afterFinish:function(){
            if(pagelinks.size()){
                pagelinks.invoke('removeClassName','active');
                pagelinks[index].addClassName('active')
                }
            }
    })
};

if(pagelinks.size()){
    new PeriodicalExecuter(function(pe){
        if(!pause){
            current++;
            if(current>=rotpub.size())current=0;
            rotate(current)
            }
        },6)
};

//var prevmagbtn=$$('.magazines .gallery .btn-prev')[0];
//var nextmagbtn=$$('.magazines .gallery .btn-next')[0];
//var magcontent=$$('.magazines .mask ul')[0];
//var magcount=magcontent.select('li').size();
//var magcurrentindex=0;
//var movemagcar=function(nbr){
//    new Effect.Move(magcontent,{
//        x:(nbr*176),
//        y:0,
//        mode:'relative',
//        duration:.3,
//        queue:{
//            position:'end',
//            scope:'magcar'
//        },
//        transition:Effect.Transitions.sinoidal
//        })
//    };
//
//prevmagbtn.observe("click",function(){
//    if(magcurrentindex>0){
//        magcurrentindex--;
//        movemagcar(1)
//        }
//    });
//nextmagbtn.observe("click",function(){
//    if(magcurrentindex+4<magcount){
//        magcurrentindex++;
//        movemagcar(-1)
//        }
//    })
//
});
