// УТФ-8

$(window).load(function() {
	$('#file1').change(checkForm);
	$('#menu span.inside').click(function() {
		if ($(this).is('.inside-selected'))	return false;
		$('#menu span').removeClass('inside-selected').addClass('inside');
		$(this).addClass('inside-selected').removeClass('inside');
		$('#menu ul').hide();
		
		var par = $(this).parent().parent();
		par.show();
		$('ul', $(this).parent()).show();
		if (par[0].id != 'menu')	$('ul', par).show();
		
		$('#addr').val(this.id);
		$('#content').html(getProgress()).load('./index.php?obj=' + this.id + '&rnd=' + getRandom(1000000));
	});
	
	checkForm();
	
	$('#menu ul').each(function(i) {
		var lis = $('li', this);
		if (lis.length > 10) {
			lis.each(function(i) {
				$(this).css('left', (Math.sin(i / 4) * 20) + 'px');
			});
		}
	});
	
	$('#menu li li').mouseover(function() {
		$(this).addClass('hover');
	}).mouseout(function() {
		$(this).removeClass('hover');
	}).css('cursor', 'pointer').click(function() {
		$('span.inside', this).click();
	});
	
	$('#add-photo span.inside').click(function() {
		$('#add-photo-form').toggle();
	});
});

function getProgress() {
	return '<img src="./images/ajax-loader.gif" width="16" height="16" alt="Загрузка данных..." />';
}

function getRandom(n) {
	return Math.floor(Math.random() * n);
}

function checkForm() {
	var dis = true;
	var file = $('#file1').val();
	if (file.search(/(\.jpg|\.jpeg)$/i) != -1) {
		dis = false;
		$('#add-photo-form .hint').removeClass('error');
	}
	else {
		if (file != '')	$('#add-photo-form .hint').addClass('error');
	}
	
	$('#submit')[0].disabled = dis;
}