function getCrossListValue(groupId, title, itemName, inputValue, contextPath) {
	return '<span>' + title + '&nbsp;' + itemName + '</span>&nbsp;'
		+ '<a href="javascript:void(0)" onclick="removeCrossListValue(' + groupId + ')" title="Удалить из списка"><img src="' + contextPath + 'img/ico_remove.gif?build=20101009" alt="Удалить из списка" class="icon" width="14" height="14" /></a>&nbsp;'		
		+ '<input type="hidden" name="crossPostList" value="' + inputValue + '" />';
}

function addCrossToMyResources(type, contextPathWithGroup) {
	hideme('crossToMyResourcesLink');
	var el = document.getElementById('crossPostControls');
	if (el.innerHTML != '') {
		showme('crossToComunityLink');	
		el.innerHTML = "";
	}
	var uri = 'my/ajax/crosscontrols?type=' + type + '&groupId=1';
	Gallery.loadSimpleContent(uri, 'crossPostControls', contextPathWithGroup);
}

function addCrossToCommunity(type, contextPathWithGroup, groupId, notCheckMyLink) {
	hideme('crossToComunityLink');	
	var el = document.getElementById('crossPostControls');
	if (el.innerHTML != '' && (notCheckMyLink == null)) {
		showme('crossToMyResourcesLink');
		el.innerHTML = "";
	}
	var uri = 'my/ajax/crosscontrols?type=' + type;
	if (groupId != null) {
		uri = uri + '&groupId=' + groupId;
	}
	uri = uri + '&sel_ids=';
	
	var listEl = document.getElementById('crossPostList');
	var len = listEl.childNodes.length;

	for (var i = 0; i < len; i++) {           
		var groupId = listEl.childNodes[i].id;
		uri = uri + groupId;
		if (i < len - 1) {
			uri = uri + "_";
		}
	}
	Gallery.loadSimpleContent(uri, 'crossPostControls', contextPathWithGroup);
}

function removeCrossListValue(groupId) {
	var listEl = document.getElementById('crossPostList');
	var len = listEl.childNodes.length;
	
	for (var i = 0; i < len; i++) {           
		if (listEl.childNodes[i].id == groupId) {
			listEl.removeChild(listEl.childNodes[i]);	
			if (groupId == 1) {
				showme('crossToMyResourcesLink');
			} else {
				showme('crossToComunityLink');
			}
			break;
		}
	}
	
}

function chooseCrossListValue(contextPath, dirTitle, myResTitle) {	
	var groupEl = document.getElementById('selectedGroupId');
	var selGroup = groupEl.options[groupEl.selectedIndex];
	var title;
	var name;
	if (selGroup.value == 1) {
		title = myResTitle;
		name = '';
	} else {
		title = 'Сообщество';
		name = selGroup.text;
		if (groupEl.options.length > 1) {
			showme('crossToComunityLink');
		}	
	}
	var idCode = selGroup.value;
	var dirEl = document.getElementById('selectedDirId');	
	if (dirEl != null) {
		var selDir = dirEl.options[dirEl.selectedIndex];
		idCode = idCode + '_' + selDir.value;
		name = getCrossPostTitle(name, dirTitle, selDir.text);
	}
	addCrossListValue(selGroup.value, title, name, idCode, contextPath);	

	document.getElementById('crossPostControls').innerHTML = "";
}

function addCrossListValue(groupId, title, itemName, inputValue, contextPath) {
	var listEl = document.getElementById('crossPostList');
	
	var newdiv = document.createElement('div');
	newdiv.setAttribute('id', groupId);
	newdiv.innerHTML = getCrossListValue(groupId, title, itemName, inputValue, contextPath);

	listEl.appendChild(newdiv);
}

function getCrossPostTitle(groupName, dirTitle, dirName) {
	return '' + groupName + '&nbsp;(' + dirTitle + '&nbsp;'+ dirName + ')';
}

function cancelCrossListValue() {
	var groupEl = document.getElementById('selectedGroupId');
	var selGroup = groupEl.options[groupEl.selectedIndex];
	if (selGroup.value == 1) {
		showme('crossToMyResourcesLink');	
	} else {
		showme('crossToComunityLink');	
	}
	document.getElementById('crossPostControls').innerHTML = "";
}

function addPostToPrivateResources(contextPath, title) {
	hideme('crossToMyResourcesLink');
	id = 'crossPostControls';
	
	var el = document.getElementById('crossPostControls');
	if (el.innerHTML != '') {
		showme('crossToComunityLink');	
		el.innerHTML = "";
	}
	addCrossListValue(1, title, '', 1, contextPath);	
}

function autoSubmitCrossPostForm(contextPath, dirTitle, myResTitle) {
	var el = document.getElementById('crossPostControls');
	if (el != null && el.innerHTML != "") {
		chooseCrossListValue(contextPath, dirTitle, myResTitle);
	}
}