function resizeScroller() {
	var height;
	if (document.documentElement && document.documentElement.clientHeight != 0) {
		height = document.documentElement.clientHeight;
	} else {
		height = document.body.clientHeight;
	}

	document.getElementById('main-container').style.height = height + 'px';
	if (document.getElementById('left-nav-body')) {
		document.getElementById('left-nav-body').style.height = (height - 80) + 'px';
	}

	height -= document.getElementById('before-scroller').offsetHeight;
	height -= document.getElementById('after-scroller').offsetHeight;
	height -= 50;

	document.getElementById('scroller').style.height = height + 'px';
}

var tooltip;
function indicateSaving(event) {
	tooltip = document.createElement('div');
	tooltip.className = 'saving-tooltip';

	var tipX = getX(event);
	var tipY = getY(event);

	tooltip.style.left = (tipX + 10) + 'px';
	tooltip.style.top = (tipY + 10) + 'px';
	tooltip.appendChild(document.createTextNode('please wait - saving'));

	setTimeout('finishIndicateSaving()', 200);
}

function finishIndicateSaving() {
	document.body.appendChild(tooltip);
	document.onmousemove = moveTip;
}

function newObject(event, button) {
	document.getElementById('categoryIdInput').value = getSelect(document.getElementById('categoryIdSelect'));
	button.name = '_ryUpdateMode:publish';
	indicateSaving(event);
}

function moveTip(event) {
	if (!event) return;
	tooltip.style.left = (getX(event) + 10) + 'px';
	tooltip.style.top = (getY(event) + 10) + 'px';
}

function getX(event) {
	if (document.all) { // msie
		if (!event) event = window.event;
		return event.clientX + document.body.scrollLeft;
	} else {
		return event.pageX;
	}
}

function getY(event) {
	if (document.all) { // msie
		if (!event) event = window.event;
		return event.clientY + document.body.scrollTop;
	} else {
		return event.pageY;
	}
}

function getXRelative(event) {
	if (document.all) { // msie
		return event.offsetX;
	} else {
		return event.layerX;
	}
}

function getYRelative(event) {
	if (document.all) { // msie
		return event.offsetY;
	} else {
		return event.layerY;
	}
}

function changePanel(i) {
	SyncEpoz();
	var form = document.forms.mainForm;
	form.elements.panel.value = i;
	form.submit();
}

function changeLanguage(event) {
	var newLangSelect = document.getElementById('newLang');
	var form = document.forms['mainForm'];

	indicateSaving(event);
	form.changeLang.value = newLangSelect.options[newLangSelect.selectedIndex].value;
	SyncEpoz();
	form.submit();
}

function changeChildRank(childId, childList) {
        var form = document.forms.mainForm;
        if (childId != null) form.elements._ryChangeChildRank.value = childList + "-" + childId;
        form.submit();
}

function popup(url, id, width, height) {
	var win = window.open(url, id, 'width=' + width + ',height=' + height + ',scrollbars=yes,resizable=yes');
	win.focus();
}

function updateTypeChoiceLink(typeInput, listInput, selectedId) {
	typeInput = document.forms.mainForm[typeInput];
	listInput = document.forms.mainForm[listInput];

	listInput.options.length = 0;

	if (typeInput.options[0].value != '' || typeInput.selectedIndex != 0) {
		var indexModifier = 0
		if (typeInput.options[0].value == '' && typeInput.selectedIndex != 0) indexModifier = 1;
		var options = linkOptions[typeInput.selectedIndex - indexModifier];

		for (i = 0; i < options.length; i++) {
			listInput.options[i] = (new Option(options[i][1], options[i][0]));
			if (selectedId == options[i][0]) listInput.options[i].selected = true;
		}
	}

	if (listInput.options.length == 0) {
		listInput.options[0] = (new Option('[None]', ''));
	}
}

function updateMailTemplateList(selectedId) {
	var typeInput = document.getElementById('type');
	var listInput = document.getElementById('list');

	listInput.options.length = 0;

	var options = linkOptions[typeInput.selectedIndex];

	for (i = 0; i < options.length; i++) {
		listInput.options[i] = (new Option(options[i][1], options[i][0]));
	}

	if (listInput.options.length == 0) {
		listInput.options[0] = (new Option('[None]', ''));
	}
}

function trim(s) {
	while ((s.substring(0, 1) == ' ') || (s.substring(0, 1) == '\n') || (s.substring(0, 1) == '\r')) {
		s = s.substring(1,s.length);
	}

	while ((s.substring(s.length - 1, s.length) == ' ') || (s.substring(s.length - 1, s.length) == '\n') || (s.substring(s.length - 1, s.length) == '\r')) {
		s = s.substring(0 ,s.length - 1);
	}
	return s;
}

function updateUploadChoice(inputName, requiredAspect) {
	var select = document.forms.mainForm[inputName];
	var warning = document.getElementById(inputName + 'Warning');

	var index = select.selectedIndex;

	if (select.options[0].value == '') {
		index--;
		if (index == -1) {
			warning.style.display = 'none';
			return;
		}
	}

	eval('var array = ' + inputName + 'Options');

	var aspect = array[index];

	if (Math.abs(aspect - requiredAspect) < 0.05) {
		warning.style.display = 'none';
	} else {
		warning.style.display = 'inline';
	}
}

function crop(id, imageid, aspect) {
	var url = 'editImagePopup?id=' + id + '&imageid=' + imageid;
	if (aspect) {
		url += '&aspect=' + aspect
	}

	window.open(url, 'img_popup', 'width=600,height=600,scrollbars=yes,resizable=yes');
}

function cropToRatio(id, inputName, aspect) {
	var select = document.forms.mainForm[inputName];
	var imageid = select.options[select.selectedIndex].value;

	crop(id, imageid, aspect);
}

function integer_input_handler(inputName) {
	var value = document.forms.mainForm[inputName].value;

	var newValue = '';

	for (i = 0; i < value.length; i++) {
		if ('' + parseInt(value.charAt(i)) != 'NaN') {
			newValue += parseInt(value.charAt(i));
		}
	}

	document.forms.mainForm[inputName].value = newValue;
}

function updateSpellWord() {
	var selector = document.getElementById('suggestions');
	var index = selector.selectedIndex; // For multiple selects, returns first selected

	if (index == -1) {
		index = 0;
	}

	for (var i = 0; i < selector.options.length; i++) {
		selector.options[i].selected = (i == index);
	}


	var word = selector.options[index].value;

	document.getElementById('word').value = word;

	spellEdit();
}

function spellEdit() {
	document.getElementById('add').disabled = true;
	document.getElementById('change').disabled = false;
	document.getElementById('undo').style.visibility = 'visible';
}

function spellUndoEdit() {
	document.getElementById('word').value = originalWord;
	document.getElementById('add').disabled = false;
	document.getElementById('change').disabled = true;
	document.getElementById('undo').style.visibility = 'hidden';
}

function updateRelatedLink(count) {
	var mode = getRadio(document.forms.mainForm.elements["mode_relatedLinks_" + count]);
	var internalDiv = document.getElementById("internal" + count);
	var externalDiv = document.getElementById("external" + count);
	var downloadDiv = document.getElementById("download" + count);

	if (mode == "INTERNAL_LINK") {
		internalDiv.style.display = "block";
		externalDiv.style.display = "none";
		downloadDiv.style.display = "none";

	} else if (mode == "EXTERNAL_LINK") {
		internalDiv.style.display = "none";
		externalDiv.style.display = "block";
		downloadDiv.style.display = "none";

	} else if (mode == "DOWNLOAD") {
		internalDiv.style.display = "none";
		externalDiv.style.display = "none";
		downloadDiv.style.display = "block";
	}
}

function getRadio(list) {
	if (list.value) {
		// Only one radio button -> not a list
		return list.value;
	}

	for (var i = 0; i < list.length; i++) {
		if (list[i].checked) {
			return list[i].value;
		}
	}
	return null;
}

function getSelect(selectInput) {
	var result = '';

	if (selectInput) {
		result = selectInput.options[selectInput.selectedIndex].value;
	}

	return result;
}

function computedWidth(element) {
	if (element.offsetWidth) {
		return element.offsetWidth;
	} else {
		return document.defaultView.getComputedStyle(element, '').getPropertyValue("width");
	}
}

function computedHeight(element) {
	if (element.offsetHeight) {
		return element.offsetHeight;
	} else {
		return document.defaultView.getComputedStyle(element, '').getPropertyValue("height");
	}
}

function UploadFolder(id, name) {
	this.id = id;
	this.name = name;
	this.files = new Array();
}

function UploadFile(id, name, path, width, height, thumbPath, thumbWidth, thumbHeight) {
	this.id = id;
	this.name = name;
	this.path = path;
	this.width = width;
	this.height = height;
	this.thumbPath = thumbPath;
	this.thumbWidth = thumbWidth;
	this.thumbHeight = thumbHeight;
}

UploadFile.prototype.getLeftMargin = function() {
	return (48 - this.thumbWidth) / 2 + 5;
}

UploadFile.prototype.getRightMargin = function() {
	return (48 - this.thumbWidth) / 2 + 10;
}

function showUploadFolder(id, selectedId) {
	if (!id) {
		// Find the folder for the selected item
		for (var i = 0; i < folders.length; i++) {
			for (var j = 0; j < folders[i].files.length; j++) {
				if (folders[i].files[j].id == selectedId) {
					id = folders[i].id;
					break;
				}
			}
		}
	}
	
	if (id == null) {
		id = folders[0].id;
	}

	if (document.getElementById('folder-list')) {
		var links = document.getElementById('folder-list').getElementsByTagName('a');

		for (var i = 0; i < links.length; i++) {
			if (links[i].id == 'folder-link-' + id) {
				links[i].className = 'on';
			} else {
				links[i].className = '';
			}
		}
	}

	var folder;
	
	for (var i = 0; i < folders.length; i++) {
		if (folders[i].id == id) {
			folder = folders[i];
		}
	}
		
	var theDiv = document.getElementById("files");
	var newList = document.createElement("ul");

	for (var i = 0; i < folder.files.length; i++) {
		var file = folder.files[i];
		var newRow = document.createElement("li");
		
		newRow.innerHTML = '<input type="radio" name="width" value="' + file.id + '" id="radio' + file.id + '"' + 
			(selectedId == file.id ? ' checked="checked"' : '') + 
			' onClick="setIt(' + file.id + ')" />' +
			'<label for="radio' + file.id + '"><span class="image-holder">' +
			'<img style="margin: 0 ' + file.getRightMargin() + 'px 0 ' + file.getLeftMargin() + 
			'px;" src="' + file.thumbPath + '" width="' + file.thumbWidth + '" height="' +
			file.thumbHeight + '" /></span>' + file.name + '</label>\n';

		newList.appendChild(newRow);
	}

	theDiv.replaceChild(newList, theDiv.childNodes[0]);
}

function uploadById(id) {
	var folder;
	
	for (var i = 0; i < folders.length; i++) {
		for (var j = 0; j < folders[i].files.length; j++) {
			if (folders[i].files[j].id == id) {
				return folders[i].files[j];
			}
		}
	}
}

function tickAllCheckboxes() {
	var inputs = document.getElementsByTagName('input');
    for (var i = 0; i < inputs.length; i++) {
		if (inputs[i].type == 'checkbox') {
        	inputs[i].checked =true;
		}
	}
}
