function send_newsletter(){
	$.ajax({
		type: "POST",
		url: "newsletter.html",
		async: false,
		data: "newsletter_eml="+encodeURI($("#newsletter_eml").val()),
		success: function(msg){
			if(msg=='eml_error'){
				$("#text_newsletter").html("Podany adres e-mail jest nieprawidłowy.");
			}
			else if(msg=='dane_error'){
				$("#text_newsletter").html("Proszę uzupełnić pole formularza.");
			}
			else if(msg=='eml_exist'){
				$("#text_newsletter").html("Podany adres e-mail znajduje się już w bazie.");
			}
			else{
				$("#text_newsletter").html("Na podany adres e-mail został wysłany link aktywacyjny.");
			}
		}
	});	
}

function czysc_pole(inp,val){
	if($("#"+inp).val()==val){
		$("#"+inp).val('');
	}else if($("#"+inp).val()==''){
		$("#"+inp).val(val);
	}
}

function przesunf(pas, gdzie, ile) {
	if(typeof(pas)!='undefined') {
		var id=pas.substr(1);
		var marginL=parseInt($('#'+id).css('marginLeft'));
		var width=parseInt($('#'+id).css('width'));
		var li_marginL=parseInt($('#'+id+' li:first-child').css('marginLeft'));
		var li_paddingL=parseInt($('#'+id+' li:first-child').css('paddingLeft'));
		var li_paddingR=parseInt($('#'+id+' li:first-child').css('paddingRight'));
		var li_width=parseInt($('#'+id+' li:first-child').css('width'));
		var li_razem=li_marginL+li_width+li_paddingL+li_paddingR;
		if(gdzie.substr(1,(pas.length))=='prev') {
			if(marginL<(li_razem*ile)*(-1)) margin=marginL+(li_razem*ile);
			else margin=0;
		}else {
			if(marginL*(-1)<width-(li_razem*ile*2)) margin=marginL-(li_razem*ile);
			else margin=(width-(li_razem*ile))*(-1);
		}
		if(marginL==margin && marginL!=0) {
			margin=0;
		}else if(marginL==margin) {
			margin=(width-(li_razem*ile))*(-1);
		}
		$('#'+id).animate({marginLeft: margin+'px'},100); 
	}
}

$(function() {

    // gwiazdki do oceny
    $(".starify").stars({
        oneVoteOnly: true,
        cancelShow: false,
        callback: function(ui, type, value)
        {
            $.post("/ajax.php?mode=star&rate="+value+"&id="+ui.options.title, {
                rate: value,
                id : ui.options.title
            }, function(json){
            	ui.select(Math.round(parseInt(json.avg)));
            	$('#'+ui.options.title).text(json.count);
            }, "json");
        }
    });

    //zwijanie/rozwijanie paneli
    $('.toggle').click(function(event) {
    	event.preventDefault();
        var box = $(this).attr('id');
        var state = $('.'+box).css("display");
        $('.'+box).toggle('fast');
//        $.ajax({
//            url: "/ajax.php?mode=toggle&state="+state+"&box="+box
//        });
    });

    // przycisk lubie komentarz
    $("span.k_like").click(function () {
        var id = $(this).attr('id').replace('k_like_', '');
        $.ajax({
            url: "/ajax.php?mode=k_like&id="+id,
            success: 
            function(data) {
                if (data!='')
                {
                    $('#k_ilike_'+id).text(data);
                }
            }
        });
    });

    // przycisk nie lubie komentarz
    $("span.k_unlike").click(function () {
        var id = $(this).attr('id').replace('k_unlike_', '');
        $.ajax({
            url: "/ajax.php?mode=k_unlike&id="+id,
            success: 
            function(data) {
                if (data!='')
                {
                    $('#k_ilike_'+id).text(data);
                }
            }
        });
    });

    // przycisk zglos do usuniecia
    $("span.k_trash").click(function () {
        var id = $(this).attr('id');
        $.ajax({
            url: "/ajax.php?mode=k_trash&id="+id,
            success: 
            $(this).parent().effect("highlight", {
                color:'#F00'
            }, 3000)
        });
    });
    
    //zwijanie/rozwijanie komentarzy
    $('.k_toggle').toggle(
        function() {
            var id = $(this).attr('id').replace('k_toggle_', '');
            $('#kom_'+id).show();
            $(this).text('Ukryj');
            
        },
        function(){
            var id = $(this).attr('id').replace('k_toggle_', '');
            $('#kom_'+id).hide();
            $(this).text('Pokaż');
        });

    //zwijanie/rozwijanie form. do odpow. na kom
    $('.k_odp').toggle(
        function() {
            var id = $(this).attr('id').replace('k_odp_', '');
            $('#kodp_'+id).show();
            $(this).text('Ukryj');
        },
        function(){
            var id = $(this).attr('id').replace('k_odp_', '');
            $('#kodp_'+id).hide();
            $(this).text('Odpowiedz');
        });

        
    //slider video
	$(".vnext").click(function() {
		var id=$(this).attr("id");
		przesunf(id, 'vnext', 2);
	});
	$(".vprev").click(function() {
		var id=$(this).attr("id");
		przesunf(id, 'vprev', 2);
	});
	
    //slider ogloszen na glownej
	$(".onext").click(function() {
		var id=$(this).attr("id");
		var width_dis=parseInt($('#'+id.substr(1)).css('width'));
		var width_all=parseInt($('#c'+id.substr(1)).css('width'));
		if(width_dis>width_all)
			przesunf(id, 'onext', 3);
	});
	$(".oprev").click(function() {
		var id=$(this).attr("id");
		var width_dis=parseInt($('#'+id.substr(1)).css('width'));
		var width_all=parseInt($('#c'+id.substr(1)).css('width'));
		if(width_dis>width_all)
			przesunf(id, 'oprev', 3);
	});
	
    //slider firm na glownej
	$(".fnext").click(function() {
		var id=$(this).attr("id");
		przesunf(id, 'fnext', 1);
	});
	$(".fprev").click(function() {
		var id=$(this).attr("id");
		przesunf(id, 'fprev', 1);
	});
	 
	//slider firm na glownej
	$(".rnext").click(function() {
		var id=$(this).attr("id");
		przesunf(id, 'rnext', 1);
	});
	$(".rprev").click(function() {
		var id=$(this).attr("id");
		przesunf(id, 'rprev', 1);
	});
	
	$("#ocontainer_block").hover(function() {
			int_o=window.clearInterval(int_o);
		},
		function() {
			if(ido!='undefined') {
				int_o=setInterval("przesunf('"+ido+"', 'onext', 3)", 5000);
			}
		}
	);
	
	$("#fcontainer_block").hover(function() {
			int_f=window.clearInterval(int_f);
		},
		function() {
			if(idf!='undefined') {
				int_f=setInterval("przesunf('"+idf+"', 'fnext', 1)", 5000);
			}
		}
	);
	
	$("#prcontainer_block").hover(function() {
			int_r=window.clearInterval(int_r);
		},
		function() {
			if(idr!='undefined') {
				int_r=setInterval("przesunf('"+idr+"', 'rnext', 1)", 5000);
			}
		}
	);
	
	var ido=$(".onext").attr("id");
	if(typeof(ido)!='undefined') {
		var width_dis=parseInt($('#'+ido.substr(1)).css('width'));
		var width_all=parseInt($('#c'+ido.substr(1)).css('width'));
		if(width_dis>width_all)
			var int_o=setInterval("przesunf('"+ido+"', 'onext', 3)", 5000);
	}
	var idf=$(".fnext").attr("id");
	if(typeof(idf)!='undefined') {
		var int_f=setInterval("przesunf('"+idf+"', 'fnext', 1)", 5000);
	}
	var idr=$(".rnext").attr("id");
	if(typeof(idr)!='undefined') {
		var int_r=setInterval("przesunf('"+idr+"', 'rnext', 1)", 5000);
	}
    // przycisk lubie komentarz
    $("#vcontainer a").click(function (event) {
    	event.preventDefault();
        var id = $(this).attr('id').replace('videobox_', '');
        $.ajax({
            url: "/ajax.php?mode=videobox&id="+id,
            success: 
            function(data) {
                if (data!='')
                {
                    $('#viframe').html(data);
                }
            }
        });
    });
    
    $(".op_szukaj").click(function (event) {
    	event.preventDefault();
    	var id=$(this).attr('id').replace('szukaj-', '');
    	$('#hidden_search').val(id);
    	$(".op_szukaj").removeClass('bold');
    	$(this).addClass('bold');
    });

    //czyszczenie inputow z headera z domyslnych wartosci
    $.fn.clearDefault = function(){
    	return this.each(function(){
	    	var default_value = $(this).val();
	    	$(this).focus(function(){
	    		if ($(this).val() == default_value) $(this).val("");
	    	});
	    	$(this).blur(function(){
	    		if ($(this).val() == "") $(this).val(default_value);
	    	});
    	});
    };
    
    $('.w2 input.input_text').clearDefault();
    
	$("#newsletter_eml").focus(
		 function () {
			czysc_pole('newsletter_eml','wpisz swój adres e-mail'); 
		  });
	$("#newsletter_eml").blur(
		  function () {
			czysc_pole('newsletter_eml','wpisz swój adres e-mail');
		  });
	
	$("#ss_btn").click(
		function(){
			$(".ss_chmurka").toggle();
		});
	
	$("select[name='select_rejon']").change(function () {
		var id_rej=$(this).val();
		if(id_rej>0) $("#select_rejon_f").submit();
	});
	
	var sky_spr=$('#sky_scroll').attr('id');
	if(typeof(sky_spr)!='undefined') {
		var sky_close=$('#sky_scroll').css('top');
		var its_a_live=1;
	    menuYloc = parseInt($('#sky_scroll').css("top").substring(0,$('#sky_scroll').css("top").indexOf("px")));
	    $(window).scroll(function () {  
	    	if(its_a_live==1) {
		        var offset = menuYloc+$(document).scrollTop()+"px";  
		        $('#sky_scroll').animate({top:offset},{duration:500,queue:false});
	    	}
	    });
        $('#sky_scroll_close').click(function() {
        	its_a_live=0;
        	$('#sky_scroll').css('top', sky_close);
        	$('#sky_scroll_close').hide();
        });
	}
	
	var top_spr=$('#TopLayer').attr('id');
	if(typeof(top_spr)!='undefined') {
		var top_close=$('#TopLayer').css('top');
		var its_a_live_top=1;
	    menuYloc_top = parseInt($('#TopLayer').css("top").substring(0,$('#TopLayer').css("top").indexOf("px")));  
	    $(window).scroll(function () {
	    	if(its_a_live_top==1) {
		        var offset = menuYloc_top+$(document).scrollTop()+"px";  
		        $('#TopLayer').animate({top:offset},{duration:500,queue:false});
	    	}
	    });
        $('#TopLayer_close').click(function() {
        	its_a_live_top=0;
        	$('#TopLayer').hide();
        });
	}
	
	$('.recaptcha_clone1').html($('.recaptcha_clone').clone(true,true));
});
