Testimonials.LOADED = "Testimonials.LOADED";

function Testimonials() {
    var self = this;
    EventDispatcher.apply(self);
    self.testimonialList = []; //Armazena uma lista com os testimonials a serem exibidos
    self.testimonialIndex = 0; //Controla a posição do cursor na lista de testimonials

    /**
     * importa os "testimonials" do arquivo testimonials.php no formato JSON
     */
    self.loadTestimonialFeed = function() {
        $.getJSON(pathURL+"/testimonials.php", function(data){
          self.testimonialList = data;
          self.dispatchEvent(new TestimonialsEvent(Testimonials.LOADED));
        });
        return self;
    }

    /**
     * Captura o testimonial do cursor atual
     * @type Testimonial
     */
    self.currentTestimonial = function() {
        return new Testimonial(self.testimonialList[self.testimonialIndex]);
    }

    /**
     * Incrementa em 1 o valor do cursor do testimonial
     * e retorna o Testimonial do cursor atual
     * @type Testimonial
     */
    self.getNextTestimonial = function() {
        self.testimonialIndex++;
        (self.testimonialIndex >= self.testimonialList.length)
        && (self.testimonialIndex = 0);
        return self.currentTestimonial();
    }

    /**
     * Decrementa em 1 o valor do cursor do testimonial
     * e retorna o Testimonial do cursor atual
     * @type Testimonial
     */
    self.getPreviousTestimonial = function() {
        self.testimonialIndex--;
        (self.testimonialIndex < 0)
        && (self.testimonialIndex = self.testimonialList.length - 1);
        return self.currentTestimonial();
    }
    
    self.loadTestimonialFeed();
    
    return self;
}

function Testimonial(data) {
    var self = this;

    self.data = data;
    
    self.getName = function() {
        return self.data.name;
    }

    self.getOccupation = function() {
        return self.data.occupation;
    }

    self.getInstitution = function() {
        return self.data.institution;
    }

    self.getText = function() {
        return self.data.text;
    }
    
    return self;
}

function TestimonialsEvent(type) {
    this.type = type;
    this.toString = function() {
        return "[TestimonialsEvent Object]";
    }
}
