$(document).ready(function(){
	var comment_count=0;
	var vote_count=0;

/* prepend */

	var kategoria_id			= $('#kategoria').attr('value');
	$('select#podkategoria').load("wybor_podkat.php", {kat_id: kategoria_id});
	
	$('.roleta').hide();

/* actions */

	$("#add_comment").click(function() {
		$('#send_comment_form').slideToggle();
		$('#comment_sent').hide();
		return false;
	});
	
	$("#show_comments").click(function() {
		$('#show_comments').hide();
		$('#comments').fadeIn();
		return false;
	});


	$("#send_comment_form").submit(function() {
		if(comment_count == 0){
			var c_shop			= $('#shopid').attr('value');
			var c_content		= $('#comment_content').attr('value');
			var c_author		= $('#comment_author').attr('value');
			var c_captcha		= $('#comment_captcha').attr('value');
			var c_email		= $('#comment_email').attr('value');
			$('#comment_sent').html('Proszę czekać <img src="./images/load.gif" alt="loading" />');
			$('#comment_sent').load("ajax.php", {comment_shop: c_shop, comment_content: c_content, comment_author: c_author, comment_captcha: c_captcha, comment_email: c_email});
			$('#send_comment_form').fadeOut();
			$('#comment_sent').fadeIn();
			$('#add_comment').show();
		}
		return false;
	});


	$("#send_trouble").click(function() {
		$('#send_trouble_form > form').slideToggle();
		$('#trouble_sent').hide();
		return false;
	});

	$("#send_trouble_form > form").submit(function() {
		var tr_shid			= $('#tr_shid').attr('value');
		var tr_spid			= $('#tr_spid').attr('value');
		var tr_text			= $('#tr_text').attr('value');
		var tr_antyspam		= $('#tr_antyspam').attr('value');
		$('#trouble_sent').html('Proszę czekać <img src="./images/load.gif" alt="loading" />');
		$('#trouble_sent').load("ajax.php", {trouble_shid: tr_shid, trouble_spid: tr_spid,	trouble_text: tr_text, trouble_antyspam: tr_antyspam});
		$('#send_trouble_form > form').fadeOut();
/*		$('#trouble_sent').fadeIn();*/
		
		return false;
	});

	
	$(".userProfile .AddComment form").submit(function() {
		var commentBody = $("#profileCommentBody", this).attr('value');
		var commentUID = $("#profileCommentUID", this).attr('value');
		$("div.ajaxback", this).html('Proszę czekać <img src="./images/load.gif" alt="loading" />');
		$("div.ajaxback", this).load("ajax.php", {pCommBody: commentBody, pCommUID: commentUID}, function(){
			if($(this).text() == "\nKomentarz dodany"){
				$("input, textarea", this.parentNode).attr('disabled', 'disabled');
				$("input", this.parentNode).addClass('disabled');
				

				$(".roleta", this.parentNode).slideUp();
			}
		});

		return false;		
	});

	$(".userProfile .AddBlogPost form").submit(function() {
        var blogPostBody = $("#blogPostBody", this).attr('value');
        $("div.ajaxback", this).html('Proszę czekać <img src="./images/load.gif" alt="loading" />');
        $("div.ajaxback", this).load("ajax.php", {blogPostBody: blogPostBody}, function(){
            if($(this).text() == "\nWpis dodany"){
                $("input, textarea", this.parentNode).attr('value', '');


                $(".roleta", this.parentNode).slideUp();
            }
        });

        return false;
    });


	var sprawdzam			= 'false';
	var sprawdzWWW			= '';
	$("#sprawdzWWW").keyup(function() {
		if(comment_count == 0){
			sprawdzWWW			= $('#sprawdzWWW').attr('value');
			if(sprawdzWWW.length >= 3){
				if(sprawdzam == 'false'){
					sprawdzam = 'true';
					$('#sprawdzWWWresults').html('<img src="./images/load.gif" alt="loading" /> '+$('#sprawdzWWWresults').html());
					setTimeout(function(){$('#sprawdzWWWresults').load("ajax.php", {sprawdzWWW: sprawdzWWW}); sprawdzam = 'false';}, 1000);
				}
			}else{
				$('#sprawdzWWWresults').text("Wpisz przynajmniej 3 znaki.");
			}
		}
		return false;
	});

	$("form .radios_box input.radio").change(function(){
		$("input.xstext", this.parentNode.parentNode).attr('value', '');
	});

	$("form .radios_box input.xstext").keydown(function(){
		$("input.radio", this.parentNode.parentNode).attr('checked', '');
	});


	$("#addvote").click(function() {
		$('#addvoteinfo').slideToggle("fast");
		return false;
	});

	$("img.vote").click(function() {
		if(vote_count == 0){
			var v_shop			= $('#shopid').attr('value');
			var vote			= $(this).attr('id');
			vote = vote.substr(4);
	
			$.ajax({
				type: "POST",
				url: "ajax.php",
				data: "vote_shop=" + v_shop + "& vote_value=" + vote,
				success: function(del){
					if(vote < 2){$('img#vote'+2).fadeOut();}
					if(vote < 3){$('img#vote'+3).fadeOut();}
					if(vote < 4){$('img#vote'+4).fadeOut();}
					if(vote < 5){$('img#vote'+5).fadeOut();}
					$('img#vote'+vote).attr("src", "./img/vote.png");
					$('img#vote'+vote).fadeIn();
					$('#voting').hide();
					$('#voting').text("Dziekujemy za oddanie głosu");
					$('#voting').fadeIn();
				}
			});	
			vote_count = 1;
		}
		return false;
	});
			

	$(".votec").click(function() {
		var vote		= $(this).attr('id');
		comm_val = vote.substring(4,5);
		comm_id = vote.substr(5);
		$(this.parentNode).load("ajax.php", {comm_id: comm_id, comm_val: comm_val});

				return false;
	 });



	$("#voting > span").hover(function() {
		$("img.vote").hover(function() {
			if(vote_count == 0){

				var hover = $(this).attr('id');
				hover = hover.substr(4);

				for(i=1;i<=5;i++){
					$('img#vote'+i).attr("src", "./img/votegrey.png");
				}
				for(h=1;h<=hover;h++){
					$('img#vote'+h).attr("src", "./img/vote.png");
				}
			}
		}, function() {
		});
	}, function() {
		for(i=1;i<=5;i++){
			$('img#vote'+i).attr("src", "./img/votegrey.png");
		}
	});

	


	$("select#kategoria").change(function() {
		var kategoria_id			= $('#kategoria').attr('value');
		$('select#podkategoria').load("wybor_podkat.php", {kat_id: kategoria_id});
		return false;
	});


	$(".czysc").focus(function () {
		if($(this).hasClass("czysc")){
			$(this).attr("value", "");
			$(this).removeClass("czysc");
			if($(this).hasClass("password")){
				$(this).removeClass("password");
			}
		}
	});

	$(".show_help").hover(function(){
		$("#helpbox").fadeIn('50');
		if($(this).attr('alt')+'' == 'undefined'){
			$("#helpbox").text($(this).attr('title'));
		}else{
			$("#helpbox").text($(this).attr('alt'));
		}
	}, function(){
		$("#helpbox").hide();
	});

	$(".zwin").click(function(){
		$(".roleta:child:first", this.parentNode).slideToggle();
	});

	$(document).mousemove(function(e){
		$("#helpbox").css("left", e.pageX + 10);
		$("#helpbox").css("top", e.pageY + 10);
	});
});
