$(document).ready(function(){
	try {
		document.execCommand("BackgroundImageCache", false, true);
	} catch(err) {}
	
	
	//$("table.text_e").addClass('table');	
	
	$("table.table").find("tr:odd").addClass("alt");
	
	
	
	
	// SEARCH ----------------------------
	$("#search-button").click(function(){
		window.document.search.submit();
		return false;
	});
	$("#search-text").focus(function() {
		if($(this).val() == "Введите слово")
			$(this).val("");
	});
	
	
	// MENU ---------------------------------
	$("a.lvl1").click(function() {
		if ($(this).hasClass("lvl1-child")) {
			var block = $(this).parent();
			block.toggleClass("lvl1-on");
			return false;
		}
	});
	$("a.lvl2").click(function() {
		if ($(this).hasClass("lvl2-child")) {
			var block = $(this).parent();
			block.toggleClass("lvl2-on");
			return false;
		}
	});
	
	
	// USER --------------------------------------
	$("#reg-submit").click(function() {
		
		var form = $("#reg-form");
		
		$.ajax({
			type: 'post',
			dataType: "html",
			url: '/ajax/user.php?act=reg',
			data: form.serialize(),
			beforeSend: function(){
				$("#reg-info").html("<span style='color: yellow;'>Отправка запроса..</span>");
				$("#reg-info").fadeIn(500);
			},
			success: function(data){
				if(data == 'ok') {
					$("#reg-info").html("<span style='color: lime;'>Вы успешно зарегистрировались.</span>");
					setTimeout(function(){
						$("#reg-close").click();
						window.location.href = window.location.href;
					}, 500);
					
				}
				else {
					$("#reg-info").html("<span style='color: #fff;'>" + data + "</span>");
				}
				$("#reg-info").fadeIn(500);
			},
			error: function(){
				alert("Ошибка отправки.");
			}
		});
		
		
		return false;
	});
	
	$("#remeber-pass").click(function() {
		if( $("#login-mail").val() == '' ) {
			$("#enter-info").html("<span style='color: #fff;'>Вы не указали email.</span>");
			$("#enter-info").fadeIn(500);
			return false;
		}
			
		
		$.ajax({
			type: 'post',
			dataType: "html",
			url: '/ajax/user.php?act=remember',
			data: 'email=' + $("#login-mail").val(),
			beforeSend: function(){
				$("#enter-info").hide();
			},
			success: function(data){
				if (data == 'ok') {
					$("#remeber-pass").hide();
					$("#enter-info").html("<span style='color: lime;'>На указанный email выслан новый пароль.</span>");
					$("#enter-info").fadeIn(500);
				}
				else {
					$("#enter-info").html("<span style='color: #fff;'>Такой пользователь не найден.</span>");
					$("#enter-info").fadeIn(500);
				}
			},
			error: function(){
				alert("Ошибка отправки.");
			}
		});
		return false;
	});
	
	$("#enter-submit").click(function() {
		
		var form = $("#enter-form");
		
		$.ajax({
			type: 'post',
			dataType: "html",
			url: '/ajax/user.php?act=login',
			data: form.serialize(),
			beforeSend: function(){
				$("#enter-info").html("<span style='color: yellow;'>Отправка запроса..</span>");
				$("#enter-info").fadeIn(500);
			},
			success: function(data){
				if(data == 'ok') {
					$("#enter-info").html("<span style='color: lime;'>Вы успешно авторизировались.</span>");
					setTimeout(function(){
						$("#enter-close").click();
						window.location.href = window.location.href;
					}, 500);
					
				}
				else {
					$("#enter-info").html("<span style='color: #fff;'>" + data + "</span>");
				}
				$("#enter-info").fadeIn(500);
			},
			error: function(){
				alert("Ошибка отправки.");
			}
		});
		return false;
	});
	$("#reg-close").click(function() {
		$("#upper-reg").hide();
		$("#upper-layer").hide();
		return false;
	});
	$("#enter-close").click(function() {
		$("#upper-enter").hide();
		$("#upper-layer").hide();
		return false;
	});
	
	$(".auth-reg").click(function() {
		$("#upper-reg").show();
		$("#upper-layer").show();
		return false;
	});
	$(".auth-enter").click(function() {
		$("#upper-enter").show();
		$("#upper-layer").show();
		return false;
	});
	
	
	$("#logout").click(function() {
		$.ajax({
			type: 'post',
			dataType: "html",
			url: '/ajax/user.php?act=logout',
			data: '',
			beforeSend: function(){
				
			},
			success: function(data){
				if(data == 'ok') {
					window.location = window.location;
				}
				else {
					alert("Ошибка отправки.");
				}
			},
			error: function(){
				alert("Ошибка отправки.");
			}
		});
		return false;
	});
	
	
	// QA -------------------------------------------------------
	$("#faq-submit").click(function() {
		
		if( $("input[name='author']").val() == '' || $("input[name='email']").val() == '' || $("input[name='question']").val() == '' ) {
			alert("Не все поля заполнены");
			return false;
		}
		
		$.ajax({
			type: "POST",
			dataType: "html",
			url: "/ajax/index.php?type=qa",	
			data: $("#faq-form").serialize(),
			success: function(data) {
				alert(data);
				$("#faq-form input, #faq-form textarea").val('');
			},
			error: function(){ alert("Ошибка отправки данных"); }
		});
		
		return false;
	});
	
	
	// CATALOGUE GALLERY --------------------------------------------
	var pic_box_current	= 0;
	var pic_box_width	= 110;
	var pic_box_count	= $("#pic-box .pic-scroll a").length;
	var pic_box_speed	= 400;
	
	$("#pic-box .pic-scroller").width( pic_box_count * pic_box_width - 10 );

	function setPicBox(i) {
		//alert(i);
		var this_img = $("#pic-box .small-img").eq(i);
		$("#pic-box .small-img").eq(pic_box_current).removeClass('active');
		this_img.addClass('active');
		if(i>0)
			scrollLeft = i*pic_box_width - pic_box_width;
		else
			scrollLeft = 0;
		$("#pic-box .pic-scroll").animate({scrollLeft: scrollLeft + 'px'}, pic_box_speed);
		$("#pic-box .big-img").attr({
			'href': '/files/image/' + this_img.find('img').attr('title') + '/' + this_img.attr('href'),
			'title': this_img.find('img').attr('alt')
		});
		$("#pic-box .big-img img").attr({
			'src' : '/images/jquery.lightbox/blank.gif'
		});
		$("#pic-box .big-img img").attr({
			'src': '/files/image/' + this_img.find('img').attr('title') + '/big_' + this_img.attr('href'),
			'alt': this_img.find('img').attr('alt')
		});
		if( this_img.find('img').attr('alt') != '') {
			$("#pic-box .big-img div.title").text(this_img.find('img').attr('alt')).show();
		}
		else {
			$("#pic-box .big-img div.title").hide();
		}
		
		pic_box_current = i;
	}
	if(pic_box_count)
		setPicBox(0);
	
	$("#pic-box .pic-scroll a").click(function() {
		setPicBox( $("#pic-box .pic-scroll a").index(this) );
		return false;
	});
	
	$("#pic-next").click(function() {
		if( pic_box_current < (pic_box_count-1) )
			setPicBox(pic_box_current+1);
		return false;
	});
	$("#pic-back").click(function() {
		if( pic_box_current > 0 )
			setPicBox(pic_box_current-1);
		return false;
	});
	
	
});
