var autostart = true;



$(document).ready(function(){ 

    var step      = 0;

    var width     = 165;

    var show      = 3;

    var items     = $('#partnerscroll').find('div').length;

    

       

    check(step, items, show); 

    

    $('#partnerscroll_next img').click(function(){     

        

        if(step + 3 < items)  step++; 

        var margin = step * width;      

        check(step, items, show); 

        $('#partnerscroll_' + (step - 1)).animate({

                'marginLeft': -margin

              }, 1000

        );

    }); 

    

    $('#partnerscroll_prev img').click(function(){     

        

        if(step > 0)  step--; 

        var margin = step * width; 

        check(step, items, show); 

        $('#partnerscroll_' + step).animate({

                'marginLeft': 0

              }, 1000

        );

    });

    

    $('#startoffer2 .links td').bind('mouseenter', function(){

        var pos = $(this).attr('axis');

        

        

        var margin = -pos * 580; 

        //init 

        $('#startoffer2 .container').css('left', margin);

        var rand1 = parseInt( Math.random() * 2);

        var rand2 = parseInt( Math.random() * 2);

        autostart = false;

        animate(pos, rand1, rand2);

        

        $('#startoffer2 .links td').removeClass('active');

        $(this).addClass('active'); 

    }); 

    

    currentProduct = 0

    

    $('#startproducts_navigation img').click(function(){

        var pos = $(this).attr('id');

         

        if(pos && pos.indexOf("_") >= 0){

            pos = pos.substr(pos.indexOf("_") + 1);

        }else{

           pos = 0;

        }

        

        var top  = (pos-1) * 400;

                

        $('#startproducts_content .first').css('top', -top);    

        $('#startproducts_navigation img').removeClass('active');

        $(this).addClass('active');

    })

    

    autorand(0);

    

    $('#niederlassungen .slider').cycle({timeout: 6000, speed: 2500});

})



function autorand(pos){

     if(!autostart) return;

     var offers = $('#startoffer2 .links td').length;

     if(pos >= offers) pos = 0;

     

     var rand1 = parseInt( Math.random() * 2);

     var rand2 = parseInt( Math.random() * 2);

     

     var margin = -pos * 580; 

     //init 

     $('#startoffer2 .container').css('left', margin);

     var rand1 = parseInt( Math.random() * 2);

     var rand2 = parseInt( Math.random() * 2);

     $('#startoffer2 .links td').removeClass('active'); 

     $('#startoffer2 .links td[axis="'+pos+'"]').addClass('active');

     

     var timer = $('#startoffer2 .offer_' + pos).attr('id');

     if(timer && timer.indexOf("_") >= 0 ){

         timer = parseInt(timer.substr(timer.indexOf("_") + 1));

     }

     if(!timer){

        timer = 8000;

     }

     

     animate(pos, rand1, rand2);

     pos++;

     window.setTimeout("autorand('"+pos+"')", timer);

}



function animate(pos, rand1, rand2){

    $('#startoffer2 .offer img').stop(true, true) ;

    

    var css_init = {width: 680};

    var css_anim = {width: 580}; 

    if(rand1){

        css_init.left = -90;

        css_anim.left = 0;

    }else{

        if(rand2){

            css_init.top = -30;

            css_anim.top = 0;

        }    

    }

    

    $('#startoffer2 .offer_' + pos + ' img').css(css_init);

    if(!jQuery.browser.msie) $('#startoffer2 .offer_' + pos + ' .inner').css({opacity: 0});

    //animation  

    $('#startoffer2 .offer_' + pos + ' img').animate(css_anim, 1500); 

    if(!jQuery.browser.msie){

        $('#startoffer2 .offer_' + pos + ' .inner').animate({opacity: 0}, 500, function(){

            $('#startoffer2 .offer_' + pos + ' .inner').animate({opacity: 1}, 700);

        });

    }

}





function check(step, items, show){

    if(step == 0){

        $('#partnerscroll_prev img').attr('src', '/templates/frontend/eisenfischer/_img/btn_left_0.gif');   

    }else{

        $('#partnerscroll_prev img').attr('src', '/templates/frontend/eisenfischer/_img/btn_left_1.gif'); 

    }

    if(items - step == show){

        $('#partnerscroll_next img').attr('src', '/templates/frontend/eisenfischer/_img/btn_right_0.gif');    

    }else{

        $('#partnerscroll_next img').attr('src', '/templates/frontend/eisenfischer/_img/btn_right_1.gif'); 

    }

}
