// JavaScript Document

//jQuery section
$(document).ready(function(){
	//снимаем фокус с кликов и чекбоксов
	$('a').focus(function() {
		this.blur();
	});
	$('input:checkbox').focus(function() {
		this.blur();
	});
	$('input:radio').focus(function() {
		this.blur();
	});
	//присваиваем формам нужные классы
	$(':text').addClass('text');
	$(':password').addClass('text');
	$(':submit').css('cursor','pointer');
	//меню
	$('.menu li.fl').each(function() {
    $(this).hover(function() {
		if ($(this).children('ul').length) {
            $(this).children('ul').css('display', 'block');
		    $(this).addClass('active');
        }
        $('.menu li.fl').addClass('inactive');
        $(this).removeClass('inactive');
    }, function() {
		if ($(this).children('ul')) {
            $(this).children('ul').css('display', 'none');
            $(this).removeClass('active');
        }
		$('.menu li.fl').removeClass('inactive');
    });
	});
	//присваеваем функции ссылкам
	$('a.showtext').click(function() {
		$(this).hide();
		showText();
		return false;
	});
	//ротатор (главная)
	var speed = 5000;
	var run = setInterval('rotate()', speed);	
	var item_width = $('div.books ul').outerWidth(); 
	$('#next').click(function() {
		var left_indent = -280;
		$('div.books ul').animate({'left' : left_indent}, 300, function () {
			$('div.books li:last').after($('div.books li:first'));                 	
			$('div.books ul').css({'left' : 0});
		});
		return false;
	});        
	$('div.books').hover(
		function() {
			clearInterval(run);
		}, 
		function() {
			run = setInterval('rotate()', speed);	
		}
	);
	$('div.list ul.news li').hover(
		function() {
			$(this).addClass('hovered');
		}, 
		function() {
			$(this).removeClass('hovered');
		}
	);
	//логин
    $("a.enter").colorbox({
        opacity:"0.6",
        width:"400px",
        height:"220px",
        initialWidth:"100px",
        initialHeight:"50px",
        inline:true,
        href:'#frm_signIn',
        html: $('#block_signIn').html()
    });

	//иллюстрации новостей	
	$('div.photos li').hide().addClass('hidden');
	$('div.photos li').eq(0).show().removeClass('hidden');
	//костыль для закрытия окна колорбокса
	$('#cboxClose').hover(function() {$(this).addClass('active')}, function() {$(this).removeClass('active');});
});

//ротатор по умолчанию 
function rotate() {
	$('#next').click();
}

//показать все иллюстрации 
function showPhotos() {
	$('div.photos a.more').hide();
	$('div.photos li.hidden').animate({height: 'toggle', opacity: 'toggle'}, 300);
	
}

//селектор поиска
function sQuery(n) {
	switch(n){
		case 1: text='книг'; action='booksearch.php'; n=2; break;
		case 2: text='по сайту'; action='sitesearch.php'; n=3; break;
		case 3: text='в интернете'; action='http://google.com?query='; n=1; break;
	}
	$('div.top form').attr('action',action);
	$('div.top form label a').html(text).attr('href','javascript:sQuery('+n+')');
	$('#query').blur();
};
//быстрые ссылки (главная)
	function ql(n) {
		$('dl.qlnx dd').hide();
		$('dl.qlnx dt').removeClass('active').addClass('inactive');
		$('#dd'+n).css('display', 'block');
		$('#dt'+n).removeClass('inactive').addClass('active');
     };
//события (главная)
	function showEvent(el) {
		$('dl.news *').removeClass('active');
		$('#dt-'+el).addClass('active');
		$('#'+el).addClass('active');
     };
//скрытый текст (о библиотеке)
	function showText(el) {
		$('p.hidden').show();
     };
     
function sign_in() {
    $('#btn_sign_in').attr('disabled', 'disabled');
    $.post("/index.html", {"login": $('#login').val(), "password": $('#password').val(), "rmbr": $('#rmbr').attr('checked')},
    function(data){
        if(data.res == true) {
            document.location = document.location;
        } else {
            $('#btn_sign_in').removeAttr('disabled');
            alert('Неверный логин или пароль');
        }
    }, "json");
}

function refresh() {
    document.location = document.location;
}

function redirect(url) {
    document.location = url;
}
