
var element;  //marka
var element2; //model
var fix; // nazwa ogloszenia w adresie marka
var fix2; // nazwa ogloszenia w adresie model
var buf; // buforek

$(document).ready(function() {
select_typ(typ);
var ajax_load='<img src="szata/img/load.gif" alt="" />';
$('#pokazbutton a').click(function(){
	var len=$(this).attr("href");
	if(len.length==19)	
            alert("Wybierz markę i/lub model !");		
        });

    reinitialiseScrollPane1=function() {
	if($("#pane1").is("div"))
		$("#pane1").jScrollPane({showArrows:true,scrollbarWidth:17,animateTo:false});};
    reinitialiseScrollPane2=function() {
	if($("#pane2").is("div")) {
		    $("#pane2").jScrollPane({showArrows:true,scrollbarWidth:17,animateTo:false});
		    $("#pane2")[0].scrollTo(0);
		}
	};
    czysc =function(){$("#pane1 p").attr("class","n_click");};
    czysc2=function(){$("#pane2 p").attr("class","n_click");};

    panel1bindy=function(){
		$("#pane1 p").bind("click",
			function() {
				$this=$(this);
				if(element!=$(this).attr("id"))	{
					$("#pane2 p").unbind("click");
					element=$(this).attr("id");
					buf=$(this).text().replace(" ",'').split('(');
					fix=buf[0];
					$(czysc);
					$(this).attr("class","m_click");
					$("#panel2").empty();
					var items = [];
					$.ajax({type:"GET",url:"ajax_szukacz.php",dataType: 'json',
							data:({typ_og:[typ],marka:[element.split('_')[1]],woj:[woj]}),
							beforeSend:function() {
							$("#pane2").html(ajax_load);
							$(panel2bindy);
							$(reinitialiseScrollPane2);
							}, success:function(data){
								$.each(data, function(key, val) {
									  items.push('<p id="mr_' +val.id+ '">' + val.nazwa+' (<b>'+val['w'+woj]+'</b>)</p>');
									  });
								$('#pane2').html(items.join(''));
								$(reinitialiseScrollPane2);
								$(panel2bindy);
							    } 
							});
                                        if(typ=='o') href='/samochod-osobowy';
                                        if(typ=='c') href='/samochod-ciezarowy';
                                        if(typ=='m') href='/motocykl-motorower';
					$('#pokazbutton a').attr("href",href+'?marka='+element.split('_')[1]+"&woj="+woj);
					}
				return false;
				});
		};
		
        panel2bindy=function(){
		$("#pane2 p").bind("click",
				function() {
				$this=$(this);
				if(element2!=$(this).attr("id")) {
					element2=$(this).attr("id");	
					buf=$(this).text().replace(" ",'').split('(');
					fix2=buf[0];
					$(czysc2);
					$(this).attr("class","m_click");
                                        if(typ=='o') href='/samochod-osobowy';
                                        if(typ=='c') href='/samochod-ciezarowy';
                                        if(typ=='m') href='/motocykl-motorower';
                                        $('#pokazbutton a').attr("href",href+'?marka='+element.split('_')[1]+"&model="+element2.split('_')[1]+"&woj="+woj);
					//$('#pokazbutton a').attr("href","ogloszenia,"+fix+"_"+fix2+","+element.split('_')[1]+","++","+typ+",0"+","+woj);
					}
				return true;
				});
		};
	$(reinitialiseScrollPane1);
	$(reinitialiseScrollPane2);
	$(panel1bindy);
	$(panel2bindy);

	$("#men_szuk li").bind("mouseover",function()
			{
			$this=$(this);
			if(typ!=$(this).find("a").attr("name"))	{
				$(this).attr("class","");
				$(this).attr("class","li_wybrany");
				}
			});
	$("#men_szuk li").bind("mouseout",function()
			{
			$this=$(this);
			if(typ!=$(this).find("a").attr("name"))	{
				$(this).attr("class","");
				}
			});
	if($("#my-dropdown ").is("select")) {
		$("#my-dropdown").change(function(){
			woj=$(this).val();
			$("#pane1 p").unbind("click");
			$("#pane2 p").unbind("click");
			$("#pane2").empty();
			$("#pane1").empty();
			var items = [];
			
			$.ajax({type:"GET",url:"ajax_szukacz.php",
				data:({typ_og:[typ],woj:[woj]}),dataType: 'json',
				beforeSend:function() {
					$("#pane1").html(ajax_load);
					$(panel1bindy);
					$(reinitialiseScrollPane1);
					},
					success:function(data) {
						element="zero";
						$('#pokazbutton a').attr("href","javascript:void(0);");
						  $.each(data,function(key,val) {
						    items.push('<p id="mr_' +val.id+ '">' + val.nazwa+' (<b>'+val['w'+woj]+'</b>)</li>');
						  });
						$('#pane1').html(items.join(''));
						$(panel1bindy);
						$(reinitialiseScrollPane1);
						$(reinitialiseScrollPane2);
					}
				});
			});
	}
});
