
	// Прелоадер
	jQuery.preloadImages = function() {
		for(var i = 0; i<arguments.length; i++) {
			jQuery("<img>").attr("src", arguments[i]);
		}
	}

	// Двигло
	$(window).load(function(){

		// Включаем меню
		menuInit();


		$('#curators input').each(function(){
			 $.preloadImages('/attach/'+$(this).val()+'/w116');
		});


        $('#rMainBlocks .arrow_p').css('cursor','pointer');
		$('#rMainBlocks .arrow_p').click(function(){
			var objId = $(this).parent().find('.mBlock-content').attr('id');
			$('.mBlock-content').each(function(index) {
				if ($(this).css('display') == 'block' && $(this).attr('id') != objId) {
					$(this).slideUp('fast');
					$(this).parent().removeClass('a_first');
					src = $(this).parent().find('img.text').attr('src');
					$(this).parent().find('img.text').attr('src', src.replace(/text_([0-9]+)_a\.png/, 'text_$1.png'));
				}
			});
			if ($(this).parent().hasClass('a_first')) {
				$(this).parent().removeClass('a_first');
				$(this).parent().find('.mBlock-content').slideUp('fast');
			} else {
				$(this).parent().addClass('a_first');
				src = $(this).parent().find('img.text').attr('src');
				$(this).parent().find('img.text').attr('src', src.replace(/text_([0-9]+)\.png/, 'text_$1_a.png'));
				$(this).parent().find('.mBlock-content').slideDown('fast');
			}

			return false;
		});

		if ($('#curators').size()) {
			var choosenCurator = 0;
						
			$('#curators img').css('cursor', 'pointer');
			$('#curators img').bind('click', function(){
				loadPeople($(this).attr('id'));
				choosenCurator = $(this).attr('id');
				$('#curators li').each(function(){
					if ($(this).attr('name') != choosenCurator)
						$(this).removeClass('active');
				});
				$('#curators li img').each(function(){
					id = $(this).attr('id');
					if (id != choosenCurator)
						$(this).attr('src', '/attach/'+$('#tutor_'+id).val()+'/w116');
				});
			});
			$('#curators li').bind('mouseover', function(){
				if ($(this).attr('name') != choosenCurator) {
					$(this).addClass('active');
					id = $(this).find('img').attr('id');
					$(this).find('img').attr('src', '/attach/'+$('#tutor_a_'+id).val()+'/w116');
				}
			});
			$('#curators li').bind('mouseout', function(){
				if ($(this).attr('name') != choosenCurator) {
					$('#curators li').each(function(){
						if ($(this).attr('name') != choosenCurator)
							$(this).removeClass('active');
					});
					$('#curators li img').each(function(){
						id = $(this).attr('id');
						if (id != choosenCurator)
							$(this).attr('src', '/attach/'+$('#tutor_'+id).val()+'/w116');
					});
				}
			});
			
			curId = location.hash.replace('#c', '');
			if (curId) {
				$('#'+curId).parent().parent().parent().mouseover();
				$('#'+curId).click();
			}
		}

		if ($('#line2').size()) {
			step = $('#step').val() - 0;
			$("#line2").slider({
				min: 0,
				max: 100,
				step: step,
				slide: function(event, ui) {
					$("#proc").val(ui.value);
					$(".num").removeClass('active');
					$(".num").each(function(){
						if ($(this).attr('name')-0 <= $("#proc").val()-0)
							$(this).addClass('active');
					});
				}
			});
		}

		if ($('#slider-range').size()) {
			$("#slider-range").slider({
				range: true,
				min: 100000,
				max: 100000000,
				values: [$("#slider_min").val()*1000000, $("#slider_max").val()*1000000],
				slide: function(event, ui) {
					$("#slider_min").val(Math.round(ui.values[0]/1000000*10)/10);
					$("#slider_max").val(Math.round(ui.values[1]/1000000*10)/10);
				}
			});
/*
			$("#slider-range").slider("option", "min", 100000);
			$("#slider-range").slider("option", "max", 100000000);
			$("#slider-range").slider("values", 0, 750000);
			$("#slider-range").slider("values", 1, 30000000);
*/

			$('#slider-range .ui-slider-range.ui-widget-header').draggable({
				axis: 'x',
				containment: 'parent',
				start: function(event, ui) { this.percentWidth = $(this).width()/$("#slider-range").width(); },
				drag: function(event,ui) {
					dragPercent = $(this).position().left/$("#slider-range").width();
					
					sliderValuesRange = $("#slider-range").slider('option', 'max') - $("#slider-range").slider('option', 'min');
					
					leftSliderValue = sliderValuesRange * dragPercent;
					rightSliderValue = sliderValuesRange * (dragPercent + this.percentWidth);

					$("#slider-range").slider("values", 0, leftSliderValue);
					$("#slider-range").slider("values", 1, rightSliderValue);
					
					$("#slider_min").val(Math.round($("#slider-range").slider("option", "values")[0]/1000000*10)/10);
					$("#slider_max").val(Math.round($("#slider-range").slider("option", "values")[1]/1000000*10)/10);
				},
				stop: function(event, ui) {
					$(this).css('left', dragPercent*100+'%');
					$(this).css('width', this.percentWidth*100+'%');
				}
			});

			$("#slider_min").val(Math.round($("#slider-range").slider("values", 0)/1000000*10)/10);
			$("#slider_max").val(Math.round($("#slider-range").slider("values", 1)/1000000*10)/10);

			$("#slider_min").bind('keyup', function(event){
				test = cutsymbols(event);
				if (!test)
					return false;

				if ($(this).val()-0 > $("#slider_max").val())
					$(this).val($("#slider_max").val());

				if ($(this).val()-0 < 0)
					$(this).val('0');

				$("#slider-range").slider("values", 0, (($(this).val()-0)*1000000));
			});
			$("#slider_min").bind('keydown', function(event){
				test = cutsymbols(event);
				if (!test)
					return false;
			});


			$("#slider_max").bind('keyup', function(event){
				test = cutsymbols(event);
				if (!test)
					return false;

				if ($(this).val()-0 < $("#slider_min").val())
					$(this).val($("#slider_min").val());

				if ($(this).val()-0 < 0)
					$(this).val($("#slider_min").val());

				if ($(this).val()-0 > 50)
					$(this).val(50);

				$("#slider-range").slider("values", 1, (($(this).val()-0)*1000000));
			});
			$("#slider_max").bind('keydown', function(event){
				test = cutsymbols(event);
				if (!test)
					return false;
			});


		}

		if ($('#slider-range-proc').size()) {
			$("#slider-range-proc").slider({
				range: true,
				min: 0,
				max: 100,
				values: [$("#slider_min_proc").val(), $("#slider_max_proc").val()],
				slide: function(event, ui) {
					$("#slider_min_proc").val(ui.values[0]);
					$("#slider_max_proc").val(ui.values[1]);
				}
			});

		$('#slider-range-proc .ui-slider-range.ui-widget-header').draggable({
				axis: 'x',
				containment: 'parent',
				start: function(event, ui) { this.percentWidth = $(this).width()/$("#slider-range-proc").width(); },
				drag: function(event,ui) {
					dragPercent = $(this).position().left/$("#slider-range-proc").width();
					
					sliderValuesRange = $("#slider-range-proc").slider('option', 'max') - $("#slider-range-proc").slider('option', 'min');
					
					leftSliderValue = sliderValuesRange * dragPercent;
					rightSliderValue = sliderValuesRange * (dragPercent + this.percentWidth);

					$("#slider-range-proc").slider("values", 0, leftSliderValue);
					$("#slider-range-proc").slider("values", 1, rightSliderValue);
					
					$("#slider_min_proc").val(Math.round($("#slider-range-proc").slider("option", "values")[0]));
					$("#slider_max_proc").val(Math.round($("#slider-range-proc").slider("option", "values")[1]));
				},
				stop: function(event, ui) {
					$(this).css('left', dragPercent*100+'%');
					$(this).css('width', this.percentWidth*100+'%');
				}
			});

			$("#slider_min_proc").val($("#slider-range-proc").slider("values", 0));
			$("#slider_max_proc").val($("#slider-range-proc").slider("values", 1));

			$("#slider_min_proc").bind('keyup', function(event){
				test = cutsymbols(event);
				if (!test)
					return false;

				if ($(this).val()-0 > $("#slider_max_proc").val())
					$(this).val($("#slider_max_proc").val());

				if ($(this).val()-0 < 0)
					$(this).val('0');

				$("#slider-range-proc").slider("values", 0, (($(this).val()-0)));
			});
			$("#slider_min_proc").bind('keydown', function(event){
				test = cutsymbols(event);
				if (!test)
					return false;
			});


			$("#slider_max_proc").bind('keyup', function(event){
				test = cutsymbols(event);
				if (!test)
					return false;

				if ($(this).val()-0 < $("#slider_min_proc").val())
					$(this).val($("#slider_min_proc").val());

				if ($(this).val()-0 < 0)
					$(this).val($("#slider_min_proc").val());

				if ($(this).val()-0 > 100)
					$(this).val(100);

				$("#slider-range-proc").slider("values", 1, (($(this).val()-0)));
			});
			$("#slider_max_proc").bind('keydown', function(event){
				test = cutsymbols(event);
				if (!test)
					return false;
			});
		}

//		$('.news_anons li.short').css('cursor', 'pointer');
//		$('.news_anons li.short').click(function(){
//			$('.news_anons li.short').slideDown('fast');
//			$('.news_anons li.active').slideUp('fast');
//			$(this).slideUp('fast');
//			$(this).next().slideDown('fast');
//			return false;
//		});
		
		$('#pc_block h2').click(function() {
			if ($(this).hasClass('active'))
				return;
			$('#pc_block h2').removeClass('active');
			$(this).addClass('active');
			$('#pc_block ul.news_anons').slideUp();
			$(this).next('ul.news_anons').slideDown();
		});
		
		// Навесим обработчик сабмита
		$('#pSearch').submit(function(event) {
			location.href = '/projects/'+$('#slider_min').val()+'-'+$('#slider_max').val();
			return false;
		});
		$('#pSearchProc').submit(function(event) {
			location.href = '/projects/proc-'+$('#slider_min_proc').val()+'-'+$('#slider_max_proc').val();
			return false;
		});


		// Аккордион фиьтров
		$('.search3 h2').css('cursor', 'pointer');
		$('.search3 h2').click(function(){
			var objId = $(this).next().attr('id');
			$('.slider-content').each(function(index) {
				if ($(this).css('display') == 'block' && $(this).attr('id') != objId) {
					$(this).slideUp('fast');
					$(this).prev().removeClass('active');
				}
			});
			if ($(this).hasClass('active')) {
				$(this).removeClass('active');
				$(this).next().slideUp('fast');
			} else {
				$(this).addClass('active');
				$(this).next().slideDown('fast');
			}
//			$('.slider-content').slideDown('fast');
//			$(this).parent().next().slideUp('fast');
			return false;
		});
		
		
		// Проверка полей на лету
		$('#phone').bind('keyup change', function(){
			if (!/^\+7([0-9]{10})$/.exec($(this).val())) {
				$('#err_phone').html('Телефон должен начинаться с +7 и содержать 10 цифр');
				$('#err_phone').css('display', 'inline');
			} else {
				$('#err_phone').css('display', 'none');
			}
		});
		$('#email').bind('keyup change', function(){
			if (!/^[A-z0-9._-]{1,30}[A-z0-9]?@[A-z][A-z0-9._-]{0,16}[A-z0-9]\.[A-z]{2,4}$/.exec($(this).val())) {
				$('#err_email').html('Этот адрес не является правильным');
				$('#err_email').css('display', 'inline');
			} else {
				$('#err_email').css('display', 'none');
			}
		});



	});


	function clearSearch(obj) {
		if ($(obj).val() == 'Поиск')
			$(obj).val('');
	}
	function restoreSearch(obj) {
		if ($(obj).val() == '')
			$(obj).val('Поиск');
	}


	function menuInit() {
		linkList = $('#menu a');

		for (i=linkList.length-1; i>=0; i--) {

			$.preloadImages(linkList[i].getElementsByTagName('img')[0].src.replace(/\.gif$/, '_a.gif'));

			if (linkList[i].parentNode.parentNode.className == 'active') continue;

			linkList[i].onmouseover = function() {
				this.parentNode.parentNode.className = 'active';
				this.getElementsByTagName('img')[0].src = this.getElementsByTagName('img')[0].src.replace(/\.gif$/, '_a.gif');
			}

			linkList[i].onmouseout = function() {
				this.parentNode.parentNode.className = '';
				this.getElementsByTagName('img')[0].src = this.getElementsByTagName('img')[0].src.replace(/_a\.gif$/, '.gif');
			}
		}
	}

	// Вырезание всего кроме цифр
	function cutsymbols(e) {
		e=e||event;
		var key=e.charCode||e.keyCode;
		if (e.shiftKey) return false;
		if ((key>95&&key<106)||(key>47&&key<58)||key==8||key==46) return true;
		else return false;
	}
	
	
	// Подгружаем инфу о человеке
	function loadPeople(id) {
		$('#pInfo .c_text').css('height', $('#pInfo .c_text').height());
		$('#pInfo .c_text').html('<img src="/images/preload.gif" style="margin-left:45%" />');
		$.ajax({
			url: "/api/about.info.php", 
			type: "POST",
			data: "people_id="+id,
			success: function(data){
				$('#pInfo .c_text').html(data);
				$('#pInfo .c_text').css('height', 'auto');
				if ($('#pInfo').css('display') != 'block') 
					$('#pInfo').slideDown('fast');
			}
		});
	}
	


