var flg=0;
$.post('/',{ajax: "show_results"},show_res);

function send_vote(v_cnt)
	{
	l=-1;
	for (i=0; i<v_cnt; i++)
		{
		if ($("#v"+i).attr("checked")==true) l=i;
		}
	if (l==-1) alert("Не выбран ни один из вариантов!");
	else
		{
		$.post('/',{ajax: "get_vote", opros: $("#v"+l).attr("value")},res);
		}
	}
	
function res(ans)
	{
	//alert(ans);
	if (ans=="ok")
		{
		$("#result_box").html('<span style="color:green; font-style:Italic;">Ваш голос принят. Спасибо за голосование!</span>');
		flg=1;
		$.post('/',{ajax: "show_results"},show_res);
		}
	else if (ans=="error1")
		$("#result_box").html('<span style="color:red;">С этого компьютера уже голосовали!</span>');
	else $("#result_box").html('<span style="color:red;">Не удалось добавить голос по неизвестной причине!</span>');
	}
	
function show_res(html)
	{
	$("#show_result").html(html);
	if (flg==1) show_results()
	}


function show_or_hide_results()
	{
	//alert("");
	if ($("#show_or_hide_results").html()=="Показать результаты") show_results(); else hide_results();
	}

function show_results()
	{
	$("#show_or_hide_results").html("Скрыть результаты");
	$("#show_result").css("display","");
	}
	
function hide_results()
	{
	$("#show_or_hide_results").html("Показать результаты");
	$("#show_result").css("display","none");
	}
	
function get_mebel(pg)
	{
	//alert("page_n = "+$("#page_n").attr("value"));
	//alert("до "+pg);
	if (pg=="empty") {pg=$("#page_n").attr("value"); /*alert("Выполнилось!");*/}
	//alert("после"+pg);
	$.post('/',{ajax: "get_mebel", CatID: $("#CatID").attr("value") ,NumPerPage:"7",page: pg,order:$("#sort_box").attr("value"),min_price:$("#min_price").attr("value"),max_price:$("#max_price").attr("value")},mebel);
	}
	
function mebel(ans)
	{
		if (ans!="0") $("#mebel_info").html(ans); else $("#mebel_info").html('<span style="color: red; font-weight: Bold">По вашему запросу ничего не найдено!</span>');
	}
	
function swtch_sort(param)
	{
	if (param=="1" || param=="")
		{
		//сортировка по цене
		$("#sort").html('<span style="color:#ffffff; background-color:#b81800;">&nbsp; цене &nbsp; </span>&nbsp;&nbsp; <a href="#" style="text-decoration: underline" onclick="swtch_sort(2)">названию</a> &nbsp;&nbsp; <a href="#" style="text-decoration: underline"  onclick="swtch_sort(3)">новизне</a>');
		$("#sort_box").attr("value","new_price");
		get_mebel("empty");
		}
	else if (param=="2")
		{
		//сортировка по названию
		$("#sort").html('<a href="#" style="text-decoration: underline" onclick="swtch_sort(1)">цене</a>&nbsp; &nbsp;&nbsp; <span style="color:#ffffff; background-color:#b81800;">&nbsp;названию</span> &nbsp;&nbsp; <a href="#" style="text-decoration: underline"  onclick="swtch_sort(3)">новизне</a>');
		$("#sort_box").attr("value","title");
		get_mebel("empty");
		}
	
	else 
		{
		//сортировка по новизне
		$("#sort").html('<a href="#" style="text-decoration: underline" onclick="swtch_sort(1)">цене</a>&nbsp; &nbsp;&nbsp; &nbsp;<a href="#" style="text-decoration: underline"  onclick="swtch_sort(2)">названию</a> &nbsp;&nbsp; <span style="color:#ffffff; background-color:#b81800;">новизне</span>');
		$("#sort_box").attr("value"," - d_date");
		get_mebel("empty");
		}
	}
	
	function get_cats()
		{
		$.post('/',{ajax: "get_obivka",target:"cats"},cats);
		}
	
	function cats(ans)
		{
		$("#cat_tel").html(ans);
		}
		
	function get_obivka_content(type_obivka,tkan_obivka)
		{
		$.post('/',{ajax: "get_obivka",type:type_obivka,tkan:tkan_obivka},obivka_content);
		}
	
	function obivka_content(ans)
		{
		$("#obivka_content").html(ans);
		}

	function choose_cathegory()
		{
		//alert("nen");
		$("#cat_tel>option").each(
  			function()
  				{
    			// внутри функции this указывает на текущий DOM-элемент
    			if ($(this).attr("selected")==true) cat_id=$(this).attr("value");
 				 }
				);
		$("#mat_tel").attr("disabled",false);
		get_obivka_content(cat_id,"all");
		if (cat_id!="all") get_sub_cats(cat_id); else {$("#mat_tel").attr("disabled",true),$("#mat_tel").html('<option>Все</option>')}
		}
		
	function get_sub_cats(cat_id)
		{
		//alert(cat_id);
		$.post('/',{ajax: "get_obivka",target:"sub_cats",cat_type: cat_id},sub_cats);
		}
		
	function sub_cats(ans)
		{
		$("#mat_tel").html(ans);
		}
		
	function choose_sub_cathegory()
		{
		//alert("nen");
		$("#cat_tel>option").each(
  			function()
  				{
    			// внутри функции this указывает на текущий DOM-элемент
    			if ($(this).attr("selected")==true) cat_id=$(this).attr("value");
 				 }
				);

		$("#mat_tel>option").each(
  			function()
  				{
    			// внутри функции this указывает на текущий DOM-элемент
    			if ($(this).attr("selected")==true) sub_cat_id=$(this).attr("value");
 				 }
				);
		get_obivka_content(cat_id,sub_cat_id);
		}

	function get_shops()
		{
		$("#select_city>option").each(
  			function()
  				{
    			// внутри функции this указывает на текущий DOM-элемент
    			if ($(this).attr("selected")==true) city=$(this).attr("value");
 				 }
				);
		$.post('/',{ajax: "get_shops",shop_city:city},shops);
		}
		
	function shops(ans)
		{
		$("#shops_box").html(ans);
		}
		

	function swtch__to(p1,p2,p3)
		{
		if (p1==1)
			{
			//alert($("#schm_"+p2).css("display"));
			if ($("#schm_"+p2).css("display")!="none") $("#schm_"+p2).css("display","none"); else $("#schm_"+p2).css("display","");
			$("#fotos_"+p2).css("display","none");
			if ($("#schm_"+p2).css("display")=="none") $("#box1_"+p2).html(""); else $("#box1_"+p2).html("&darr;");
			$("#box2_"+p2).html("");
			//alert($("#schm_"+p2).html());
			if ($("#schm_"+p2).html()=="") get_schem(p3,"#schm_"+p2);
			}
		else 
			{
			if ($("#fotos_"+p2).css("display")!="none") $("#fotos_"+p2).css("display","none"); else $("#fotos_"+p2).css("display","");
			$("#schm_"+p2).css("display","none");
			if ($("#fotos_"+p2).css("display")=="none") $("#box2_"+p2).html(""); else $("#box2_"+p2).html("&darr;");
			$("#box1_"+p2).html("");
			}

	
	}


	function get_schem(id,box)
		{
		$("#tmp").attr("value",box);
		$.post('/',{ajax: "get_schem",schm: id},schem);
		}

	function schem(ans)
		{
		$($("#tmp").attr("value")).html(ans);
		$("#tmp").attr("value","");
		}
	
	function get_price()
		{
		$("#select_meb>option").each(
  			function()
  				{
    			// внутри функции this указывает на текущий DOM-элемент
    			if ($(this).attr("selected")==true) meb=$(this).attr("value");
 				 }
				);

		$("#select_tkan>option").each(
  			function()
  				{
    			// внутри функции this указывает на текущий DOM-элемент
    			if ($(this).attr("selected")==true) tkan=$(this).attr("value");
 				 }
				);



//alert($("#select_meb").html());

//alert(meb);
		$.post('/',{ajax: "get_price",target: "get_table",meb:meb,tkan:tkan},prices);
		}
		
	function prices(ans)
		{
		$("#price_box").html(ans);
		}
		
		
	function get_meb_tit()
		{
		$.post('/?divan_name='+$("#divan_name").attr("value"),{ajax: "get_price"},meb_tit_res);
		}
		
	function meb_tit_res(ans)
		{
		$("#select_meb").html(ans);
		get_price();
		}


	function put_banner(html)
		{
		$("#banner_box").html(html);
		}

	 function gallery_show_full_info(id_foto)
		{
		$("#und").attr("height",document.body.scrollHeight);
		$("#und").css("display","");
		$("#gallery_full_info").css("top",getClientCenterY()+"px");
		$("#gallery_full_info").css("left",getClientCenterX()+"px");
		$.post('/',{ajax: "get_foto",foto_id:id_foto},put_foto);
		}

	function put_foto(html)
		{
		//alert(html);
		$("#gallery_full_info").html(html);
		$("#gallery_full_info").css("display","");
		}


	function getClientCenterX()
		{
   	 	return parseInt(document.body.clientWidth/3)+getBodyScrollLeft();
		}

	function getClientCenterY()
		{
   		 return parseInt(document.body.clientHeight/4)+getBodyScrollTop();
		}

	function getBodyScrollTop()
		{
		return self.pageYOffset || (document.documentElement && document.documentElement.scrollTop) || (document.body && document.body.scrollTop);
		}

	function getBodyScrollLeft()
		{
		return self.pageXOffset || (document.documentElement && document.documentElement.scrollLeft) || (document.body && document.body.scrollLeft);
		}


 	function tnoff()
		{
		$("#und").css("display","none");
		$("#gallery_full_info").css("display","none");
		}		
		
		
jQuery.fn.extend({
	everyTime: function(interval, label, fn, times, belay) {
		return this.each(function() {
			jQuery.timer.add(this, interval, label, fn, times, belay);
		});
	},
	oneTime: function(interval, label, fn) {
		return this.each(function() {
			jQuery.timer.add(this, interval, label, fn, 1);
		});
	},
	stopTime: function(label, fn) {
		return this.each(function() {
			jQuery.timer.remove(this, label, fn);
		});
	}
});

