
column_total = 3;
$(document).ready(function() {
	if($('.allNews').val()>3) { //if there are more than 3 calendar items, we must iterate
		slide_calendar();
	} else {
		if ($('.allNews').val() >= 1){
			$('#calendar_unit1').clone().appendTo('.calendar_units');
		} 
		if ($('.allNews').val() >= 2){
			$('#calendar_unit2').clone().appendTo('.calendar_units');
		}
		if ($('.allNews').val() == 3){
			$('#calendar_unit3').clone().appendTo('.calendar_units');
		}
	}
});

//function to erase all unneeded calendar units 
function hideNews(){
	
	$('.calendar_units').empty();
}
//function to move units
function slide_calendar(){
		calculate();
		showNews();
		$.timer(5000, function (timer) {
			calculate();
			hideNews();
			showNews();
		});
}
function calculate(){
	var active=$('#active').val();
	active=parseInt(active);
	var res = $('.allNews').val() - active;
	if (res == 1){
		one = active;
		two = active+1;
		three = 1;
		$('#active').val(active+1);
	} else if (res == 0){
		one = active;
		two = 1;
		three = 2;
		$('#active').val(1);
	} else {
		one = active;
		two = active+1;
		three = active+2;
		$('#active').val( active+1);
	}
}
function showNews(){
	
	$('#calendar_unit'+one).clone().appendTo('.calendar_units');
	$('#calendar_unit'+two).clone().appendTo('.calendar_units');
	$('#calendar_unit'+three).clone().appendTo('.calendar_units');
	

}


 //////////////////////////////////////////////////////////////////////////////
/*
 *
 *	jQuery Timer plugin v0.1
 *		Matt Schmidt [http://www.mattptr.net]
 *
 *	Licensed under the BSD License:
 *		http://mattptr.net/license/license.txt
 *
 */
 
 jQuery.timer = function (interval, callback)
 {
 /**
  *
  * timer() provides a cleaner way to handle intervals  
  *
  *	@usage
  * $.timer(interval, callback);
  *
  *
  * @example
  * $.timer(1000, function (timer) {
  * 	alert("hello");
  * 	timer.stop();
  * });
  * @desc Show an alert box after 1 second and stop
  * 
  * @example
  * var second = false;
  *	$.timer(1000, function (timer) {
  *		if (!second) {
  *			alert('First time!');
  *			second = true;
  *			timer.reset(3000);
  *		}
  *		else {
  *			alert('Second time');
  *			timer.stop();
  *		}
  *	});
  * @desc Show an alert box after 1 second and show another after 3 seconds
  *
  * 
  */

	var interval = interval || 100;

	if (!callback)
		return false;
	
	_timer = function (interval, callback) {
		this.stop = function () {
			clearInterval(self.id);
		};
		
		this.internalCallback = function () {
			callback(self);
		};
		
		this.reset = function (val) {
			if (self.id)
				clearInterval(self.id);
			
			var val = val || 100;
			this.id = setInterval(this.internalCallback, val);
		};
		
		this.interval = interval;
		this.id = setInterval(this.internalCallback, this.interval);
		
		var self = this;
	};
	
	return new _timer(interval, callback);
 };
 //////////////////////////////////////////////////////////////////////////////
