﻿/// <reference path="ext.js" />

TestimonialsView = function() {
    this.divs = [];
    this.index = 0;
    this.content = { };
};

TestimonialsView.prototype = {
    init: function() {
        var l = Ext.DomQuery.select("div.lglobe");
        var c = l.length;

        for (var i = 0; i < c; i++) {
            this.divs.push({ el: Ext.get(l[i]) });
        }
        this.collapseAll();
//        this.slide();
    },

    slide: function() {
        if (this.index >= this.divs.length) return;
        var x = this.divs[this.index];
        x.el.slideIn('l', { duration: 0.5, callback: this.slide, scope: this });
        this.index++;
    },

    collapseAll: function() {
        var divs = Ext.DomQuery.select('div.testimonial');
        for (var i = 0; i < divs.length; i++) {
            this.collapse(divs[i]);
        }
    },

    collapse: function(div) {
        var id = Ext.id();
        var text = div.innerHTML;
        this.content[id] = text;
        div.id = id;
        div.innerHTML = this.shrinkText(text);
        var linkid = Ext.id();
        Ext.DomHelper.append(id, {
            tag: 'div', cls: 'readmore', children: [
                { id: linkid, tag: 'a', href: 'javascript:void(null);', children: [
                    { tag: 'span', html: 'read more' }
                ]
                }
            ]
        }, true);
        Ext.fly(linkid).on('click', this.expand.createDelegate(this, [id]), this);
    },

    expand: function(id) {
        Ext.fly(id).dom.innerHTML = this.content[id];
    },

    shrinkText: function(text) {
        var count = 1;
        var index = text.indexOf('.');
        index = text.indexOf('.', index + 1);
        index = text.indexOf('.', index + 1);
        if (index == -1) return text;
        return text.substring(0, index + 1) + '.. ';
    }
};

Ext.onReady(function() {
    var tv = new TestimonialsView();
    tv.init();
});

