// articlegroup_create takes two or more arguments:
// 1) The article_id for which we create the group
// 2) a list of article_group_ids to join
// It joins the groups,  sorts the articles and filters 
// away doublets and the article it self. 
// It returns an Array object with the related articles

function feature_create() {
    if(document.article_features) {

	var article_id = arguments[0];
	var sort_method = arguments[1];
	var related_articles = new Array();
	for(var i = 2; i < arguments.length; i++) {
	    related_articles = related_articles.concat(document.article_features[arguments[i]]);
	}

	if(sort_method == 'priority') {
	    related_articles.sort(function(a,b) { return (a.priority < b.priority ? -1 : 1) });
	} else {
	    related_articles.sort(function(a,b) { return (a.publishing_time < b.publishing_time ? 1 : -1) });
	}

 	// remove doublets

 	var filtered_articles = new Array();

 	var j=0;

 	for (i=0; i < related_articles.length; i++) {
 	    if ((i == related_articles.length - 1 || related_articles[i].article_id != related_articles[i+1].article_id) && related_articles[i].article_id != article_id) {
 		filtered_articles[j] = related_articles[i];
 		j++;
 	    }
 	}

	return filtered_articles;
    }
}


// merge_relarts merges a number of arrays of articles - either article_groups ar feature_articles.

function merge_relarts () {

    var related_articles = new Array();
    for(var i = 0; i < arguments.length; i++) {
	related_articles = related_articles.concat(arguments[i]);
    }

    related_articles.sort(function(a,b) { return (a.publishing_time < b.publishing_time ? 1 : -1) });

    return related_articles;
}

