var owner_id;
var reload_action;

function addAddWallContent(action, type, owner_id)
{
	var content = null;
	if (type == 'text')
		content = $('.write-area textarea').val();
	if (type == 'url-video')
		content = $('.url-video-input').val();

	if (trim(content) != '')
	{
		var data = {
			'wall[type]': type,
			'wall[content]': content,
			'wall[owner_id]': owner_id
		};

		$('.comment-list').load(action, data, function(result){
			$('.clear-val').val('');
			$('.white-bar').slideUp('slow');
			if ($.browser.msie)
			{
				$(document).pngFix();
				$('.user-rating').corner({tl: { radius: 4 }, tr: { radius: 4 }, bl: { radius: 4 }, br: { radius: 4 }, antiAlias: true });
			}
		});
	}
}

function getNextPage(url, page)
{
	if (page > 0)
		parent.location = window.location+'?page='+page;
	else
		parent.location = window.location;
}

var cur_page = null;
function openDeleteWallContentPopup(id, owner_id, page)
{
	$('.delete-button').attr('item_id', id);
	$('.delete-button').attr('owner_id', owner_id);
	
	$("#dialog").dialog("destroy");

	$("#dialog-delete-wall-item").dialog({
		height: 150,
		width: 500,
		modal: true
	}).parent().find(".ui-dialog-titlebar-close").remove();

	if (jQuery.browser.msie)
		$('.ui-corner-all').corner();

	cur_page = page;
	/*$('.delete-button').attr('item_id', id);
	$('.delete-button').attr('owner_id', owner_id);
	openSmallPopup('delete-popup-content');*/
}

function deleteWallContent(_button, action)
{
	var id = $(_button).attr('item_id');
	var data = {
		'wall[id]': id,
		'wall[owner_id]': $(_button).attr('owner_id'),
		'page': cur_page
	};

	$('.comment-list').load(action, data, function(){
		//$('.comment[rel='+id+']').remove();
		//closeSmallPopup('delete-popup-content');
		$('#dialog-delete-wall-item').dialog('destroy');
		if ($.browser.msie)
		{
			$(document).pngFix();
			$('.user-rating').corner({tl: { radius: 4 }, tr: { radius: 4 }, bl: { radius: 4 }, br: { radius: 4 }, antiAlias: true });
		}
	});
}

function closeGraffityWindow()
{
	$('.add-graffiti').click();
}

function SendGraffiti()
{
	var data = {
		'wall[owner_id]': owner_id
	};
	$('.comment-list').load(reload_action, data, function(){
		closeGraffityWindow();
	});
}

function reloadWallContent()
{
	var data = {
		'wall[owner_id]': owner_id
	};
	$('.comment-list').load(reload_action, data, function(){
		$('.ajax-loader').hide();
		$('.clear-val').val('');
		$('.white-bar').slideUp('slow');
		$('.action-button').removeClass('unclickable');
		$('.file').show();
		if ($.browser.msie)
		{
			$(document).pngFix();
			$('.user-rating').corner({tl: { radius: 4 }, tr: { radius: 4 }, bl: { radius: 4 }, br: { radius: 4 }, antiAlias: true });
		}
	});
}

function initActions()
{
	$('.write-action').click(function(){
		if (!$(this).hasClass('unclickable'))
		{
			$('.clear-val').val('');
			$('.white-bar[rel!=write-action]').hide();
			$('.white-bar[rel=write-action]').slideToggle('slow', function(){
				if ($.browser.msie)
					$('.rounded').corner();
			});
		}
		return false;
	});
	$('.add-action').click(function(){
		if (!$(this).hasClass('unclickable'))
		{
			$('.white-bar[rel!=add-action]').hide();
			$('.white-bar[rel=add-action]').slideToggle('slow', function(){
				if ($.browser.msie)
					$('.rounded').corner();
			});
		}
		return false;
	});
	$('.add-graffiti').click(function(){
		if (!$(this).hasClass('unclickable'))
		{
			$('.white-bar[rel!=add-graffiti]').hide();
			$('.white-bar[rel=add-graffiti]').slideToggle('slow', function(){
				if ($.browser.msie)
					$('.rounded').corner();
			});
		}
		return false;
	});
	$('.add-url-video').click(function(){
		if (!$(this).hasClass('unclickable'))
		{
			$('.clear-val').val('');
			$('.white-bar[rel!=add-url-video]').hide();
			$('.white-bar[rel=add-url-video]').slideToggle('slow', function(){
				if ($.browser.msie)
					$('.rounded').corner();
			});
		}
		return false;
	});
	$('.add-img').click(function(){
		if (!$(this).hasClass('unclickable'))
		{
			$('.clear-val').val('');
			$('.white-bar[rel!=add-img]').hide();
			$('.white-bar[rel=add-img]').slideToggle('slow', function(){
				if ($.browser.msie)
					$('.rounded').corner();
			});
		}
		return false;
	});
	$('.add-video').click(function(){
		if (!$(this).hasClass('unclickable'))
		{
			$('.clear-val').val('');
			$('.white-bar[rel!=add-video]').hide();
			$('.white-bar[rel=add-video]').slideToggle('slow', function(){
				if ($.browser.msie)
					$('.rounded').corner();
			});
		}
		return false;
	});
	$('.add-audio').click(function(){
		if (!$(this).hasClass('unclickable'))
		{
			$('.clear-val').val('');
			$('.white-bar[rel!=add-audio]').hide();
			$('.white-bar[rel=add-audio]').slideToggle('slow', function(){
				if ($.browser.msie)
					$('.rounded').corner();
			});
		}
		return false;
	});
}

function modDeleteWallContent(action, id)
{
	if (confirm('Удалить контент?'))
	{
		$.post(action, {'id': id}, function(){
			$('#content_tr'+id).remove();
		});
	}
}

function openImgPopup(_this, title)
{
	$("#dialog-img-wall-item .img").empty().append('<img src="'+$(_this).attr('rel')+'"/>');
	$("#dialog").dialog("destroy");

	$("#dialog-img-wall-item").dialog({
		height: 450,
		width: 530,
		modal: true
	}).parent().find(".ui-dialog-titlebar-close").remove();

	if (jQuery.browser.msie)
		$('.ui-corner-all').corner();
}

function uploadImg(_this)
{
	$(_this).parents('.input-file-mask').find('input[type=text]').val($(_this).val());
	$(_this).parents('form').find('.ajax-loader').show();
	$('.action-button').addClass('unclickable');
	$('.file').hide();
	$(_this).parents('form').submit();
}

function uploadVideo(_this)
{
	$(_this).parents('.input-file-mask').find('input[type=text]').val($(_this).val());
	$(_this).parents('form').find('.ajax-loader').show();
	$('.action-button').addClass('unclickable');
	$('.file').hide();
	$(_this).parents('form').submit();
}

function uploadAudio(_this)
{
	$(_this).parents('.input-file-mask').find('input[type=text]').val($(_this).val());
	$(_this).parents('form').find('.ajax-loader').show();
	$('.action-button').addClass('unclickable');
	$('.file').hide();
	$(_this).parents('form').submit();
}


function UploadError(data)
{
	$("#dialog-error-wall-item .text").empty().html(data);
	$("#dialog").dialog("destroy");

	$("#dialog-error-wall-item").dialog({
		height: 150,
		width: 500,
		modal: true
	}).parent().find(".ui-dialog-titlebar-close").remove();

	if (jQuery.browser.msie)
		$('.ui-corner-all').corner();
	//alert('error');
	$('.ajax-loader').hide();
	$('.clear-val').val('');
	$('.white-bar').slideUp('slow');
	$('.action-button').removeClass('unclickable');
	$('.file').show();
}
