$(document).ready(function(){		
	$.Lightbox.construct({
		"show_linkback": false,
		"show_helper_text": false,
		"show_info": true,
		"show_extended_info": true,
		"download_link": false
	}); 
	$("a[rel=lightbox-slubne-auto-mygallery]").lightbox();
	        
	        $("th[rel=sortable]").click(function(){
	    	    document.sortform.sort.value = $(this).attr("name");
	    	    if($(this).attr("name") == currSort){
	    		if(document.sortform.sortOrder.value=="asc")document.sortform.sortOrder.value="desc";
	    		else document.sortform.sortOrder.value="asc";
	    	    }
	    	    else
	    		document.sortform.sortOrder.value="asc";
	    	    document.sortform.submit();
	    	    
	        }).css("cursor","pointer").css("color","#ff0").each(function(){
	    	    if($(this).attr("name") == currSort) $(this).css("text-decoration","underline");
	        });
	        
	        obj = $("input");
	        jQuery.each(obj, function(i, val){
	    	    if($(val).attr("class")=="")
	    		$(val).attr("class", $(val).attr("type"));
	        });
	        
		$("input[name=ocena]").radioToStars({star: "img/star_grey.png", star_hover: "img/star.png"});
		
		/*$("table[class=wyszukaj] input[class=text]").focus(function(){
		    msg = $(this).attr("rel");
		    topx = $(this).position().top + 23;
		    left = $(this).position().left;
		    o = $(this).parent();
		    o.append("<div style='position:absolute; top:"+topx+"px; left:"+left+"px;' class='tooltipWrapper'>"+msg+"</div>");
		}).blur(function(){
		    $("div[class=tooltipWrapper]").remove();
		}).keyup(function(){
		    if(this.value.length>3)
			$("div[class=tooltipWrapper]").remove();
		});*/
		
		$("div[class=wyszukaj] input[type=text]").focus(function(){
		    msg = $(this).attr("rel");
		    //topx = $(this).position().top + 23;
		    //left = $(this).position().left;
		    topx = $(this).offset().top + 23;
		    left= $(this).offset().left;
		    o = $(this).parent();
		    o.append("<div style='position:absolute; top:"+topx+"px; left:"+left+"px;' class='tooltipWrapper'>"+msg+"</div>");
		}).blur(function(){
		    $("div[class=tooltipWrapper]").remove();
		}).keyup(function(){
		    if(this.value.length>3)
			$("div[class=tooltipWrapper]").remove();
		});
		
		var num_cat=$("input[rel=cbkat]:checked").length;
		if(num_cat>0)max_num_cat=max_num_cat+num_cat-1;
		$("input[rel=cbkat]").click(function(){
		    c = $("input[rel=cbkat]:checked").length;
		    if(c>max_num_cat && c>num_cat){
			$("#cbkatinfo").html("Nie masz wystarczającej ilości punktów");
			return false;
		    }
			if (num_cat>0)c=c-num_cat;
		    else if(c>0)c=c-1;
		    if(cost*c>0) $("#cbkatinfo").html("Koszt: "+cost*c+" pkt"); 
		    else $("#cbkatinfo").html(""); 
		});
		
		//$("input[name=adres]").autocomplete("ajax/ajax_miasto.php", {});
		
		$("input[rel=date]").datepicker();
		
		$("input[name=data]").change(function(){
		    td = $("input[name=today]").val();
		    tdy= $("input[name=todayplusyear]").val();
		    if($(this).val()<td){
			alert("Podany termin to już przeszłość!");
			$(this).val("");
		    }
		    if($(this).val()>tdy){
			alert("Termin wydaje się zbyt odległy!");
			$(this).val("");
		    }
		});
		
		/*	$("input[name=tytul]").each(function(index){
			  
			  $(this).keyup(function(){
				    x = 200 - $(this).val().length;
				    if(x==0) x = "0";
				    $(this).parent().find("#pozostalo").html(x);
				});
		   $(this).parent().find("#pozostalo").html("");
		 });*/

		$("input[name=tytul]").keyup(function(){
		    x = 200 - $(this).val().length;
		    if(x==0) x = "0";
		    $(this).parent().find("#pozostalo").html(x);
		});
		
		setInterval(function(){
			obj = $("a[rel=blink] span");
			if($(obj).css("visibility") == "visible"){
				$(obj).css('visibility','hidden');
			}
			else{
				$(obj).css('visibility','visible');
			}
		}, 700);

	//NEW
		$('#polec_mailem_link').click(function() {
		    $('#polec_gg').hide();
		    $('#polec_social').hide();
		    $('#polec_mailem').slideToggle('normal');
	        });
		
		$('#polec_gg_link').click(function() {
		    $('#polec_mailem').hide();
		    $('#polec_social').hide();
		    $('#polec_gg').slideToggle('normal');
	        });
		
		$('#polec_social_link').click(function() {
		    $('#polec_mailem').hide();
		    $('#polec_gg').hide();
		    $('#polec_social').slideToggle('normal');
	        });
		
		$('#dodaj_opinie_button').click(function() {
		    $('#dodaj_opinie_button').slideToggle('normal');
		    $('#dodaj_opinie_form').slideToggle('normal');
	        });

		$("input[name=dodaj_ogloszenie]").click(function(){
			
			if (max_num_cat<$("input[rel=cbkat]:checked").length)
			{
				alert("Nie masz wystarczającej ilości punktów aby wybrać dodatkowe kategorie");
				return false;	
			}
			});
		
		$('div.komunikat_prio').show("fast",function(){
		
			if(window.addEventListener)
			{
			window.addEventListener("scroll", scroll, false);

			}
			else if (window.attachEvent)
			{
			   window.attachEvent('onscroll', scroll);
			}
		  
		  $('div.komunikat_prio_bg').css({opacity:0.6,height:$(document).height()}).fadeIn("slow");
		  $(this).css({top:0,left:($(document).width()-$(this).width())/2});
		  
		   $('div.komunikat_prio').animate({top:150+$(window).scrollTop()},300);	
		  	$(this).find('.komunikat_prio_submit').click(function(){
    			
    			$('div.komunikat_prio').fadeOut("fast");
    			$('div.komunikat_prio_bg').fadeOut("fast");
  			});
		  
		  });
		  
		  $("div.moje_ogloszenia").mouseover(function(){
				$(this).stop().animate({right:0},1000,function(){
				if ($(this).css("right")=="0px")
				$(this).find('div.top_bg_moje_ogloszenia').css("z-index",-1);		
				});
			}).mouseout(function() {
				$(this).stop().animate({right:-240},500,function(){
				if ($(this).css("right")=="-240px")
					$(this).find('div.top_bg_moje_ogloszenia').css("z-index",5);
				});
			});
			
		$('a.kontakt_show').click(function(){
					$(this).parent().next().slideToggle("normal");
					return false;
		});
		
		$('a.no-link').click(function(){
				$.post($(this).attr('href'),{"ajax":true});
				return false;
		});		
				
    }	    
);

function scroll()
{
	if ($('div.komunikat_prio').css('display')!="none")$('div.komunikat_prio').stop().animate({top:150+$(window).scrollTop()},300);
	if ($('div.komunikat_prio_small').css('display')!="none")$('div.komunikat_prio_small').stop().animate({top:150+$(window).scrollTop()},100);
}

function usun_termin(ogl)
{
  	if (confirm("Usunąć wybrany gorący termin?"))
  	{
  	  	if(document.form_usunTermin.name!=null)
  	  	{
       			document.form_usunTermin.usun.value=ogl;
       			document.form_usunTermin.submit();
		}
		else
		{
  	  		document.form_usunTermin[0].usun.value=ogl;
     			document.form_usunTermin[0].submit();
     		}
     		
   	}
}
function submit_search_form(f)
{
  	var form_name=f.name;
  	var f_n=$("form[name="+form_name+"]");
  	var samochod="samochod-dla-wszystkich";
  	//if (f_n.find("input[name='typ_1']").attr('checked')==false && f_n.find("input[name='typ_2']").attr('checked')==false)
	if ($("input[name=typ_1]").is(':checked')==false && $("input[name=typ_2]").is(':checked')==false)
  	{
     		alert("Błąd formularza - wybierz dla kogo szukasz samochodu");
     		return false;
   	}
   	
  	if ($("input[name=typ_1]").is(':checked')==false)
  	{
     		samochod="samochod-dla-gosci";
   	}
   	
   	if ($("input[name=typ_2]").is(':checked')==false)
  	{
     		samochod="samochod-dla-mlodej-pary";
   	}
   	
   	
   	var wartosc=$.trim(f_n.find("input[name='cena_max']").val());
  	if (wartosc!="")
  	{
  	  	var reg = /^\d*$/;
  	  	if(!reg.test(wartosc))
  	  	{
  	  	  	alert("Błąd w polu: Cena maks!");
  	  	  	f_n.find("input[name='cena_max']").focus();
        		return false;
      		}
      		
		samochod+=",do-"+wartosc;
   	}
   	
	wartosc=$.trim(f_n.find("input[name='kolor']").val());
   	if (wartosc!="" && wartosc.length>2)
  	{
		samochod+=",kolor-"+wartosc;
   	}
   	
   	wartosc=f_n.find("select[name='kategoria'] :selected").val();
	if (wartosc!=null)
  	{
		if (wartosc!="0") samochod+=",kategoria-"+f_n.find("select[name='kategoria'] :selected").text();
   	}
   	
   	wartosc=$.trim(f_n.find("input[name='adres']").val());
   	if (wartosc!="" && wartosc.length>2)
  	{
		samochod+=","+wartosc;
   	}
   	
   	wartosc=f_n.find("select[name='wojewodztwo'] :selected").val();
  	if (wartosc!=null)
  	{
		if (wartosc!="0") samochod+=",woj-"+f_n.find("select[name='wojewodztwo'] :selected").text();
   	}
   	
   	wartosc=f_n.find("select[name='marka'] :selected").val();
   	if (wartosc!=null)
  	{
		if (wartosc!="0") samochod+=","+f_n.find("select[name='marka'] :selected").text();
   	}
   	wartosc=$.trim(f_n.find("input[name='fraza']").val());
   	if (wartosc!="" && wartosc.length>2)
  	{
		samochod+=",fraza-"+wartosc;
   	}
  	var url="szukaj,"+samochod+".html";
  	window.location.href="http://www.slubne-auto.pl/"+url;
  	return false;
  	//return true;
}

window.onload = function()
{	
	$('div.main_body').height($('td.main_body_tab').height()-$('div.main_area_top').height()-$('div.main_area_top_path').height()+2+28-$('div.main_details').height()-$('div.main_area_bottom_details').outerHeight(true));
}




	

