function ajaxInit() {
	Custom.init();
	crir.init();
	if (typeof ajaxDocumentInit == "function") {
		ajaxDocumentInit();
	}
}

function transfer(source, targetSelector) {
//	source.appendTo($j(targetSelector).empty());
	source.appendTo($j(targetSelector));
}

function listShowAll(listId) {
	$j('#' + listId).find("li").show();
}

function listHideAfter(listId, limit) {
	var listElements = $j('#' + listId).find("li");
	for(i = limit; i<listElements.length; i++) {
		$j(listElements[i]).hide();
	}
}

function reRenderPage() {
	document.getElementById(reRenderPageId).onclick();
}

function handlePopupAction(action) {
	if (action == "closeAndRerender") {
		reRenderPage();
		popupLayer.close();
	} else if (action == "close") {
		popupLayer.close();
	}
}

function updateCheckboxImage(imageElement, checkboxElement, type) {
//	if (state == null) {
//		state = $j(checkboxElement).is(":checked");
//	}
	var state = checkboxElement.frentsCustomCheckboxState;
	var imageUrl;
	if (state) {
		if (type == "people") {
			imageUrl = contextPath + "/v2/images/icon/checkbox_checked_green.gif";
		} else if (type == "header") {
			imageUrl = contextPath + "/v2/images/icon/checkbox_blue_act.gif";
		} else { // things. default.
			imageUrl = contextPath + "/v2/images/icon/checkbox_checked.gif";
		}
	} else {
		if (type == "header") {
			imageUrl = contextPath + "/v2/images/icon/checkbox_blue_pas.gif";
		} else { // people, things. default.
			imageUrl = contextPath + "/v2/images/icon/checkbox_unchecked.gif";
		}
	}
	$j(imageElement).attr("src", imageUrl).attr("alt", state? "[x]" : "[ ]");
}

function initCustomCheckbox(checkboxElement) {
	var state = $j(checkboxElement).is(":checked");
	checkboxElement.frentsCustomCheckboxState = state;
}

function clickCustomCheckbox(imageElement, checkboxElement, type) {
	checkboxElement.frentsCustomCheckboxState = !checkboxElement.frentsCustomCheckboxState;
	updateCheckboxImage(imageElement, checkboxElement, type);
}

function setCustomCheckbox(checkboxElement, type, value) {
	if (value) {
		$j(checkboxElement).attr("checked", "checked");
	} else {
		$j(checkboxElement).removeAttr("checked");
	}
	checkboxElement.frentsCustomCheckboxState = value;
	var imageElement = $j(checkboxElement).nextAll("img.checkboxSymbol").get(0);
	updateCheckboxImage(imageElement, checkboxElement, type);
}

function toggleCheckbox(checkboxElement) {
	checkboxElement.click();
}

function isCustomCheckboxChecked(checkboxElement) {
	return checkboxElement.frentsCustomCheckboxState;
}

function updateSwitchbuttonImage(checkboxElement, imageChecked, imageUnchecked) {
	if (checkboxElement.frentsCustomCheckboxState) {
		$j(imageUnchecked).hide();
		$j(imageChecked).show();
	} else {
		$j(imageChecked).hide();
		$j(imageUnchecked).show();
	}
}

function clickCustomSwitchbutton(checkboxElement, imageChecked, imageUnchecked) {
	checkboxElement.frentsCustomCheckboxState = !checkboxElement.frentsCustomCheckboxState;
	updateSwitchbuttonImage(checkboxElement, imageChecked, imageUnchecked);
}

function updateSelectedLabel(selectElement, selectedLabelElement, color) {
	var selectedOption = $j(selectElement).find("option:selected");
	if (!selectedOption) {
		selectedOption = $j(selectElement).find("option:first");
	}
	if (selectedOption) {
		var selectedLabel = $j(selectedLabelElement);
		selectedLabel.html(selectedOption.html());
		if (color) {
			selectedLabel.css("color", color);
		}
	}
}

function setInputDisabled(rootElement, disabled) {
	var root = $j(rootElement);
	if (disabled) {
		root.addClass("disabled");
		root.filter("input").attr("disabled", disabled);
		root.find("input").attr("disabled", disabled);
	} else {
		root.removeClass("disabled");
		root.filter("input:not(.disabled *):not(.disabled)").attr("disabled", disabled);
		root.find("input:not(.disabled *):not(.disabled)").attr("disabled", disabled);
	}
}

function openLinkElement(linkElement) {
	if (linkElement.target) {
		open(linkElement.href, linkElement.target);
	} else {
		open(linkElement.href, "_self");
	}
}

function removeConversation(s) {
	var pos = s.indexOf("&cid=");
	if (pos < 0) {
		pos = s.indexOf("?cid=");
	}
	if (pos < 0) {
		return s;
	}
	var i = pos + 5;
	while (i < s.length && s.charAt(i) != "&" && s.charAt(i) != "#") {
		i++;
	}
	if (i >= s.length) {
		return s.substring(0, pos);
	}
	if (s.charAt(i) == "&") {
		return s.substring(0, pos + 1) + s.substring(i+1);
	} else {
		return s.substring(0, pos) + s.substring(i);
	}
}

function saveAjaxUrl() {
	var pos = currentPageUrl.indexOf("?");
	var ajaxParams = pos >= 0? currentPageUrl.substring(pos) : "";
	var originalParams = window.location.search;
	if (removeConversation(originalParams) != removeConversation(ajaxParams)) {
		location.hash = "#ajaxQuery=" + (ajaxParams.length > 0? ajaxParams.substring(1) : "");
	}
}

function restoreAjaxUrl() {
	var hash = location.hash;
	if (hash.length > 11 && hash.substring(0, 11) == "#ajaxQuery=") {
		location.replace(location.pathname + "?" + hash.substring(11));
	} 
}

function hideMessages(element) {
	$j(element).closest(".annotatedInput").children(".errorContainer").children().fadeOut(2000);
}

function adjustTextareaHeight(element) {
	element.style.height = element.scrollHeight+"px";
}

function scrollToElement(element) {
	window.scrollTo(0, $j(element).offset().top);
}

