/*

	UkrainianBlogs.com
	AJAX functions
	
	Authors: UkrainianBlogs.com team
	Copyright: 2009, UkrainianBlogs.com. All rights resevered

-----------------------------------------------------------------------*/

/*
 *  Reloads blogs using AJAX
 */
function ReloadBlogs(id, param, element)
{
	//loads new blogs with AJAX
	var url = '/blogs/ajax_blogs_rotation.html';
	var href = $(element).attr('href');
	if(href!=undefined && href!='')
	{
		if(href.indexOf(window.location)>-1)
		{
			   href = href.replace(window.location, '');
		}

		var reg = /page(\d+)\/(.+)\/(.+)/;
		var parts = href.match(reg);
		if( parts )
		{			parts[1] = parts[1].replace('.html', '');			parts[2] = parts[2].replace('.html', '');			parts[3] = parts[3].replace('.html', '');
			href = '?page=' + escape(parts[1]) + '&name=' + escape(parts[2]) + '&order=' + escape(parts[3]);
		}
		else
		{
			reg = /page(\d+)/;
			parts = href.match(reg);
			href = '?page=' + escape(parts[1]);
		}

		url += href;
		url += '&'+param+'=' + id;

		$.get(url, {}, function(xml)
		{
			//replacec /./ with current url
			$('#pagenator').html(ReplaceCurrDirWithLocation($("pagination", xml).text()));
			$('#blogs_list').html(ReplaceCurrDirWithLocation($("blog", xml).text()));
			SetPagination();
				SetSortings();
		});
	}
}


/*
 *  Sets one of the reload types
 */
function ReloadAll(element)
{	if(typeof(query) != "undefined")
	{
	   ReloadBlogs(query, 'query', element);
	}
	if(typeof(category_id) != "undefined")
	{
	   ReloadBlogs(category_id, 'cat_id', element);
	}
	if(typeof(city_id) != "undefined")
	{
	   ReloadBlogs(city_id, 'city_id', element)
	}}

/*
 *  Sets sorting click handlers
 */
function SetSortings()
{	$('#tableheader th a').each(function(index) {			$(this).click(function()
			{				ReloadAll(this);				return false;
			});
	});}

/*
 *  Sets pagination click handlers
 */
function SetPagination()
{	$('.pagination li a').each(function(index) {
		$(this).click(function()
			{				ReloadAll(this);
				return false;
			});
	});
}

/*
 *  Sets all after document load
 */
$(document).ready(function()
{	SetPagination();
	SetSortings();
});
