﻿var SentenceOfTheDay = {
    refresh: function(_params, divId, timeout) {
        setTimeout(function() {
            $.post('/TestimonialService.asmx/Load', _params, function(xml) {
                $('#' + divId).fadeOut(function() {
                    $('#' + divId).fadeIn();
                    TypeImitation.imitate($("#" + divId), $(xml).find('Text').text().replace(/\n/g, "").replace(/\x5Bbr\x5D/g, '\n'), 0);
                    //print
                });
                SentenceOfTheDay.refresh({
                    contentType: _params.contentType,
                    langId: _params.langId,
                    lastId: $(xml).find('DocumentId').text()
                }, divId, $(xml).find('SecondsToDisplay').text());
            });
        }, timeout * 1000);
    }
}

var TypeImitation = {
    timeout: null,
    msPerSymbol: 125,

    imitate: function(div, str, pos) {
        clearTimeout(this.timeout);
        this.timeout = null;
        var len = str.length;
        if (pos <= len) {
            div.html(str.substr(0, pos).replace(/\n/g, "<br/>") + (pos < len - 1 ? "_" : ""));
            this.timeout = setTimeout(function() { TypeImitation.imitate(div, str, pos + 1); }, this.msPerSymbol);
        }
    }
}