var changingTableInProgress = false;

function nextPage()
{
	if(!changingTableInProgress)
	{
		if(curPage < maxPage)
		{
			curPage++;
			changingTableInProgress = true;
			hideTable()
		}
	}
}

function prevPage()
{
	if(!changingTableInProgress)
	{
		if(curPage > 1)
		{
			curPage--;
			changingTableInProgress = true;
			hideTable()
		}
	}
}

function hideTable()
{
	$("#mainWindow .hlavniTabulka").ready(function()
	{
		changingTableInProgress = true;
		$("#mainWindow .hlavniTabulka").fadeOut(300, tableHided);
	})
}

function tableHided()
{
	$("#mainWindow .hlavniTabulka").remove();
	getNewPage()
}

function getNewPage()
{
	$.get('http://www.'+DOMENA+'/data/casti/TableStrankyContent.php?str='+curPage, function(data) 
	{
		changeStrankovani()
		
		if(curPage >= maxPage)
		{
			inactiveRightArrow()
		}else
		{
			activeRightArrow()
		}
		
		if(curPage > 1)
		{
			activeLeftArrow()
		}else
		{
			inactiveLeftArrow()
		}
		
	  $('#mainWindow .tableData').html(data);
	  $("#mainWindow .hlavniTabulka").css("display","none");
	  
	  	$("#mainWindow .hlavniTabulka").ready(function()
		{
			$("#mainWindow .hlavniTabulka").fadeIn(300,function()
	  		{
		  		changingTableInProgress = false;
	  		});
		})
	  
	});
}

function inactiveRightArrow()
{
	$('#sipkaNext').html('<img src="'+ROOTRELURL+'/data/img/sipkaPravaInactiv.png" alt="" />');
}

function inactiveLeftArrow()
{
	$('#sipkaPrev').html('<img src="'+ROOTRELURL+'/data/img/sipkaLevaInactiv.png" alt="" />');
}

function activeRightArrow()
{
	$('#sipkaNext').html('<a onclick="nextPage()" class="pointer"><img src="'+ROOTRELURL+'/data/img/sipkaPravaActiv.png" alt="" /></a>');
}

function activeLeftArrow()
{
	$('#sipkaPrev').html('<a onclick="prevPage()" class="pointer"><img src="'+ROOTRELURL+'/data/img/sipkaLevaActiv.png" alt="" /></a>');
}

function changeStrankovani()
{
	//alert('changeStrankovani ' + maxPage)
	for(i=1; i<=maxPage; i++)
	{
		$('#strankovani .p'+i).removeClass("strDotActive strDot").addClass("strDot");
	}
	
	$('#strankovani .p'+curPage).removeClass("strDotActive strDot").addClass("strDotActive")
}

function changePage(page)
{
	if(!changingTableInProgress)
	{
		curPage = page;
		changingTableInProgress = true;
		hideTable()
	}
}






jQuery.fn.centerMainCont = function () 
{
	var minHeight = this.outerHeight() + $('#mainHeader').outerHeight();
	var minWidth = this.outerWidth();
	
	this.css("position","absolute");
	$('#vosyZapati').css("position","absolute");
	
	if($(window).height() > minHeight)
	{		
		this.css("top", (($(window).height() - this.outerHeight()) / 2) + $(window).scrollTop() + "px");
		$('#vosyZapati').css("top", (($(window).height() - $('#vosyZapati').outerHeight()) / 2) + $(window).scrollTop() + "px");		
	}
	
	if($(window).width() > minWidth)
	{
		this.css("left", (($(window).width() - this.outerWidth()) / 2) + $(window).scrollLeft() + "px");
		$('#vosyZapati').css("left", (($(window).width() - $('#vosyZapati').outerWidth()) / 2) + $(window).scrollLeft() + "px");
	}
	
	
	
    return this;
}

jQuery.fn.centerMainHead = function () 
{
	var minWidth =  $('#mainWindow').outerWidth();
	
	this.css("position","absolute");
	
	if($(window).width() > minWidth)
	{
		this.css("left", (($(window).width() - this.outerWidth()) / 2) + $(window).scrollLeft() + "px");
	}
	
    return this;
}

$(window).resize(function() 
{
  $('#mainWindow').centerMainCont();
  $('#mainHeader').centerMainHead();
});

$(document).ready(function()
{
	$('#sipkaPrev a').removeAttr("href")
	$('#sipkaNext a').removeAttr("href")

	$('#mainWindow').centerMainCont();
  	$('#mainHeader').centerMainHead();
	
	for(i=1; i<=maxPage; i++)
	{
		$('#strankovani .p'+i).removeAttr("href");
	}
});

