﻿/// <reference name="MicrosoftAjax.debug.js" />
/// <reference name="MicrosoftAjaxTimer.debug.js" />
/// <reference name="MicrosoftAjaxWebForms.debug.js" />

/// <reference path="jquery.intellisense.js"/>

/// <reference path="Kleenex.Core.js"/>

Kleenex.QuickRelease = function(clientId, questionId) 
{
    this.ClientID = clientId;
    this.QuestionID = questionId;
    this.Selector = "#" + clientId;
    this.Initialize();
};
Kleenex.QuickRelease.prototype =
{
    Name : 'Kleenex.QuickRelease',
    __typeName : 'Kleenex.QuickRelease',
    __class : true,
    Interval : undefined,
    IntervalDelay : 10000,
    AttachEvents : function()
    {
        var __app = this;
        
        var navigation = $(this.Selector + ' .Navigation a');
        
        // previous link
        $(navigation[0]).click(function()
        {
            this.blur();
            __app.StopInterval();
            __app.ShowPreviousAnswer();
            // restart the interval after 30 seconds
            setTimeout(function()
            {
                __app.StartInterval();
            }, __app.IntervalDelay * 2);
        });
        
        // next link
        $(navigation[1]).click(function()
        {
            this.blur();
            __app.StopInterval();
            // restart the interval after 30 seconds
            __app.ShowNextAnswer();
            setTimeout(function()
            {
                __app.StartInterval();
            }, __app.IntervalDelay * 2);
        });
        
        var submitAnswer = function()
        {
            this.blur();
            var firstName = $(__app.Selector + ' .Form input[type="text"]').val();
            var answerText = $(__app.Selector + ' .Form textarea').val();
            
            if (firstName === '' || answerText === '')
            {
                // TODO show an error message
                return;
            }
            
            var handleResponse = function()
            {
                var delay = 250;
                $(__app.Selector + ' .Form').fadeOut(delay, function()
                {
                    $(__app.Selector + ' .Response').fadeIn(delay, function()
                    {
                        setTimeout(function()
                        {
                            $(__app.Selector + ' .Response').fadeOut(delay, function()
                            {
                                $(__app.Selector + ' .Form textarea').val('');
                                $(__app.Selector + ' .Form').fadeIn(delay);
                            });
                        }, 10000);
                    });
                });
            };
            
            __app.QuickReleaseService.SaveAnswer(__app.QuestionID, firstName, answerText, handleResponse);
        };
        
        $(this.Selector + ' .Form a').click(submitAnswer);
        
        setTimeout(function()
        {
            __app.StartInterval();
        }, this.IntervalDelay);
    },
    ShowPreviousAnswer : function()
    {
        var index = this.CurrentIndex - 1;
        if (index < 0)
        {
            index = this.Answers.length - 1;
        }
        this.ShowAnswer(index);
    },
    ShowNextAnswer : function()
    {
        var index = this.CurrentIndex + 1;
        if (index >= this.Answers.length)
        {
            index = 0;
        }
        this.ShowAnswer(index);
    },
    ShowAnswer : function(index)
    {
        var __app = this;
        
        var answer = this.Answers[index];
        var delay = 600;
        
        $(this.Selector + ' .Answer').hide();
        $(__app.Selector + ' .Answer').empty().append(answer.AnswerText).show();
        $(this.Selector + ' .Name').hide();
        $(__app.Selector + ' .Name').empty().append('- ' + answer.Author).show();
        
        this.CurrentIndex = index;
    },
    StartInterval : function()
    {
        var __app = this;
        
        this.StopInterval();
        this.Interval = setInterval(function()
        {
            __app.ShowNextAnswer();
        }, __app.IntervalDelay);
    },
    StopInterval : function()
    {
        clearInterval(this.Interval);
        this.Interval = undefined;
    },
    Display : function()
    {
        var __app = this;
        
        $(this.Selector + ' .Answer').empty();
        $(this.Selector + ' .Name').empty();
        
        var counterId = this.ClientID + '_charcounter';
        $(this.Selector + ' span.charcounter:first').attr('id', counterId);
        $(this.Selector + ' .Form textarea').charCounter(120, 
            { container : '#' + counterId, format : '(%1)' });
        
        var onSuccess = function(answers)
        {
            if (answers !== undefined && answers.length > 0)
            {
                __app.Answers = answers;
                $(__app.Selector + ' .Answer').empty().append(__app.Answers[0].AnswerText);
                $(__app.Selector + ' .Name').empty().append('- ' + __app.Answers[0].Author);
                __app.CurrentIndex = 0;
                setTimeout(function()
                {
                    __app.StartInterval();
                }, __app.IntervalDelay);
            }
        };
        
        this.QuickReleaseService.GetAnswers(this.QuestionID, onSuccess);
    }
};

Kleenex.Extend(Kleenex.QuickRelease, Kleenex.UserControl);
