Ext.namespace( 'Index', 'Index.Kviz' );

Index.Kviz = {
    submitZh: function( button ) {
        var form = Ext.get( 'kerdoiv' ) ? Ext.get( 'kerdoiv' ) : Ext.get( 'kvizjatek' ); 
        form.load({
            method: 'post',
            params: { noauth: 1 },
            form: Ext.fly( button ).parent( 'form' ),
            success: function() {
                var form = Ext.get( 'kerdoiv' ) ? Ext.get( 'kerdoiv' ) : Ext.get( 'kvizjatek' ); 
                form.dom.scrollIntoView( true );
            },
            failure: function( response, opts ) {
                Ext.fly( button ).parent( 'form' ).child( '.errorMsg' ).show();
            }
        });
    },
    submitNyeremenyjatek:function ( button ) {
        Index.Kviz.submitZh( button );
    }
}

Index.TimeQuiz = {
    processing: false,
    start: function() {
        if( !this.seconds )
        {
            return;
        }
        
        Ext.Ajax.request({
            form: Ext.fly( 'startQuizForm' ),
            success: function( response, options ) {
                if( response.responseText === '__sessionLost__' )
                {
                    document.location.href = document.location.href;
                    return;
                }
                
                Ext.getDom( Ext.fly( 'timeQuizContainer' ) ).innerHTML = response.responseText;
                
                this.initTimer();
            },
            failure: function() {
                Ext.fly( 'timeQuizContainer' ).child( '.errorMsg' ).show();
            },
            scope: this
        });
    },
    task: {
        run: function() {
            if( Index.TimeQuiz.processing )
            {
                return false;
            }
            var span = Ext.fly( 'timeQuizContainer' ).child( 'span.num' );
            if( !span )
            {
                Ext.TaskMgr.stop( this );
                return;
            }
            var value = span.dom.innerHTML;
            var newValue = value - 1;
            span.update( newValue );
            if( newValue <= 5 )
            {
                if( !span.hasClass( 'alert' ) )
                {
                    span.addClass( 'alert' );
                }
                span.fadeOut({
                    endOpacity: 0,
                    easing: 'elasticBoth',
                    duration: .5,
                    remove: false,
                    useDisplay: false,
                    callback: function() {
                        span.show();
                        span.fadeOut({
                            endOpacity: 0,
                            easing: 'elasticBoth',
                            duration: .5,
                            remove: false,
                            useDisplay: false,
                            callback: function() {
                                span.show();
                            }
                        });
                     }
                });
            }
            if( newValue == 0 )
            {
                Ext.TaskMgr.stop( this );
                Index.TimeQuiz.submitQuestion();
            }
        },
        interval: 1000
    },
    initTimer: function() {
        Ext.TaskMgr.start.defer( 1000, Ext.TaskMgr, [ this.task ] );
    },
    submitQuestion: function( link ) {
        if( Index.TimeQuiz.processing )
        {
            return false;
        }
        Ext.fly( Ext.select( '.timekviz_tovabb' ).elements[0] ).addClass( 'disabled' );
        Ext.fly( Ext.select( '.timekviz_tovabb' ).elements[0] ).child( 'span' ).update( 'Küldés...' );
        Index.TimeQuiz.processing = true;
        Ext.TaskMgr.stop( this.task );
        Ext.Ajax.request({
            form: Ext.fly( 'quizQuestionForm' ),
            success: function( response, options ) {
                if( response.responseText === '__sessionLost__' )
                {
                    document.location.href = document.location.href;
                    return;
                }
                
                Ext.getDom( Ext.fly( 'timeQuizContainer' ) ).innerHTML = response.responseText;
                
                Index.TimeQuiz.processing = false;
                
                this.initTimer();
            },
            failure: function() {
                Ext.fly( 'timeQuizContainer' ).child( '.errorMsg' ).show();
            },
            scope: this
        });
    },
    checkForm: function( button ) {
        
        var errorMsg = [];
        var ok = true;
        Ext.fly( button ).parent( 'form' ).select( 'select' ).each( function( element ) {
            if( !element.getValue() )
            {
                errorMsg.push( element.getAttribute( 'errorMsg' ) );
                ok = false;
            }
        });
        
        var li = Ext.fly( Ext.fly( button ).parent( 'form' ).select( '.errorMsg' ).elements[0] );
        li.update( errorMsg.join( '<br />' ) );
        li.show();
        
        return ok;
    }
}

Index.MegyeKviz = Index.TimeQuiz;
