$(document).ready(function() {
// Executed once all the page elements are loaded

	lastVal = totHistory;

	/*
	 * Slider erstellen
	 */
	$("#slider").slider({

			value: 		3,
			min: 		1,
			max: 		totHistory,
			animate: 	true,
			slide: 		function(event, ui) {

							if(lastVal > ui.value)
								$(buildQ(lastVal, ui.value)).hide('fast');
							// Using buildQ to build the jQuery selector
							// If we are moving the slider backward, hide the previous comment


							else if(lastVal < ui.value)
								$(buildQ(lastVal,ui.value)).show('fast');
							// Otherwise show it

							lastVal = ui.value;
						}
	});
});

var totHistory = 0;
// Holds the number of comments

var positions = new Array();
var lastVal;

function addHistory(obj) {

	/*
	 * Gets called on page load for each comment, and on comment submit
	 */
	totHistory++;
	positions.push(obj.id);
}

function buildQ(from,to) {

	/*
	 * Building a jQuery selector from the begin
	 * and end point of the slide
	 */

	if(from > to) {
		var tmp = to;
		to = from;
		from = tmp;
	}

	from++;
	to++;

	var query = '';

	for(var i = from; i < to; i++) {

		if (i != from) {
			query += ',';
		}

		query += '.com-' + positions[i - 1];
	}

	/*
	 * Each comment has an unique com-(Comment ID) class
	 * that we are using to address it
	 */

	return query;
}

function transForm(text,cText) {

	var tmpStr = '<span class="name">Demo:</span> ' + text;
	cText.html(tmpStr);
}
