$(document).ready(function(){

$("a.delete-category").click(function(event){
	var current = $(this);
	if(confirm(current.attr('confirm')))
		{
		$("li[category-id='"+current.attr('category-id')+"']").remove();
		$.get(this.href);
		}
	return false;
	});

$("a.edit-category").click(function(event){
	var offset = $(this).offset();
	
	ShowDivShadowForForum(offset, "#edit-category-form");
	
	// устанавливаем данные в форме
	$("#div_enlarged input[name=category]").val($("*[category-title='"+$(this).attr('category-id')+"']").html());
	$("#div_enlarged textarea[name=descr]").val($("*[category-descr='"+$(this).attr('category-id')+"']").html());
	$("#div_enlarged input[name=id]").val($(this).attr('category-id'));
	
	return false;
	});

$(".cancel-popup").live('click', function(){
	CloseEnlarged();
	CancelPopupForm();
	});

$("a.enable-disable img").bind('mouseenter mouseleave click', function(e){
	//alert(this.tagName);
	var src = this.src;
	this.src = $(this).attr('toggle');
	$(this).attr('toggle', src);
	}).click(function(){
	var href = this.parentNode.href;
	$.get(href);
	$(this.parentNode).attr('href', $(this.parentNode).attr('toggle')).attr('toggle', href);
	return false;
	});

$("#fade").live('click', CancelPopupForm);

$("a.delete-topic").click(function(event){
	var current = $(this);
	if(confirm(current.attr('confirm')))
		{
		$("li[topic-id='"+current.attr('topic-id')+"']").remove();
		$.get(this.href);
		}
	return false;
	});

$("a.delete-message").click(function(event){
	var current = $(this);
	if(confirm(current.attr('confirm')))
		{
		$("li[message-id='"+current.attr('message-id')+"']").remove();
		$.get(this.href);
		}
	return false;
	});

$("#sortable-categories").sortable({
	stop: function(ev, ui) {
		$("#submit-sort:hidden").show(500);
		$("li input.listsort", ui.sender).each(function(i){
			this.value=i+1;
			});
		}
	});


$("a.edit-topic").click(function(event){
	var offset = $(this).offset();
	
	ShowDivShadowForForum(offset, "#edit-topic-form");
	
	// устанавливаем данные в форме
	$("#div_enlarged input[name=topic]").val($("*[topic-title='"+$(this).attr('topic-id')+"']").html());
	$("#div_enlarged textarea[name=message]").val($("*[topic-message='"+$(this).attr('topic-id')+"']").html());
	$("#div_enlarged input[name=id]").val($(this).attr('topic-id'));
	
	return false;
	});

$("a.edit-message").click(function(event){
	var offset = $(this).offset();
	
	ShowDivShadowForForum(offset, "#edit-message-form");
	
	// устанавливаем данные в форме
	$("#div_enlarged textarea[name=message]").val($("span[message-id='"+$(this).attr('message-id')+"']").html());
	$("#div_enlarged input[name=id]").val($(this).attr('message-id'));
	
	return false;
	});

});

function ShowDivShadowForForum(offset, elementToCopy)
	{
	$("#div_enlarged").html($(elementToCopy).html()).addClass('popup-padding');
	$("#div_shadow").css({width:'400px', visibility: 'hidden'}).show();
	
	// подгоняем коодрдинату слоя
	var x = offset.left + $("#div_shadow").width() + 16 > $(document).width() ? $(document).width() - $("#div_shadow").width() - 13 : offset.left + 16;
	var y = offset.top + $("#div_shadow").height() + 16 > document.body.clientHeight ? document.body.clientHeight - $("#div_shadow").height() - 14 : offset.top + 16;
	$("#div_shadow").css({left:x, top:y, visibility:'visible'});
	
	// прячем контролы для картинок
	$("#div_shadow .pup-controls").hide();
	
	// добавляем белый фон
	$("#fade").show();
	}

function CancelPopupForm()
	{
	$("#div_shadow").css({width:'auto', visibility: 'hidden'});
	$("#div_enlarged").removeClass('popup-padding');
	$("#div_shadow .pup-controls").show();
	}
