$(document).ready(function() {
$("#autologin").tooltip({position: "bottom center"});
  // Funkcja dodająca komentarz
  $("#dodajkomentarz").click(function() {
	var nowykoment = $("#nowykoment").val();
	var userid = $("[name=kom_u]").val();
	var nick = $("#driver").text();
	var art = $("[name=kom_a]").val();
    var dataString = 'nowykoment=' + nowykoment + '&userid=' + userid + '&nick=' + nick + '&art=' + art;
		
	if(nowykoment=='')
     {
		$("#flash2").fadeIn(400).html("Pole treści nie może być puste");
     }
	else
	{
		$("#flash2").show();
		$("#flash2").fadeIn(400).html('<img src="/layout/ajax-loader.gif" align="absmiddle" />');
		
		$.ajax({
			type: "POST",
			url: "/scripts/ajax/newkoment.php",
			data: dataString,
			cache: false,
			success: function(html)
			{
				$("#koment_view").prepend(html);
				$("#koment_view div:first").fadeIn("slow");
				document.getElementById('nowykoment').value='';
				$("#nowykoment").focus();
				$("#flash2").hide();
				$("#flash2").remove();
			}
		});
	}return false;
	});
	
	$("#Iconekspert").hover(function(){$(this).attr("src","/layout/ekspert_mini_a.png");},function(){$(this).attr("src","/layout/ekspert_mini.png");});
	
	// Funkcja pokaż / zwiń komentarze
	$("#komentarze").toggle(
			function () {
				$(this).html("Zwiń komentarze");
		if($("#th").html()==1)
		 $("#koment_view_add").slideDown("slow");
		  },
		  function () {
		  $("#koment_view_add").slideUp("slow");
			$(this).html("Pokaż wszystkie komentarze");
		  }
		);
	// Funkcja Ajax pokazująca resztę komentarzy
	$("#komentarze").click(function() 
		{
			var art = $("[name=kom_a]").val();
			var dataString = 'art=' + art;
			if($("#th").html()==0)
				{
					$("#flash2").show();
					$("#flash2").fadeIn(400).html('<img src="/layout/ajax-loader.gif" align="absmiddle" />');
					
					$.ajax({
						type: "POST",
						url: "/scripts/ajax/allcomments.php",
						data: dataString,
						cache: false,
						success: function(html)
						{
							$("#koment_view").append(html);
							$("#koment_view_add").fadeIn("slow");
							document.getElementById('nowykoment').value='';
							$("#nowykoment").focus();
							$("#flash2").hide();
							$("#flash2").remove();
							}
					});
					$("#th").html("1");
				}
		});
	
	// Skrypt odpowiedzialny za rotację cytatów
	$('#cytaty .slide');
	setInterval(function(){
		$('#cytaty .slide').filter(':visible').fadeOut(600,function(){
			if($(this).next('li.slide').size()){
				$(this).next().fadeIn(600);
			}
			else{
				$('#cytaty .slide').eq(0).fadeIn(600);
			}
		});
	},10000);
		
		// Nowy temat zaproponuj
	$(".submit").click(function() {

	var nowytemat = $("#nowytemat").val();
	var case_id = $("#case_id").val();
    var dataString = 'nowytemat=' + nowytemat + '&case_id=' + case_id;
	
	if(nowytemat=='')
     {
		$("#flash").fadeIn(400).html("Pole treści nie może być puste");
     }
	else
	{
		$("#flash").show();
		$("#flash").fadeIn(400).html('<img src="/layout/ajax-loader.gif" align="absmiddle" />');
		
		$.ajax({
			type: "POST",
			url: "/scripts/ajax/recenttopic.php",
			data: dataString,
			cache: false,
			success: function(html)
			{
				$("#topic_blackout").append(html);
				document.getElementById('nowytemat').value='';
				$("#nowytemat").focus();
				$("#flash").hide();
				$("#flash").remove();
				$(".topic_vote_write").remove();
				$("#inNoTem").hide();
				$("#inNoTem").remove();
				nlh =  $('#wide').height();
			$('#wide').css('top',-(nlh-200)+'px');
				}
		});
	}return false;
	});
	// Nowy temat zagłosuj
	$("[name=topicvote]").click(function() {
	var tvote = $(this).attr("value");
	var u = $("[name=topic_u]").val();
		var dataString = 'tvote=' + tvote +  '&u=' + u;	
		$.ajax({
			type: "POST",
			url: "/scripts/ajax/topic_vote.php",
			data: dataString,
			cache: false,
			success: function(html)
			{
				$("#wide").html(html);
			}
		});
	});
	// Sonda głosowanie
	$('input[name="vote"]:radio').click(function() {
	var art=$("[name=sonda_a]").val();
	var sonda = $(this).val();
	var u = $("[name=sonda_u]").val();
		var dataString = 'art=' + art +  '&sonda=' + sonda +  '&u=' + u;	
		$.ajax({
			type: "POST",
			url: "/scripts/ajax/poll_vote.php",
			data: dataString,
			cache: false,
			success: function(html)
			{
				$("#poll_inside").html(html);
			}
		});
	});
	// Dodaj odpowiedź na podchwytliwe pytanie
	$("#dodajst").click(function() {
	var nowest = $("#nowest").val();
	var nick = $("#driver").text();
	var st = $("[name=st_id]").val();
	var u = $("[name=st_u]").val();
    var dataString = 'nowest=' + nowest + '&nick=' + nick + '&st=' + st + '&u=' + u;
	
	
	if(nowest=='')
     {
		$("#flash3").fadeIn(400).html("Pole treści nie może być puste");
     }
	else
	{
		$("#flash3").show();
		$("#flash3").fadeIn(400).html('<img src="/layout/ajax-loader.gif" align="absmiddle" />');
		
		$.ajax({
			type: "POST",
			url: "/scripts/ajax/newstodp.php",
			data: dataString,
			cache: false,
			success: function(html)
			{
				$("#countstodp").html(parseInt($("#countstodp").text())+1);
				$("#st_view_odp").prepend(html);
				$("#st_view_odp div:first").fadeIn("slow");
				document.getElementById('nowest').value='';
				$("#nowest").focus();
				$("#flash3").hide();
				$("#flash3").remove();
			}
		});
	}return false;
	});	
	$("#odpact").toggle(
			function () {
				$(this).html("Zwiń / Dodaj");
				$("#dodaj2").slideToggle("slow");
		if($("#ts").html()==1)
		 $("#st_view_odp_add").slideDown("slow");
		  },
		  function () {
			$("#dodaj2").slideToggle("slow");
		  $("#st_view_odp_add").slideUp("slow");
			$(this).html("Rozwiń / Dodaj");
		  }
		);
	$("#odpact").click(function() 
		{
			var a = $("[name=st_a]").val();
			var st = $("[name=st_id]").val();
			var dataString = 'st=' + st +  '&a=' + a;
			if($("#ts").html()==0)
				{
					$("#flash3").show();
					$("#flash3").fadeIn(400).html('<img src="/layout/ajax-loader.gif" align="absmiddle" />');
					
					$.ajax({
						type: "POST",
						url: "/scripts/ajax/allstodp.php",
						data: dataString,
						cache: false,
						success: function(html)
						{
							$("#st_view_odp").append(html);
							$("#st_view_odp_add").fadeIn("slow");
							$("#flash3").hide();
							$("#flash3").remove();

							}
					});
					$("#ts").html("1");
				}
		});
	//
	//  Nowa wiadomość - login main
	//
	$("#wiadomtoogle").click(function() {
		$("#subjectwiado").fadeIn(400);
		$("#response").fadeIn(400);
		$("#wiadomtoogle").hide();		
	});
	$("#loginramka").click(function() {
		$("#loginramkamain").fadeOut(600);
	});
	$("#wyslijwiad").click(function() {
	var nowawiadom = $("#pomozsagio").val();
	var userid = $("[name=userid]").val();
    var dataString = 'nowawiadom=' + nowawiadom + '&userid=' + userid;
		
	if(nowawiadom=='')
     {
		$("#wiadflash").fadeIn(400).html("Pole treści nie może być puste");
     }
	else
	{
		$("#wiadflash").fadeIn(400).html('<img src="/layout/ajax-loader.gif" align="absmiddle" />');
		
		$.ajax({
			type: "POST",
			url: "/scripts/ajax/newkontaktmain.php",
			data: dataString,
			cache: false,
			success: function(html)
			{
				$("#response").html(html);
				$("#response").fadeIn("slow");
				$("#wiadflash").hide();
			}
		});
	}return false;
	});
});
function slidedown() {
  nlh =  $('#wide').height();
  nl =  $('#wide').css('top');
  nl = nl.substr(0,nl.length-2);
  nl = parseInt(nl)-2;
  $('#wide').css('top',nl+'px');
  if(nl>=-(nlh-200))
	czas =  setTimeout("slidedown()",10);
  else
    clearTimeout(czas);
}
function slideup() {
  nl =  $('#wide').css('top');
  nl = nl.substr(0,nl.length-2);
  nl = parseInt(nl)+2;
  $('#wide').css('top',nl+'px');
  if(nl<0)
	czas =  setTimeout("slideup()",10);
  else
	clearTimeout(czas);
}

var czas;
$(document).ready(function() {
$('#slide').hover(function(){$('#slideup').slideDown(500);$("#dost_filmy").slideUp(400);var top = $("#wide").css("top").replace("px","");nlh =  $('#wide').height();if(top>=-(nlh-200)){slidedown();}else{clearTimeout(czas)}},function(){clearTimeout(czas)});
$('#slideup').hover(function(){var top = $("#wide").css("top").replace("px","");if(top<0){slideup();}else{clearTimeout(czas);}},function(){clearTimeout(czas); if($("#wide").css("top")=="0px"){$('#slideup').slideUp(400);$("#dost_filmy").slideDown(500);}});}

);


