$(document).ready(function(){
	// topmenu > submenu
	if ($.browser.msie && parseInt($.browser.version) == 6) $('#topmenu > li').hover(function(){$(this).addClass('sfhover');},function(){$(this).removeClass('sfhover');});
	
	$('#fast_search .sl_elms > a , .fast_search .sl_elms > a').click(function()
	{
		var curElm = $(this);
		if (curElm.hasClass('choosen') || curElm.hasClass('choose')) return false;

		var parentElm = curElm.parent('.sl_elms');
		parentElm.find('.choosen').removeClass('choosen');
		parentElm.find('.choosen_manual b').empty();
		curElm.addClass('choosen');
		if (curElm.parents('#fs_city').size() != 0){
			$('#f_city').val(curElm.attr('rel'));
		} else if (curElm.parents('#fs_type').size() != 0) {
			_id = curElm.attr('id');
			$('#f_type').val(_id);
			$('#f_type').attr('rel', _id);
			
			if (_id == 'a'){
				$('#fs_arenda').show();
				$('#fs_price').hide();
			} else {
				$('#fs_arenda').hide();
				$('#fs_price').show();
			}
		} else if (curElm.parents('#fs_price').size() != 0) {
			if (curElm.attr('id') == 'chas'){
				$('#fs_chas').show();
				$('#fs_chas').next().hide();
			} else {
				$('#fs_chas').hide();
				$('#fs_' + curElm.attr('id')).show();
			}
		}
		return false;
	});
	
	$('#fast_search #fs_submit').css({cursor:'pointer'});
	$('#fast_search #fs_submit').click(function(){
		if ($('#f_technics').val() == ''){
			alert('Для поиска выберите, пожалуйста, необходимый Вам вид техники!');
			return false;
		}
		return true;
	});
	
	$('#fast_search .f_price').click(function(){
		$("#f_price").val($(this).attr('id'));
		return false;
	});
	
	/* FIX: menu*/
	$('li.withsub > a').each(function(){
		if ($(this).next().size() == 0){
           $(this).parent().removeClass();
		}
	});
	
	// photo gallery
	$("a[rel^=group], .photo").fancybox({
		'padding': 35, 'overlayOpacity': 0.7, 'overlayColor': '#000', 'titlePosition': 'over', 'cyclic': true
	});
	
	$("a.photo_a").fancybox({
		'padding': 35, 'overlayOpacity': 0.7, 'overlayColor': '#000', 'titlePosition': 'over', 'cyclic': true
	});
	
	//drop attached files
	$('a[id^=del_button_]').live('click', function(){
		if (confirm("Вы уверены, что хотите удалить объявление?")) {
			getJSONData('/city.php?type=dropfile&id=' + $(this).attr('id').replace('del_button_', ''), function(_1){
				$('#lease_' + _1.id).remove();
			});
		}	
	});
	
	$('a.imagedrop').live('click', function(){
		if (confirm("Вы уверены, что хотите удалить фотографию ?")) {
			getJSONData('/city.php?type=dropimg&id=' + $(this).attr('rel'), function(_1){
				location.reload(true);
			});
		}
	});
	
	$('a.filedrop').live('click', function(){
		if (confirm("Вы уверены, что хотите удалить прикрепленный файл?")) {
			getJSONData('/city.php?type=dropfile&id=' + $(this).attr('rel').replace('file_id_', ''), function(_1){
				$('a[rel=file_id_' + _1.id + ']').parent().remove();
			});
		}
	});
	
	$('.jblock_left .jcarousel-skin-tango, .jblock_right .jcarousel-skin-tango').jcarousel({
		auto: 2,  wrap: 'last', scroll: 1, initCallback: mycarousel_initCallback
	});
	
	$('div.vblock > .jcarousel-skin-tango').jcarousel({
		auto: 2,  wrap: 'last', scroll: 1, vertical: true, initCallback: mycarousel_initCallback
	});
	
	// popups
	// login popup
	$("#login").fancybox({
		'padding' : 15,
		'overlayOpacity' : 0.7,
		'overlayColor' : '#000',
		'scrolling' : 'no',	
		'titleShow' : false,
		'href' : "popup/popup_login.html",
		'ajax' : {
		    type : "POST"
		},
		'onComplete' : function() {
			$.fancybox.center();
			$('#pl_login').trigger('focus');
			
			window.scrollTo(1,1);
			
			$('#pl_close','#popup_login').live('click',function(){
				var error = $('.error','#popup_login');
				var login = $.trim($('#pl_login').val());
				var passw = $.trim($('#pl_psw').val());
				error.hide();
				if (login && passw) {
					if (passw.length < 6){
						error.text('Неверный пароль!').show();
						return false;
					}
					if (!/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/.test(login)){
						error.text('Неверный логин!').show();
						return false;
					}
					$('#popup_login form').submit();
					//todo: post form
					$.fancybox.close();
				} else {
					error.text('Заполните все поля!').show();
				}
				return false;
			});
		}
	});
	// welcome popup
	$("#welcome").fancybox({
		'padding' : 15,
		'overlayOpacity' : 0.7,
		'overlayColor' : '#000',
		'scrolling' : 'no',	
		'titleShow' : false,
		'href' : "popup/popup_welcome.html",
		'ajax' : {
		    type : "POST"
		},
		'onComplete' : function() {
			$('#welcome_data').html('');
			$.ajax({
	            url		: "/welcome.php",
				type	: "GET",
				dataType: "html",
				timeout	: 3000,
				success	: function(response) {
	                $("#welcome_data").html(response);
					$.fancybox.center();
					window.scrollTo(1,1);
	            }
	        });
			$('#pl_close','#popup_welcome').live('click',function(){
				$.fancybox.close();
				return false;
			});
		}
	});	
	// choose city popup
	$("#choose_city").fancybox({
		'padding' : 15,
		'overlayOpacity' : 0.7,
		'overlayColor' : '#000',
		'scrolling' : 'no',
		'titleShow' : false,
		'centerOnScroll': true,
		'href' : "popup/popup_city.html",
		'ajax' : {
		    type : "POST"
		},
		'onComplete' : function() {
			$('#listbox_region > ul > li, #listbox_city > ul > li, #listbox_state > ul > li').remove();
			getJSONData('/city.php', regionJSON);
			$('#listbox_region, #listbox_city, #listbox_state').show();
			
			window.scrollTo(1,1);
			
			$('#pc_close','#popup_choose').live('click',function(){
				var error = $('.error','#popup_choose');
				error.hide();
				
				_1 = $('#listbox_city a.selected');

				_is_static = false;
				$('#fs_city a').each(function(){
					if (_1.size() > 0){
						if ($(this).attr('rel') != '' && $(this).attr('rel') == _1.attr('id').substr(2) && !_is_static){
							$('#f_city').val(_1.attr('id').substr(2));
							_is_static = true;
							$(this).trigger('click');
						}
					}
				});
				
				_2 = true;
				if (location.pathname == '/zakazy/' || location.pathname == '/quest/' || location.pathname == '/' ||
					location.pathname.indexOf('/arenda_tehniki/') != -1 ||
					location.pathname.indexOf('/prodazha_tehniki/') != -1
					){
					if (_1.size() == 0){
						_2 = false;
						_1 = $('#listbox_state a.selected');
					}
				}
				
				if (!_is_static){
					_selectedCity = _1.text();
					if (!_selectedCity) {
						error.show();
						return false;
					}
					$('.choosen','#fs_city').removeClass('choosen');
					$('.choosen_manual b','#fs_city').text(_selectedCity).parent('span').addClass('choosen');
					$('#f_city').val(_1.attr('id').substr(2));
					$('#russia').attr('checked', false);
				}
				$.fancybox.close();
				return false;
			});
		}
	});
	$('.listbox a','.popup_window').live('click',function(){
		var curElm = $(this);
		if (curElm.hasClass('selected')) return false;
		var parentElm = curElm.parents('ul');
		parentElm.find('a.selected').removeClass('selected');
		curElm.addClass('selected');
		return false;
	});
	// end choose city popup
	
	$('div.moderate_window div.allow, div.moderate_window div.deny').live('click',function(){
		_1 = $(this).attr('rel');
		$('#type_' + _1).val($(this).attr('class'));
		$(this).parents('form').submit();
		$('#moderate_button_' + _1).click();
	});

	// choose technic popup
	$("#choose_tech").fancybox({
		'padding' : 15,
		'overlayOpacity' : 0.7,
		'overlayColor' : '#000',
		'scrolling' : 'no',
		'titleShow' : false,
		'centerOnScroll': true,
		'href' : "popup/popup_tech.html",
		'ajax' : {
		    type : "POST"
		},
		'onComplete' : function() {
			$('#listbox_type1 > ul > li, #listbox_type2 > ul > li, #listbox_type3 > ul > li').remove();
			getJSONData('/tech.php?type=' + $('#f_type').attr('rel'), typeTechJSON);
			$('#listbox_type1, #listbox_type2, #listbox_type3').show();
			
			window.scrollTo(1,1);
			
			$('#pt_close','#popup_choose').live('click',function(){
				var error = $('.error','#popup_choose');
				error.hide();
				
				_2 = true;
				_1 = $('#listbox_type3 a.selected');
				if (location.pathname == '/' || location.pathname == '/zakazy/'){
					if (_1.size() == 0){
						_2 = false;
						_1 = $('#listbox_type2 a.selected');
					}
				}

				var selectedTech3 = _1.text();
				if (selectedTech3) {
					var selectedTech1 = '';
					if (location.pathname.indexOf('/zakazy/') == -1){
						selectedTech1 = $('#listbox_type1').find('a.selected').text();
						selectedTech1 = selectedTech1 ? selectedTech1 + ' / ' : '';
					}
					var selectedTech2 = $('#listbox_type2').find('a.selected').text();
					if (_2){
						selectedTech2 = selectedTech2 ? selectedTech2 + ' / ' : '';
						selectedTech = selectedTech1 + selectedTech2 + selectedTech3;
						$('#level').val(3);
					} else {
						selectedTech = selectedTech1 + selectedTech2;
						$('#level').val(2);
					}
					$('#f_technics', '#fs_technics').val(_1.attr('id').substr(2));
				} else {
					error.show();
					return false;
				}
				$('.choosen','#fs_technics').removeClass('choosen');
				$('.choosen_manual b','#fs_technics').text(selectedTech).parent('span').addClass('choosen');
				$.fancybox.close();
				return false;
			});
		}
	});
	// end choose technic popup
	
	// choose brand popup
	$("#choose_brand").fancybox({
		/*'padding' : 15,*/
		'overlayOpacity' : 0.7,
		'overlayColor' : '#000',
		'scrolling' : 'no',
		'titleShow' : false,
		'centerOnScroll': true,
		'href' : "popup/popup_brand.html",
		'ajax' : {
		    type : "POST"
		},
		'onComplete' : function() {
			if ($('#not_other').size() != 0){
				$('#other_block').hide();
			}
			$('#listbox_brand > ul > li').remove();
			getJSONData('/tech.php?mode=brand&id=' + trim($('#tech_type').val()), typeBrandJSON);
			$('#listbox_brand').show();

			/*window.scrollTo(1,1);*/
			
			$('#pt_close','#popup_brand').live('click',function(){
				var error = $('.error','#popup_brand');
				error.hide();
				_1 = $('#listbox_brand').find('a.selected');
				var selectedBrand = _1.text();
				if (!selectedBrand) {
					/* Проверка на другую марку */
					if ($('#other').attr('checked')){
						$('.choosen','#fs_brand').removeClass('choosen');
						$('.choosen_manual b','#fs_brand').text('Другая марка').parent('span').addClass('choosen');	
						$('#f_brand').val(0);
						$('#fs_brand_marka').show();
						$.fancybox.close();
						return false;
					} else {
						error.show();
						return false;
					}
				}
				$('.choosen','#fs_brand').removeClass('choosen');
				$('.choosen_manual b','#fs_brand').text(selectedBrand).parent('span').addClass('choosen');	
				$('#f_brand').val(_1.attr('id').substr(2));
				$('#fs_brand_marka').hide();
				$.fancybox.close();
				return false;
			});
		}
	});
	
	// choose brand popup
	$("#choose_trailer").fancybox({
		/*'padding' : 15,*/
		'overlayOpacity' : 0.7,
		'overlayColor' : '#000',
		'scrolling' : 'no',
		'titleShow' : false,
		'centerOnScroll': true,
		'href' : "popup/popup_brand.html",
		'ajax' : {
		    type : "POST"
		},
		'onComplete' : function() {
			if ($('#not_other').size() != 0){
				$('#other_block').hide();
			}
			
			$('#listbox_brand > ul > li').remove();
			getJSONData('/tech.php?mode=trailer&id=' + trim($('#tech_type').val()), typeBrandJSON);
			$('#listbox_brand').show();
			
			window.scrollTo(1,1);
			
			$('#pt_close','#popup_brand').live('click',function(){
				var error = $('.error','#popup_brand');
				error.hide();
				_1 = $('#listbox_brand').find('a.selected');
				var selectedBrand = _1.text();
				if (!selectedBrand) {
					/* Проверка на другую марку */
					if ($('#other').attr('checked')){
						$('.choosen','#fs_trailer').removeClass('choosen');
						$('.choosen_manual b','#fs_trailer').text('Другая марка').parent('span').addClass('choosen');	
						$('#f_trailer').val(0);
						$('#fs_trailer_marka').show();
						$.fancybox.close();
						return false;
					} else {
						error.show();
						return false;
					}
				}
				$('.choosen','#fs_trailer').removeClass('choosen');
				$('.choosen_manual b','#fs_trailer').text(selectedBrand).parent('span').addClass('choosen');	
				$('#f_trailer').val(_1.attr('id').substr(2));
				$('#fs_trailer_marka').hide();
				$.fancybox.close();
				return false;
			});
		}
	});
	// choose brand popup
	$("#choose_beacon").fancybox({
		/*'padding' : 15,*/
		'overlayOpacity' : 0.7,
		'overlayColor' : '#000',
		'scrolling' : 'no',
		'titleShow' : false,
		'centerOnScroll': true,
		'href' : "popup/popup_brand.html",
		'ajax' : {
		    type : "POST"
		},
		'onComplete' : function() {
			if ($('#not_other').size() != 0){
				$('#other_block').hide();
			}
			$('#listbox_brand > ul > li').remove();
			getJSONData('/tech.php?mode=beacon&id=' + trim($('#tech_type').val()), typeBrandJSON);
			$('#listbox_brand').show();
			
			window.scrollTo(1,1);
			
			$('#pt_close','#popup_brand').live('click',function(){
				var error = $('.error','#popup_brand');
				error.hide();
				_1 = $('#listbox_brand').find('a.selected');
				var selectedBrand = _1.text();
				if (!selectedBrand) {
					/* Проверка на другую марку */
					if ($('#other').attr('checked')){
						$('.choosen','#fs_beacon').removeClass('choosen');
						$('.choosen_manual b','#fs_beacon').text('Другая марка').parent('span').addClass('choosen');	
						$('#f_beacon').val(0);
						$('#fs_beacon_marka').show();
						$.fancybox.close();
						return false;
					} else {
						error.show();
						return false;
					}
				}
				$('.choosen','#fs_beacon').removeClass('choosen');
				$('.choosen_manual b','#fs_beacon').text(selectedBrand).parent('span').addClass('choosen');	
				$('#f_beacon').val(_1.attr('id').substr(2));
				
				$('#fs_beacon_marka').hide();
				$.fancybox.close();
				return false;
			});
		}
	});
	$("#choose_markap").fancybox({
		'overlayOpacity' : 0.7,
		'overlayColor' : '#000',
		'scrolling' : 'no',
		'titleShow' : false,
		'centerOnScroll': true,
		'href' : "popup/popup_brand.html",
		'ajax' : {
		    type : "POST"
		},
		'onComplete' : function() {
			if ($('#not_other').size() != 0){
				$('#other_block').hide();
			}
			$('#listbox_brand > ul > li').remove();
			getJSONData('/tech.php?mode=trailer&id=' + trim($('#tech_type').val()), typeBrandJSON);
			$('#listbox_brand').show();
			
			window.scrollTo(1,1);
			
			$('#pt_close','#popup_brand').die('click');
			$('#pt_close','#popup_brand').live('click', function(){
				var error = $('.error','#popup_brand');
				error.hide();
				_1 = $('#listbox_brand').find('a.selected');
				var selectedBrand = _1.text();
				if (!selectedBrand) {
					/* Проверка на другую марку */
					if ($('#other').attr('checked')){
						$('.choosen','#fs_markap').removeClass('choosen');
						$('.choosen_manual b','#fs_markap').text('Другая марка').parent('span').addClass('choosen');	
						$('#f_markap').val(0);
						$('#fs_markap_marka').show();
						$.fancybox.close();
						return false;
					} else {
						error.show();
						return false;
					}
				}
				$('.choosen','#fs_markap').removeClass('choosen');
				$('.choosen_manual b','#fs_markap').text(selectedBrand).parent('span').addClass('choosen');	
				$('#f_markap').val(_1.attr('id').substr(2));
				
				$('#fs_markap_marka').hide();
				$.fancybox.close();
				return false;
			});
		}
	});
	var markat = $("#choose_markat").fancybox({
		'overlayOpacity' : 0.7,
		'overlayColor' : '#000',
		'scrolling' : 'no',
		'titleShow' : false,
		'centerOnScroll': true,
		'href' : "popup/popup_brand.html",
		'ajax' : {
		    type : "POST"
		},
		'onComplete' : function() {
			if ($('#not_other').size() != 0){
				$('#other_block').hide();
			}
			$('#listbox_brand > ul > li').remove();
			getJSONData('/tech.php?mode=beacon&id=' + trim($('#tech_type').val()), typeBrandJSON);
			$('#listbox_brand').show();
			
			window.scrollTo(1,1);
			
			$('#pt_close','#popup_brand').die('click');
			$('#pt_close','#popup_brand').live('click',function(){
				var error = $('.error','#popup_brand');
				error.hide();
				_1 = $('#listbox_brand').find('a.selected');
				var selectedBrand = _1.text();
				if (!selectedBrand) {
					/* Проверка на другую марку */
					if ($('#other').attr('checked')){
						$('.choosen','#fs_markat').removeClass('choosen');
						$('.choosen_manual b','#fs_markat').text('Другая марка').parent('span').addClass('choosen');	
						$('#f_markat').val(0);
						$('#fs_markat_marka').show();

						$.fancybox.close();
						return false;
					} else {
						error.show();
						return false;
					}
				}
				$('.choosen','#fs_markat').removeClass('choosen');
				$('.choosen_manual b','#fs_markat').text(selectedBrand).parent('span').addClass('choosen');	
				$('#f_markat').val(_1.attr('id').substr(2));
				
				$('#fs_markat_marka').hide();
				$.fancybox.close();
				return false;
			});
		}
	});
	// end choose brand popup
	// choose model popup
	$("#choose_model").fancybox({
		'padding' : 15,
		'overlayOpacity' : 0.7,
		'overlayColor' : '#000',
		'scrolling' : 'no',
		'titleShow' : false,
		'href' : "popup/popup_model.html",
		'ajax' : {
		    type : "POST"
		},
		'onComplete' : function() {
			$('#pt_close','#popup_model').live('click',function(){
				var error = $('.error','.popup_window');
				error.hide();
				var selectedBrand = $('#listbox_brand').find('a.selected').text();
				if (!selectedBrand) {error.show();return false;}
				$('.choosen','#fs_model').removeClass('choosen');
				$('.choosen_manual b','#fs_model').text(selectedBrand).parent('span').addClass('choosen');
				$.fancybox.close();
				return false;
			});
		}
	});
	// end choose model popup
	
	// orders popups
	$(".button_orders","#content").each(function(){
		_1 = $(this).parent().attr('id').replace('element_', '');
		$(this).fancybox({
			'padding' : 15,
			'overlayOpacity' : 0.7,
			'overlayColor' : '#000',
			'scrolling' : 'no',
			'titleShow' : false,
			'href' : "rate.php?mode=new_rate&element_id=" + _1,
			'ajax' : {
				type : "POST"
			},
			'onComplete' : function() {
				$('#pt_close','#popup_order1').live('click',function(){
					var error = $('.error','#popup_order1');
					error.hide();
					var sum = $.trim($('#price').val());
					sum = parseFloat(sum);
					if (!sum) {$('#price').val(''); error.show(); return false;}
					$('#popup_order1 form').submit();
					setTimeout('alert("Ставка успешно создана!");$.fancybox.close();', 2000);
					return false;
				});
			}
		});
	});
	$(".button_orders_view","#content").each(function(){
		_1 = $(this).parent().parent().attr('id').replace('element_', '');
		$(this).fancybox({
			'padding' : 15,
			'overlayOpacity' : 0.7,
			'overlayColor' : '#000',
			'scrolling' : 'no',
			'titleShow' : false,
			'href' : "rate.php?mode=rates&element_id=" + _1,
			'ajax' : {
				type : "POST"
			},
			'onComplete' : function() {
			}
		});
	});
	// end orders popup
	
	
	// textarea with radiobutton disable
	$('textarea[rel]').parents('.radio').find('input:radio').change(function(){
		var ta = $(this).parents('.radio').find('textarea[rel]');
		var taRel = ta.attr('rel');
		if (taRel == $(this).attr('id')) ta.attr('disabled', false);
		else ta.attr('disabled', true);
	});
	
	// tabs
	var tabs = $('.tabs','#content');
	if (tabs.length > 0) {
		var activeTab = tabs.children('li:first').addClass('selected');
		$('#'+activeTab.children('a').attr('rel')).show();
	}
	/* tab menu */
	$('.tabs > li > a').click(function(){
		$(this).parents('ul').find('li.selected').removeClass('selected');
		$(this).parent('li').addClass('selected');
		var relAttr = $(this).attr('rel');
		$('.tab').hide(0,function(){$('#'+relAttr).show();});
		return false;
   	});
	
	$('.moderate_button','#content').click(function(){
		var thisElm = $(this);
		var thisElmWindow = thisElm.parent('div').children('.moderate_window');
		$('.moderate_window', '#content').not(thisElmWindow).slideUp();
		thisElm.parent('div').children('.moderate_window').slideToggle();
		return false;
	});
	$('.m_textbox','#content').focus(function(){if($(this).val() == 'Укажите причину') $(this).val('');});
	$('.m_textbox','#content').blur(function(){if($(this).val() == '') $(this).val('Укажите причину');});
	
	$("#fast_search #fs_submit").live('click',function()
	{	
		var ftechnics	= $('#f_technics').val();
		_ftype = $('#f_type').val();
		str = '';
		
		if(ftechnics) {
			str += 'catid=' + ftechnics;
		} else {
			str += 'catid=' + (_ftype == 'a' ? '1' : '5');
		}
		str +=  (_ftype=='a') ? '&isRent=true' : '';
		str +=  '&level=' + $('#level').val();
		
		if (_ftype=='a'){
			_fsfrom		= $('#fs_arenda_from').val();
			_fsto		= $('#fs_arenda_to').val();
			if($("#f_price").val() == 'smena'){
				str += '&fields_filter[arenda_8chas][0]=' + _fsfrom + '&fields_filter[arenda_8chas][1]=' + _fsto;
			} else {
				str += '&fields_filter[arenda_chas][0]=' + _fsfrom + '&fields_filter[arenda_chas][1]=' + _fsto;
			}
		} else {
			str += '&fields_filter[price][0]=' + $('#fs_sale_from').val() + '&fields_filter[price][1]=' + $('#fs_sale_to').val();
		}
		
		location.href='http://tio.su/quest/?'+str+'&fields_filter[gorod]=' + $('#f_city').val();
		return false;
	});
	
	/* welcome dialog */
	window.welcome();
});
function welcome(){
	_w = $.cookie('welcome');
	if (~~_w != 1){
		$.cookie('welcome', 1, { expires: 10000 });
		$("#welcome").trigger('click');
	}
};
function getJSONData(_1, _2){
	$.getJSON(_1, _2);
};
function regionJSON(_1){
	$('#listbox_region > ul > li, #listbox_city > ul > li, #listbox_state > ul > li').remove();
	$.each(_1, function(key, val) {
		_2 = '<li><a id="r_'+val.id+'" href="#">'+val.name+'</a></li>';
		$(_2).appendTo($('#listbox_region > ul'));
		$('#r_'+val.id).click(function(){
			$('#listbox_city > ul > li, #listbox_state > ul > li').remove();
			getJSONData('/city.php?type=state&id=' + this.id.substr(2), stateJSON);
		});
	});
};
function stateJSON(_1){
	$('#listbox_city > ul > li, #listbox_state > ul > li').remove();
	$.each(_1, function(key, val) {
		_2 = '<li><a id="s_'+val.id+'" href="#">'+val.name+'</a></li>';
		$(_2).appendTo($('#listbox_state > ul'));
		$('#s_'+val.id).click(function(){
			$('#listbox_city > ul > li').remove();
			getJSONData('/city.php?type=city&id=' + this.id.substr(2), cityJSON);
		});
	});
};
function cityJSON(_1){
	$('#listbox_city > ul > li').remove();
	$.each(_1, function(key, val) {
		_2 = '<li><a id="c_'+val.id+'" href="#">'+val.name+'</a></li>';
		$(_2).appendTo($('#listbox_city > ul'));
	});
};
function typeTechJSON(_1){
	$('#listbox_type1 > ul > li, #listbox_type2 > ul > li, #listbox_type3 > ul > li').remove();
	$.each(_1, function(key, val) {
		_2 = '<li><a id="t_'+key+'" href="#">'+val+'</a></li>';
		$(_2).appendTo($('#listbox_type1 > ul'));
		$('#t_'+key).click(function(){
			$('#listbox_type2 > ul > li, #listbox_type3 > ul > li').remove();
			getJSONData('/tech.php?mode=vid&id=' + this.id.substr(2), vidTechJSON);
		});
	});
};
function vidTechJSON(_1){
	$('#listbox_type2 > ul > li, #listbox_type3 > ul > li').remove();
	$.each(_1, function(key, val) {
		_2 = '<li><a id="v_'+key+'" href="#">'+val+'</a></li>';
		$(_2).appendTo($('#listbox_type2 > ul'));
		$('#v_'+key).click(function(){
			$('#listbox_type3 > ul > li').remove();
			getJSONData('/tech.php?mode=cat&id=' + this.id.substr(2), catTechJSON);
		});
	});
};
function typeBrandJSON(_1){
	$('#listbox_brand > ul > li').remove();
	$.each(_1, function(key, val) {
		_2 = '<li><a id="b_'+val.id+'" href="#">'+val.name+'</a></li>';
		$(_2).appendTo($('#listbox_brand > ul'));
	});
};
function catTechJSON(_1){
	$('#listbox_type3 > ul > li').remove();
	$.each(_1, function(key, val) {
		_2 = '<li><a id="c_'+key+'" href="#">'+val+'</a></li>';
		$(_2).appendTo($('#listbox_type3 > ul'));
	});
};
function addTechJSON(_1){
	$('#listbox_type2 > ul > li, #listbox_type3 > ul > li').remove();
	$.each(_1, function(key, val) {
		_2 = '<li><a id="c_'+key+'" href="#">' + val + '</a></li>';
		$(_2).appendTo($('#listbox_type2 > ul'));
		$('#c_' + key).click(function(){
			getJSONData('/tech.php?mode=adds&type=' + $('#f_type').val() + '&id=' + this.id.substr(2), addsTechJSON);
			return false;
		});
	});
};
function addsTechJSON(_1){
	$('#listbox_type3 > ul > li').remove();
	$.each(_1, function(key, val) {
		_2 = '<li><a id="d_' + key + '" href="' + val.link + '" title="' + val.name + '">' + val.name + '</a></li>';
		$(_2).appendTo($('#listbox_type3 > ul'));
	});
};
function mycarousel_initCallback(carousel){
    carousel.buttonNext.bind('click', function() {
        carousel.startAuto(0);
    });

    carousel.buttonPrev.bind('click', function() {
        carousel.startAuto(0);
    });

    carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });
};
function trim(s) {
	if (typeof s == 'undefined') return '';
	return s.replace(/^\s+/g, '').replace(/\s+$/g, '');
};
function buttonBehaviour() {
	var curElm = $(this);
	if (curElm.hasClass('choosen') || curElm.hasClass('choose')) return false;
	var parentElm = curElm.parent('.sl_elms');
	parentElm.find('.choosen').removeClass('choosen');
	parentElm.find('.choosen_manual b').empty();
	curElm.addClass('choosen');
};
function saveFormData(_1){
	if(!_1.id || $('#' + _1.id).size() == 0) {
		return true;
	}
	var _data = [];
	$('#' + _1.id + ' input[name^=data[new]], #' + _1.id + ' select[name^=data[new]], #' + _1.id + ' textarea[name^=data[new]], #' + _1.id + ' input[name^=f_]').each(function(){
		_2 = $(this);
		switch(_2.attr('type')){
			case 'password': break;
			case "text":
			case "textarea": {
				_data.push({type: 'T', name: _2.attr('name'), value: _2.val()});
				break;
			}
			case "checkbox":
			case "radio": {
				_data.push({type: 'C', name: _2.attr('name')/*, value: (input.checked ? 1 : 0)*/});
				break;
			}
			case "select-multiple":
			case "select-one": {
				_data.push({type: 'S', name: _2.attr('name'), value: $(this).find('option:selected').val()});
				break;
			}
		}
	});
	for(i in _data){
		if (_data[i].value == '' || _data[i].name == ''){
			continue;
		}
		$.cookie(_data[i].name, _data[i].value);
	}
	return true;
};
function restoreFormData(_1) {
	if(!_1 || $('#'+_1).size() == 0) {
		return true;
	}
	$('#' + _1 + ' input[name^=f_], #' + _1 + ' input[name^=data[new]], #' + _1 + ' select[name^=data[new]], #' + _1 + ' textarea[name^=data[new]]').each(function(){
		_2 = $(this);
		switch(_2.attr('type')){
			case 'password': break;
			case "text":
			case "textarea": 
			case "select-multiple":
			case "select-one": {
				_val = $.cookie(_2.attr('name'));
				if (typeof _val != 'undefined' && _val != '' && _val != null){
					_2.val(_val);
				}
				$.cookie(_2.attr('name'), null);
				break;
			}
			case "checkbox":
			case "radio": {
				//form.elements[elementData.name].checked = elementData.value ? true : false;
				break;
			}
		}
	});
};
function removeFormData(_1){
	if(!_1 || $('#'+_1).size() == 0) {
		return true;
	}
	$('#' + _1 + ' input[name^=f_], #' + _1 + ' input[name^=data[new]], #' + _1 + ' select[name^=data[new]], #' + _1 + ' textarea[name^=data[new]]').each(function(){
		$.cookie($(this).attr('name'), null);
	});
};
