$(document).ready(function(){
	
	Cufon.replace('.font-goth');

	// -==[ buttons ]==-
	
	$(".button, .subbutton, .button-top").click(function(){ window.location = $(this).find("a").attr('href'); });
	
	$("#menu .button-left").hover(function() {
		$(this).animate({left : '-100px'}, 200);
	}, function() {
		$(this).animate({left : '-80px'}, 150);
	});
	$("#menu .button-right").hover(function() {
		$(this).animate({right : '-100px'}, 200);
	}, function() {
		$(this).animate({right : '-80px'}, 150);
	});
	
	$("#button-inet-block").hover(function(){
		$(this).css({'height': '120px'}).animate({left : '-100px'}, 150);
		$("#button-inet-test").animate({left : '-20px'}, 200);
		$("#button-inet-doc").animate({left : '-20px'}, 250);
	}, function() {
		$("#button-inet-doc").animate({left : '100px'}, 100);
		$("#button-inet-test").animate({left : '100px'}, 150);
		$("#button-inet-block").animate({left : '-80px'}, 200, function(){
			$(this).css({'height': '40px'});
		});
	});
	
	
	
	
	function upperOpen( block, speed ) {

		if(speed < 200) speed = 200;
		var w = parseInt( block.width() );
		var h = parseInt( block.height() );
		
		$("#upper-left").css({
			'margin-top' : '-' + (h/2) + 'px',
			'left' : '-' + (w/2) + 'px',
			'width' : (w/2) + 'px',
			'height' : h + 'px'
		});
		$("#upper-right").css({
			'margin-top' : '-' + (h/2) + 'px',
			'right' : '-' + (w/2) + 'px',
			'width' : (w/2) + 'px',
			'height' : h + 'px'
		});
		$("#upper-center").css({
			'margin-top' : '-' + (h/2) + 'px',
			'margin-left' : '-' + (w/2) + 'px',
			'width' : w + 'px',
			'height' : h + 'px'
		});
		$("#upper-center").append(block);
		
		$("#upper-layer").css({'display' : 'block', 'opacity' : '0.0'}).animate({opacity: '0.8'}, speed);
		$("#upper-center").css({'opacity' : '0.0'});
		$("#upper").css("display", "block");
		if( $.browser.msie && $.browser.version == 6) {
			$("html").css({'overflow' : 'hidden', 'overflow-y' : 'hidden', 'overflow-x' : 'hidden'});
		}
		
		$("#upper-left").animate({left: '50%'}, (speed + 200));
		$("#upper-right").animate({right: '50%'}, (speed + 200), function(){
			$("#upper-center").animate({opacity: '1.0'}, (speed - 200));
		});
		
	}
	
	function upperClose( block, speed ) {
		
		if(speed < 200) speed = 200;
		
		$("#upper-layer").animate({opacity: '0.0'}, (speed + 200));
		
		$("#upper-center").animate({opacity: '0.0'}, (speed - 200), function(){
				$("#upper-left").animate({left: '-' + $("#upper-left").width() + 'px'}, speed);
				$("#upper-right").animate({right: '-' + $("#upper-right").width() + 'px'}, speed, function(){
					$("#upper").css("display", "none");
					$("#upper-layer").css("display", "none");
					if( $.browser.msie && $.browser.version == 6) {
						$("html").css({'overflow-y' : 'scroll', 'overflow-x' : 'auto'});
					}
				});
		});
		$("body").append(block);
		//$("#upper-center").empty();
	}
	
	function upperInfo( text ) {
		$("#upper-info").html(text);
		upperOpen($("#upper-info"), 300);
		setTimeout( function(){upperClose($("#upper-info"), 300)}, 2000 );
	}
	$("#upper-info").click(  function(){ upperClose($(this), 300) } );
	$("#button-auth").click( function(){
		if($(this).find('a').attr('name') == 'enter')
			upperOpen($("#auth"), 400);
		else
			window.location = '/?exit';
	});
	$("#auth .close").click( function(){ upperClose($("#auth"), 300) });
	$("#button-reg").click( function(){ upperInfo( 'В данный момент недоступна.' ); } );
	
	
	
	// message-form --------------------------------------------

	$("#message-form input.text[name='message_guest']").addClass('off').val('Имя');
	$("#message-form input.text[name='message_mail']").addClass('off').val('E-mail');
	$("#message-form textarea.text").addClass('off').val('Текст сообщения');
	$("#message-form .off").focus(function(){
		$("#message-form .off").val('').removeClass('off');
	});
	
	$("#message-form").submit(function(){
		form = $(this);
		
		if($("input.text", this).val() == '' || $("textarea", this).val() == '' || $(".off", this).length > 0) {
			upperInfo( 'Заполнены не все поля!' );
			return false;
		}
		
		$.ajax({
			type: form.attr("method"),
			dataType: "json",
			url: form.attr("action"),
			data: form.serialize(),
			beforeSend: function() {
				
			},
			success: function(data){				
				switch (data.result) {
					case 'done':
						upperInfo( 'Сообщение отправлено успешно.' );
						$("input.text, textarea", form).val('');
						break;
					case 'error':
						upperInfo( 'Ошибка отправки.' );
						break;
				}
			},
			error: function() {
				upperInfo( 'Невозможно отправить сообщение.' );
			}
		});

		return false;
	});
	
	
	
	// test ----------------------------------------------------
	
	$("#test-body .test-block .desert-button").click(function(){
		var i = $("#test-body .test-block .desert-button").index($(this));
		
		if( $("#test-body textarea").eq(i).val() != '') {
			if($("#test-body .test-block").length != (i+1)) {
				$("#test-body .test-block").eq(i).css({'display' : 'none'});
				$("#test-body .test-block").eq(i+1).css({'display' : 'block'});
			}
			else {
				$("#test-body").submit();
			}
		}
		
	});
	
	$("#test-body .test-eblock input").click(function(){
		$("#test-body .test-eblock").css({'display' : 'none'});
		$("#test-body .test-block").eq(0).css({'display' : 'block'});
	});

	$(".switch_code a").click(function(){
		var block = $(this).attr("href");
		$(".switch_code textarea").hide();
		$(block).show();
	});
	$(".switch_code textarea").focus(function() {
		$(this).select();
	})
	
});