var move = 0;
$(document).ready(function(){
	
	widthfix();
	$(window).resize(function(){
		widthfix();
	});
	
	$('.search-field input').blur(function() {
		//if(this.value=='') {this.value=this.defaultValue; this.className='text';}
		$('.logo img').attr('src','/i/logo-inactive.gif');
	});
	$('.search-field input').focus(function() {
		//if(this.value==this.defaultValue) {this.value=''; this.className='active-text';}
		$('.logo img').attr('src','/i/logo.gif');		
	});
	
	$('.comment-form .comment-input input.submit').attr('disabled','disabled');
	
	$('.comment-answer a').click(function(){
		$('.comment-form').not('#comment-form').hide();
		$(this).parent().parent().children('.comment-form').show();
		//$(this).parent().parent().children('.comment-form').find('.comment-form-name').children('input').focus();
		$(this).parent().parent().children('.comment-form').find('.comment-field').children('textarea').focus();
		$(this).parent().parent().children('.comment-form').find('.comment-input').children('input.submit').attr('disabled','disabled');
		
		return false
	});

	$('.links a.enter').click(function(){
		$('.links .form').animate({top:'10px'},300).animate({top:'0px'},100).animate({top:'7px'},100);
		$('.form .mail input').focus();
		return false
	});
	
	$('.links .form .close').click(function(){
		$('.links .form').animate({top:-300},500);
	});
	
	$('#reg .gray input.text').blur(function() {
		if(this.value=='') {this.value=this.defaultValue; this.className='text';}
	});
	$('#reg .gray input.text').focus(function() {
		if(this.value==this.defaultValue) {this.value=''; this.className='active-text';}
	});
	
	$('#head .links .form .bg').height($('#head .links .form .wrap').height()+27);
	if (document.location.pathname.length < 2) $('.search-field input').focus();


	$('.buffer a').click(function(){
		$('.buffer .bl').show();
        var clip = new ZeroClipboard.Client();   
        var insertflash = $('.buffer button');
        clip.glue(insertflash[0]);
        var txt = $.trim($("#clip").text());
        clip.setText(txt);
		return false
	});
	

	try
    {
	form();
	commentvalid();
	oform();
    }
    catch(e){;}
	
	$('.form .mail input,.form .password input').keydown(function(){oform();}).keyup(function(){oform();}).blur(function(){oform();}).focus(function(){oform();});
	
	//$('#comment-form .comment-form-name input,#comment-form .comment-form-mail input,#comment-form .comment-field textarea').keydown(function(){commentvalid();}).keyup(function(){commentvalid();}).blur(function(){commentvalid();}).focus(function(){commentvalid();});
	//$('.comment-form .comment-form-name input,.comment-form .comment-form-mail input,.comment-form .comment-field textarea').keydown(function(){commentvalid();}).keyup(function(){commentvalid();}).blur(function(){commentvalid();}).focus(function(){commentvalid();});
	$('.comment-form .comment-form-name input,.comment-form .comment-form-mail input,.comment-form .comment-field textarea').keydown(function(){commentvalid(this);}).keyup(function(){commentvalid(this);}).blur(function(){commentvalid(this);}).focus(function(){commentvalid(this);});
	
	$('#dev div a').hover(function(){
		$('#dev div a').attr('id','');
		$(this).attr('id','active');
								   
	},function(){
		
	});
	
	
	$('#dev div a.ros').click(function(){
		$('#dev span').hide();		
		$('#dev span:eq(0)').show();
		return false
	});
	$('#dev div a.host').click(function(){
		$('#dev span').hide();		
		$('#dev span:eq(1)').show();	
		return false						   
	});
	
	$('.search-block li').hover(function(){
		//$('.search-block li').removeClass('active');
		//$(this).addClass('active');
	},function(){
		
	});
	
	
	
	$('.down li a').click(function(){
		htmllii = $(this).html();
		$('#find').val(htmllii);
	});
	
	
	
	$('#find').bind('keydown', function(e) {
		var code = (e.keyCode ? e.keyCode : e.which);
		sizeli = $('.down li').size()-1;
		move = $('.down li.active').prevAll().size();
		if ($('.down').is(':visible')){
			
		} else {
			$('.down').slideDown();
		}
		if (code==40){
			if ($('.down li').hasClass('active')){
				
			} else {
				move=-1
			}
			if (move==sizeli){move=-1}
			move=move+1
			$('.down li').removeClass('active');
			$('.down li:eq('+move+')').addClass('active');
			htmlli = $('.down li.active a').html();
			$('#find').val(htmlli)
		}
		if (code==38){
			if (move==0){move=sizeli+1}
			move=move-1
			$('.down li').removeClass('active');
			$('.down li:eq('+move+')').addClass('active');
			htmlli = $('.down li.active a').html();
			$('#find').val(htmlli)
		}
	});
	
	$('.close a').click(function(){
		$('.down').slideUp();
		return false
	});

	 

});

function widthfix(){
	$('.another .left').css('margin-right',$('.another .right').width()+380);		
}

function commentvalid(_this){
	var form = _this.form;
	if (form) {
		//ename = $(form('.comment-form-name input')).val();
		//email = $('#comment-form .comment-form-mail input').val();
		//etext = $('#comment-form .comment-field textarea').val();
		etext = form.elements[2].value;
		//if (ename && ename.length > 0 && email && email.length > 0 && etext && etext.length > 0) {
		if (etext && etext.length > 0) {
			form.elements[4].disabled = '';
		} else {
			form.elements[4].disabled = 'disabled';
		}
	}
}

function form(){
	$('#reg').validate({
		rules: {
			login: {
				required: true,
				minlength: 5
			},
			password: {
				required: true,
				minlength: 5
			},
			email: {
				required: true,
				email: true
			}
		},
		messages: {
			login: {
				required: "Введите логин",
				minlength: "Минимальная длина логина 5 сиволов"
			},
			password: {
				required: "Введите пароль",
				minlength: "Минимальная длина пароля 5 сиволов"
			},
			email: {
				required: "Введите почту",
				email: "Введите правильную почту"
			}
		},
	    errorPlacement: function(error, element) {
	      var er = element.attr("name");
	      error.appendTo( element.parent().find('strong') );
	    }
	});
}

function oform() {
	name = $('#oname').val();
	pass = $('#opass').val();
	if ((name.length > 0) && (pass.length > 0)) {
		$('.form .submit input').removeAttr('disabled');
	} else {
		$('.form .submit input').attr('disabled','disabled');
	}
}



