//toggle
function toggle(id) {
	$("#"+id).toggleClass("hide");
}
//toggle2
function toggle2(id) {
	if ("hide"!=$("#"+id).attr("class")){
		$("#"+id).fadeOut(1000);
		$("#"+id).toggleClass("hide");
	}
	else {
		$("#"+id).fadeIn(1000);
		$("#"+id).toggleClass("hide");
	}
}
//toggle3
function toggle3(id) {
	if ("hide"!=$("#"+id).attr("class")){
		$("#"+id).slideUp(800);
		$("#"+id).toggleClass("hide");
	}
	else {
		$("#"+id).slideDown(800);
		$("#"+id).toggleClass("hide");
	}
}
//ajax
function ajax_load(url,id){
	if ("hide"!=$("#"+id).attr("class")){
		$("#"+id).slideUp(800);
		$("#"+id).toggleClass("hide");
	}
	else{
		$.post(url, {}, function(text){
			$("#"+id).html(text);
			toggle3(id);
		});
	}
}
function ajax_load2(url,id){
	$.post(url, {}, function(text){
		if ("hide"!=$("#"+id).attr("class")){
			$("#"+id).slideUp(800,function(){
				$("#"+id).html(text);
				$("#"+id).toggleClass("hide");
			});
		}
	});
}
//comment window
function create_comment(id) {
	if (id=='0') {
		$("#comment_create").insertAfter("#add_comment");
	}
	else {
		$("#comment_create").insertAfter("#comment_"+id);
	}
	$("#comment_parent").attr("value",''+id);
}
//highlight href
function URLDecode(encoded) {
	// Replace + with ' '
	// Replace %xx with equivalent character
	// Put [ERROR] in output if %xx is invalid.
	var HEXCHARS = "0123456789ABCDEFabcdef";
	var plaintext = "";
	var i = 0;
	while (i < encoded.length) {
	var ch = encoded.charAt(i);
		if (ch == "+") {
			plaintext += " ";
			i++;
		} else if (ch == "%") {
			if (i < (encoded.length-2)
			&& HEXCHARS.indexOf(encoded.charAt(i+1)) != -1
			&& HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ) {
			plaintext += unescape( encoded.substr(i,3) );
			i += 3;
			} else {
			alert( 'Bad escape combination near ...' + encoded.substr(i) );
			plaintext += "%[ERROR]";
			i++;
			}
		} else {
		plaintext += ch;
		i++;
		}
	} // while
	return plaintext;
};
function href_find() {
	var url=document.URL;
	url=url.substring(7,url.length);
	url=url.substring(url.indexOf('/'),url.length);
	$("a[href="+url+"]").addClass("bold");
	$("a.nobold[href="+url+"]").removeClass("bold");
	$("a[href="+url+"]").attr("href","#");
	$("a[href="+URLDecode(url)+"]").addClass("bold");
	$("a.nobold[href="+URLDecode(url)+"]").removeClass("bold");
	$("a[href="+URLDecode(url)+"]").attr("href","#");
	var url2=url.substring(1,url.length);
	url2='/'+url2.substring(0,url2.indexOf('/'))+'/';
	//$(".menu a[href="+url2+"]").addClass("bold");
	url=url.substring(1,url.length-1);
	url=url.substring(url.lastIndexOf('/'),url.length);
	$("a[href="+url+"]").addClass("bold");
	$("a.nobold[href="+url+"]").removeClass("bold");
	$("a[href="+url+"]").attr("href","#");
}
function addwall(id){
	toggle3("wall"+id);
}
function loadwall(id,page){
	$.ajax({
	type: "POST",
	url: "/ajax/loadwall/"+id+"/"+page,
	data: "",
	success: function(msg){
		$("#wall").html(msg);
	}
	});
}
function loadwall2(id,page){
	$.ajax({
	type: "POST",
	url: "/ajax/loadwall2/"+id+"/"+page,
	data: "",
	success: function(msg){
		$("#wall").html(msg);
	}
	});
}
//default actions
//chat actions
var readtime = 0;
function chat_loadusers(id){
	$.ajax({
	type: "POST",
	url: "/ajax/chat/"+id+"/users",
	data: "",
	success: function(msg){
		$(".player_list").html(msg);
	}
	});
	setTimeout('chat_loadusers('+id+');', 8000);
}
function chat_lastreadtime(){
	var max = 0;
	var buf;
	$(".chatmsg").each(function(i){
		buf = $(this).attr("rel");
		if (buf>max){
			max=buf;
		}
		});
	readtime = max;
}
function chat_height(){
var h = 0;
$('.chatroom div').each(function(i){h=h+$(this).height();});
return h;
}
function chat_read(id){	
	chat_lastreadtime();	
	$.ajax({
	type: "POST",
	url: "/ajax/chat/"+id+"/read",
	data: { 'readtime':readtime, 'small_chat':small_chat },
	error: function (){
		setTimeout("chat_read('"+id+"');", 2000);
	},	
	success: function(msg){
		if(msg.length>1){
			$(".chatroom").append(msg);
			if($("#autoscroll").attr("checked")>0 || small_chat=='Y')
				$(".chatroom").scrollTop(chat_height());
		}
		setTimeout("chat_read('"+id+"');", 2000);
	}
	});	
}
function chat_reply(text){
	var testv=$("#chatmsg").attr('value');
	$("#chatmsg").attr('value','2'+text+': '+testv);
	$("#chatmsg").focus();
}
function chat_query_post(id){
	arr_post_messages.push($("#chatmsg").val());
	$("#chatmsg").attr('value','');	
	$("#chatmsg").focus();	
	if (now_post!=1){
		chat_post(id);
	}
	return false;	
}
function chat_post(id){	
	now_post=1;
	$.ajax({
	type: "POST",
	url: "/ajax/chat/"+id+"/post",
	data: { 'msg': arr_post_messages[0] },
	error: function (){
		now_post=0;
		if (arr_post_messages.length>0){
			chat_post(id);
		}
	},
	success: function(msg){				
		arr_post_messages.splice(0,1);
		now_post=0;
		if (arr_post_messages.length>0){
			chat_post(id);
		}				
	}
	});	
}
function chat_changetopic2(id){
	var chattopic;
	chattopic=$("#chattopic .topic").attr('value');
	$.ajax({
	type: "POST",
	url: "/ajax/chat/"+id+"/topic",
	data: { 'topic':chattopic },
	success: function(msg){
		$("#chattopic").html(""+chattopic);
	}
	});
	$("#chatmsg").focus();
	return false;
}
function htmlspecialchars(html) {
	html = html.replace(/&/g, "&amp;");
	html = html.replace(/</g, "&lt;");
	html = html.replace(/>/g, "&gt;");
	html = html.replace(/"/g, "&quot;");//"
	return html;
}
function chat_changetopic(id){
	var chattopic;
	chattopic=$("#chattopic").html();
	if("topic"!=$("#chattopic .topic").attr('class')){
	$("#chattopic").html("<input type=\"text\" class=\"topic\" value=\""+htmlspecialchars(chattopic)+"\"> <input type=\"button\" value=\"Изменить тему канала\" onclick=\"chat_changetopic2('"+id+"');\">");
	$("#chattopic .topic").focus();
	}
	return false;
}
function free_input(id){
	$(id).focus(function(){
		if(this.value=="Без названия") {
			this.value="";
		}
	});
	$(id).blur(function(){
		if(this.value=="") {
			this.value="Без названия";
		}
	});
}
$(window).load( function() {
	href_find();
	free_input(".free_input");
} );
//// обновление сообщений и уведомлений
function refresh_messages(){
	$("#messages_contener").load("/ajax/?get_messages=Y");
}
function refresh_notices(){
	$("#notices_contener").load("/ajax/?get_notices=Y");
}		

