﻿var jHide, jShow, jFirst;

$(function () {
	jHide = $('#new_post_on_forum .li_text:visible');
	jFirst = $('#new_post_on_forum .li_text:first');
	if ($(jHide).length != 0) {
		window.setInterval(showNextNewPost, 5000);
	}
	
	var jCP = $('#control_panel');
	var jCPPD = $('div.control_panel_pulldown', jCP);
	$(jCPPD).hide();
	
	$(jCP).hover(
		function () {
			$(jCPPD).slideDown("slow");
		},
		function () {
			$(jCPPD).slideUp("slow");
		}
	);
	bShadowBox = Shadowbox.init();
})

//Показывает/прячет форму залогинивания
function show_logon_form(){
	if($.browser.opera) { return true; }
	$('#logon_form').toggle();
	return false;
}

//Сабмит формы залогинивания
//Данные уходят аяксом
//В сулчае успешного логина страница перегружается
function logon(jsFormLogon){
	var email = $('#logon_form input#email').val();
	var pswd = $('#logon_form input#pswd').val();
	
	$('#logon_form input').attr('disabled', 'true');
	$.post( '/common/logon.html', {email: email, pswd: pswd, ajax: 'true'}, function(data) {
		if (data == 'ok'){
			window.location = window.location;
		} else if (data == 'error') {
			alert('Неправильная пара логин/пароль. Попробуйте еще раз.');
		} else {
			alert('Ошибка на сервере. Свяжитесь с администратором.');
		}
		$('#logon_form input').removeAttr('disabled');
	});
	
	return false;
}
//Разлогинивание
function logout(jsALogOut){
	$.post( '/common/logout.html', {ajax: 'true'}, function(data) {
		window.location = window.location;
	});
	return false;
}

//Проверка заполнения поля "Поиск"
function checkSearch(jSearch) {
	var sSearch = jSearch.value;
	if (sSearch.replace(/\s/g, '') == '') {
		jSearch.value = 'Поиск:';
	}
}
//При входе в стркоу поиска убираем значение "Поиск"
function startInputSearch(jSearch) {
	var sSearch = jSearch.value;
	if (sSearch == 'Поиск:') {
		jSearch.value = '';
	}
}

//Прокрутка новых сообщений форума
function showNextNewPost() {
//	var jHide = $('#new_post_on_forum .li_text:visible');
//	var jShow = $('#new_post_on_forum .li_text:visible').next('#new_post_on_forum .li_text:hidden');
//	if($(jShow).length == 0){
//		jShow = $('#new_post_on_forum .li_text:hidden:first');
//	}
	jShow = $(jHide).next('#new_post_on_forum .li_text:hidden');
	if($(jShow).length == 0){
		jShow = jFirst;
	}
	if(! ($.browser.msie && parseInt(jQuery.browser.version) < 7)) {
		$(jHide).fadeOut('slow', function() {
			$(jShow).fadeIn('slow');
		});
	} else {
		$(jHide).hide();
		$(jShow).show();
	}
	jHide = jShow;
}

//Работа с полями ввода файлов для галерей
$(function () {
	var jInputsGallery = $('input.inputgalleryphoto');
	if (jInputsGallery.length == 0) {
		return;
	}
	
	jInputsGallery.after('<img class="gallerylink_remove" src="/img/delete.png" onclick="removeInputPhotoGallery(this)" />');
	
	jInputsGallery.filter(':last').next('img.gallerylink_remove').next('br').after('<img class="gallerylink_add" src="/img/add.png" onclick="addInputPhotoGallery(this)" />');
})

function removeInputPhotoGallery (thisLink) {
	$(thisLink).prev('input.inputgalleryphoto').remove();
	$(thisLink).next('br').remove();
	$(thisLink).remove();
};

function addInputPhotoGallery (thisLink) {
	$(thisLink).before('<input type="file" name="photogallery" class="inputgalleryphoto" /><img class="gallerylink_remove" src="/img/delete.png" onclick="removeInputPhotoGallery(this)" /><br>');
};
