var site_root = 'http://maskas.ru/';
var dialog_shown;
var recreate_id;

var min_pos;
var old_pos;
var new_pos;

var mp_id = -1;
var content = '';
var mpcont;

$(document).ready(function()
{	
	if( window.tree_component )
	{
		var tree = new tree_component();
		tree.init( $('.tree') );
	}
});

function show_dialog(name,callback)
{
	if( dialog_shown == name )
	{
		hide_dialog();
		return;
	}
	
	hide_dialog();		
	if( $("#dialog_holder").length == 0 )
	{
		var div1 = document.createElement('div');
		document.body.appendChild(div1);
		var outer = $(div1);
		outer.attr('id',name);
		outer.addClass('outer_dialog');
		
		var div2 = document.createElement('div');
		var container = $(div2);
		div1.appendChild(div2);
		container.addClass('container_dialog');
		
		var div3 = document.createElement('div');
		var inner = $(div3);
		div2.appendChild(div3);
		inner.addClass('inner_dialog')
		
		inner.load(site_root+'php/'+name+'.php',callback);
		dialog_shown = name;
	}
}

function toggle_filter()
{
	if( $('.filter').css('height')=='0px' )
	{
		$('.filter').css('height','auto');
		$('.showfilter span').html('&#9650;');
		$.cookie("show_filter", "on", {expires: 100, path: '/'}); 
	}
	else
	{
		$('.filter').css('height','0px');
		$('.showfilter span').html('&#9660;');
		$.cookie("show_filter", "off", {expires: 100, path: '/'}); 
	}
}

function hide_dialog()
{
	$('#'+dialog_shown).remove();
	dialog_shown = '';
}

function show_ajaxload()
{
  // Эээээээ.....
}

function hide_ajaxload()
{
    $("#ajaxload").hide();
}

function logout_callback()
{
	window.location.reload();
}

function logout()
{
	$.post(site_root + 'op/user.php',{'op':'logout'},logout_callback);
}

function show_error(err)
{
	alert(err);
}

function reload_callback(res_text)
{
	var res = eval('(' +res_text  + ')' );
	
	if( !res.ok )
	{
		show_error(res.error);
		return;
	}
	
	document.location.reload();
}


function select_maska(id)
{
	if( id == $.cookie('maska') ) return alert("Эта маска и так на Вас.");
	
	var old = $.cookie('maska');
	$.cookie('maska', id, {expires: 100, path: '/'});
	$.cookie('maska_pic', 0, {expires: 100, path: '/'});
	
	recreate_id = -1;
	if( $("#add_comment").css('display') != 'none' && $("#add_comment").length )
	{
		recreate_id = 0;
		if( $("#add_comment").parent().prevAll().attr('id')!=undefined )
			recreate_id = $("#add_comment").parent().prevAll().attr('id').substring(1);
		content = $("#comment_text").val();
		toggle_addcomment(-1);
	}

	h = $("#private").height();
	w = $("#private").width();
	$("#private").html("<div class='loading' style='height: "+h+"px; width: "+w+"px;'></div>").load(site_root+"php/private.php");
	
	if( $("#add_comment").length==1 )
		$("#add_comment").load(site_root+"php/add_comment_form.php",select_maska_callback);
}

function select_maska_callback(res_text)
{	
	if( recreate_id!=-1 )
	{
		toggle_addcomment(recreate_id);
		$("#comment_text").val(content);
	}
}

function unselect_maska(id)
{
	$.cookie('maska', 0, {expires: 100, path: '/'});
	$.cookie('maska_pic', 0, {expires: 100, path: '/'}); 
	
	h = $("#private").height();
	$("#private").html("<div class='loading' style='height: "+h+"px;'></div>").load(site_root+"php/private.php");
}

function vote_post(post_id,vote)
{
	$(".sign").hide();
  $.post(site_root + 'op/post.php',{'op':'vote_post','post_id':post_id,'vote':vote},vote_post_callback);
}

function vote_post_callback(res_text)
{
  var res = eval('(' +res_text  + ')' );
	
	if( !res.ok )
	{
			show_error(res.error);
			return;
	}
	
	$("#vote_post_" + res.post_id).html(res.content);
	$(".sign").show();
}

function vote_comment(comment_id,vote)
{
	$(".sign").hide();
  $.post(site_root + 'op/post.php',{'op':'vote_comment','comment_id':comment_id,'vote':vote},vote_comment_callback);
}

function vote_comment_callback(res_text)
{

    var res = eval('(' +res_text  + ')' );
    
    if( !res.ok )
    {
        show_error(res.error);
        return;
    }
    
    if( res.reload )
    {
        document.location.reload();
        return;
    }
    
  $("#vote_comment_" + res.comment_id).html(res.content);
	
	if( res.mana != null )
	{
		$(".sign").show();
		$(".mana").html(res.mana);
	}
	if( res.goodness != null )
		$(".goodness").html(res.goodness);
}

function submitenter(e,funct)
{
var keycode;
if (window.event) keycode = window.event.keyCode;
else if (e) keycode = e.which;
else return true;

if (keycode == 13)
   {
   funct();
   return false;
   }
else
   return true;
}

function expand_post_link(elem)
{
	$(elem).html($(elem).attr('href'));	
	$(elem).attr('onClick','');
	return false;
}

function change_posts()
{
	var opt = '';
	if(  $("#posts_opt").val() )
		opt = '/' + $("#posts_opt").val(); 
	document.location = site_root + $("#posts_topic").val() + '/' + mode +	 opt;
}

function delete_pic(id)
{
    $.post(site_root + 'op/maska.php',{'op':'delete_pic','pic_id':id},reload_callback)
}

function hide_message()
{
	document.location = document.location + '?h_m=1';
}

function toggle_movepost(id)
{
	var mp = $("#move_post");
	if( mp_id != id )
	{
		if(mp_id!=-1) toggle_movepost(mp_id);
	
		mp_id = id;
		
		if(mp.length==0) return;
		
		mpcont = $("#p"+id+" .actions").html();
		$("#p"+id+" .actions").html('').append(mp);
		mp.find("select").val( $("#p"+id+" .partition").attr('id') );
		mp.show();
	}
	else
	{
		mp.hide().appendTo('body');
		$("#p"+id+" .actions").html(mpcont);
		
		mp_id = -1;
	}
}

function toggle(id)
{
    $("#"+id).toggle();
}

function move_post()
{
	$("#move_post select").attr('disabled','disabled');
	$.post(site_root + "op/post.php",{'op':'move','post_id':mp_id,'topic_id':$("#move_post select").val()},move_post_callback);    
}

function move_post_callback(res_txt)
{
	var res = eval( '(' + res_txt +')' );

	if( !res.ok )
	{
		show_error(res.error);
		return;
	}
	
	$("#move_post select").attr('disabled','');
	$("#p"+mp_id+" .partition").html($("#move_post option:selected").text()).attr('id',$("#move_post select").val());

	toggle_movepost();
}

function toogle_panel()
{
	if( $('.fixed').length==1 )
	{
		$('.right').removeClass('fixed');
		$.cookie("fixed", "0", {expires: 100, path: '/'}); 
	}
	else
	{
		$('.right').addClass('fixed');
		$.cookie("fixed", "1", {expires: 100, path: '/'}); 
	}
}

function html_entity_decode(str) {
  var ta=document.createElement("textarea");
  ta.innerHTML=str.replace(/</g,"&lt;").replace(/>/g,"&gt;");
  return ta.value;
}