/**
 * @author Viames Marino
 */

jQuery(document).ready(function() {

	var speed = 150;

	/* Action Plan */
	
	/*jQuery('.hp-month').click(function () {
		if (jQuery(this).is('.hp-close')) {
			jQuery(this)
				.removeClass('hp-close')
				.addClass('hp-open');
			jQuery(this)
				.children('.hp-events')
				.slideUp(speed);
		} else {
			jQuery('.hp-events').slideUp(speed);
			jQuery('.hp-month')
				.not(this)
				.removeClass('hp-close')
				.addClass('hp-open');
			jQuery(this)
				.removeClass('hp-open')
				.addClass('hp-close');
			jQuery(this)
				.children('.hp-events')
				.slideDown(speed);
		}
	});*/
	
	jQuery('.month').click(function () {
		if (jQuery(this).is('.close')) {
				jQuery(this)
				.removeClass('close')
				.addClass('open');
		if (first){
					jQuery('.month')
				.not(this)
				.removeClass('close')
				.addClass('open');
			jQuery(this)
				.children('.events')
				.slideDown(speed);
				first=false;
				}
		else {


				jQuery(this)
				.children('.events')
				.slideUp(speed);
				}

		} else {
			jQuery('.events').slideUp(speed);
			jQuery('.month')
				.not(this)
				.removeClass('close')
				.addClass('open');
			jQuery(this)
				.removeClass('open')
				.addClass('close');
			jQuery(this)
				.children('.events')
				.slideDown(speed);
		}
	});
	
	/* timeline agenda */
	
	var timeline = jQuery('#timeline');
	var timelineWidth = timeline.width();
	var boxWidth = jQuery('#timeline-box').width();
	var limit = timelineWidth - boxWidth;
	var step = 500; // intervallo di spostamento in pixel
	
	jQuery.fn.position = function() {
		var value = jQuery(this[0]).css("left");
		var position = parseInt(value.substring(0,value.indexOf('px')));
		return 0-position;
	};
	    
	jQuery('#ctl-left').click(function () {
		var position = timeline.position();
		if (position - step >= 0) {
			timeline.animate({'left': - position + step + 'px'}, 500);
		} else if (position > 0) {
			timeline.animate({'left': 0 + 'px'}, 500);
		}
	});

	jQuery('#ctl-right').click(function () {
		var position = timeline.position();
		if (position + step < limit) {
			timeline.animate({'left': - position - step + 'px'}, 500);
		} else if (position <= limit) {
			newPosition = - limit + 'px';
			timeline.animate({'left': - limit + 'px'}, 500);
		}
	});

});
