$(document).ready(function() {

    function BackgroundRotator() {
        // internal
        this._changeInternal = null;
        this._isRunning = false;
        this._currentIndex = 1;
        this._numFlash = 1;
        this._isFocus = true;
        this._currentImage = 'img1';
        
        // public
        this.delay = 8000;
        
        this.init = function() {
            this._onResize();
            var _self = this;
            $(window).resize(function() {
	            _self._onResize();
            });
            $('#bg img').load(function() {
                _self._onResize();
            });
            return this;
        };
        
        this.start = function() {
            var _self = this;
            clearInterval(this._changeInternal);
            this._changeInternal = setInterval(function() {
                _self._onChange();
            }, 7000);
            this._isRunning = true;
        };
        
        this._onChange = function() {
            var lastImage = this._currentIndex;
            this._currentImage = 'img' + this.next();
            
            $('#bg li img').hide();
            this._onResize();
            
            this._numFlash++;
            
            if (this._numFlash > this._getTotal()){
                if (this._isFocus){
                    $('#bg .' + this._currentImage + ' img.focus').fadeIn(500);
                }
                else{
                    $('#bg .' + this._currentImage + ' img.blur').fadeIn(500);
                }
            }
            else{
                $('#flash').show();
			    $('#flash').fadeOut(300);
			    
			    if (this._isFocus) {
				    $('#bg .' + this._currentImage + ' img.focus').show();
			    } else {
				    $('#bg .' + this._currentImage + ' img.blur').show();
			    }
            } 
        };
        
        this._onResize = function() {
	        var winHeight 	= $(window).height();
	        var offSet 		= ($(window).height() - $('#bg img').height()) / 2;
	        $('#bg .' + this._currentImage + ' img').css('marginTop', offSet + "px" );
        };
        
        this.stop = function() {
            this._isRunning = false;
            clearInterval(this._changeInternal);
        };
        
        this.next = function(){
            this._currentIndex++;
            if(this._currentIndex > this._getTotal())
               this._currentIndex=1;
            return this._currentIndex;
        };
        this._getTotal = function() {
            return $('#bg li').length;
        };
        
        this.focus = function() {
            this._isFocus = true;
            
            $('#bg .' + this._currentImage + ' img.focus').fadeIn(300);
		    $('#bg .' + this._currentImage + ' img.blur').fadeOut(300);
        };
        
        this.blur = function() {
            this._isFocus = false;
            
            $('#bg .' + this._currentImage + ' img.focus').fadeOut(300);
		    $('#bg .' + this._currentImage + ' img.blur').fadeIn(300);
        };
    }
    
    window.BackgroundRotator = BackgroundRotator;
});

