
$(document).ready(function(){
    HoofdsponsorRotator.init();
    createCKEditors();
});

function createCKEditors(){
    $( 'textarea.editor' ).each(function(){
        var editor = $(this);
        editor.ckeditor(function(){
            $(this.container).addClass(editor.className);
        }, {
            skin:'v2'
        });
    })
}

var HoofdsponsorRotator = {
    _container: null,
    _current: null,
    _rotating: false,
    
    init: function(){
        // Inital state
        HoofdsponsorRotator._container = $('#sponsorSlideshow');
        HoofdsponsorRotator._current = HoofdsponsorRotator._container.find('.hoofdsponsor').first()
        HoofdsponsorRotator._hideSponsors();
        HoofdsponsorRotator._current.show();
        HoofdsponsorRotator._current.addClass('active');  
        HoofdsponsorRotator._rotateSponsorSwitch();
    },
    
    hide: function (){
        HoofdsponsorRotator._container.hide();
    },
    
    show: function(){
        HoofdsponsorRotator._container.show();
        HoofdsponsorRotator._hideSponsors();
    },
    
    _hideSponsors: function (){
        HoofdsponsorRotator._container.find('.hoofdsponsor').hide();
    },
    
    _rotateSponsor: function(){
        if(!HoofdsponsorRotator._rotating){
            HoofdsponsorRotator._rotating = true;
            HoofdsponsorRotator._current = HoofdsponsorRotator._container.find('.active');
            var next = HoofdsponsorRotator._current.next();
            if(next.length == 0){
                next = HoofdsponsorRotator._container.find('.hoofdsponsor').first();
            }
            HoofdsponsorRotator._current.fadeOut(300, function(){
                next.fadeIn(300, function(){                
                    HoofdsponsorRotator._current.removeClass('active');
                    next.addClass('active');
                    HoofdsponsorRotator._rotating = false;
                });
            });
        }
    },
    
    _rotateSponsorSwitch: function(){		
        playSponsors = setInterval(function(){ 
            HoofdsponsorRotator._rotateSponsor();
        }, 3000);
    }
}
