﻿Type.registerNamespace('Appulate.WebUI.Controls');

Appulate.WebUI.Controls.TestimonialRotator = function(element) {
	Appulate.WebUI.Controls.TestimonialRotator.initializeBase(this, [element]);
	this._currentTestimonialId = null;
	this._handlerUrl = null;
	this._product = null;
	this._general = null;
	this._showMode = null;
	this._intervalId = null;
	this._delay = null;
	this._tickHandler = null;
	this._getJsonCompleteHandler = null;
}
Appulate.WebUI.Controls.TestimonialRotator.prototype = {
	initialize: function() {
		Appulate.WebUI.Controls.TestimonialRotator.callBaseMethod(this, 'initialize');
		this.set_delay(8000);
		this._tickHandler = Function.createDelegate(this, this.getRandomTestimonial);
		this._getJsonCompleteHandler = Function.createDelegate(this, this.getJsonComplete);
		var intervalId = window.setInterval(this._tickHandler, this.get_delay());
		this.set_intervalId(intervalId);
	},
	dispose: function() {
		window.clearInterval(this.get_intervalId());
		this._tickHandler = null;
		this._getJsonCompleteHandler = null;
		Appulate.WebUI.Controls.TestimonialRotator.callBaseMethod(this, 'dispose');
	},
	getRandomTestimonial: function() {
		var url = this.get_handlerUrl();
		var now = new Date();
		var milliseconds = now.getTime().toString();
		var data = {
			excludeId: this.get_currentTestimonialId(),
			product: this.get_product(),
			showGeneral: this.get_general(),
			noCache: milliseconds
		};
		$.getJSON(url, data, this._getJsonCompleteHandler);
	},
	getJsonComplete: function(data, textStatus) {
		if (textStatus != 'success') {
			return;
		}
		var comments = data.comments;
		var customerName = data.customerName;
		var customerCompany = data.customerCompany;
		var $testimonial = this.get_showMode() == 0 ? $('div.testimonial') : $('div.testimonial-small');
		$testimonial.find('.cite').animate({ opacity: 0.1 }, 200, 'swing', function() {
			$(this).text(comments).animate({ opacity: 1 }, 200, 'swing');
		});
		$testimonial.find('.auth').animate({ opacity: 0.1 }, 200, 'swing', function() {
			$(this).find('.auth-name').text(customerName).parent().find('.auth-title').text(customerCompany).parent().animate({ opacity: 1 }, 200, 'swing');
		});
		this.set_currentTestimonialId(data.id);
	},
	get_currentTestimonialId: function() {
		return this._currentTestimonialId;
	},
	set_currentTestimonialId: function(value) {
		this._currentTestimonialId = value;
	},
	get_handlerUrl: function() {
		return this._handlerUrl;
	},
	set_handlerUrl: function(value) {
		this._handlerUrl = value;
	},
	get_product: function() {
		return this._product;
	},
	set_product: function(value) {
		this._product = value;
	},
	get_general: function() {
		return this._general;
	},
	set_general: function(value) {
		this._general = value;
	},
	get_showMode: function() {
		return this._showMode;
	},
	set_showMode: function(value) {
		this._showMode = value;
	},
	get_intervalId: function() {
		return this._intervalId;
	},
	set_intervalId: function(value) {
		this._intervalId = value;
	},
	get_delay: function() {
		return this._delay;
	},
	set_delay: function(value) {
		this._delay = value;
	}
}
Appulate.WebUI.Controls.TestimonialRotator.registerClass('Appulate.WebUI.Controls.TestimonialRotator', Sys.UI.Control);