// ********************************************************************************
// Homepage Hero Shot

$(document).ready(function () {

	var wdm = {
        currentPos : 1,
        rotationSpeed : ('undefined' != typeof(rotationSpeed) ? rotationSpeed : 5),
        rotation : null,
        maxPos : 5,
        delimiter : '-',
        className : 'wdm-',
        inProcess : false,
        setElement : function (elem, pos) {
            return elem + '.' + this.className + pos;
        },
        init : function (options) {
            $.extend(this, options);

            $('#wdm-nav-link li a').click( function() {
                var _current = $(this).parent('li').attr('class');
                 wdm.next( parseInt(_current.split(wdm.delimiter)[1]) );
                return false;
            });

            this.maxPos = $("#wdm-nav-link li a").length;

            this.startRotation();
        },

        startRotation : function () {
             this.rotation = setInterval( function(){ wdm.next(); },  this.rotationSpeed * 1000);
        },

        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('normal', 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();
                    });
            });
        }

    };

    wdm.init({
        // maximum rotation
        //maxPos : 5,
        // equals to 15 seconds
        rotationSpeed : ('undefined' != typeof(rotationSpeed) ? rotationSpeed : 5)
    });

});

// End: Homepage Hero Shot
