// the array with all the attached articles
var articles_attached = new Array();
// the index number of the attached articles
var articles_attached_index = 0 ;
// the index number of the attached articles row
var empty_row_article_id = 0 ;

// the array with all the attached files
var files_attached = new Array();
// the index number of the attached files
var files_attached_index = 0;
// the index number of the attached files row
var empty_row_file_id = 0;

// the array with all the attached links
var links_attached = new Array();
// the index number of the attached links
var links_attached_index = 0;
// the index number of the attached links row
var empty_row_link_id = 0 ;

function init_articles()
{
	articles_attached = new Array();
	articles_attached_index = 0 ;
	empty_row_article_id = 0 ;
}

function init_links()
{
	links_attached = new Array();
	links_attached_index = 0;
	empty_row_link_id = 0 ;
}

function init_files()
{
	files_attached = new Array();
	files_attached_index = 0;
	empty_row_file_id = 0;
}

/**
* @desc     attach an article to another accordion table
* @author   Septimiu CERNAT
* @since    2009-10-23
* @param    string - tr_id - the basic id name of the attached article rows
* @param    string - id - the id of the article
* @param    string - name - the name of the article
* @param    string - input_id - the id of the html name for the input where the attached articles will be saved
*/
function attach_article(tr_id, id, name, input_id)
{
	// attach an article only it is not in the attached articles
	if (!in_array(id, articles_attached))
	{
		var row_count = empty_row_article_id;
		var row_id = tr_id + "_" + row_count;
		var row = $('#' + tr_id + '_' + row_count);
		row.find('td a').html(name);
		row.after('<tr id="' + tr_id + '_' + (row_count + 1) + '" style="display:none"><td><a href="javascript:;"></a></td></tr>')
			.hover(function(){
				over_table_row(row_id);
			},
			function(){
				out_table_row(row_id);
			})
			.click(function(){
				detach_article(tr_id, id, row_count, input_id);
			})
			.show();
		articles_attached_index = articles_attached.push(id);
		empty_row_article_id++;
		$('#'+input_id).val(articles_attached.join(','));
	}
}

/**
 * @desc
 * @author   Septimiu CERNAT
 * @since    2009-10-23
 */
function detach_article(tr_id, id, row_id, input_id)
{
	for (var index in articles_attached)
	{
		if (articles_attached[index] == id)
		{
			articles_attached.splice(index, 1);
			articles_attached_index--;
			$('#' + input_id).val(articles_attached.join(','));
			$('#' + tr_id + '_' + row_id).remove();
			break;
		}
	}
}

/**
 * attach file
 */
function attach_file(tr_id, id, name, input_id)
{
	if (!in_array(id, files_attached))
	{
		var row_count = empty_row_file_id;
		var row_id = tr_id + "_" + row_count;
		var row = $('#' + tr_id + '_' + row_count);
		row.find('td a').html(name);
		row.after('<tr id="' + tr_id + '_' + (row_count + 1) + '" style="display:none"><td><a href="javascript:;"></a></td></tr>')
			.hover(function(){
				over_table_row(row_id);
			},
			function(){
				out_table_row(row_id);
			})
			.click(function(){
				detach_file(tr_id, id, row_count, input_id);
			})
			.show();
		files_attached_index = files_attached.push(id);
		empty_row_file_id++;
		$('#'+input_id).val(files_attached.join(','));
	}
}

/**
 * detach file
 */
function detach_file(tr_id, id, row_id, input_id)
{
	for (var index in files_attached)
	{
		if (files_attached[index] == id)
		{
			files_attached.splice(index, 1);
			files_attached_index--;
			$('#' + input_id).val(files_attached.join(','));
			$('#' + tr_id + '_' + row_id).remove();
			break;
		}
	}
}

/**
 * attach link
 */
function attach_link(tr_id, id, name, input_id)
{
	if (!in_array(id, links_attached))
	{
		var row_count = empty_row_link_id;
		var row_id = tr_id + "_" + row_count;
		var row = $('#' + tr_id + '_' + row_count);
		row.find('td a').html(name);
		row.after('<tr id="' + tr_id + '_' + (row_count + 1) + '" style="display:none"><td><a href="javascript:;"></a></td></tr>')
			.hover(function(){
				over_table_row(row_id);
			},
			function(){
				out_table_row(row_id);
			})
			.click(function(){
				detach_link(tr_id, id, row_count, input_id);
			})
			.show();
		links_attached_index = links_attached.push(id);
		empty_row_link_id++;
		$('#'+input_id).val(links_attached.join(','));
	}
}

/**
 * detach link
 */
function detach_link(tr_id,id,row_id,input_id)
{
	for (var index in links_attached)
	{
		if (links_attached[index] == id)
		{
			links_attached.splice(index, 1);
			links_attached_index--;
			$('#' + input_id).val(links_attached.join(','));
			$('#' + tr_id + '_' + row_id).remove();
			break;
		}
	}
}

/**
 * over table row
 */
function over_table_row(id)
{
	$('#' + id).addClass('hover');
}

/**
 * out table row
 */
function out_table_row(id)
{
	$('#' + id).removeClass('hover');
}
