$(function(){

	/************AJAX******************/
		get_present = function(){
			$.get('/present_now.php', {}, function(data){
				content_page = $(data).find("div.content").html();
				$("div.page_box_lb div.fl").html(content_page);
				$("div.page_box_lb").find("div.nav_line").remove();
				$("div.page_box_lb").find("h1.page_titler").remove();
				$("div.page_box_lb").css("display","block");
				
				$("div.close_pop_up").click(closePage);
			});
		}
		vip = function(){
			$.get('/vip.php', {}, function(data){
				content_page = $(data).find("div.content").html();
				$("div.page_box_lb div.fl").html(content_page);
				$("div.page_box_lb").find("div.nav_line").remove();
				$("div.page_box_lb").find("h1.page_titler").remove();
				$("div.page_box_lb").css("display","block");
				
				$("div.close_pop_up").click(closePage);
			});
		}
		party = function(){
			$.get('/event.php', {}, function(data){
				content_page = $(data).find("div.content").html();
				$("div.page_box_lb div.fl").html(content_page);
				$("div.page_box_lb").find("div.nav_line").remove();
				$("div.page_box_lb").find("h1.page_titler").remove();
				$("div.page_box_lb").css("display","block");
				
				$("div.close_pop_up").click(closePage);
			});
		}
		closePage = function(){
			$("div.page_box_lb").css("display","none");
		}
		goToBacket = function(){
			location.href = "/personal/cart/";
		}
		goToCat = function(){
			location.href = "";
		}
		addToCart = function(){
			img = "/bitrix/templates/darim_copy_copy/" + $(this).attr("alt");
			$(this).css("background","url('" + img + "') no-repeat center center");
			url = $(this).attr("link");
			$.get(url, {}, function(data){
				content_cart = $(data).find("div.box_fo_cart").html();
				//alert(content_cart);
				$("div.box_fo_cart").html(content_cart);
				$("div.box_fo_cart").css("display","block");
				
				$("div.box_fo_cart div.bb_1").click(goToBacket);
				$("div.box_fo_cart div.bb_2").click(goToCat);
				$("div.bi input").css("background","url('/bitrix/templates/darim_copy_copy/images/bs_ico.png') no-repeat center center");	
			});
			
		}
		$("div.bi input").click(addToCart);
		//alert($("div.bi input").length);
	/************AJAX******************/		

	
	/************TEL_ORDER******************/
	GetTel = function(){
		$(this).parent().animate({"width": "246px"}, 300);
		$("div.order_call_wrapper").css("zIndex", "15");
	}
	CloseTel = function(){
		$(this).parent().animate({"width": "39px"}, 300);
		$("div.order_call_wrapper").css("zIndex", "5");
	}
	$("div.click_item").toggle(GetTel, CloseTel);

	/************TEL_ORDER******************/

	/***************FILTER**************/
	getVal = function(){
		textS = $(this).text();
		$(this).parents(".sl_2").find("div").removeClass("selected_d");
		$(this).parent().addClass("selected_d");
		valS = $(this).attr("id");
		//console.log(valS);
		$(this).parents(".sl_2").prev().find("select").val(valS);
		//var i=$(this).parents(".sl_2").prev().find("select").val();
		//console.log(i);
		$(this).parents(".sl_2").find("div.select_box_top").find("p").text(textS);
	}
	initValF = function(cl_g){
		id = $("select[name='arrFilter_pf[" + cl_g + "]']").val();
		if(id != ""){
			$("td." + cl_g).find("div").removeClass("selected_d");
			$("td." + cl_g).find("#" + id).parent().addClass("selected_d");
		}
	}
	initValF("C_DAY");
	initValF("C_WHOIS");
	$("div.sl_2").find("div.select_box_list").find("p").click(getVal);
	
	/***************FILTER**************/

	/**********nav_string**************/
	cat_filterGP = function(){
		dPrice = $(this).find("p").attr("id");
		indexS = dPrice.indexOf("-");
		f_price = dPrice.substring(0,indexS);
		l_price = dPrice.substring(indexS+1);
		//alert($("input[name='arrFilter_cf[5][LEFT]']").length);
		$("form[name='arrFilter_form']").find("input[name='arrFilter_cf[5][LEFT]']").val(f_price);
		$("form[name='arrFilter_form']").find("input[name='arrFilter_cf[5][RIGHT]']").val(l_price);
		$("form[name='arrFilter_form']").submit();
	}
	
	initNav = function(){
		$("div.fPrice").find("div.select_box_list").find("div").removeClass("selected_d");
		$("div.fPrice").find("div.select_box_list").find("p").removeClass("selPrice");
		
		f_price = $("form[name='arrFilter_form']").find("input[name='arrFilter_cf[5][LEFT]']").val();
		l_price = $("form[name='arrFilter_form']").find("input[name='arrFilter_cf[5][RIGHT]']").val();
		str = f_price + "-" + l_price;
		//alert(str);
		if($("div.fPrice").find("div.select_box_list").find("#" + str).length > 0){
			$("div.fPrice").find("div.select_box_list").find("#" + str).addClass("selPrice");
			$("div.fPrice").find("div.select_box_list").find("#" + str).parent().addClass("selected_d");
			$("div.fPrice").find(".select_box_top").find("p").text($("p.selPrice").text());
		}
	}
	initNav();
	$("div.fPrice").find("div.select_box_list").find("div").click(cat_filterGP);
	/**********nav_string**************/
	
	/**********nav_string**************/
	cat_filterGP1 = function(){
		dPrice = $(this).find("p").attr("id");
		indexS = dPrice.indexOf("-");
		f_price = dPrice.substring(0,indexS);
		l_price = dPrice.substring(indexS+1);
		//alert($("input[name='arrFilter_cf[5][LEFT]']").length);
		$("form[name='arrFilter_form2']").find("input[name='arrFilter_cf[5][LEFT]']").val(f_price);
		$("form[name='arrFilter_form2']").find("input[name='arrFilter_cf[5][RIGHT]']").val(l_price);
		$("form[name='arrFilter_form2']").submit();
	}
	
	initNav1 = function(){
		$("div.fPrice2").find("div.select_box_list").find("p").removeClass("selPrice");
		$("div.fPrice2").find("div.select_box_list").find("div").removeClass("selected_d");
		f_price = $("form[name='arrFilter_form2']").find("input[name='arrFilter_cf[5][LEFT]']").val();
		l_price = $("form[name='arrFilter_form2']").find("input[name='arrFilter_cf[5][RIGHT]']").val();
		str = f_price + "-" + l_price;
		//alert(str);
		if($("div.fPrice2").find("div.select_box_list").find("#" + str).length > 0){
			$("div.fPrice2").find("div.select_box_list").find("#" + str).addClass("selPrice");
			$("div.fPrice2").find("div.select_box_list").find("#" + str).parent().addClass("selected_d");
			$("div.fPrice2").find(".select_box_top").find("p").text($("p.selPrice").text());
		}
	}
	initNav1();
	$("div.fPrice2").find("div.select_box_list").find("div").click(cat_filterGP1);
	/**********nav_string**************/

	
	showImgB = function(){
		id_img = $(this).attr("id");
		$("div.big_img").css("display","none");
		$("div.small_img").css("display","inline");
		$("#" + id_img).css("display","inline");
		$(this).css("display","none");
		$("a.name_link").text($(this).find("img").attr("alt"));
		$("a.name_link").attr("href", $(this).attr("href"));
		
	}
	
	getTopC = function(){
		colDiv = $("div.b_top").find("div").length;
		indexDiv = $("div.b_top").find("div").index(this);
		indexDiv1 = indexDiv + 1;
		id_topC = $(this).attr("id");
		for(var i = 1; i < 4; i++){
			$("div.b_top").find("div").removeClass("cshp_" + i + "_a");
			$("div.b_top").find("div").removeClass("cshp_" + i + "_f");
			$("div.b_top").find("div").removeClass("cshp_" + i + "_f_1");
		}
		if(indexDiv1 == 1){
			$(this).addClass("cshp_" + indexDiv1 + "_a");
			$("div.b_top").find("div").eq(1).addClass("cshp_2_f");
			$("div.b_top").find("div").eq(2).addClass("cshp_3_f");
		}
		if(indexDiv1 == 2){
			$(this).addClass("cshp_" + indexDiv1 + "_a");
			$("div.b_top").find("div").eq(0).addClass("cshp_1_f");
			$("div.b_top").find("div").eq(2).addClass("cshp_3_f");			
		}
		if(indexDiv1 == 3){
			$(this).addClass("cshp_" + indexDiv1 + "_a");
			$("div.b_top").find("div").eq(0).addClass("cshp_1_f_1");
			$("div.b_top").find("div").eq(1).addClass("cshp_2_f_1");
		}
		$("div.top_cont").html('<div class="csbw l">'+
			'<div class="preload"><img src="/bitrix/templates/darim_copy_copy/ajax-loader.gif"></div></div>');
		$("div.top_cont").find("div.preload").css("display","inline");
		$.get("", {"prop":id_topC}, function(data){
			cont_top = $(data).find("div.top_cont").html();
			$("div.top_cont").html(cont_top);
			$("div.b_top").find("div.preload").css("display","none");
		});
	}
	
	getSideMenu = function(){$(this).find("div.drop_cat").css("display","");}
	closeSideMenu = function(){$(this).find("div.drop_cat").css("display","none");}
	
	ReloadCont = function(){
		$("div.reload_cont").html('<img src="/bitrix/templates/darim_copy_copy/ajax-loader1.gif">');
		$.post($(this).attr("href"), function(data){
			Acont_r = $(data).find("div.reload_cont").html();
			$("div.reload_cont").html(Acont_r);
		});
	}
	
	getPAsc = function(){location.href="&sort=asc";}
	getPDesc = function(){location.href="&sort=desc";}
	
	showAuthF = function(){
		$("div.login_box").css("display","inline");
		$("a.close_auth").click(function(){
			$("div.login_box").css("display","none");
		});
	}

	col_b = $("div.big_img").length;
	col_b = $("div.small_img").length;
								
	$("div.small_img").eq(0).addClass("sel");
	$("div.small_img").eq(0).css("display","none");
								
	$("div.big_img").css("display","none");
	$("div.big_img").eq(0).css("display","inline");
									
	$("div.small_img").bind("click", showImgB);
	$("div.b_top").find("div").bind("click", getTopC);
									
	$("div.menu_block").click(function(){
		if($(this).next(".sbl").hasClass("open")){
			$("div.menu_block").next(".sbl").removeClass("open");
			$("div.menu_block").find("p").removeClass("a");
			$("div.menu_block").next().not(".open").slideUp(500);
		}else{
			$("div.menu_block").next(".sbl").removeClass("open");
			$("div.menu_block").find("p").removeClass("a");
											
			$(this).next(".sbl").addClass("open");
			$(this).find("p").addClass("a");
							
			$("div.menu_block").next(".open").slideDown(1000);
			$("div.menu_block").next().not(".open").slideUp(500);
		}
	});
	
	/***************** Show&Hide Blocks (filter and top elements)************************/
	
	$("div.top_block").toggle(function(){
		$(this).next(".sbl").slideUp(300); $(this).find("p").removeClass("a");
	},
	function(){
		$(this).next(".sbl").slideDown(500); $(this).find("p").addClass("a");
	});
	
	$("div.fil_block").toggle(function(){
		$(this).next(".sbl").slideUp(300); $(this).find("p").removeClass("a");
	},
	function(){
		$(this).next(".sbl").slideDown(500); $(this).find("p").addClass("a");
	});
		
	/***************** Show&Hide Blocks (filter and top elements)************************/
		
	$("a.reload_a").toggle(ReloadCont, ReloadCont);

	$("div.menu_b_2l").hover(getSideMenu, closeSideMenu);
	$("a.log_in").click(showAuthF);
	
	/******************CALENDAR***********************/
	d_result = function(){
		if($(this).parent().attr("class") == "any_day"){
			$("div.events_list").html($(this).parent().next().html());
			//alert($(this).parent().next().html());
		}else{
			$("div.events_list").html($(this).parent().parent().next().html());
		}
	}
	m_result = function(){
		linkM = $(this).attr("href");
		$("div.news-calendar").html("<img class='preload_img' src='/bitrix/templates/darim_copy_copy/ajax-loader3.gif'>");
		$.get(linkM, {}, function(data){
			contentM = $(data).find("div.news-calendar").html();
			$("div.news-calendar").html(contentM);
			$("a.day_n").click(d_result);
			$("a.getM").toggle(m_result, m_result);
		});
	}
	initCal = function(){
		colCA = $("div.NewsCalDiv").find(".NewsCalToday").find("a").length;
		if(colCA > 0){
			$("div.events_list").html($("div.NewsCalDiv").find("td.NewsCalToday").find("a").parent().parent().next().html());
		}else{
			$("div.events_list").html("сегодня нет праздников...");
		}
	}
	$("a.day_n").click(d_result);
	$("a.getM").toggle(m_result, m_result);
	initCal();
	
		/******************CATALOG***********************/
		//$("#sort_asc").click(getPAsc);
		//$("#sort_desc").click(getPDesc);
		
		/******************ORDER***********************/
			getProf = function(){
				
				$('#ID_PROFILE_ID').val($(this).attr("id"));
				
				//lastTxt = $("div.select_box_top1 p").text();
				//lastId = $("div.select_box_top1 p").attr("id");
				 
				$("div.select_box_top1 p").text($(this).text());
				$("div.select_box_top1 p").attr("id", $(this).attr("id"));
				$("div.select_box_list1 div").removeClass("selDiv");
				$(this).parent().addClass("selDiv");
				
				document.getElementById("profile_change").value = "Y";
				submitForm();
				initProf();
			}
			initProf = function(){
				id_s = $('#ID_PROFILE_ID').val();
				$("div.select_box_list1").find("#" + id_s).parent().addClass("selDiv");
				$("div.select_box_top1 p").text($("div.select_box_list1").find("#" + id_s).text());
				$("div.select_box_top1 p").attr("id", $("div.select_box_list1").find("#" + id_s).attr("id"));
			}
			initProf();
			$("div.select_box_list1 div p").live("click", getProf);
			
	/***********FORMS***********************/
	showDDList = function(){
		$("div.select_box_list").css("display", "none");
		$(this).next("div.select_box_list").css("display","block");
	}
	showDDList1 = function(){
		$("div.select_box_list").css("display", "none");
		$(this).css("display","block");
	}
	closeDDList = function(){
		$(this).css("display","none");
	}
	closeDDList_1 = function(){
		$("div.select_box_list").css("display","none");
	}
	$("div.select_box_top").click(showDDList);
	$("div.select_box_list").hover(showDDList1, closeDDList);
	$("div.select_box_list").find("p").click(closeDDList_1);
	
	/***********FORMS***********************/
	
});
