function toggle() {
	for (var i = 0; i < arguments.length; i++) {
		var obj = $(arguments[i]);
		obj.style.display = obj.style.display == 'none' ? '' : 'none';
	}
}

function hide() {
	for (var i = 0; i < arguments.length; i++) {
		var obj = $(arguments[i]);
		obj.style.display = 'none';
	}
}

function show() {
	for (var i = 0; i < arguments.length; i++) {
		var obj = $(arguments[i]);
		obj.style.display = '';
	}
}

function commentReply(parent, lev, num, nick) {
	document.leaveCommentForm.comment_parent.value = parent;
	document.leaveCommentForm.level.value = lev + 1;
	document.leaveCommentForm.number.value = num;
	document.leaveCommentForm.text.value = nick + ': ' + document.leaveCommentForm.text.value;
	return false;
}

function AddStep() {
	var num = eval(document.addRecipeForm.num_pictures.value);
	if (num == 9)
		return false;
	num++;
	var c =
'<h3 id="h3step' + num + '"><span>Шаг ' + num + '</span></h3>\
<label for="addRecipeFormStep' + num + '">Описание:</label><textarea name="step' + num + '" id="addRecipeFormStep' + num + '"></textarea><br />\
<label>Фото</label><span class="radios">\
	<input type="radio" class="radio" name="recipe_img_' + num + '_check" id="addRecipeFormRecipeImg' + num + 'Check0" value="0" checked="checked" onclick="photoChange(this);" /><label for="addRecipeFormRecipeImg' + num + 'Check0"><span>С компьютера</span></label>\
	<input type="radio" class="radio" name="recipe_img_' + num + '_check" id="addRecipeFormRecipeImg' + num + 'Check1" value="1" onclick="photoChange(this);" /><label for="addRecipeFormRecipeImg' + num + 'Check1">По ссылке</label>\
</span>\
<div>\
	<span><label for="addRecipeFormPhoto' + num + '">С компьютера:</label><input type="file" class="file" name="photo' + num + '" id="addRecipeFormPhoto' + num + '" /><br /></span>\
	<span style="display:none;"><label for="addRecipeFormPhoto' + num + 'L">По ссылке:</label><input type="text" class="text" name="photo' + num + '_l" id="addRecipeFormPhoto' + num + 'L" /><br /></span>\
</div>';
	$('addRecipeSteps').appendChild($c('li', {}, c));
	document.addRecipeForm.num_pictures.value = num;
	return false;
}

function EditAddStep() {
	var num = eval(document.addRecipeForm.num_pictures.value);
	if (num == 9)
		return false;
	num++;
	var c =
'<h3 id="h3step' + num + '"><span>Шаг ' + num + '</span></h3>\
<label for="addRecipeFormStep' + num + '">Описание:</label><textarea name="step' + num + '" id="addRecipeFormStep' + num + '"></textarea><br />\
<label>Фото</label><span class="radios">\
	<input type="radio" class="radio" name="recipe_img_' + num + '_check" id="addRecipeFormRecipeImg' + num + 'Check0" value="2" checked="checked" onclick="photoChange(this);" /><label for="addRecipeFormRecipeImg' + num + 'Check0"><span>С компьютера</span></label>\
	<input type="radio" class="radio" name="recipe_img_' + num + '_check" id="addRecipeFormRecipeImg' + num + 'Check1" value="1" onclick="photoChange(this);" /><label for="addRecipeFormRecipeImg' + num + 'Check1">По ссылке</label>\
</span>\
<div>\
	<span><label for="addRecipeFormPhoto' + num + '">С компьютера:</label><input type="file" class="file" name="photo' + num + '" id="addRecipeFormPhoto' + num + '" /><br /></span>\
	<span style="display:none;"><label for="addRecipeFormPhoto' + num + 'L">По ссылке:</label><input type="text" class="text" name="photo' + num + '_l" id="addRecipeFormPhoto' + num + 'L" /><br /></span>\
</div>';
	$('addRecipeSteps').appendChild($c('li', {}, c));
	document.addRecipeForm.num_pictures.value = num;
	return false;
}

function DelStep() {
	var num = eval(document.addRecipeForm.num_pictures.value);
	if (num == 1)
		return false;
	num--
	var a = $('addRecipeSteps');
	var b = $(a, 'li');
	a.removeChild(b[b.length-1]);
	document.addRecipeForm.num_pictures.value = num;
	return false;
}

function  photoChange(obj) {
	var vs = $(obj, 'parent', 'parent', 'div', 0, 'span');
	for (var i in vs)
		hide(vs[i]);
	show(vs[obj.value]);
}

function photoChangeE(obj) {
	var vs = $(obj, 'parent', 'parent', 'div', 0, 'span');
	for (var i in vs)
		hide(vs[i]);
	var tmp = { '2': 0, '1': 1, '0': 2, '-1': 3 };
	show(vs[tmp[obj.value]]);
}

function getCheckedRadio(name) {
	alert(name);
	if (arguments.length == 1)
		var inputs = document.getElementsByTagName('input');
	else
		var inputs = $(arguments[1]).getElementsByTagName('input');
	alert(inputs.length);
	for (var i in inputs) {
		alert(inputs[i].name);
		if (inputs[i].name == name && inputs[i].checked) {
			alert(inputs[i].value);
			return inputs[i];
		}
	}
	return null;
}

function getPhotoCheckedRadio(obj) {
	var ch = $(obj, 'input');
	for (var i in ch) {
		if (ch[i].checked)
			return ch[i];
	}
}

function addRecipeOnLoad() {
	if ($browser.ie)
		document.addRecipeForm.num_pictures.value = 1;
	photoChange(getPhotoCheckedRadio($('addRecipe', 'div', 0, 'span', 0)));
	var st = $('addRecipeSteps', 'li');
	for (var i in st) {
		photoChange(getPhotoCheckedRadio($(st[i], 'span', 0)));
	}
}

function editRecipeOnLoad() {
	photoChangeE(getPhotoCheckedRadio($('addRecipe', 'div', 0, 'span', 0)));
	var st = $('addRecipeSteps', 'li');
	for (var i in st) {
		photoChangeE(getPhotoCheckedRadio($(st[i], 'span', 0)));
	}
}

function clearV(obj) {
	obj.oldvalue = obj.value;
	obj.value = '';
	obj.onfocus = '';
	obj.onblur = function () { returnV(obj); };
}

function returnV(obj) {
	if (obj.value == '') {
		obj.value = obj.oldvalue;
		obj.onfocus = function () { clearV(obj); }
	}
	obj.onblur = '';
}

var cat = [
	['* Выберите *','Холодные закуски','Горячие закуски','Бутерброды и сэндвичи','Салаты с мясом','Салаты с овощами и грибами','Салаты с птицей','Салаты с рыбой и морепродуктами','Заправки для салатов'],
	['* Выберите *','Холодные супы','Супы с мясом и птицей','Супы с рыбой и морепродуктами','Грибные супы','Овощные супы','Прозрачные супы и бульоны','Супы пюре','Молочные и сырные супы'],
	['* Выберите *','Блюда из мяса','Блюда из морепродуктов','Блюда из птицы','Блюда из рыбы','Блюда из круп и мучные блюда','Блюда из овощей','Гарниры','Соусы и маринады'],
	['* Выберите *','Торты и пирожные','Пироги и пирожки','Начинки, кремы, и украшения','Тесто','Варенья и джемы','Десерты из фруктов','Печенье, кексы и рулеты','Другие десерты и выпечка'],
	['* Выберите *','Чай','Кофе','Какао и шоколад','Соки, компоты и морсы','Безалкогольные коктейли','Алкогольные коктейли','Пунши и глинтвейны','Прочее'],
	['* Выберите *','К дню святого Валентина','К Пасхе','К Хелоуину','К Рождеству','К Новому Году'],
	['* Выберите *','Консервирование','Вегетарианские блюда','Диетические блюда','Постные блюда','Для микроволновой печи','Для кормящих мам','Для самых маленьких'],
	[false,'Нет подразбиения']
];

function addSelectCat(a) {
	var type = $('addRecipeFormType');
	var extype = $('addRecipeFormTypeExt');
	extype.innerHTML = '';
	var num = type.selectedIndex - a;
	if (num < 0) {
		extype.appendChild($c('option', {value: 0}, '* Сначала выберите категорию *'))
	} else {
		for (var i = 0; i < cat[num].length; i++) {
			if (cat[num][i])
				extype.appendChild($c('option', {value: ( i == 0 ? 0 : (num + 1) * 10 + i)}, cat[num][i]));
		}
	}
}

function addInfoOnLoad() {
	photoChange(getPhotoCheckedRadio($('addInfo', 'div', 0, 'span', 0)));
}

function getPageHeight() {    
    var xScroll;
    if (window.innerHeight && window.scrollMaxY) {	
        yScroll = window.innerHeight + window.scrollMaxY;
    } else if (document.body.scrollHeight > document.body.offsetHeight){
        yScroll = document.body.scrollHeight;
    } else {
        yScroll = document.body.offsetHeight;
    }
    var windowWidth;
    if (self.innerHeight) {
        windowHeight = self.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) {
        windowHeight = document.documentElement.clientHeight;
    } else if (document.body) {
        windowHeight = document.body.clientHeight;
    }	
    if (yScroll < windowHeight){
        pageHeight = windowHeight;
    } else { 
        pageHeight = yScroll;
    }
	return pageHeight;
}

function openImg(adr) {
	$('openImg').style.display = '';
	$('cover').style.height = getPageHeight() + 'px';
	$('largeImage').src = adr;
	return false;
}

function closeImg() {
	$('openImg').style.display = 'none';
}

function $post(url, obj) {
	if (typeof url == "object") {
		obj = url;
		url = window.location.pathname;
	}
	var innForm = '';
	for (var i in obj) {
		innForm += '<input type="hidden" name="' + i + '" value="' + obj[i] + '" />';
	}
	var postForm = $c('form', { method: "post", action: url}, innForm);
	postForm.style.display = 'none';
	document.body.appendChild(postForm);
	postForm.submit();
	return false;
}
