$(function(){

/** Главная страница */

	$('div.info-links strong').click(function()
	{
		if( this.className != 'active' )
		{
			var index = $(this).index();
			
			$(this).siblings().removeClass('active');
			$(this).addClass('active');
			
			$('div.info span:visible').hide('drop', {}, 'fast', function()
			{
				$('div.info span:eq('+ index +')').show('drop', {'direction': 'right'});
			});
		}
	});
	
	$('div.menu a.link').hover(
		function(){ $(this).animate({ backgroundColor: '#B3D966', color: 'white' }, 200) },
		function(){ $(this).animate({ backgroundColor: 'white', color: '#66764C' }, 100) }
	);
	$('div.menu a.link').click(function()
	{
		var _this = $(this);
		_this.parent().effect('explode', {}, 'fast', function(){ location.replace( _this.attr('href') ) });
		return false;
	});

/** Портфолио */

	$('div.portfolio a').hover( 
		function(){ $('span:first', this).show('bounce', {times: 3}); },
		function(){ $('span:first', this).stop(true, true).fadeOut() }
	);

/** Блог */

	$('#rates i').hover( function(){ $(this).prevAll('i').add(this).toggleClass('hover'); } );
	
	$('#rates i').click( function()
	{
		if( ! $(this).parent().hasClass('disabled') )
		{
			var loader = $('#loader');
			loader.show();
			
			$.ajax({
			       url: location.href,
			       type: 'post',
			       data: 'rating=' + $(this).index(),
			       dataType: 'text',
			       success: function( rates )
			       {
					loader.hide('fast');
					$('div.ratings').html( rates );
			       }
			});
		}
	});

/** Обработка форм */

	$(':text, textarea').focus(function(){ this.value = this.value == this.defaultValue ? '' : this.value });

	$(':text, textarea').blur(function()
	{ 
		if( this.value.match(/^\s*$/) != null )
		{
			this.value = this.defaultValue;
			$(this.parentNode).removeClass('filled');
		}
		else $(this.parentNode).addClass('filled');
	});

	$(':file').change(function()
	{ 
		$(this).prev().text( this.value.replace(/.*[\\\/](.*?)$/, '$1') );
		$(this.parentNode).addClass('filled');
		
	});
	
	$('form').submit(function()
	{
		var errors = 0;
		
		$(':input', this).each(function(){
			if( $(this).attr('required') && this.value == this.defaultValue )
			{
				this.focus();
				errors++;
			}
		});
		
		if( this['feedback'] != null ) this['js_antispam'].value = 'checked';
		
		return !errors;
	});
	
	// сквозняк для отправки сообщения
	
	$('#feedback, a.feedback').click(function()
	{
		var shadow = $('#alpha-shadow'), 
		    feedback = $('#feedback-form');
		
		$('i.close', feedback)[0].onclick = arguments.callee;
		
		if( $.browser.msie && $.browser.version <= 7 )
		{
			if( $.browser.version <= 6 ) shadow.css('height', document.body.clientHeight || document.documentElement.clientHeight);
			shadow.toggle();
			feedback.toggle();
		}
		else {
			shadow.slideToggle('fast');
			feedback.toggle('drop', {distance: 70});
		}
		
		return false;
	});
	
	// форма для добавления комментария в блоге
	
	$('#antispam img').click(function()
	{
		$('#antispam-checker').val( this.alt );
		$(this).addClass('selected').siblings().removeClass('selected');
	});
	
	//$('#add-comment-form').hide();
	$('#add-comment-form textarea').keypress(function(evt)
	{
		var enters = this.value.match(/\n/g);
		
		if( $(this).scrollTop() > 0 ) $(this).animate({'height': $(this).height() + $(this).scrollTop() + 'px'}, {duration: 200, queue: false});
		if( ! enters || enters.length < 8 ) $(this).css('height', '');
	});
	
	$('#add-comment').click( function(){ 
		$(this).hide().next().slideDown('fast', function()
		{
			window.scrollBy(0, 500); 
			$('textarea', this).focus(); 
		}) 
	});


/* Разное */

	$('pre.code').hover(
		//function(){ $(this).css('overflow', 'auto') }, 
		//function(){ $(this).css('overflow', 'hidden') }
	);
	
	$('div.faq div.links a').click(function()
	{
		var anchor = this.href.substr( this.href.lastIndexOf('#')+1 );
		$('a[name='+ anchor +']').next().find('.arr').effect('bounce', {direction: 'left', distance: 20, times: 5}, 500, function(){ $(this).hide() });
	});
	
});




