/**
 * Accordion for document list (Finance and Return blocks)
 * @param {Object} element_list
 * @param {Object} container
 */
function Accordion (element_list, container) {
	var animation_in_progress = false;
	var current_index = 0;
	var container = container;
	var list = [];
	var list_length = 0;
	var heights = [];
	var _self = this;
	
	/* Open handle */
	this.open = function (element_index) {
		if (element_index == current_index) return;
		
		var _tmp_index_2 = element_index;
		
		animation_in_progress = true;
		container.animate({height: list[element_index]['max_height']});
		
		list[element_index]['trigger'].removeClass('closed');
		list[element_index]['content'].slideDown(function () {
			animation_in_progress = false;
			current_index = _tmp_index_2;
		});
	};
	/**
	 * Close handle
	 * 
	 * @param {Boolean} no_open If true then container element height is changed
	 */
	this.close = function (no_open) {
		if (current_index == -1) return;

		animation_in_progress = true;
		
		if (no_open)
		{
			container.animate({height: tmp_height + 'px'});
		}
		
		list[current_index]['trigger'].addClass('closed');
		list[current_index]['content'].slideUp(function () {
			animation_in_progress = false;
		});
		current_index = -1;
	};
	
	
	/* Constructor */
	for(var i=0, j=element_list.length; i<j; i++)
	{
		var element = $(element_list.get(i));
		var element_id = element.attr('id').substr(8);
		var content_node = $('#content-' + element_id);
		
		list[i] = {'trigger': element, 'id': element_id, 'index': i, 'content': content_node};
		/*
		 * trigger - h2 element which will trigger open/close fucntions
		 * element_id - id
		 * index - index in the element list
		 * content - content element 
		 */
		
		heights[i] = content_node.height();
		
		if (i != 0)
		{
			content_node.addClass('el_closed');
			element.addClass('closed');
		}
		
		element[0].accordion_index = i;
		element.click(function () {
			if (animation_in_progress) return;

			if (current_index != this.accordion_index)
			{
				_self.close(false);	//don't change container height, since 'open()' will do it
				_self.open(this.accordion_index);
			}
			else
			{
				_self.close(true);	//change container height 
			}
			
			return false;
		});
	}
	
	var tmp_height = 50 + 35 + 37 * element_list.length;		//50 - space at the bottom by design, 35 - title height, 37 - height of the heading + space
	for(var i=0, j=element_list.length; i<j; i++)
	{
		list[i]['max_height'] = tmp_height + heights[i] + 'px';
	}
	
	container.css({height: list[current_index]['max_height']});
}

