$(document).ready(function() {
	if($.cookie("selected-tab")==null)
		$.cookie("selected-tab",1,{expires: 1});
	 
		$("#tabs").css("background-position","0 "+(-$.cookie("selected-tab")*33-0+33)+"px");
		$.ajax({  
			type: "post",
			url: "php/getNews.php",
			data: "newstype="+$.cookie("selected-tab"), 
			dataType: "json",
			cache: true, 
			beforeSend: function() {
				$("#news-holder").css("width",626+'px'); 
				$("#news-holder").html('<div class="ajax-loader news-loader"></div>');
			},
			success: function(data) {
				var dane = data;
				$.get("_js/jst/tabs.jst", function(data){
					tpl = data; 
					$("#news-holder").html(tpl.process(dane));
					$("#news-holder").css("width",(dane.count*313)+'px'); 
					$('#slider').slider('option', 'value',0);					
					$("#tabs-box").attr("scrollLeft", 0);
					if(dane.count<3)
					{
						$("#arrow-left").css("display","none");
						$("#slider").css("display","none");
						$("#arrow-right").css("display","none");
					}
					else
					{
						$("#arrow-left").css("display","");
						$("#slider").css("display","");
						$("#arrow-right").css("display","");
					}
					
				});
					
			},
			error: function(XMLHttpRequest, textStatus, errorThrown) {
				$("#news-holder").css("width",626+'px'); 
				$("#news-holder").html('<div class="Nieudane połączenie z bazą - spróbuj odświeżyć stronę"></div>');
			}
		}); 
		
	
	$("#tab-1").click(function() {
		$("#tabs").css("background-position","0 0");
		$.ajax({  
			type: "post",
			url: "php/getNews.php",
			data: "newstype=1", 
			dataType: "json",
			cache: true, 
			beforeSend: function() {
				$("#news-holder").css("width",626+'px'); 
				$("#news-holder").html('<div class="ajax-loader news-loader"></div>');
			},
			success: function(data) {
				var dane = data;
				$.get("_js/jst/tabs.jst", function(data){
					tpl = data; 
					$("#news-holder").html(tpl.process(dane));
					$("#news-holder").css("width",(dane.count*313)+'px'); 
					$('#slider').slider('option', 'value',0);					
					$("#tabs-box").attr("scrollLeft", 0);
					$.cookie("selected-tab",1,{expires: 1});
					if(dane.count<3)
					{
						$("#arrow-left").css("display","none");
						$("#slider").css("display","none");
						$("#arrow-right").css("display","none");
					}
					else
					{
						$("#arrow-left").css("display","");
						$("#slider").css("display","");
						$("#arrow-right").css("display","");
					}
				});
					
			},
			error: function() {
				$("#news-holder").css("width",626+'px'); 
				$("#news-holder").html('<div class="Nieudane połączenie z bazą - spróbuj odświeżyć stronę"></div>');
			}
		}); 
		return false;
	});

	$("#tab-2").click(function() {
		$("#tabs").css("background-position","0 -33px");
		$.ajax({  
			type: "post",
			url: "php/getNews.php",
			data: "newstype=2", 
			dataType: "json",
			cache: true, 
			beforeSend: function() {
				$("#news-holder").css("width",626+'px'); 
				$("#news-holder").html('<div class="ajax-loader news-loader"></div>');
			},
			success: function(data) {
				var dane = data;
				$.get("_js/jst/tabs.jst", function(data){
					tpl = data; 
					$("#news-holder").html(tpl.process(dane));
					$("#news-holder").css("width",(dane.count*313)+'px'); 
					$('#slider').slider('option', 'value',0);			
					$("#tabs-box").attr("scrollLeft", 0);
					$.cookie("selected-tab",2,{expires: 1});
					if(dane.count<3)
					{
						$("#arrow-left").css("display","none");
						$("#slider").css("display","none");
						$("#arrow-right").css("display","none");
					}
					else
					{
						$("#arrow-left").css("display","");
						$("#slider").css("display","");
						$("#arrow-right").css("display","");
					}
				});
					
			},
			error: function() {
				$("#news-holder").css("width",626+'px'); 
				$("#news-holder").html('<div class="Nieudane połączenie z bazą - spróbuj odświeżyć stronę"></div>');
			}
		}); 
		return false;
	});

	$("#tab-3").click(function() {
		$("#tabs").css("background-position","0 -66px");
		$.ajax({  
			type: "post",
			url: "php/getNews.php",
			data: "newstype=3", 
			dataType: "json",
			cache: true, 
			beforeSend: function() {
				$("#news-holder").css("width",626+'px'); 
				$("#news-holder").html('<div class="ajax-loader news-loader"></div>');
			},
			success: function(data) {
				var dane = data;
				$.get("_js/jst/tabs.jst", function(data){
					tpl = data; 
					$("#news-holder").html(tpl.process(dane));
					$("#news-holder").css("width",(dane.count*313)+'px'); 
					$('#slider').slider('option', 'value',0);			
					$("#tabs-box").attr("scrollLeft", 0);
					$.cookie("selected-tab",3,{expires: 1});
					if(dane.count<3)
					{
						$("#arrow-left").css("display","none");
						$("#slider").css("display","none");
						$("#arrow-right").css("display","none");
					}
					else
					{
						$("#arrow-left").css("display","");
						$("#slider").css("display","");
						$("#arrow-right").css("display","");
					}
				});
					
			},
			error: function() {
				$("#news-holder").css("width",626+'px'); 
				$("#news-holder").html('<div class="Nieudane połączenie z bazą - spróbuj odświeżyć stronę"></div>');
			}
		}); 
		return false;
	});
	
});