$(document).ready(function(){

//кнопочки добавить/удалить производителя	
	$('.selects').hide();
	$('.showselect').hide();
	$('.hideselect').hide();
	$('.showselect:first').show();
	$('.selects:first').show();
	$('.showselect').click(function(){
		$('#select'+($(this).attr('id').slice(10)-1+2)+'').show();
		$(this).hide();
		$('#hideselect'+$(this).attr('id').slice(10)+'').hide();
		$('#showselect'+($(this).attr('id').slice(10)-1+2)+'').show();
		$('#hideselect'+($(this).attr('id').slice(10)-1+2)+'').show();
	})
	$('.hideselect').click(function(){
		$('#select'+$(this).attr('id').slice(10)+' option:last').attr('selected','selected');
		$('#select'+$(this).attr('id').slice(10)+'').hide();
		$(this).hide();
		$('#showselect'+$(this).attr('id').slice(10)+'').hide();
		$('#showselect'+($(this).attr('id').slice(10)-1)+'').show();
		$('#hideselect'+($(this).attr('id').slice(10)-1)+'').show();
	})
	$('.selects').each(function(i,elem){
//			 var num=0;
			 i++;
//			 for(x=0;x<i;x++)
//				 {
//				 	if($('#select'+i+' option:selected').val()==$('#select'+x+' option:selected').val())
//				 		num++;
//				 }
//			 if( num==0)
			 if($('#select'+i+' :last').attr("selected")!=true)
				 {
				 	$('#select'+i).show();
				 	$('.showselect').hide();
				 	$('.hideselect').hide();
				 	$('#showselect'+i).show();
				 	$('#hideselect'+i).show();
				 	$('#memo').text($('#memo').text()+" i="+i+" val="+$('#select'+i+' option:selected').val());
				 }
			 if($('#select'+i).is(':hidden'))
				 {	 	
				 	$('#select'+i+' :last').attr("selected", "selected");
				 }
			 });
	
	
//получение урла
	 $('#submitselector').click(function() {
		 var fields = $("#selector").serializeArray();
		 var request_uri = location.href.substring(0,location.href.lastIndexOf("/"));
		 var cat = "";
		 $('.selects').each(function(i,elem){
			 var j=i+1;
			 if($('#select'+j+' :last').attr("selected")!=true)
				 {
					 if(cat!="")
					 cat += ",";
					 cat += $('#select'+j+' option:selected').val();
				 }
			 });
//		 var get = "";
//		 if(location.href.indexOf("?")!=-1)
//			 {
//			 	get="&";
//			 	if(location.href.indexOf("?submitselector")!=-1)
//			 		get="?";
//			 }
//		 else
//		 	get="?";
		 var get = "?submitselector=true";
		 if(cat=="")
		 cat=$('#select1 option:selected').val();
		 get+="&cat="+cat;
		 get+="&categoryID="+$('#select1 option:selected').val();
		 $("#memo").empty();
//		 if(location.href.indexOf("submitselector")!=-1)
		 var site = location.href.substring(0,location.href.lastIndexOf("/"));
//		 else
//			 var site = location.href;
		 jQuery.each(fields, function(i, field){
		 if(field.value)
		 if(field.value!="не важно")
		 if(field.name!="category")	
			 get += "&"+field.name+"="+field.value;
		 location.href = site+get;
		 });
	 });
	 
	 $('#name_asc').mouseover(function()
			 {
		 		var url = location.href;
		 		if(location.href.indexOf("&sort")!=-1)
		 			url = location.href.substring(0,location.href.indexOf("&sort"));
		 		url+="&sort=name&direction=ASC";
		 		$('#name_asc').attr('href',url);
			 });
	 $('#name_desc').mouseover(function()
			 {
		 		var url = location.href;
		 		if(location.href.indexOf("&sort")!=-1)
		 			url = location.href.substring(0,location.href.indexOf("&sort"));
		 		url+="&sort=name&direction=DESC";
		 		$('#name_desc').attr('href',url);
			 });
	 $('#price_asc').mouseover(function()
			 {
		 		var url = location.href;
		 		if(location.href.indexOf("&sort")!=-1)
		 			url = location.href.substring(0,location.href.indexOf("&sort"));
		 		url+="&sort=Price&direction=ASC";
		 		$('#price_asc').attr('href',url);
			 });
	 $('#price_desc').mouseover(function()
			 {
		 		var url = location.href;
		 		if(location.href.indexOf("&sort")!=-1)
		 			url = location.href.substring(0,location.href.indexOf("&sort"));
		 		url+="&sort=Price&direction=DESC";
		 		$('#price_desc').attr('href',url);
			 });
	 $('#customers_asc').mouseover(function()
			 {
		 		var url = location.href;
		 		if(location.href.indexOf("&sort")!=-1)
		 			url = location.href.substring(0,location.href.indexOf("&sort"));
		 		url+="&sort=customers_rating&direction=ASC";
		 		$('#customers_asc').attr('href',url);
			 });
	 $('#customers_desc').mouseover(function()
			 {
		 		var url = location.href;
		 		if(location.href.indexOf("&sort")!=-1)
		 			url = location.href.substring(0,location.href.indexOf("&sort"));
		 		url+="&sort=customers_rating&direction=ASC";
		 		$('#customers_desc').attr('href',url);
			 });
});
