$(document).ready(function(){	//	авторизация
	$('form.auth a.remind').click(function(){		$('form.remind').show();
		$('form.auth').hide();
		return false;	});
	$('form.remind a.register').click(function(){
		$('form.remind').hide();
		$('form.auth').show();
		return false;
	});
	$('div.auth a.enter').click(function(){		$('div.auth-form').show();
		return false;	});
	$('div.auth a.close').click(function(){
		$('div.auth-form').hide();
		return false;
	});

	$('div.auth-form input.text').each(function(){		if ($(this).attr('value')=='' || $(this).attr('value')==$(this).attr('title')){			$(this).attr('value',$(this).attr('title'));		}else{			$(this).addClass('active');		}
		$(this).blur(function(){			if ($(this).attr('value')==''){				$(this).attr('value',$(this).attr('title'));
				$(this).removeClass('active');			}		});
		$(this).focus(function(){
			if ($(this).attr('value')==$(this).attr('title')){
				$(this).attr('value','');
			}
			$(this).addClass('active');
		});	});

	//	scroll
	if ($('div.pages').length){
		$('div.pages-container').scrollLeft(0);
		var offsetw = $('div.pages-container').get(0).offsetWidth;
		var scrollw = $('div.pages-container').get(0).scrollWidth;
	}

	if ($('div.pages').length && offsetw<scrollw){
		offsetw = offsetw-5;
		$('div.pages-container').css({'overflow': 'hidden'});
		var scrollcontainer = $('<div class="scroll"></div>').appendTo('div.pages');
		var scrollh = $('<span class="active"></span>').appendTo(scrollcontainer);
		var scrollpoint = $('<span class="point"><span></span></span>').appendTo(scrollcontainer);
		var hpos = $('div.pages-container a.active').position().left;
		scrollh.css('left',hpos * offsetw/scrollw);
		scrollpoint.css('left',hpos * offsetw/scrollw);
		$('div.pages-container').scrollLeft(hpos-(hpos * offsetw/scrollw));

		scrollpoint.draggable({
			containment:scrollcontainer,
			drag: function(event,ui){
				$('div.pages-container').scrollLeft(ui.position.left*(scrollw-offsetw)/offsetw);
			}
		});

		$(window).resize(function(){
			scrollpoint.draggable('option', 'refreshPositions', true);
		});
	}

	//	для ie
	if ($.browser.msie){
		$('img').load(function(){
			$('div.footer').get(0).innerHTML+='';
		});
	}

	//	комментарии

	$('#comment-name, #comment-email, #comment-content').each(function(){
		if (this.value!=this.title) $(this).addClass('active');
	});
	$('#comment-name, #comment-email, #comment-content').blur(function(){
		if (this.value=='') $(this).removeClass('active');
		this.value = (this.value=='')? this.title : this.value;
	});

	$('#comment-name, #comment-email, #comment-content').focus(function(){
		this.value = (this.value==this.title)? '' : this.value;
		$(this).addClass('active');
	});

	$('<input />').attr({
		'type':'hidden',
		'value':'add-comment',
		'name':'formid'
	}).appendTo('form.add-comment');

	$('div.comments dl dt a').click(function(){
		$('#comment-content').attr('value',$(this).html()+': ');
	});

	//	поиск каталог

	if ($('div.search-catalog').length){
		var stype = 0;
		var town = $('dl.towns ul li.active span').attr('rel');
		var category = $('dl.catalog-category ul li.active span').attr('rel');

		$('ul.list-type span').click(function(){
			stype = $(this).attr('rel');
			$('ul.list-type li.active').animate({top:$(this).parent().position().top},500);
			catalogtableload();
		});

		$('dl.towns, dl.catalog-category').hover(function(){
			$(this).find('dd').show();
		},function(){
			$(this).find('dd').hide();
		});

		$('dl.towns ul span').click(function(){
			$('dl.towns dt').html($(this).html());
			town = $(this).attr('rel');
			$('dl.towns ul li.active').removeClass('active');
			$(this).parent().addClass('active');
			catalogtableload();
			$('dl.towns dd').hide();
			$('a#all-category').removeClass('active');

			$('dl.towns').removeClass('disabled');
		});

		$('dl.catalog-category ul span').click(function(){
			$('dl.catalog-category dt').html($(this).html());
			category = $(this).attr('rel');
			$('dl.catalog-category ul li.active').removeClass('active');
			$(this).parent().addClass('active');
			catalogtableload();
			$('dl.catalog-category dd').hide();
			$('a#all-town').removeClass('active');

			$('dl.catalog-category').removeClass('disabled');
		});

		function catalogtableload(){
			$('div.results').css('height',$('div.results').height()).addClass('loading');
			$('div.results').empty();
			$.get("/ajax/",{
				'town': town,
				'type': stype,
				'category': category,
				'stype':'catalog'
			},function (data, textStatus){
				$('div.results').empty();
				$(data).prependTo('div.results');
				$('div.results').animate({
					height: ($('div.results table').height())? $('div.results table').height(): $('div.results p').height()
				},500,'',function(){
					if ($.browser.msie)	$('div.footer .copyright span').get(0).innerHTML+='';
				}).removeClass('loading');
			});
		}

		$('a#all-town').click(function(){
			category = '';
			town = $('dl.towns ul li.active span').attr('rel');
			$('dl.towns').removeClass('disabled');
			$('dl.catalog-category').addClass('disabled');

			$(this).addClass('active');
			$('a#all-category').removeClass('active');
			catalogtableload();
			return false;
		});

		$('a#all-category').click(function(){
			category = $('dl.catalog-category ul li.active span').attr('rel');
			town = 0;
			$('dl.towns').addClass('disabled');
			$('dl.catalog-category').removeClass('disabled');

			$(this).addClass('active');
			$('a#all-town').removeClass('active');
			catalogtableload();
			return false;
		});


		var ind = location.href.indexOf('#');
		if (ind != -1){			var l = location.href.substring(ind+1);
			$('dl.catalog-category ul li:eq('+(l-1)+') span').click();
		}
	}
	
		//	поиск cms

	if ($('div.search-cms').length){
		var stype = 0;
		var town = $('dl.towns ul li.active span').attr('rel');
		var cms = $('dl.cms ul li.active span').attr('rel');

		$('ul.list-type span').click(function(){
			stype = $(this).attr('rel');
			$('ul.list-type li.active').animate({top:$(this).parent().position().top},500);
			cmstableload();
		});

		$('dl.towns, dl.cms').hover(function(){
			$(this).find('dd').show();
		},function(){
			$(this).find('dd').hide();
		});

		$('dl.towns ul span').click(function(){
			$('dl.towns dt').html($(this).html());
			town = $(this).attr('rel');
			$('dl.towns ul li.active').removeClass('active');
			$(this).parent().addClass('active');
			cmstableload();
			$('dl.towns dd').hide();
			$('a#all-cms').removeClass('active');

			$('dl.towns').removeClass('disabled');
		});

		$('dl.cms ul span').click(function(){
			$('dl.cms dt').html($(this).html());
			cms = $(this).attr('rel');
			$('dl.cms ul li.active').removeClass('active');
			$(this).parent().addClass('active');
			cmstableload();
			$('dl.cms dd').hide();
			$('a#all-town').removeClass('active');

			$('dl.cms').removeClass('disabled');
		});

		function cmstableload(){
			$('div.results').css('height',$('div.results').height()).addClass('loading');
			$('div.results').empty();
			$.get("/ajax/",{
				'town': town,
				'type': stype,
				'cms': cms,
				'stype': 'cms'
			},function (data, textStatus){
				$('div.results').empty();
				$(data).prependTo('div.results');
				$('div.results').animate({
					height: ($('div.results table').height())? $('div.results table').height(): $('div.results p').height()
				},500,'',function(){
					if ($.browser.msie)	$('div.footer .copyright span').get(0).innerHTML+='';
				}).removeClass('loading');
			});
		}

		$('a#all-town').click(function(){
			cms = 0;
			town = $('dl.towns ul li.active span').attr('rel');
			$('dl.towns').removeClass('disabled');
			$('dl.cms').addClass('disabled');

			$(this).addClass('active');
			$('a#all-cms').removeClass('active');
			cmstableload();
			
			return false;
		});

		$('a#all-cms').click(function(){
			cms = $('dl.cms ul li.active span').attr('rel');
			town = 0;
			$('dl.towns').addClass('disabled');
			$('dl.cms').removeClass('disabled');

			$(this).addClass('active');
			$('a#all-town').removeClass('active');
			cmstableload();

			return false;
		})
	}
	
	//	поиск

	if ($('div.search-studio').length){
		var stype = 0;
		var town = $('dl.towns ul li.active span').attr('rel');
		var sum = $('dl.sum ul li.active span').attr('rel');

		$('ul.list-type span').click(function(){
			stype = $(this).attr('rel');
			$('ul.list-type li.active').animate({top:$(this).parent().position().top},500);
			tableload();
		});

		$('dl.towns, dl.sum').hover(function(){
			$(this).find('dd').show();
		},function(){
			$(this).find('dd').hide();
		});

		$('dl.towns ul span').click(function(){
			$('dl.towns dt').html($(this).html());
			town = $(this).attr('rel');
			$('dl.towns ul li.active').removeClass('active');
			$(this).parent().addClass('active');
			tableload();
			$('dl.towns dd').hide();
			$('a#all-sum').removeClass('active');

			$('dl.towns').removeClass('disabled');

			$('div.linkform').show(500);
			$('div.linkform input').attr('value',"<a style='color:#000' href='http://ziik.ru/budget/#"+$(this).attr('rel')+"'><span style='color:#900'>zi.</span>"+$(this).html()+"</a>");
			$('div.linkform h2').html('<span>zi.</span>'+$(this).html());
		});

		$('dl.sum ul span').click(function(){
			$('dl.sum dt').html($(this).html());
			sum = $(this).attr('rel');
			$('dl.sum ul li.active').removeClass('active');
			$(this).parent().addClass('active');
			tableload();
			$('dl.sum dd').hide();
			$('a#all-town').removeClass('active');

			$('dl.sum').removeClass('disabled');
		});

		function tableload(){
			$('div.results').css('height',$('div.results').height()).addClass('loading');
			$('div.results').empty();
			$.get("/ajax/",{
				'town': town,
				'type': stype,
				'sum': sum
			},function (data, textStatus){
				$('div.results').empty();
				$(data).prependTo('div.results');
				$('div.results').animate({
					height: ($('div.results table').height())? $('div.results table').height(): $('div.results p').height()
				},500,'',function(){
					if ($.browser.msie)	$('div.footer .copyright span').get(0).innerHTML+='';
				}).removeClass('loading');
			});
		}

		$('a#all-town').click(function(){			sum = 0;
			town = $('dl.towns ul li.active span').attr('rel');
			$('dl.towns').removeClass('disabled');
			$('dl.sum').addClass('disabled');

			$(this).addClass('active');
			$('a#all-sum').removeClass('active');
			tableload();

			$('div.linkform').show(500);			return false;		});

		$('a#all-sum').click(function(){
			sum = $('dl.sum ul li.active span').attr('rel');
			town = 0;
			$('dl.towns').addClass('disabled');
			$('dl.sum').removeClass('disabled');

			$(this).addClass('active');
			$('a#all-town').removeClass('active');
			tableload();

			$('div.linkform').hide(500);
			return false;
		});

		var ind = location.href.indexOf('#');
		if (ind != -1){
			var l = location.href.substring(ind+1);
			if ($('dl.towns ul li span[rel='+l+']').length){
	            $('dl.towns ul li').removeClass('active');
				$('dl.towns ul li span[rel='+l+']').parent().addClass('active');
				$('dl.towns dt').html($('dl.towns ul li span[rel='+l+']').html());

				sum = 0;
				town = $('dl.towns ul li.active span').attr('rel');
				tableload();
				$('a#all-town').addClass('active');
				$('dl.towns').removeClass('disabled');
				$('dl.sum').addClass('disabled');


				$('div.linkform').show(500);
				$('div.linkform input').attr('value',"<a style='color:#000' href='http://ziik.ru/budget/#"+l+"'><span style='color:#900'>zi.</span>"+$('dl.towns ul li span[rel='+l+']').html()+"</a>");
				$('div.linkform h2').html('<span>zi.</span>'+$('dl.towns ul li span[rel='+l+']').html());
			}
		}
	}

	//	поиск

	if ($('div.search-specialist').length){
		var stype = '';
		var town = $('dl.towns ul li.active span').attr('rel');
		var mtype = $('dl.mtype ul li.active span').attr('rel');

		$('ul.list-type span').click(function(){
			stype = $(this).attr('rel');
			mtype = $('dl.mtype ul li.active span').attr('rel');
			$(this).parents('ul.list-type').find('li.active').animate({top:$(this).parent().position().top},500);
			$(this).parents('ul.list-type').find('span').removeClass('ltactive');
			$(this).addClass('ltactive');

			$('a#all-town').removeClass('active');
			$('dl.mtype').removeClass('disabled');
			specialisttableload();
		});

		$('dl.towns, dl.mtype').hover(function(){
			$(this).find('dd').show();
		},function(){
			$(this).find('dd').hide();
		});

		$('dl.towns ul span').click(function(){
			$('dl.towns dt').html($(this).html());
			town = $(this).attr('rel');
			$('dl.towns ul li.active').removeClass('active');
			$(this).parent().addClass('active');
			specialisttableload();
			$('dl.towns dd').hide();

			$('a#all-profile').removeClass('active');
			$('dl.towns').removeClass('disabled');
		});

		$('dl.mtype ul span').click(function(){
			$('dl.mtype dt').html($(this).html());
			mtype = $(this).attr('rel');
			$('dl.mtype ul li.active').removeClass('active');
			$(this).parent().addClass('active');
			specialisttableload();
			$('dl.mtype dd').hide();

			$('ul.specialization').hide();
			if (mtype=='verstka'){				$('div.list-type h4').hide();			}else{				$('div.list-type h4').show();
				$('ul.spz'+mtype).show();
				styp = $('ul.spz'+mtype+' span.ltactive').attr('rel');			}
			$('a#all-town').removeClass('active');
			$('dl.mtype').removeClass('disabled');
		});

		function specialisttableload(){
			$('div.results').css('height',$('div.results').height()).addClass('loading');
			$('div.results').empty();
			$.get("/ajax/",{
				'stype': 'specialist',
				'town': town,
				'type': stype,
				'mtype': mtype
			},function (data, textStatus){
				$('div.results').empty();
				$(data).prependTo('div.results');
				$('div.results').animate({
					height: ($('div.results table').height())? $('div.results table').height(): $('div.results p').height()
				},500,'',function(){
					if ($.browser.msie)	$('div.footer .copyright span').get(0).innerHTML+='';
				}).removeClass('loading');
			});
		}

		$('a#all-town').click(function(){
			town = $('dl.towns ul li.active span').attr('rel');
			mtype = 0;
			$('dl.towns').removeClass('disabled');
			$('dl.mtype').addClass('disabled');

			$(this).addClass('active');
			$('a#all-profile').removeClass('active');
			specialisttableload();
			return false;
		});

		$('a#all-profile').click(function(){
			town = 0;
			mtype = $('dl.mtype ul li.active span').attr('rel');
			$('dl.towns').addClass('disabled');
			$('dl.mtype').removeClass('disabled');

			$(this).addClass('active');
			$('a#all-town').removeClass('active');
			specialisttableload();
			return false;
		});
	}
	
		//	поиск

	if ($('div.search-events').length){
		var stype = 0;
		var rtype = $('dl.rtype ul li.active span').attr('rel');
		var ktype = $('dl.ktype ul li.active span').attr('rel');

		$('ul.list-type span').click(function(){
			stype = $(this).attr('rel');
			$(this).parents('ul.list-type').find('li.active').animate({top:$(this).parent().position().top},500);
			$(this).parents('ul.list-type').find('span').removeClass('ltactive');
			$(this).addClass('ltactive');
			eventstableload();
		});

		$('dl.rtype, dl.ktype').hover(function(){
			$(this).find('dd').show();
		},function(){
			$(this).find('dd').hide();
		});

		$('dl.rtype ul span').click(function(){
			$('dl.rtype dt').html($(this).html());
			rtype = $(this).attr('rel');
			$('dl.rtype ul li.active').removeClass('active');
			$(this).parent().addClass('active');
			eventstableload();
			$('dl.rtype dd').hide();
			
			$('a#all-status').removeClass('active');
			$('dl.rtype').removeClass('disabled');
		});

		$('dl.ktype ul span').click(function(){
			$('dl.ktype dt').html($(this).html());
			ktype = $(this).attr('rel');
			$('dl.ktype ul li.active').removeClass('active');
			$(this).parent().addClass('active');
			eventstableload();
			$('dl.ktype dd').hide();
			
			$('a#all-category').removeClass('active');
			$('dl.ktype').removeClass('disabled');
		});

		function eventstableload(){
			$('div.results').css('height',$('div.results').height()).addClass('loading');
			$('div.results').empty();
			$.get("/ajax/",{
				'stype': 'events',
				'rtype': rtype,
				'type': stype,
				'ktype': ktype
			},function (data, textStatus){
				$('div.results').empty();
				$(data).prependTo('div.results');
				$('div.results').animate({
					height: ($('div.results table').height())? $('div.results table').height(): $('div.results p').height()
				},500,'',function(){
					if ($.browser.msie)	$('div.footer .copyright span').get(0).innerHTML+='';
				}).removeClass('loading');
			});
		}
		
		
		$('a#all-category').click(function(){
			rtype = $('dl.rtype ul li.active span').attr('rel');
			ktype = 0;
			$('dl.rtype').removeClass('disabled');
			$('dl.ktype').addClass('disabled');

			$(this).addClass('active');
			$('a#all-status').removeClass('active');
			eventstableload();
			return false;
		});

		$('a#all-status').click(function(){
			rtype = 0;
			ktype = $('dl.ktype ul li.active span').attr('rel');
			$('dl.rtype').addClass('disabled');
			$('dl.ktype').removeClass('disabled');

			$(this).addClass('active');
			$('a#all-category').removeClass('active');
			eventstableload();
			return false;
		});
	}

	//	a.crash
	var crash = false;
	var cr;
	$('a.crash').click(function(){		crash = !crash;
		if (crash){
			cr = $('<div class="crash"><span class="sst"></span>На сервер было совершено нападение, данные восстанавливаются. Приносим&nbsp;свои извинения.</div>').appendTo(document.body);
		}else{			cr.remove();		}
		return false;	});

	//	галерея
	if ($('div.cgallery').length){
		var gcnt = $('<div class="big-image"><span>1 из '+($('div.preview a').length)+'</span><p></p><div></div></div>').insertBefore('div.preview');
		$('<img src='+$('div.preview a:eq(0)').attr('href')+'>').appendTo(gcnt.find('div'));
		gcnt.find('p').html($('div.preview a:eq(0) img').attr('alt'));

		$('div.preview a:eq(0)').addClass('active');
		$('div.preview div').css('width',$('div.preview a').length*135);

		$('div.preview a').click(function(){
			var turl = this.href;
			var tdes = $(this).find('img').attr('alt');
            var index = $('div.preview a').index(this);
            var total = $('div.preview a').length;
			gcnt.find('span').html((index+1)+' из '+total);

			$('div.preview a').removeClass('active');
			$(this).addClass('active');

			gcnt.find('div').empty().addClass('load');
			var timg = $('<img />').load(function(){
				gcnt.find('div').empty().removeClass('load');
				$(this).appendTo(gcnt.find('div'));
			});

			timg.attr('src',turl);
			gcnt.find('p').html(tdes);

			var mleft = $(this).position().left;
            if (index+3<=total){
				$('div.preview div').animate({
					marginLeft: (mleft<135)? 0: (-1)*mleft+135
				},500);
			}else{
				$('div.preview div').animate({
					marginLeft: -(($('div.preview a').length*135)-540)
				},500);
			}

			return false;
		});
	}

	//	регистрация
	function codetitle(el){
		while (!el.hasClass('title') || !el.length){			el = el.prev();		}
		return el.text();	}

	if ($('form.register').length){		$('form.reg-spec div.cf1 input').bind('click',function(){			var i = 0;
			$('form.reg-spec div.cf1 input').each(function(){				$(this).attr('disabled',false);				if (this.checked) i++;			});
			if (i>0){				$('form.reg-spec div.cf1 input').each(function(){
					if (!this.checked) $(this).attr('disabled',true);
				});			}		});
		$('form.reg-spec div.cf2 input').bind('click',function(){
			var i = 0;
			$('form.reg-spec div.cf2 input').each(function(){
				$(this).attr('disabled',false);
				if (this.checked) i++;
			});
			if (i>2){
				$('form.reg-spec div.cf2 input').each(function(){
					if (!this.checked) $(this).attr('disabled',true);
				});
			}
		});
		$('form.reg-spec div.cf4 input').bind('click',function(){
			var i = 0;
			$('form.reg-spec div.cf4 input').each(function(){
				$(this).attr('disabled',false);
				if (this.checked) i++;
			});
			if (i>1){
				$('form.reg-spec div.cf4 input').each(function(){
					if (!this.checked) $(this).attr('disabled',true);
				});
			}
		});
		$('form.reg-spec div.cf5 input').bind('click',function(){
			var i = 0;
			$('form.reg-spec div.cf5 input').each(function(){
				$(this).attr('disabled',false);
				if (this.checked) i++;
			});
			if (i>0){				$('form.reg-spec div.cf5 input').each(function(){
					if (!this.checked) $(this).attr('disabled',true);
				});
			}
		});
		$('form.reg-spec div.cf6 input').bind('click',function(){
			var i = 0;
			$('form.reg-spec div.cf6 input').each(function(){
				$(this).attr('disabled',false);
				if (this.checked) i++;
			});
			if (i>1){
				$('form.reg-spec div.cf6 input').each(function(){
					if (!this.checked) $(this).attr('disabled',true);
				});
			}
		});
		$('form.reg-spec div.cf7 input').bind('click',function(){
			var i = 0;
			$('form.reg-spec div.cf7 input').each(function(){
				$(this).attr('disabled',false);
				if (this.checked) i++;
			});
			if (i>0){
				$('form.reg-spec div.cf7 input').each(function(){
					if (!this.checked) $(this).attr('disabled',true);
				});
			}
		});

		//	чекбоксы
		$('div.stype p input[type=checkbox]').each(function(){			if (!$(this).attr('checked')){				$(this).parents('p').addClass('disabled');
				$(this).parents('p').find('input[type=text]').attr('disabled',true);			}		})

		$('div.stype p input[type=checkbox]').bind('change',function(){
			if ($(this).attr('checked')){				$(this).parents('p').removeClass('disabled');
				$(this).parents('p').find('input[type=text]').attr('disabled',false);			}else{				$(this).parents('p').addClass('disabled');
				$(this).parents('p').find('input[type=text]').attr('disabled',true);			}		});

		$('dl.select').hover(function(){
			$(this).parent().addClass('hover');
			$(this).find('dd').show();
		},function(){
			$(this).parent().removeClass('hover');
			$(this).find('dd').hide();
		});

		$('dl.select ul span').live('click',function(){
			$(this).parents('dl.select dd').hide();
			$(this).parents('dl.select dd').find('li').removeClass('active');
			$(this).parent().addClass('active');
			$(this).parents('dl.select').find('dt').html($(this).html());
			$(this).parents('dl.select dd').find('input[type=hidden]').attr('value',$(this).attr('rel'));

			if ($(this).parents('dl.select').hasClass('scrollselect')){				var code = '+7 (';
				var ct = $(this).parents('div.multi-towns').find('span.code');
				ct.css('left','-2.2em');
				if ($.trim(codetitle($(this).parent()))=='Украина'){					code = '+380 (';
					ct.css('left','-3.3em');
				}
				if ($.trim(codetitle($(this).parent()))=='Белоруссия'){
					code = '+375 (';
					ct.css('left','-3.3em');
				}
				if ($.trim(codetitle($(this).parent()))=='Латвия'){
					code = '+371 (';
					ct.css('left','-3.3em');
				}				ct.html(code);			}
		});

		$('h3.reg-title dl.select dd ul li:eq(0)').addClass('active');

		$('h3.reg-title dl.select ul span').click(function(){
			var index = $('h3.reg-title dl.select ul li').index($(this).parent());
			$('form.register').hide();
			$('form.register:eq('+index+')').show();
		});

		$('h4.select-profile dl.select ul span').click(function(){
			var index = $(this).attr('rel');
			$('div.specialization').hide();
			$('div.cf'+index).show();
		});

		//	inputs

		$('form.register input.text').each(function(){
			if ($(this).attr('value')=='' || $(this).attr('value')==$(this).attr('title')){
				$(this).attr('value',$(this).attr('title'));
			}else{
				$(this).addClass('active');
			}
		});

		$('p:not(.button) span.hint:not(.town-show)').css('display','none');

		$('form.register input.text').blur(function(){
			$(this).parent().find('span.hint').css('display','none');
			if ($(this).attr('value')==''){
				$(this).attr('value',$(this).attr('title'));
				$(this).removeClass('active');
			}
		});

		$('form.register input.text').focus(function(){			$(this).parent().find('span.hint').css('display','inline');
			$(this).addClass('active');
			if ($(this).attr('value')==$(this).attr('title')){
				$(this).attr('value','');
			}
		});

		//	url

		$('#surl').blur(function(){
			$('#sturl').text(this.value);
		});

		//	reg-studio
		var rstitle = $('form.reg-studio input[name=title]');
		var rslink  = $('form.reg-studio input[name=link]');
		var rsemail = $('form.reg-studio input[name=email]');
		function rsvalidate(){			var flag = true;

			if (rstitle.attr('value')==rstitle.attr('title')) flag = false;
			if (rslink.attr('value')==rslink.attr('title')) flag = false;
			if (rsemail.attr('value')==rsemail.attr('title')) flag = false;
			if (flag)	$('form.reg-studio input[type=submit]').attr('disabled',false);			else	$('form.reg-studio input[type=submit]').attr('disabled',true);		}
		rsvalidate();
		rsemail.blur(rsvalidate);
		rslink.blur(rsvalidate);
		rstitle.blur(rsvalidate);

        //	reg-spec
		var rcprice = $('form.reg-spec input[name=price]');
		var rccontact = $('form.reg-spec input[name=contact]');
		var rclink  = $('form.reg-spec input[name=link]');
		var rcemail = $('form.reg-spec input[name=email]');
		function rcvalidate(){
			var flag = true;

			if (rcprice.attr('value')==rcprice.attr('title')) flag = false;
			if (rclink.attr('value')==rclink.attr('title')) flag = false;
			if (rcemail.attr('value')==rcemail.attr('title')) flag = false;
			if (rccontact.attr('value')==rccontact.attr('title')) flag = false;
			if (flag)	$('form.reg-spec input[type=submit]').attr('disabled',false);
			else	$('form.reg-spec input[type=submit]').attr('disabled',true);
		}
		rcvalidate();
		rcemail.blur(rcvalidate);
		rclink.blur(rcvalidate);
		rcprice.blur(rcvalidate);
		rccontact.blur(rcvalidate);

		//	reg-nb
		var rnemail = $('form.reg-nb input[name=email]');
		function rnvalidate(){
			var flag = true;
			if (rnemail.attr('value')==rnemail.attr('title')) flag = false;
			if (flag)	$('form.reg-nb input[type=submit]').attr('disabled',false);
			else	$('form.reg-nb input[type=submit]').attr('disabled',true);
		}
		rnvalidate();
		rnemail.blur(rnvalidate);

		//	submit
		$('form.register').submit(function(){
			$('form.register input.text').each(function(){
				if ($(this).attr('value')==$(this).attr('title')){
					$(this).attr('value','');
				}
			});
		});

		var ind = location.href.indexOf('#');
		if (ind != -1){var l = location.href.substring(ind+1);}
		if (l==1) $('h3.reg-title dl.select ul li:eq(0) span').click();
		if (l==2) $('h3.reg-title dl.select ul li:eq(1) span').click();
		if (l==3) $('h3.reg-title dl.select ul li:eq(2) span').click();

		var ctown = false;
		$('form.reg-studio span.town-show a').click(function(){			ctown = !ctown;			if (ctown){				$(this).parents('div.multi-towns').find('p.custom-town').show();
				$(this).parents('div.multi-towns').find('dl.select').addClass('disabled');
			}else{				$(this).parents('div.multi-towns').find('p.custom-town').hide();
				$(this).parents('div.multi-towns').find('dl.select').removeClass('disabled');
				var it = $(this).parents('div.multi-towns').find('p.custom-town input.text');
				it.attr('value',it.attr('title'));			}
			return false;		});

		$('form.reg-studio a.add-town').click(function(){			var cv = $('div.multi-towns:first').clone();
			cv.find('dl.select').removeClass('disabled')
			.hover(function(){				$(this).parent().addClass('hover');
				$(this).find('dd').show();
			},function(){				$(this).parent().removeClass('hover');
				$(this).find('dd').hide();
			});

			cv.find('input.text').blur(function(){
				$(this).parent().find('span.hint').css('display','none');
				if ($(this).attr('value')==''){
					$(this).attr('value',$(this).attr('title'));
					$(this).removeClass('active');
				}
			});

			cv.find('input.text').focus(function(){
				$(this).parent().find('span.hint').css('display','inline');
				$(this).addClass('active');
				if ($(this).attr('value')==$(this).attr('title')){
					$(this).attr('value','');
				}
			});
			cv.find('p.custom-town').remove();
			var ca = cv.find('span.town-show').html('× <a href="#">удалить</a>');
			ca.find('a').click(function(){				$(this).parents('div.multi-towns').remove();				return false;			});
			cv.insertBefore($(this).parent());
			return false;		});

	}

	//	профиль
	if ($('form.profile-studio').length){
		//
		var l = location.href;
		var ind = l.indexOf('#');
		if (ind != -1){			var l = l.substring(0,ind);
		}
		$('ul.list-type span').click(function(){
			$('div.profile-block1,div.profile-block2,div.profile-block3,div.profile-block4').hide();
			$('div.profile-block'+$(this).attr('rel')).show();
			document.location = l+'#'+$(this).attr('rel');
			$('form.profile-studio').attr('action',l+'#'+$(this).attr('rel'));

			$('ul.list-type li.active').animate({top:$(this).parent().position().top},500);
		});

		if (ind != -1){
			$('ul.list-type span[rel='+parseInt(location.href.substring(ind+1))+']').click();
		}

		$('a.p-edit').click(function(){
			var ttr = $(this).parent().parent();
			$('input.portfolio-title').attr('value',ttr.find('td:eq(0)').html());
			$('input.portfolio-link').attr('value',ttr.find('td:eq(1) a').attr('rel'));
			$('h4.portfolio-year dl span[rel='+$.trim(ttr.find('td:eq(2)').html())+']').click();
			$('h4.portfolio-type dl span[rel='+parseInt(ttr.find('td:eq(3)').attr('rel'))+']').click();
			$('h4.portfolio-category dl span[rel='+$.trim(ttr.find('td:eq(4)').html())+']').click();

			$('#portfolio-id').attr('value',parseInt(ttr.find('td:eq(5)').attr('rel')));

			$('body, html').animate({
				scrollTop: 0
			},500);
			return false;
		});

		/*$('form.reg-studio span.town-show a').click(function(){
			ctown = !ctown;
			if (ctown){
				$(this).parents('div.multi-towns').find('p.custom-town').show();
				$(this).parents('div.multi-towns').find('dl.select').addClass('disabled');
			}else{
				$(this).parents('div.multi-towns').find('p.custom-town').hide();
				$(this).parents('div.multi-towns').find('dl.select').removeClass('disabled');
				var it = $(this).parents('div.multi-towns').find('p.custom-town input.text');
				it.attr('value',it.attr('title'));
			}
			return false;
		});*/
		$('form.profile-studio span.town-show a').click(function(){
			$(this).parents('div.multi-towns').remove();
			return false;
		});

		$('form.profile-studio a.add-town').click(function(){
			var cv = $('div.multi-towns:first').clone();
			cv.find('dl.select').hover(function(){
				$(this).parent().addClass('hover');
				$(this).find('dd').show();
			},function(){
				$(this).parent().removeClass('hover');
				$(this).find('dd').hide();
			});

            cv.find('input.text').each(function(){            	$(this).attr('value',$(this).attr('title')).removeClass('active');            });
			cv.find('input.text').blur(function(){
				$(this).parent().find('span.hint').css('display','none');
				if ($(this).attr('value')==''){
					$(this).attr('value',$(this).attr('title'));
					$(this).removeClass('active');
				}
			});

			cv.find('input.text').focus(function(){
				$(this).parent().find('span.hint').css('display','inline');
				$(this).addClass('active');
				if ($(this).attr('value')==$(this).attr('title')){
					$(this).attr('value','');
				}
			});
			cv.find('p.custom-town').remove();
			cv.find('input[type=hidden]:last').remove();

			var ca = cv.find('span.town-show').html('× <a href="#">удалить</a>');
			ca.find('a').click(function(){
				$(this).parents('div.multi-towns').remove();
				return false;
			});
			cv.insertBefore($(this).parent());
			return false;
		});	}

	//	спецы

	if ($('form.profile-special').length){
		$('form.profile-special div.cf1 input').bind('click',function(){
			var i = 0;
			$('form.profile-special div.cf1 input').each(function(){
				$(this).attr('disabled',false);
				if (this.checked) i++;
			});
			if (i>0){
				$('form.profile-special div.cf1 input').each(function(){
					if (!this.checked) $(this).attr('disabled',true);
				});
			}
		});
		$('form.profile-special div.cf2 input').bind('click',function(){
			var i = 0;
			$('form.profile-special div.cf2 input').each(function(){
				$(this).attr('disabled',false);
				if (this.checked) i++;
			});
			if (i>2){
				$('form.profile-special div.cf2 input').each(function(){
					if (!this.checked) $(this).attr('disabled',true);
				});
			}
		});
		$('form.profile-special div.cf4 input').bind('click',function(){
			var i = 0;
			$('form.profile-special div.cf4 input').each(function(){
				$(this).attr('disabled',false);
				if (this.checked) i++;
			});
			if (i>1){
				$('form.profile-special div.cf4 input').each(function(){
					if (!this.checked) $(this).attr('disabled',true);
				});
			}
		});
		$('form.profile-special div.cf5 input').bind('click',function(){
			var i = 0;
			$('form.profile-special div.cf5 input').each(function(){
				$(this).attr('disabled',false);
				if (this.checked) i++;
			});
			if (i>0){
				$('form.profile-special div.cf5 input').each(function(){
					if (!this.checked) $(this).attr('disabled',true);
				});
			}
		});
		$('form.profile-special div.cf6 input').bind('click',function(){
			var i = 0;
			$('form.profile-special div.cf6 input').each(function(){
				$(this).attr('disabled',false);
				if (this.checked) i++;
			});
			if (i>1){
				$('form.profile-special div.cf6 input').each(function(){
					if (!this.checked) $(this).attr('disabled',true);
				});
			}
		});
		$('form.profile-special div.cf7 input').bind('click',function(){
			var i = 0;
			$('form.profile-special div.cf7 input').each(function(){
				$(this).attr('disabled',false);
				if (this.checked) i++;
			});
			if (i>0){
				$('form.profile-special div.cf7 input').each(function(){
					if (!this.checked) $(this).attr('disabled',true);
				});
			}
		});


		var i = 0;
		$('form.profile-special div.cf1 input').each(function(){$(this).attr('disabled',false);	if (this.checked) i++; });
		if (i>2) $('form.profile-special div.cf1 input').each(function(){if (!this.checked) $(this).attr('disabled',true);});

		i = 0;
		$('form.profile-special div.cf2 input').each(function(){$(this).attr('disabled',false);	if (this.checked) i++; });
		if (i>2) $('form.profile-special div.cf2 input').each(function(){if (!this.checked) $(this).attr('disabled',true);});

		i = 0;
		$('form.profile-special div.cf4 input').each(function(){$(this).attr('disabled',false);	if (this.checked) i++; });
		if (i>1) $('form.profile-special div.cf4 input').each(function(){if (!this.checked) $(this).attr('disabled',true);});

		i = 0;
		$('form.profile-special div.cf5 input').each(function(){$(this).attr('disabled',false);	if (this.checked) i++; });
		if (i>0) $('form.profile-special div.cf5 input').each(function(){if (!this.checked) $(this).attr('disabled',true);});

		i = 0;
		$('form.profile-special div.cf6 input').each(function(){$(this).attr('disabled',false);	if (this.checked) i++; });
		if (i>1) $('form.profile-special div.cf6 input').each(function(){if (!this.checked) $(this).attr('disabled',true);});

		i = 0;
		$('form.profile-special div.cf7 input').each(function(){$(this).attr('disabled',false);	if (this.checked) i++; });
		if (i>0) $('form.profile-special div.cf7 input').each(function(){if (!this.checked) $(this).attr('disabled',true);});
	}
});
