var $j = jQuery.noConflict();
(function($j){
	$j.fn.jTruncate = function(options) {
		var defaults = {
			length: 300,
			minTrail: 20,
			moreText: "wiecej",
			lessText: "mniej",
			ellipsisText: "...",
			moreAni: "",
			lessAni: ""
		};
		var options = $j.extend(defaults, options);
		return this.each(function() {
			obj = $j(this);
			var body = obj.html();

			if(body.length > options.length + options.minTrail) {
				var splitLocation = body.indexOf(' ', options.length);
				if(splitLocation != -1) {
					// trunkujemy
					var splitLocation = body.indexOf(' ', options.length);
					var str1 = body.substring(0, splitLocation);
					var str2 = body.substring(splitLocation, body.length );
					obj.html(str1 + '<span class="truncate_ellipsis">' + options.ellipsisText +
						'</span>' + '<span class="truncate_more">' + str2 + '</span>');
					obj.find('.truncate_more').css("display", "none");
					//  link wiecej/mniej
					obj.append(
						'<div class="clearboth">' +
							'<a href="#" class="truncate_more_link">' + options.moreText + '</a>' +
						'</div>'
					);
					// event dla wiecej/mniej
					var moreLink = $j('.truncate_more_link', obj);
					var moreContent = $j('.truncate_more', obj);
					var ellipsis = $j('.truncate_ellipsis', obj);
					moreLink.click(function() {
						if(moreLink.text() == options.moreText) {
							moreContent.show(options.moreAni);
							moreLink.text(options.lessText);
							ellipsis.css("display", "none");
						} else {
							moreContent.hide(options.lessAni);
							moreLink.text(options.moreText);
							ellipsis.css("display", "inline");
						}
						return false;
				  	});
				}
			}

		});
	}
})(jQuery);
$j(document).ready(
function(){
        var image1 = $j('<img />').attr('src', '../gfx/but65_red.png');
        var image2 = $j('<img />').attr('src', '../gfx/but65_orng.png');
        var image3 = $j('<img />').attr('src', '../gfx/but100c_red.png');
        $j('li.tip').css('display','none');
        $j('li.tip').next().css('display','none');
        $j('li.spec').mouseover(function(){
                $j(this).css('background','#EFEFEF').children('span').css('text-decoration','underline').css('color','#006').css('cursor','pointer');
        }).mouseout(function(){
                $j(this).css('background','#FFFFFF').children('span').css('text-decoration','none').css('color','#000');
        }).click(function(){
                if($j(this).attr('class')=='spec active') {
                        $j(this).next().next().slideToggle('fast').prev().slideToggle(500);
                } else {
                        $j(this).parent().children('li.spec').removeClass('active');
                        $j(this).addClass('active').parent().children('li:not(.spec):not(.active)').slideUp('slow');
                        $j(this).next().next().slideToggle('fast').prev().slideToggle(500);
                }
        });
        $j('#opis_db').click(function(event) {
                event.preventDefault();;
                $j('#opis_l').slideToggle(500);
        });
        $j('span.trunc,span.trunc100').jTruncate( {
                length: 100,
                minTrail: 0,
                moreText: "[pokaż]",
                lessText: "[ukryj]",
                ellipsisText: " ...",
                moreAni: "fast",
                lessAni: "slow"
        });
        $j('span.trunc200').jTruncate( {
                length: 200,
                minTrail: 0,
                moreText: "[pokaż]",
                lessText: "[ukryj]",
                ellipsisText: " ...",
                moreAni: "fast",
                lessAni: "slow"
        });
        $j('span.trunc300').jTruncate( {
                length: 300,
                minTrail: 0,
                moreText: "[pokaż]",
                lessText: "[ukryj]",
                ellipsisText: " ...",
                moreAni: "fast",
                lessAni: "slow"
        });
        $j('input.szuk').mouseover(function(){
                $j(this).css('background-image','url("../gfx/but65_red.png")');
        }).mouseout(function(){
                $j(this).css('background-image','url("../gfx/but65_blue.png")');
        });
        $j('input.szukd').mouseover(function(){
                $j(this).css('background-image','url("../gfx/but65_orng.png")');
        }).mouseout(function(){
                $j(this).css('background-image','url("../gfx/but65_none.png")');
        });
        $j('input.szukw').mouseover(function(){
                $j(this).css('background-image','url("../gfx/but100c_red.png")');
        }).mouseout(function(){
                $j(this).css('background-image','url("../gfx/but100c_blue.png")');
        });
        $j("a[id^='hr_']").click(function(event) {
                event.preventDefault();
                $j(this).next().toggle("slow");
        });
        function validate(formData, jqForm, options, czkj) {
            var form = jqForm[0];
            var acpt = true;
            var err = "";
            $j("#zglos").html("<img src=\"gfx/inprogress.gif\" alt=\"in progress...\">");
            if (!form.im_naz.value) {
                err = " - imie nazwisko -";
                $j("#im_naz").focus();
                acpt = false;
            }
            else if (!form.nm_plac.value) {
                err = " - nazwa placowki -";
                $j("#nm_plac").focus();
                acpt = false;
            }
            else if (!form.miasto.value) {
                err = " - miasto -";
                $j("#miasto").focus();
                acpt = false;
            }
            else if (!form.adres.value) {
                err = " - adres -";
                $j("#adres").focus();
                acpt = false;
            }
            else if (!form.tel.value) {
                err = " - telefon -";
                $j("#tel").focus();
                acpt = false;
            }
            else if (!form.zgoda.checked) {
                err = "check";
                $j("#zgoda").focus();
                acpt = false;
            }
            if(!acpt) {
                    if (err=="check") {
                            alert("Wymagana jest zgoda na przetwarzanie danych.");
                    
                    } else {
                            alert("Pole " + err + " musi być wypełnione.");
                    }
                    $j("#zglos").html("");
                    return false;
            } else {
                    //alert("wszystkie pola wypelnione");
                    return true;
            }
        }
        function pokReq(formData, jqForm, options) {
            var queryString = $j.param(formData);

            alert("Wysylane: \n\n" + queryString);
            return true;
        }

        function pokOdp(responseText, statusText, xhr, $form)  {
            //
            //
            $("#zglos").hide().fadein("slow");
        }
        //$j.get('ajax/zglosz.php', function(data) {
          //      $j("#zglos").children("fieldset").html(data)
        //});
        var options = {
                target:             "#zglos",
                url:                "ajax/pst.php",
                beforeSubmit:       validate,
                statusText:         "zgloszenie wyslane",
                success:            pokOdp,
                clearForm:          true
        };
        $j("#zgloszenie").ajaxForm(options);
        //$j("#zgloszenie").submit(function() {
           //     $j(this).ajaxSubmit();
          //      return false;
        //});

        $j("#zg_plac").click(function() {
                $j("#d_zglos").css("display","block");
                $j("#im_naz").focus();
        });
        $j("#oll").change(function() {
                var stt = $j(this).attr("checked");
                if(stt) {
                        $j(this).parent().children(":checkbox").removeAttr("disabled").attr("checked","checked");
                } else {
                        $j(this).parent().children(":checkbox").removeAttr("checked");
                }
        });
        //$j("#ch_5").attr("disabled","disabled");
        $j("#ch_4").change(function() {
                var hnf = $j(this).attr("checked");
                var nnf = $j("#ch_5").attr("checked");
                //if(hnf) {
                  //      $j("#ch_5").removeAttr("disabled");
                //} else {
                  //      $j("#ch_5").removeAttr("checked").attr("disabled","disabled");
                //}
                if(!hnf && nnf) {
                        $j("#ch_5").removeAttr("checked");
                }
        });
        $j("#ch_5").change(function() {
                var hnf = $j("#ch_4").attr("checked");
                var nnf = $j(this).attr("checked");
                //if(hnf) {
                  //      $j("#ch_5").removeAttr("disabled");
                //} else {
                  //      $j("#ch_5").removeAttr("checked").attr("disabled","disabled");
                //}
                if(nnf && !hnf) {
                        $j("#ch_4").attr("checked","checked");
                }
        });

        $j("#widget_facebook").hover(
                function() {
                        $j("#widget_facebook").stop(true, false).animate({ right: "0" }, 500);
                },
                function() {
			$j("#widget_facebook").stop(true, false).animate({ right: "-307" }, 500);
		},
                500
        );
        $j('a[href=#top]').click(function(){
                $j('html, body').animate({scrollTop:0}, 800);
                return false;
        });
        var dheight = $j(document).height();
        var wheight = $j(window).height();
        var moov    = 0;
        var npos    = 0;
        $j('#dol').click(function(){
                var wpos    = $j(window).scrollTop();
                if(wpos != dheight - wheight) {
                        $j(this).css('opacity',.8);
                        if(wpos + wheight*3/4 < dheight ) {
                                npos = wpos + wheight*3/4;
                        } else {
                                npos = dheight - wheight;
                        }
                        $j('html, body').animate({scrollTop:npos}, 800, function() {
                                $j('#dol').css('opacity',1);
                        });
                }
                return false;
        });
        $j('#gora').click(function(){
                var wpos    = $j(window).scrollTop();
                if(wpos != 0) {
                        $j(this).css('opacity',.8);
                        if(wpos - wheight*3/4 > 0 ) {
                                npos = wpos - wheight*3/4;
                        } else {
                                npos = 0;
                        }
                        $j('html, body').animate({scrollTop:npos}, 800, function() {
                                $j('#gora').css('opacity',1);
                        });
                }
                return false;
        });
        $j('#selmsc,#w_sp').sb();
        $j('#ch_').click(function(){
                $j('#czacik').slideToggle('fast');
        });
});

