
function post_comment()
{
	var request_data =
	{
		'pid': $('#comments_item_id').attr('value'), 'comments_ajax':'1',
		'post_comment': '1', 'comments_offset': 'last',
		'comment_author': $('#comment_author').attr('value'),
		'comment_email': $('#comment_email').attr('value'),
		'comment_text': $('#comment_text').attr('value')
	};
	if (jQuery.trim(request_data.comment_text) == '')
	{
		alert('Введите текст сообщения!');
		$('#comment_text').get(0).select();
		return;
	};
	$.ajax(
	{
		type: "POST", url: "/", data: request_data,
		success: function(response) { $('DIV#comments').empty().html(response); }
	});
};

function delete_comment(comm_id)
{
	var request_data = {'pid': $('#comments_item_id').attr('value'), 'delete_comment':comm_id, 'comments_ajax':'1'};
	$.ajax(
	{
		type: "GET", url: "/", data: request_data,
		success: function(response) { $('DIV#comments').empty().html(response); }
	});
};

function fetch_comments(offset)
{
	var request_data = {'pid': $('#comments_item_id').attr('value'), 'comments_offset':offset, 'comments_ajax':'1'};
	$.ajax(
	{
		type: "GET", url: "/", data: request_data,
		success: function(response){ $('DIV#comments').empty().html(response) }
	});
};

//==================================================================================================

var sport_region =
{
	//--------------------------------------------------------------------------
	TOP_STORIES: function()
	{
		if ($('#top_stories_data').length == 0) return false;

		var current_story = 0;
		var $main_area = $('#top_stories');
		var $nav_cells = $('#top_stories_nav TR TD');
		var maxw = $main_area.width();
		var items = new Array;
		var preload = new Array;
		var interval;
		put_top_story = function(i)
		{
			if (i >= items.length) return false;
			$main_area.find('P.title A').html(items[i].title);
			$main_area.find('P.title A').attr('href', items[i].href);
			$main_area.find('A.image_link').attr('href', items[i].href);
			$main_area.find('P.summary').html(items[i].summary);
			$main_area.find('IMG').attr('src', items[i].img);
			$main_area.find('IMG').fadeIn('fast');
			$nav_cells.removeClass('current');
			$nav_cells.eq(i).addClass('current');
		};

		$('#top_stories_data DIV').each(function(i)
		{
			var img_src = $(this).find('.img').html();
			img_src = '/?resized_thumbnail='+img_src+'&width='+maxw+'&height=10000';
			preload.push(new Image);
			preload[preload.length-1].src = img_src;
			items.push(
			{
				href: '/?pid='+$(this).find('.id').html(),
				title: $(this).find('.title').html(),
				summary: $(this).find('.summary').html(),
				img: img_src
			});
			$nav_cells.eq(i).click(function()
			{
				put_top_story(i);
				current_story = 0;
				clearInterval(interval);
			});
		});
		put_top_story(0);
		interval = setInterval(function()
		{
			current_story++;
			if (current_story == items.length) current_story = 0;
			$main_area.find('IMG').fadeOut('fast', function(){put_top_story(current_story)});
		},5*1000);
	}
};

//==================================================================================================
$(document).ready(function()
{
	
	// View images
	$('IMG.resized_thumbnail').each(function()
	{
		var code = '<a href="_sport_region_images/'+this.src.match(/[a-z0-9]+\.(jpg|jpeg|gif|png)/i)[0]+'"'+
			' class="img_link" target="_blank" title="Нажмите, чтобы просмотреть в полном размере"></a>';
		$(this).wrap(code);
	});
	
	// Init everything other
	sport_region.TOP_STORIES();
	
});

