jQuery.noConflict();

	function get_checked_and_submit(){
		checked_cars=jQuery("input[name^='car']:checked").length;
		jQuery("form[name='car_table_form']").submit();	
	}
	
	function SortSearchResult(e_class) {
		var s = "asc";
		if (jQuery("th."+e_class).hasClass("asc")) {
			s = "desc";
		}
		jQuery("#car_table_header>th").removeClass("asc");
		jQuery("#car_table_header>th").removeClass("desc");
//		alert(jQuery("th."+e_class).attr("class"));
		submit_search(jQuery("span.current_page").html(),jQuery("th."+e_class).attr("class"));
		jQuery("th."+e_class).addClass(s);
	}


jQuery(document).ready(function(){
	jQuery('#search_link').css('overflow', 'hidden');

	jQuery("#main_img_link_1,#main_img_link_11").click(function(e){window.location.href='/cat/goods/spec/'});
	jQuery("#main_img_link_2,#main_img_link_22").click(function(e){window.location.href='/cat/goods/new/'});
	
	jQuery('#check_all').change(function(){
		jQuery(':check_pos').attr('checked',true);
		//alert(this.checked);
		//jQuery('#check_all').attr('checked',!jQuery('#check_all').attr('checked'));
	});
	
	if (jQuery("#spec").val() == 1 || jQuery("#new").val() == 1|| jQuery("#all").val() == 1 && !search_now) {
		submit_search(1);
	}
	
	jQuery(".anyClass").jCarouselLite({
        btnNext: ".next",
        btnPrev: ".prev"
    });

	/*{
		//sortColumn: 'name',			// Integer or String of the name of the column to sort by.
		sortClassAsc: 'headerSortUp',		// class name for ascending sorting action to header
		sortClassDesc: 'headerSortDown',	// class name for descending sorting action to header
		headerClass: 'header'			// class name for headers (th's)
	});
*/


	/*jQuery("#model").attr('disabled','true');
	jQuery("#model").css('background-color','#e2dcd9');*/
	
	
	if (jQuery.isFunction(jQuery.fn.jCarouselLite) && jQuery("#promo_container")) {
		jQuery("#promo_carousel").jCarouselLite({
	    	btnNext: "#promo_carousel_right",
	        btnPrev: "#promo_carousel_left",
	        circular: false
	    });	
	}
	
	jQuery("#promo_carousel_right, #promo_carousel_left").click(function() { return false; })
	
	
	jQuery("#menu_top li").hover(
		  function () {
			toggle_bg(jQuery(this));
		  }, 
		  function () {
			toggle_bg(jQuery(this));
		  }
		);

	function toggle_bg(OBJ){
	  OBJ.find(".left_bg").toggleClass("top_menu_left_bg");
	  OBJ.find(".right_bg").toggleClass("top_menu_right_bg");
	  OBJ.find('a:first').toggleClass('hover');
	  OBJ.find("ul").slideToggle(1);
	}
	
	
	/* для раздела Карточка товара */
	jQuery(".compare_link").click(function(e){
		if (jQuery(this).attr("class")!="clicked"){
			  e.preventDefault();
			  link=jQuery(this);	 
			  jQuery.get("/js/_ajax/filter/compare.php", { car_id: link.attr("rel")},
			  function(data){
					if (data>1) {
						  link.attr("href","/js/_ajax/filter/compare_vis.php").html("Сравнить модели").attr("class","clicked compare_cars");
						  compare_cars();
						  return;
					}
					else{
						  link.attr("href","/cat/goods").html("Добавить другую модель").attr("class","clicked");
					}
			  });
		  }
	});
	function compare_cars(){
	jQuery(".compare_cars").click(function(e){
	  e.preventDefault();
	  popupWin = window.open(jQuery(this).attr("href"), '', 'location=0,scrollbars=1,resizable=1,width=610,height=500,top=0');
	  //window.open('', 'popup');
	  //popupWin.focus();
	});
	}
							
	/* для раздела Карточка товара и Просмотр выбранных машин */		
			
			
	jQuery(".photo").click(function(e){
		e.preventDefault();
		big_photo = jQuery(this).parent().parent().find("img.car_photo")
		new_thumb_src=big_photo.attr("src");
		 var reg=/([^\/]+)$/;
			new_thumb_src=new_thumb_src.replace(reg,"thumbs/$1");
		thislink=jQuery(this);
		new_big_photo_src=jQuery(this).attr("href");	

		jQuery(this).attr("href",big_photo.attr("src"));
		big_photo.fadeOut("slow", function(){
				big_photo.attr("src",new_big_photo_src);
				big_photo.fadeIn("slow");
				thislink.children().attr("src",new_thumb_src);
		});
	 });
	
	jQuery.each(jQuery(".photo"), function(){
		jQuery.get(this.href); //preloading all big photos;		 
	});

	jQuery("input:reset").click(function(){
		jQuery(".el_search_form").val('');
		jQuery(".el_search_form").find("option").removeAttr("selected");
		jQuery("#model>*").remove();
		jQuery(".el_search_form").find("option:first").attr("selected","selected");
//		jQuery(".search_form").selectedIndex = 1;
//		jQuery(".search_form").val('');
	});
	
	
	/* для раздела Каталог */
	jQuery("#mark").change(function () {
        var str = "";		  
		if (jQuery("#mark").val() == 0)  
		{
			jQuery("#model").html("");
		}
		else
			jQuery("#mark option:selected").each(function () 
			{
				//mark=jQuery(this).text();
				var mark=jQuery("#mark").val();			
				jQuery.getJSON("/js/_ajax/filter/get_models.php",{mark: mark},
				function(data)
				{
					models='';
					  models+='<option value="all">Любой</option>';
					  jQuery.each(data, function(i,item){
						models+='<option value="'+item+'"'+(sel_model==item ? ' selected' : '')+'>'+item+'</option>';
					  });
					  jQuery("#model").html(models);
					  if (search_now) {
						  	search_now = false;
						  	submit_search(1);
					  }
					});
				
              });
        })
        .change();
	
	jQuery("form[name='search']").submit(function () {
						submit_search(1);
						document.location = "#results";
						return false; });
	jQuery("#show_selected_link").live('click',function(e){
						e.preventDefault();
						get_checked_and_submit();
						});
	jQuery("#show_all_link").live('click',function(e){
						e.preventDefault();
						jQuery("input[name^='car']").attr("checked","ckecked");
						get_checked_and_submit();
						});
	
	/* для раздела Каталог */
	jQuery('#car_table tr.car_row').hover(function(){
								jQuery(this).next('tr.addition_row').show();					 
													 },
								function(){
								jQuery(this).next('tr.addition_row').hide();				 
													 }
													 );
	jQuery('#car_table tr.addition_row').hover(function(){
								jQuery(this).show();					 
													 },
								function(){
								jQuery(this).hide();				 
													 }
													 );		
	
	/* для основных трёх блоков с меню */
	jQuery("#menu_main_1").hover(function(){
								jQuery(this).find(".menu_main_list_full").fadeIn("fast");					 
													 },
								function(){
								jQuery(this).find(".menu_main_list_full").fadeOut("fast");
													 }
													 );

	jQuery("#menu_main_2").hover(function(){
								jQuery(this).find(".menu_main_list_full2").fadeIn("fast");
													 },
								function(){
								jQuery(this).find(".menu_main_list_full2").fadeOut("fast");
													 }
													 );

	jQuery("#menu_main_3").hover(function(){
								jQuery(this).find(".menu_main_list_full3").fadeIn("fast");
													 },
								function(){
								jQuery(this).find(".menu_main_list_full3").fadeOut("fast");
													 }
													 );
	
	/* кнопка печати */
	jQuery('.printit').click(function(e)
	{
		e.preventDefault();
		window.print();		
		//printit();
	});
	
});

 var first_submit_search=false;
 function page_link(pages_number,current_page){
	jQuery("#search_results").show();
	jQuery(".page_link").click(function(e){
		e.preventDefault();
		submit_search(jQuery(this).attr("rel"));
		pages_number=pages_number-0;
		
		if ((jQuery(this).attr("rel")-0)<pages_number)
		{ jQuery("#next_page").attr("rel",jQuery(this).html()-0+1); }
		else
		{ jQuery("#next_page").attr("rel",jQuery(this).html()-0); }
	});
	
//	if (first_submit_search==false) {
		jQuery("#next_page").click(function(e){
			e.preventDefault();
			submit_search(jQuery(this).attr("rel"));
		});
//	}
	
	if (current_page < pages_number)
		jQuery("#next_page").attr("rel",current_page-0+1);
	
	first_submit_search=true;
	
	if (pages_number<=1) jQuery("#pages").hide();
 }
	
 
 function submit_search(page,sort_field){
 	Progressbox("Осуществляется поиск");
	
	if (sort_field==undefined) sort_field = '';
	 
 	var req = new JsHttpRequest();
	req.open(null, '/js/_ajax/filter/search.php', true);
	var qString = jQuery("#search").formSerialize();
	req.send({query:qString+'&sort='+sort_field+'&current_page='+page});
	req.onreadystatechange = function() {
		if (req.readyState == 4) {
	  		//$('request_form').innerHTML = req.responseJS.html;
				data = req.responseJS;

				var link_to_this_page = data.qstr ? data.qstr : "";
				
				jQuery('#generate').unbind('click');
				jQuery('#link').val('');
				jQuery('#link').attr('disabled','true');		  
				
				jQuery('#bt_link').click(function() {
					jQuery('#link').val(link_to_this_page);
					jQuery('#link').removeAttr('disabled');
					jQuery('#link').select();
				});
				//jQuery('#generate').click(function() {copyToClipboard(link_to_this_page)});

				jQuery("div#search_results").html(data.html);
				
				if (data.count) {
					links="";
					first_visible_page=1;
					last_visible_page=data.pages_number<=9 ? data.pages_number : 9;
					
					if (data.current_page>7) //>4
					{
					  //first_visible_page=data.current_page-0-4; 
					  //last_visible_page=data.current_page-0+4;
					  if (data.pages_number<=last_visible_page)
					  {
						  last_visible_page=data.pages_number;
						  first_visible_page=data.pages_number-0-7;
					  } 
					}
					
					for (i=first_visible_page; i<=last_visible_page; i++)
					{
					page = i;
					page = ( (i==last_visible_page && (!(last_visible_page>=(data.pages_number-0-4)))) || (i==last_visible_page && (data.pages_number>=7 ||  data.pages_number!=last_visible_page)) ) ? ".." : i ;
					link1 = i==data.current_page-0 ? "<span class='current_page'>"+i+"</span> " : "<a href='#' class='page_link' rel="+i+">"+page+"</a> ";
					links+=link1;
					}
					if (data.pages_number>1) jQuery("#pages span").html(links);
					page_link(data.pages_number,data.current_page);
				}
//			alert(req.responseJS);
		}
	}

 
 
/* 
		price_from=jQuery("#price_from").val();
		price_to=jQuery("#price_to").val();
		year_from=jQuery("#year_from").val();
		year_to=jQuery("#year_to").val();
		engine_volume=jQuery("#engine_volume").val();
		engine_type=jQuery("#engine_type").val();
		color=jQuery("#color").val();
		type=jQuery("#type").val();
		mark=jQuery("#mark").val();
		model=jQuery("#model").val();*/
		
		/*
		kpp=jQuery("#kpp").val();
		warranty=jQuery("#warranty").val();
		gear=jQuery("#gear").val();
		kilometres=jQuery("#kilometres").val();
		*/
		
		//alert(qString);
		
//		jQuery.getJSON(jQuery("#search").attr("action")+'?'+qString+'&sort='+sort_field,{current_page:page},function(data){
																													 /*
		jQuery.ajax({
			type: 'POST',
			url: jQuery("#search").attr("action"),
			data: qString+'&sort='+sort_field+'&current_page='+page,
			success: function(data) {

			});
		}
		
	});
		/*
		
			  model_rows="";
			  var counter = 0;

			 
			  
			  
			  
			  
			  
			  
			  if (data.count==0)
				{
					jQuery("#search_results").hide();
					jQuery("#request_form").show();
//					load_request_form();
					return
				} else jQuery("#request_form").hide();

			  jQuery.each(data.cars, function(i,car){							  
				model_rows+='<tr><td class="check" width=30><input type="checkbox" name="car['+car.id+']" class="check_pos" value="'+car.id+'"/></td><td class="mark" width=94><a class="preview" href="/'+car.altname+'/goods/'+car.id+'/" rel="/upload/catalog/7/thumbs/'+car.photo+'">'+car.model+' '+car.mark+'</a></td><td class="engine" width=77>'+car.engine+'</td><td class="kpp">'+car.kpp+'</td><td class="type">'+car.type+'</td><td class="year">'+car.year+'</td><td class="kilometres">'+car.kilometres+'</td><td class="gear">'+car.gear+'</td><td class="color">'+car.color+'</td><td class="price">'+car.price+'</td></tr>';
			  });
			  
			  jQuery("table#car_table_rows").html(model_rows);
			  make_tooltip();
			  
			
		});
*/

		//jQuery("input:find").click();
		ProgressboxClose();
		//document.location = "#results";
		return false;
	}
	
//   
<!--
var browser_name = navigator.appName;
function printit() {
	if (browser_name == "Netscape") {
		window.print();
	} else {
		var WebBrowser = '<object id="WebBrowser1" width=0 height=0 classid="clsid:8856F961-340A-11D0-A96B-00C04FD705A2"></object>';
		document.body.insertAdjacentHTML('beforeEnd', WebBrowser);
		WebBrowser1.ExecWB(6, 2);
	}
}  

function copyToClipboard(text) {
  {  /*
      if(window.clipboardData)  
      {  
      window.clipboardData.setData('text',text);  
      }  
      else  
      {  
          var clipboarddiv=document.getElementById('divclipboardswf');  
      if(clipboarddiv==null)  
      {  
         clipboarddiv=document.createElement('div');  
             clipboarddiv.setAttribute("name", "divclipboardswf");  
         clipboarddiv.setAttribute("id", "divclipboardswf");  
         document.body.appendChild(clipboarddiv);  
      }  
          clipboarddiv.innerHTML='<embed src="clipboard.swf" FlashVars="clipboard='+  
  encodeURIComponent(text)+'" width="0" height="0" type="application/x-shockwave-flash"></embed>';  
      }  
      return false;  
	  */
  }
}
