var wdm = {

    currentPos : 1,
    
    rotationSpeed : 20,
    
    rotation : null,
    
    maxPos : 5,
    
    delimiter : '-',
    
    className : 'wdm-',
    
    inProcess : false, 
    
    setElement : function (elem, pos) {
        return elem + '.' + this.className + pos;
    },
    
    init : function (options) {
        $.extend(this, options);
        
        // events
        $('#next').attr('class','next').click( function() {
            wdm.next();
            return false;
        });
    
        $('#prev').attr('class','prev').click( function() {
            wdm.prev();
            return false;
        });
    
        $('#wdm-nav-link a').click( function() {
            var _current = $(this).parent('li').attr('class');
             wdm.next( parseInt(_current.split(wdm.delimiter)[1]) );
            return false;
        });
        
        this.startRotation();
    },
    
    startRotation : function () {
         this.rotation = setInterval( function(){ wdm.next(); },  this.rotationSpeed * 500);
    },
    
    stopRotation : function() {
        window.clearInterval(this.rotation);
    },
    
    next : function(nextPos) {
        var _next = nextPos || this.currentPos + 1;
        
        if ( _next > this.maxPos ) _next = 1;
       
        if ( this.inProcess || $(this.setElement('div', _next)).length < 1 ) return;
        
        this.inProcess = true;
        this.stopRotation();
       
        $( this.setElement('div', this.currentPos) ).fadeOut('100', function() {
            $(this).addClass('hide');
            $(wdm.setElement('li', wdm.currentPos)).removeClass('active');
                $( wdm.setElement('div', _next) ).fadeIn('normal', function() {
                    $(this).removeClass('hide');
                    $(wdm.setElement('li', _next)).addClass('active');
                    wdm.currentPos = _next;
                    wdm.inProcess = false;
                    wdm.startRotation();
                });
        });
    },
    
    prev : function(prevPos) {
        var _prev = prevPos || this.currentPos - 1;
        
         if ( _prev < 1 ) _prev = this.maxPos;
       
        if ( this.inProcess || $(this.setElement('div', _prev)).length < 1 ) return;
        
        this.inProcess = true;
        this.stopRotation();
        
        $( this.setElement('div', this.currentPos) ).fadeOut('normal', function() {
            $(this).addClass('hide');
            $(wdm.setElement('li', wdm.currentPos)).removeClass('active');
                $( wdm.setElement('div', _prev) ).fadeIn('normal', function() {
                    $(this).removeClass('hide');
                    $(wdm.setElement('li', _prev)).addClass('active');
                    wdm.currentPos = _prev;
                    wdm.inProcess = false;
                    wdm.startRotation();
                });
        });
    }
    
};

$(document).ready(function () {
    wdm.init({
        // maximum rotation
        maxPos : 5,
        // equals to 15 seconds
        rotationSpeed : 20
    });
});