﻿function CustomerCommentsGroup() {
	this.elements = [];
	
	this.show = function() {
		for(var i=0; i<this.elements.length; i++)
			this.elements[i].fadeIn(250);
	};
	this.hide = function() {
		for(var i=0; i<this.elements.length; i++)
			this.elements[i].fadeOut(250);
	};
	this.setElemsDisplayStyle = function(value) {
		
	};
}

function CustomerComments(container, showInterval, hideInterval) {
	this.container = container;
	this.showInterval = showInterval;
	this.hideInterval = hideInterval;
	this.groups = [];
	this.activeGroupIndex = -1;
	this.timer = null;
	this.showMode = false;
	
	this.init = function() {
		var childList = this.container.children();
		var currentGroup = null;
		for(var i=0; i<childList.length; i++) {
			var elem = childList.get(i);
			var tagName = elem.tagName.toLowerCase();
			if ('h3' == tagName) {
				if (currentGroup != null) 
					this.groups.push(currentGroup);
				currentGroup = new CustomerCommentsGroup();
			}
			if (currentGroup != null) 
				currentGroup.elements.push($(elem));
		}
		if (currentGroup != null)
			this.groups.push(currentGroup);
	};
	
	this.onShowTimer = function() {
		this.activeGroupIndex++;
		if (this.activeGroupIndex >= this.groups.length)
			this.activeGroupIndex = 0;
		this.groups[this.activeGroupIndex].show();
		this.showMode = true;
		this.startTimer();
	};
	this.onHideTimer = function() {
		this.groups[this.activeGroupIndex].hide();
		this.showMode = false;
		this.startTimer();
	};
	
	this.startTimer = function() {
		if (this.showMode)
			this.timer = setTimeout(jQuery.proxy(function(){this.onHideTimer()}, this), this.showInterval);
		else 
			this.timer = setTimeout(jQuery.proxy(function(){this.onShowTimer()}, this), this.hideInterval);
	};
	
	this.init();
	
	if (this.groups.length > 0)
		this.onShowTimer();
}

$(document).ready(function() {
	new CustomerComments($('#customer_comment'), 5000, 500);
});

