/* КОММЕНТАРИИ */
$(document).ready(function(){	
	if($('#comment-block .message').size()<=2&&!$('#comment-block textarea.comment-ta').attr('name')) {
		$('#comment-manage').remove();
	}
	else {
		$('#comment-manage').css('display','block');
	}
});

function comment_manage(comment_block,comment_content,obj) {
	cb=gid(comment_block);
	cc=gid(comment_content);
	
	
	var form=0;
	if(document.com_form) {
		form=document.com_form;
	}

	if(obj.className=='comment-manage') {
		comment_action(cb,cc,form,0);
		obj.className=obj.className+' comment-manage-oppened';			
		obj.innerHTML='Свернуть комментарии <span id="com_count"></span>';
	}
	else if(obj.className=='comment-manage comment-manage-oppened'){
		comment_action(cb,cc,form,0);
		obj.className=obj.className.replace(' comment-manage-oppened','');		
		obj.innerHTML='Развернуть комментарии <span></span>';
	}
	else {
		if(form) {			
			comment_action(cb,cc,form,1);	
			gid('comment-manage').className='comment-manage comment-manage-oppened';
			gid('comment-manage').innerHTML='Свернуть комментарии <span></span>';
		}
		else {
			//alert('Тут будет авторизация');	
			load_login_form(584,91);	
		}
	}	
}

function comment_height(comment_block,comment_content) {
	cb=gid(comment_block);
	cc=gid(comment_content);	
	cb.style.height=get_com_height(cb,cc)['minimal']+'px';
}


function comment_action (cb,cc,form,focus) {
	cb_height=cb.offsetHeight;
	form_height=0;
	
	if(form) {
		form_height=form.offsetHeight;
	}
	
	var Height=get_com_height(cb,cc);
	Height['max']=Height['cc']+form_height;
	
	if(cb_height<Height['max']||focus==1) {
		var start	=cb_height;
		var end		=Height['max'];	
	}
	else {		
		var start	=Height['max'];
		var end		=Height['minimal'];
	
	}
	var tmp			=start;
	var step		=Height['max']/15;
	

	timer=setInterval(go_comment_action,50);

	
	function go_comment_action() {	
		
		if	(start<end) {
			tmp+=step;			
			if(tmp>end) tmp=end;
					
		}
		else if	(start>end) {
			tmp-=step;
			if(tmp<end) tmp=end;
		}
		
		cb.style.height=tmp+'px';

		if (tmp==end) {
			clearInterval(timer);
			if(focus) form.message.focus();
		}
		
	}	
	return true;	
}

function get_com_height (cb,cc) {
	var messages=new Array();
	var tmp=0;
	var Height=new Array();
	Height['minimal']=0;
	Height['cc']=0;
	
	if(divs=cc.getElementsByTagName('div')) {		
		for(i=0;i<divs.length;i++) {
			if(divs[i].className=='message last-message-0' || divs[i].className=='message last-message-1') 
			{
				Height['cc']+=divs[i].offsetHeight;
				messages[tmp]=divs[i];				
				tmp++;
			}	
			if(divs[i].className=='pagenav') {
				Height['cc']+=divs[i].offsetHeight;
			}
		}
	}
	if(messages.length>1) {			
		Height['minimal']=messages[0].offsetHeight+messages[1].offsetHeight-2;			
	}
	else if(messages.length==1) {			
		Height['minimal']=messages[0].offsetHeight;
	}
	else {
		Height['minimal']=40;				
	}
	
	return Height;
}

var Area_manage = function() {
	this.clear = function(obj,text) {	
		if(obj.value==text) {
			obj.value='';
			obj.style.color='#000';
		}	
	}
	this.set = function(obj,text) {	
		if(obj.value=='') {
			obj.style.color='#817f84';
			obj.value=text;		
		}	
	}
}
var area_manage=new Area_manage();
