$(function() {
		$("a[rel^='prettyPhoto']").prettyPhoto();
	
		$('.inv_part .infoElement').click(function() {
			var htmlStr = $(this).html();
			$(this).parent().parent().find('.sell').removeClass('sell');
			var infoElement = $(this).parent().parent().parent().find('.open_part .infoElement');
			if(infoElement.length > 1) {
				infoElement.eq(1).html(htmlStr);
			} else {
				infoElement.eq(0).html(htmlStr);
			}
			$(this).parent().addClass("sell");
			
		});
		
		$(".open_part").click(function() {
			$(this).next('.inv_part').addClass("selected");	
			$(this).next('.inv_part').show();				
			$('.selected').parent().css('z-index','100')
			if($(this).next('.inv_part').hasClass("activ")){
				$(this).next('.inv_part').parent().css('z-index','0')	
				$(this).next('.inv_part').hide();
				$('.inv_part').removeClass('selected');
				$('.inv_part').removeClass('activ');
			}	
		});
		
		$(".materialTypes").click(function () {
			$('#Material').val('');
			var mt = $(this).attr('rel');
			var visibleItemCount = 0;
			$('.materials').each(function() {
				if($(this).attr('rel')==mt) {
					$(this).show();
					$(this).next().show();
					visibleItemCount++;
				}
				else {
					$(this).hide();
					$(this).next().hide();
				}
			});
			
			if(!visibleItemCount) {
				$('#MaterialP').fadeOut();
				//$('#MaterialP').animate({opacity:0.5});
			} else {
				var current  = $('.materials[rel='+mt+']').eq(0);
				$('#MaterialP div.infoElement').html(current.find('div.infoElement').html());
				$('#Material').val(current.attr('id').replace('m', ''));
				$('#MaterialP').fadeIn();
				//$('#MaterialP').animate({opacity:1});
			}
			$('#MaterialType').val(mt);
		}) ;
		$(".materials").click(function () {
			$('#Material').val($(this).attr('id').replace('m', ''));
		});
		$(".constructons").click(function () {
			$('#Constructions').val($(this).attr('id').substr(1));
		});
		$(".walls").click(function () {
			$('#WallType').val($(this).attr('id').substr(1));
		});
		$(".floorcount").click(function () {
			$('#FloorCount').val($(this).attr('rel'));
		});
		$(".areas").click(function () {
			$('#Area').val($(this).attr('rel'));
		});
		$('body').click(				
			function(){
				$('.activ').parent().css('z-index','0')	
				$('.selected').removeClass("activ")
				$('.activ').hide();
				$('.inv_part').removeClass('activ');
				$('.selected').addClass('activ')
				$('.inv_part').removeClass('selected');									
			}
		);
		$('.currency .noact').click(
			function(){
				$('.currency .act').addClass('noact')
				$('.currency .act').removeClass('act')				
				$(this).addClass('act');
			}
		);
		var slider_1 = $("#Slider1");
		if(slider_1.length) {
			$("#Slider1").slider({
				range: true,
				slide: function(event, ui){
					$("#SpaceStart").val(ui.values[0]);
					$("#SpaceEnd").val(ui.values[1]);
					$("#SpaceLow").val(ui.values[0]);
					$("#SpaceHight").val(ui.values[1]);
				}
			});
			//$("#amount1").html('От <span class="btre">' + $("#Slider1").slider("values", 0) + '</span>м<span class=sup>3</span> до <span class="btre">' + $("#Slider1").slider("values", 1) + '</span>м<span class=sup>3</span>');
		}
		
		$("#SpaceStart").keypress(function(e) {
			switch(e.keyCode) {
				case 38: {
					var value = parseInt($(this).val());
					var max = $("#Slider1").slider("values", 1);
					if(value +1 <= max) {
						value++; 
					}
					$(this).val(value);
					$("#SpaceLow").val(value);
					$("#Slider1").slider("values", 0, value);
					break;
				}
				case 40: {
					var value = parseInt($(this).val());
					var min = $("#Slider1").slider("option", "min");
					if(value - 1 >= min) {
						value--; 
					}
					$(this).val(value);
					$("#SpaceLow").val(value);
					$("#Slider1").slider("values", 0, value);
					break;
				}
				default: {
					if(e.keyCode != 8) {
						var value = parseInt($(this).val() + String.fromCharCode(e.which));
					} else {
						//нажали backspace
						var value = $(this).val();
						value = value.substr(0, value.length-1);
					}
					var slider = $("#Slider1");
					var max = slider.slider("values", 1);
					var min = slider.slider("option", "min");
					if(value >=min && value <=max) {
						$("#SpaceLow").val(value);
						$("#Slider1").slider("values", 0, value);
					}
				}
			}
		});
		
		$("#SpaceEnd").keypress(function(e) {
			switch(e.keyCode) {
				case 38: {
					var value = parseInt($(this).val());
					var max = $("#Slider1").slider("option", "max");
					if(value +1 <= max) {
						value++; 
					}
					value = parseInt(value);
					$(this).val(value);
					$("#SpaceHight").val(value);
					$("#Slider1").slider("values", 1, value);
					break;
				}
				case 40: {
					var value = parseInt($(this).val());
					var min = $("#Slider1").slider("values", 0);
					if(value - 1 >= min) {
						value--; 
					}
					value = parseInt(value);
					$(this).val(value);
					$("#SpaceHight").val(value);
					$("#Slider1").slider("values", 1, value);
					break;
				}
				default: {
					if(e.keyCode != 8) {
						var value = parseInt($(this).val() + String.fromCharCode(e.which));
					} else {
						//нажали backspace
						var value = $(this).val();
						value = value.substr(0, value.length-1);
					}
					var slider = $("#Slider1");
					var max = slider.slider("option", "max");
					var min = slider.slider("values", 0);
					if(value >=min && value <=max) {
						value = parseInt(value);
						$("#SpaceHight").val(value);
						$("#Slider1").slider("values", 1, value);
					}
				}
			}
		});
		//------------------------------------------ЦЕНА ПРОЕКТА-------------------------------------------
		var slider_4 = $("#Slider4");
		if(slider_4.length) {
			$("#Slider4").slider({
				range: true,
				slide: function(event, ui){
					$("#PriceStart").val(ui.values[0]*ui.values[0]);
					$("#PriceEnd").val(ui.values[1]*ui.values[1]);
					//$("#amount4").html('От <span class="btre">' + parseInt(ui.values[0]*ui.values[0]) + '</span> <img src="/img/blank.gif" class="icoImg2" alt="Рублей" /> до <span class="btre">' + parseInt(ui.values[1]*ui.values[1]) + '</span> <img src="/img/blank.gif" class="icoImg2" alt="Рублей" />');
					$("#CostLow").val(parseInt(ui.values[0]*ui.values[0]));
					$("#CostHight").val(parseInt(ui.values[1]*ui.values[1]));
				}
			});
		}
		
		$("#PriceStart").keypress(function(e) {
			switch(e.keyCode) {
				case 38: {
					var value = parseInt($(this).val());
					var max = $("#Slider4").slider("values", 1);
					max = max * max;
					if(value +1000 <= max) {
						value+=1000; 
					}
					$(this).val(value);
					$("#CostLow").val(value);
					$("#Slider4").slider("values", 0, Math.sqrt(value));
					break;
				}
				case 40: {
					var value = parseInt($(this).val());
					var min = $("#Slider4").slider("option", "min");
					min = min * min;
					if(value - 1000 >= min) {
						value-=1000; 
					}
					$(this).val(value);
					$("#CostLow").val(value);
					$("#Slider4").slider("values", 0, Math.sqrt(value));
					break;
				}
				default: {
					if(e.keyCode != 8) {
						var value = parseInt($(this).val() + String.fromCharCode(e.which));
					} else {
						//нажали backspace
						var value = $(this).val();
						value = value.substr(0, value.length-1);
					}
					var slider = $("#Slider4");
					var max = slider.slider("values", 1);
					var min = slider.slider("option", "min");
					max = max * max;
					min = min * min;
					if(value >=min && value <=max) {
						$("#CostLow").val(value);
						$("#Slider4").slider("values", 0, Math.sqrt(value));
					}
				}
			}
		});
		
		$("#PriceEnd").keypress(function(e) {
			switch(e.keyCode) {
				case 38: {
					var value = parseInt($(this).val());
					var max = $("#Slider4").slider("option", "max");
					max = max * max;
					if(value +1000 <= max) {
						value+=1000; 
					}
					value = parseInt(value);
					$(this).val(value);
					$("#CostHight").val(value);
					$("#Slider4").slider("values", 1, Math.sqrt(value));
					break;
				}
				case 40: {
					var value = parseInt($(this).val());
					var min = $("#Slider4").slider("values", 0);
					min = min * min;
					if(value - 1000 >= min) {
						value-=1000; 
					}
					value = parseInt(value);
					$(this).val(value);
					$("#CostHight").val(value);
					$("#Slider4").slider("values", 1, Math.sqrt(value));
					break;
				}
				default: {
					if(e.keyCode != 8) {
						var value = parseInt($(this).val() + String.fromCharCode(e.which));
					} else {
						//нажали backspace
						var value = $(this).val();
						value = value.substr(0, value.length-1);
					}
					var slider = $("#Slider4");
					var max = slider.slider("option", "max");
					var min = slider.slider("values", 0);
					max = max * max;
					min = min * min;
					if(value >=min && value <=max) {
						value = parseInt(value);
						$("#CostHight").val(value);
						$("#Slider4").slider("values", 1, Math.sqrt(value));
					}
				}
			}
		});
		
		//----------------------------------------ВЫСОТА ПЕРВОГО ЭТАЖА-----------------------------------
		var slider_2 = $("#Slider2");
		if(slider_2.length) {
			$("#Slider2").slider({
				range: true,
				slide: function(event, ui){
					$("#HeightStart").val(ui.values[0]);
					$("#HeightEnd").val(ui.values[1]);
					
					$("#FFLow").val(ui.values[0]);
					$("#FFHigh").val(ui.values[1]);
				}
			});
		}
		
		$("#HeightStart").keypress(function(e) {
			switch(e.keyCode) {
				case 38: {
					var value = parseInt($(this).val());
					var max = $("#Slider2").slider("values", 1);
					if(value +1 <= max) {
						value++; 
					}
					$(this).val(value);
					$("#FFLow").val(value);
					$("#Slider2").slider("values", 0, value);
					break;
				}
				case 40: {
					var value = parseInt($(this).val());
					var min = $("#Slider2").slider("option", "min");
					if(value - 1 >= min) {
						value--; 
					}
					$(this).val(value);
					$("#FFLow").val(value);
					$("#Slider2").slider("values", 0, value);
					break;
				}
				default: {
					if(e.keyCode != 8) {
						var value = parseInt($(this).val() + String.fromCharCode(e.which));
					} else {
						//нажали backspace
						var value = $(this).val();
						value = value.substr(0, value.length-1);
					}
					var slider = $("#Slider2");
					var max = slider.slider("values", 1);
					var min = slider.slider("option", "min");
					if(value >=min && value <=max) {
						$("#FFLow").val(value);
						$("#Slider2").slider("values", 0, value);
					}
				}
			}
		});
		
		$("#HeightEnd").keypress(function(e) {
			switch(e.keyCode) {
				case 38: {
					var value = parseInt($(this).val());
					var max = $("#Slider2").slider("option", "max");
					if(value +1 <= max) {
						value++; 
					}
					value = parseInt(value);
					$(this).val(value);
					$("#FFHigh").val(value);
					$("#Slider2").slider("values", 1, value);
					break;
				}
				case 40: {
					var value = parseInt($(this).val());
					var min = $("#Slider2").slider("values", 0);
					if(value - 1 >= min) {
						value--; 
					}
					value = parseInt(value);
					$(this).val(value);
					$("#FFHigh").val(value);
					$("#Slider2").slider("values", 1, value);
					break;
				}
				default: {
					if(e.keyCode != 8) {
						var value = parseInt($(this).val() + String.fromCharCode(e.which));
					} else {
						//нажали backspace
						var value = $(this).val();
						value = value.substr(0, value.length-1);
					}
					var slider = $("#Slider2");
					var max = slider.slider("option", "max");
					var min = slider.slider("values", 0);
					if(value >=min && value <=max) {
						value = parseInt(value);
						$("#FFHigh").val(value);
						$("#Slider2").slider("values", 1, value);
					}
				}
			}
		});
		
		//-----------------------------------------------------СРОК СТРОИТЕЛЬСТВА-------------------------------------
		var slider_3 = $("#Slider3");
		if(slider_3.length) {
			$("#Slider3").slider({
				range: "min",
				slide: function(event, ui) {
					$("#CTime").val(ui.value);
					$("#DaysStart").val(ui.value);
				}
			});
		}
		$("#DaysStart").keypress(function(e) {
			switch(e.keyCode) {
				case 38: {
					var value = parseInt($(this).val());
					var max = $("#Slider3").slider("option", "max");
					if(value +1 <= max) {
						value++; 
					}
					$(this).val(value);
					$("#DaysStart").val(value);
					$("#Slider3").slider("value", value);
					break;
				}
				case 40: {
					var value = parseInt($(this).val());
					var min = $("#Slider3").slider("option", "min");
					if(value - 1 >= min) {
						value--; 
					}
					$(this).val(value);
					$("#DaysStart").val(value);
					$("#Slider3").slider("value", value);
					break;
				}
				default: {
					if(e.keyCode != 8) {
						var value = parseInt($(this).val() + String.fromCharCode(e.which));
					} else {
						//нажали backspace
						var value = $(this).val();
						value = value.substr(0, value.length-1);
					}
					var slider = $("#Slider3");
					var max = slider.slider("option", "max");
					var min = slider.slider("option", "min");
					if(value >=min && value <=max) {
						$("#DaysStart").val(value);
						$("#Slider3").slider("value", value);
					}
				}
			}
		});
		
		//повесим обработчик для кнопки "удалить из сравнения"
		$('.compareRemove').click(function() {
			var id = $(this).attr('rel');
			$.get('/actions/compareremove/', {id:id}, function(html) {
				$('#cr'+id).hide();
			});
			return false;
		});
		
		//подсказки
		$('.hint').mouseover(function () {
			var x =$(this).offset().left;
			var y = $(this).offset().top;
			var clientWidth = document.documentElement.clientWidth;
			
			if(clientWidth && (x + 320 > clientWidth) ) {
				x =  clientWidth - 322;
			}
			$('#Hint').css('left',x);
			$('#Hint').css('top',y+20);
			$('#Hint').text($(this).attr('rel'));
			$('#Hint').show();
		});
		$('.hint').mouseout(function() {
			$('#Hint').hide();
		});
		
		//joke
		/*var prevKey = 0; 
		var srcs = new Array();
		var ind = [0, 1, 3, 5, 4, 2];
		var K = 0;
		
		$('.imgIn').each(function () {
			srcs.push($(this).attr('src'));
		});
		
		$(document).keyup(function (e) {
			if(e.keyCode==51 && prevKey==50) {
				window.setInterval(function () {
					var imgs = $('.imgIn');
					//установим в начальноезначение все картинки
					for(var i = 0; i < imgs.length; i++) {
						$(imgs[i]).attr('src', srcs[i]);
					}
					$(imgs[ind[K]]).attr('src', '/img/fire.jpg');
					
					K = (K+1 == ind.length)?0:K+1;
					
				}, 500)
				
			}
			if(e.keyCode==50 && prevKey==51) {
				$('#Logo').attr('src', '/img/Logo.gif');
			} 
			prevKey = e.keyCode;
		});*/
	});
