//-----------------------------------------------------------------------------------------------------------
function tpl_replace(_content, _tpl, _out_tpl){
	_obj = '<div id=tmpcontent style="display:none;"></div>'
	$(document.body).append(_obj)
	_obj = $('#tmpcontent')
	_tpl = _obj.append(_tpl)
	_tpl = _tpl.html()
	//alert(_tpl)
	//alert(_content)
	_out_tpl = _obj.append(_out_tpl)
	_out_tpl = _out_tpl.html()
	
	_obj.remove()
	
	_out_content = ''
	_tmp = _tpl
	var vars_arr = new Array()
	_b = 1
	i = 0
	while(_b != -1){
		_b = _tmp.indexOf('{')
		_e = _tmp.indexOf('}')
		_l = _e - _b + 1
		_var = _tmp.substr(_b,_l)
		vars_arr[i] = _var
		_tmp = _tmp.substr(_e + 1)
		i++
	}

	_b = 1
	i = 0
	_tmp = _tpl
	_c = _content
	var content_arr = new Array()
	while(_b != -1){
		_p = _tmp.indexOf('{')
		_b = _tmp.indexOf('}')
		_str = _tmp.substr(0,_p)

		_nstr = _tmp.substr(_b + 1, _tmp.substr(_b+1).indexOf('{'))
		//alert(_tmp + '\n\n\n'+_str+'\n\n\n'+_nstr)
		
		_cp = _c.indexOf(_str)+_str.length
		_ce = _c.indexOf(_nstr)
		_var = _c.substr(_cp,_ce - _cp)	
		//alert(_c+'\n\n\n'+_str+'\n\n\n'+_nstr+'\n\n\n'+_cp+'    '+_ce)
		//alert(_var)
		
		content_arr[i] = _var
		_tmp = _tmp.substr(_b + 1)
		i++
	}
	
	_b = 1
	i = 0
	_tmp = _out_tpl
	while(i < vars_arr.length){
		//alert(vars_arr[i] + ' = ' + content_arr[i])
		//_b = _tmp.indexOf(vars_arr[i])
		//_e = _tmp.indexOf(vars_arr[i]) + vars_arr[i] - 1
		_tmp.replace(vars_arr[i],content_arr[i])
		i++
	}
	_out_content = _tmp
	return _out_content	
}
//---------------------------------------------------------------------------------------------
	function comment_delete_init(_url){
		s_confirm = 'Удалить?'
		$('.comment_delete').each(function(){
			$(this).click(function(){
				_p = $(this).parent()
				_i = 0
				while((_p.attr('class') != 'comment')&&(_p.attr('class') != 'comment_nomod')){
					_p = _p.parent()
					_i ++
					if(_i > 10 ) break
				}

				comment_delete(_p,false,_url)
				return false
			})
		})
	}
//---------------------------------------------------------------------------------------------
function comment_tplReplace(_comment,_comment_tpl){
		var new_comment = ''
		var tmp_content = '<div id=tmp_content style="visible:none"></div>'
		_c = $('#comments')
		_c.append(tmp_content)
		_comment_a = _comment.find('.comment_img')
		_comment = $(_comment)
		_comments = _comment.parent()
		_comment_id = _comment.attr('id')
		_comment.attr('id','no_id')
		_tc = $('#tmp_content')
		_tc.append(_comment_tpl)
		
		_ct = _tc.find('.comment')
		_ct_a = _ct.find('.comment_img')
				
		_ct.attr('id',_comment_id)
		
		_ct_a.attr('href',_comment_a.attr('href'))
		
		_ct.find('img').attr('src',_comment.find('img').attr('src'))
		
		_ct.find('.comment_datetime').text(_comment.find('.comment_datetime').text())
		
		_ct.find('.comment_text').text(_comment.find('.comment_text').text())
		
		_ct.find('.user_title').text(_comment.find('.user_title').text())
		
		_ct.find('.user_name').text(_comment.find('.user_name').text())
		
		_ct.find('.user_id').text(_comment.find('.user_id').text())
		
		_ct.find('.user_surname').text(_comment.find('.user_surname').text())
		
		_ct.find('.user_email').text(_comment.find('.user_email').text())
		
		_ct.find('.comment_edit').attr('href',_comment.find('.comment_edit').attr('href'))
		
		_ct.find('.comment_delete').click(function(){
			_t = $(this)
			comment_delete(_t.parent())
		})
	
		_comment.remove()
		
		_c.append(_ct)
		
		_tc.remove()
		
		
		
		
		return new_comment
	}
//---------------------------------------------------------------------------------------------
function comment_del(_comment, _alert){

	_url = '/comments/comment_edit.php?a=d'
	_id = _comment.attr('id')
	_id = parseInt(_id.substr(8))
	if(!_id){
		_id = _comment.attr('id')
		_id = parseInt(_id.substr(14))		
	}
	
	if(_alert){
		_html = '<div class="alert"><div class="text_alert">Вы уверены что хотите удалить комментарий? </div><input class="yes" type="image" src="img/yes.jpg"><input class="no" type="image" src="img/no.jpg"></div>'
		_comment.find('.notify').html(_html)
		_comment.find('.yes').click(function(){
			comment_del(_comment, false)
		})
		
		_comment.find('.no').click(function(){
			$(this).parent().remove()
		})
	
	}else{
		$.post(_url,{id:_id},function(_data){
			//alert(_data)
			$('#comment_'+_id).remove()
			//if(_comment.attr('class') == 'comment_nomod')
				//comments_nomod_num_refresh(-1)
		},'html')
	}
}	
//---------------------------------------------------------------------------------------------
	function comment_delete(_comment,_alert,_url){
		if(!_url)
			_url = 'comments.php?page=d'
		_id = _comment.attr('id')
		_id = parseInt(_id.substr(8))
		if(!_id){
			_id = _comment.attr('id')
			_id = parseInt(_id.substr(14))		
		}
		if(!_alert){
			if(confirm(s_confirm)){
				$.post(_url,{id:_id},function(_data){
					alert(_data)
					$('#comment_'+_id).remove()
					if(_comment.attr('class') == 'comment_nomod')
						comments_nomod_num_refresh(-1)
				},'html')
			}
		}else{
			$('#comment_'+_id).remove()
			if(_comment.attr('class') == 'comment_nomod')
				comments_nomod_num_refresh(-1)
		}
	}
//---------------------------------------------------------------------------------------------
	function comment_accept(_comment,_alert){
		
		_id = _comment.attr('id')
		_id = parseInt(_id.substr(8))
		/*
		_url = 'comments.php?page=a'
		if(!_alert){
			$.post(_url,{id:_id},function(_data){
				_comment.remove()
				if(_data){
					comment_tplReplace(_comment,comment_tpl)
					comments_nomod_num_refresh(-1)
				}
			},'html')
		}else{
			_comment.remove()
			comment_tplReplace(_comment,comment_tpl)
			comments_nomod_num_refresh(-1)
		}
		*/
		_url = 'comments.php'
		_page = parseInt($($(".comments_nav")[0]).find('span').text()) - 1
		$.post(_url,{id:_id,sa:'a',nav:_page},function(_data){
			if(_data){
				comments_nomod_num_refresh(-1)
				$('#page_content').html(_data)
			}
		},'html')
		
		
	}
//---------------------------------------------------------------------------------------------
	function comments_nomod_num_refresh(_num){
		_n = parseInt(_num)
		if(_n){
			_a = $('#menu_comments > a')
			if(!_a.html()){
				_a = $('#menu_comments > b')
			}
			_b = _a.text().indexOf('(')
			_e = _a.text().indexOf(')')
			_text = _a.text().substr(0,_b)
			_num_text = _a.text().substr(_b + 1,_e - _b - 1)
			_now_num = parseInt(_num_text)
			_new_num = 0
			if(_now_num){
				_new_num = _now_num + _num	
			}
			if(_new_num)
				_a.text(_text + '('+_new_num+')')
			else
				_a.text(_text)
		}
	}		
//------------------------------------------------------------------------------
	function set_realHeight(element,without){
		if(without)	arr = without.split(',')
		element.each(function(){
			_t = $(this)
			_newHeight = 0
			_cn = 0
			_t.children().each(function(){
				_c = $(this)
				_cdt = this
				_offset = _c.attr('clientHeight')
				if(!_offset)
					_offset = _c.attr('offsetHeight')
				if(without){
					for(_i = 0; _i < arr.length; _i ++){
						var _wdt
						_t.find(arr[_i]).each(function(){
							_wdt = this
						})
							if(_cdt != _wdt)
								_newHeight += parseInt(_offset)	
					}
				}else{
					//alert(_offset)
					_newHeight += parseInt(_offset)
				}
			})
			//alert(_newHeight+'px')
			_t.css('height',_newHeight+'px')
		})
	}


