$(function() {
	$.get("/action.php?actionType=CategoryAjaxAction&method=getOpenCategories", function(data) {
		var blocks = data.split(",");
		for (var i = 0; i < blocks.length; i++) {
			$('ul[name = "' + $.trim(blocks[i]) + '"]').css('display', 'block');
			$('ul[name = "' + $.trim(blocks[i]) + '"]').parent().children().children().attr('src', '/graph/minus.gif');
		}
	});

//Левое Меню
	$('.main_menu img').click(function() {
		var submenu = $(this).parent().parent().children('ul');
		
		submenu.slideToggle('fast', function() {
			$(this).children('ul').css('display', 'block');
		});
		if ($(this).attr('src') == '/graph/minus.gif') {
			$(this).attr('src', '/graph/plus.gif');
			$.get("/action.php?actionType=CategoryAjaxAction&method=toggleCategory&cat=" + submenu.attr('name') + "&toggle=off");
		}
		else {
			$(this).attr('src', '/graph/minus.gif');
			$.get("/action.php?actionType=CategoryAjaxAction&method=toggleCategory&cat=" + submenu.attr('name') + "&toggle=on");
		}
		return false;
	});

//Фильтр
	
	$('#category').change(function() {
		var category = $('#category').attr('value');
		$.post('/action.php', {actionType: 'CategoryAjaxAction', method: 'getGenres', category: category, genre: $.cookie('genres')}, function(data) {
			$('#genres').html(data);
		});
		$.cookie('genres', null);
	});
	
	$('#genres').change(function() {
		$.cookie("genres", $('#genres').attr('value'));
	});
	
	var category = $('#category').attr('value');
	$.post('/action.php', {actionType: 'CategoryAjaxAction', method: 'getGenres', category: category, genre: $.cookie('genres')}, function(data) {
		$('#genres').html(data);
	});
	
	$('#afilter td input[type = "checkbox"]').change(function() {
		$('#content_block2 #afilter td input:checked').not(this).click();
	});
	
//Комментарии		
	$('#asc').click(function() {
		$(this).css('font-weight', 'bold');
		$('#desc').css('font-weight', 'normal');
		reloadComments('ASC');
		return false;
	});
	
	$('#desc').click(function() {
		$(this).css('font-weight', 'bold');
		$('#asc').css('font-weight', 'normal');
		reloadComments('DESC');
		return false;
	});

// Регистрация
	$('input#email').blur(function() {
		var email = $('input#email').attr('value');
		if(email == '') {
			return false;
		}
		$.get('/action.php', {actionType: 'UserAjaxAction', method: 'checkMail', email: email }, function(data) {
			if(data == 'error') {
				$('input#email').addClass('error');
				$('input#email').css('border', '1px solid red');
				$('span#email').text('Пользователь с таким адресом e-mail уже существует');
			} else {
				$('input#email').removeClass('error');
				$('input#email').css('border', '1px solid #92CB30');
				$('span#email').text('');
			}
		});
	});
	
	$('input#nick').blur(function() {
		var nick = $('input#nick').attr('value');
		if(nick == '') {
			return false;
		}
		$.get('/action.php', {actionType: 'UserAjaxAction', method: 'checkNick', nick: nick }, function(data) {
			if(data == 'error') {
				$('input#nick').addClass('error');
				$('input#nick').css('border', '1px solid red');
				$('span#nick').text('Пользователь с таким ником уже существует');
			}
			else {
				$('input#nick').removeClass('error');
				$('input#nick').css('border', '1px solid #92CB30');
				$('span#nick').text('');
			}
		});
	});
	
	$('input#system_id').blur(function() {
		var sysId = $('input#system_id').attr('value');
		if(sysId == '') {
			return false;
		}
		$.get('/action.php', {actionType: 'UserAjaxAction', method: 'checkSysId', system_id: sysId }, function(data) {
			if(data == 'error') {
				$('input#system_id').addClass('error');
				$('input#system_id').css('border', '1px solid red');
				$('span#system_id').text('Пользователь с таким системным ID уже существует');
			} else {
				$('input#system_id').removeClass('error');
				$('input#system_id').css('border', '1px solid #92CB30');
				$('span#system_id').text('');
			}
		});
	});

	
	$('input#register').click(function() {
		$('#register input').blur();
		if($('.error').length > 0) {
			return false;
		}
	});
});

function verticalAlign() {
	var count = $('.info').length;
	for(var i = 0 ; i < count; i = i + 2) {
		height1 = $('.info').eq(i).height();
		height2 = $('.info').eq(i+1).height();
		var height = Math.max(height1, height2);
		
		$('.info').eq(i).height(height);
		$('.info').eq(i+1).height(height);
		
		height1 = $('.rounds').eq(i).height();
		height2 = $('.rounds').eq(i+1).height();
		var height = Math.max(height1, height2);
		
		$('.rounds').eq(i).height(height);
		$('.rounds').eq(i+1).height(height);
		
		height1 = $('.block').eq(i).height();
		height2 = $('.block').eq(i+1).height();
		var height = Math.max(height1, height2);
		
		$('.block').eq(i).height(height);
		$('.block').eq(i+1).height(height);
	}
};

jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};
