var moveTimer = 0;

function startMove(dir, max)
{
	if (moveTimer)
	{
		var cont = document.getElementById('photoWorkThumbs');
		if (!cont.style.left)
		{
			cont.style.left = '0px';
		}
		var left = parseInt(cont.style.left) + dir;
		if ((left > 490 - max) && (left < 1))
		{
			cont.style.left = left + 'px';
		}
	} else {
		moveTimer = setInterval('startMove(' + dir + ',' + max + ')', 10);
	}
}

function stopMove()
{
	if (moveTimer)
	{
		clearInterval(moveTimer);
		moveTimer = 0;
	}
}

function changePage(base, page)
{
	window.location.href = base + page.options[page.selectedIndex].value;
}

function openPhotoSet(id)
{
	win=window.open(
		'/photoworks/showSet/id/' + id,
		'big',
		'width=760,height=750,status=0,toolbar=0,location=0,menubar=0,directories=0,resizable=1,scrollbars=1'
	);
	return false;
}

function openPhotoEdit(id)
{
	win=window.open(
		'/photoworks/editSet/id/' + id,
		'big',
		'width=760,height=750,status=0,toolbar=0,location=0,menubar=0,directories=0,resizable=1,scrollbars=1'
	);
	return false;
}

function openCategory(id)
{
	window.onunload = function() {};
	window.opener.location.href = '/photoworks/index/cat/' + id;
	window.close();
	return false;
}

function voteSet(id)
{
	var sel = document.getElementById('photoSetVoteRating');
	var rating = parseInt(sel.options[sel.selectedIndex].value);
	window.location.href = '/photoworks/voteSet/id/' + id + '/rating/' + rating;
	return false;
}

function changePhoto(id)
{
	var thumbs = document.getElementById('photoWorkThumbs').getElementsByTagName('a');
	for (i = 0; i < thumbs.length; i++)
	{
		thumbs[i].className = 'thumb';
	}
	var thumb = document.getElementById('photo_' + id);
	thumb.className = 'thumb current';
	thumb.blur();
	document.getElementById('photoDescription').innerHTML = photos[photoNumber['p_' + id] - 1].descr;
	document.getElementById('photoImage').style.backgroundImage = 'url(\'' + photos[photoNumber['p_' + id] - 1].big + '\')';
	document.getElementById('originalPhotoLink').href = photos[photoNumber['p_' + id] - 1].original;
	document.getElementById('photoNumber').innerHTML = photoNumber['p_' + id];
	return false;
}

function prevPhoto()
{
	var curPhotoNum = parseInt(document.getElementById('photoNumber').innerHTML);
	if (curPhotoNum == 1)
	{
		return false;
	}
	changePhoto(photos[curPhotoNum - 2].id);
	return false;
}

function nextPhoto()
{
	var curPhotoNum = parseInt(document.getElementById('photoNumber').innerHTML);
	if (curPhotoNum == photos.length)
	{
		return false;
	}
	changePhoto(photos[curPhotoNum].id);
	return false;
}

function backToParent(url)
{
	window.onunload = function() {};
	window.opener.location.href = url;
	window.close();
	return false;
}

function switchAddSetForm()
{
	var body = document.getElementById('photoworksBody');
	var addSetForm = document.getElementById('addSetForm');
	if (body.style.display == 'none')
	{
		addSetForm.style.display = 'none';
		body.style.display = 'block';
	} else {
		addSetForm.style.display = 'block';
		body.style.display = 'none';
	}
	return false;
}

function confirmSet(id)
{
	if (window.confirm('Подтвердить размещение сессии в разделе "Фотоработы"?'))
	{
		window.location.href = '/photoworks/confirmSet/id/' + id + '/ret/empty';
		window.opener.location.reload();
	}
	return false;
}

function returnSetToEdit(id)
{
	if (window.confirm('Потребуется повторная модерация! Продолжить?'))
	{
		document.getElementById('returnToEditForm').submit();
	}
	return false;
}

function deleteSet(id)
{
	if (window.confirm('Вы действительно хотите удалить сессию?'))
	{
		window.location.href = '/photoworks/deleteSet/id/' + id + '/ret/empty';
	}
	return false;
}

function deletePhoto()
{
	if (window.confirm('Вы действительно хотите удалить фотографию?'))
	{
		var photo = parseInt(document.getElementById('photoNumber').innerHTML) - 1;
		window.location.href = '/photoworks/delPhoto/id/' + photos[photo].id;
	}
	return false;
}

function movePhoto(dir)
{
	var photo = parseInt(document.getElementById('photoNumber').innerHTML) - 1;
	if (((photo < 1) && (dir < 0)) || ((photo > photos.length - 2) && (dir > 0)))
	{
		return false;
	}
	window.location.href = '/photoworks/movePhoto/id/' + photos[photo].id + '/dir/' + dir;
	return false;
}

function addCategory()
{
	var title = window.prompt('Введите название для новой категории');
	if (title)
	{
		window.location.href = '/photoworks/adminCategory/action/add/title/' + encodeURIComponent(title);
	}
	return false;
}

function checkSetTitle(but)
{
	but.disabled = true;
	var form = but.parentNode;
	var title = document.getElementById('add_set_title').value.replace(/(^\s+)|(\s+$)/g, "");
	var mainCat = 0;
	var secondCat = 0;
	var cats = form.getElementsByTagName('select');
	for (var i = 0; i < cats.length; i++)
	{
		if (cats[i].getAttribute('name') == 'set_category')
		{
			mainCat = parseInt(cats[i].options[cats[i].selectedIndex].value);
		} else if (cats[i].getAttribute('name') == 'set_second_category') {
			secondCat = parseInt(cats[i].options[cats[i].selectedIndex].value);
			if (!secondCat)
			{
				secondCat = '';
			}
		}
	}
	if ((title == '') || !mainCat)
	{
		window.alert('Не заполнено поле названия фотосессии');
		document.getElementById('add_set_title').focus();
		but.disabled = false;
	} else {
		win=window.open('about:blank', 'big', 'width=760,height=750,status=0,toolbar=0,location=0,menubar=0,directories=0,resizable=1,scrollbars=1');
		new Ajax.Request('/photoworks/addSet', {
			method: 'post',
			parameters: {'set_title': title, 'set_category': mainCat, 'set_second_category': secondCat},
			onSuccess: function(transport) {
				var id = parseInt(transport.responseText);
				if (id)
				{
					openPhotoEdit(id);
				} else {
					alert('Ошибка создания фотосессии!\n Убедитесь, что все необходимые поля заполнены правильно и попробуйте ещё раз');
					var buts = document.getElementById('add_set_title').parentNode.getElementsByTagName('input');
					for (var i = 0; i < buts.length; i++)
					{
						buts[i].disabled = false;
					}
					win.close();
				}
			}
		});
	}
	return false;
}

function checkPhoto(but)
{
	if (document.getElementById('photo_file_field').value)
	{
		but.disabled = true;
		but.parentNode.submit();
	} else {
		window.alert('Выберите фотографию!');
	}
	return false;
}
