$(document).ready(function(){
$('textarea[limit]').limit({leftChars:'осталось'});

   if ($('#userAvatarInput').length > 0) {
        var logo_upload = new AjaxUpload('userAvatarInput', {
            action: '/xajax/xajax_broker.php',
            autoSubmit: true,
            data: {
                func: 'brNewPhotoSave',
                responseType: 'xml'
            },
            onChange: function(file, ext){
                if (!(ext && /^(jpg|jpeg|png|gif)$/.test(ext))) {
                    alert('Необходимо выбрать файл JPG, PNG или GIF.')
                    return false;
                }
            },
            onSubmit: function(file, ext){
                showWaitWindow();
            },
            onComplete: function(file, response){
                closeWaitWindow();
                if ($('error', response).text()) {
                    alert($('error', response).text());
                } else {
                    var fname = $('filename', response).text();
                    $("#userAvatar").attr('src',fname);
                    $("#photoDeletor").show();
                }
            }
        });
    }
    if ($("#photoDeletor").length > 0)
    $("#photoDeletor").click(function(){
        if (confirm("Удалить Вашу фотографию?")) {
            $.post("/xajax/xajax.php", {
                func: "delUserPhoto"
            }, function(xml){
                var state = $("state", xml).text();
                
                if (state = 'deleted') {
                    $("#userAvatar").attr('src', '/img/def/66x66.gif');
                    $("#photoDeletor").hide();
                }
            }, "xml");
            
        }
        return false;
    }); 
    
	$('#agree').click(function(){
		if ($(this).attr('checked')) {
			$('#refresh').attr('disabled', false);
		} else {
			$('#refresh').attr('disabled', true);
		}
	});
	
	$("#topMenuShower").click(function(e){
		e.preventDefault();
		$("#topMenuSmall").hide();
		$("#topMenuExtended").show();
	});
	
	$("#topMenuHider").click(function(e){
		e.preventDefault();
		$("#topMenuExtended").hide();
		$("#topMenuSmall").show();
	});
	
	var user_cur_city = $("input[name='user_cur_city']").attr('value');
	var user_cur_region = $("input[name='user_cur_region']").attr('value');
	var user_cur_country = $("input[name='user_cur_country']").attr('value');
	
	var comp_cur_city = $("input[name='comp_cur_city']").attr('value');
	var comp_cur_region = $("input[name='comp_cur_region']").attr('value');
	var comp_cur_country = $("input[name='comp_cur_country']").attr('value');
	
	$.post("/xajax/xajax.php", {
		func: "getGeoList",
		geoTarget: "country"
	}, function(xml){
		//Пришел ответ-список регионов
		$("#user_country").html($("option", xml).get());
		$("#user_country > option[value=0]").attr('checked', true);
		
		$("#comp_country").html($("option", xml).get());
		$("#comp_country > option[value=0]").attr('checked', true);
		
		//Обработчик выбора страны 
		$("#user_country").change(function(e){
			e.preventDefault();
			$("#user_country option[value=0]").attr('checked', false);
			
			$.post("/xajax/xajax.php", {
				func: "getGeoList",
				geoParent: $(this).attr('value'),
				geoTarget: "region"
			}, function(xml){
			
				//Пришел ответ-список регионов
				$("#user_region").html($("option", xml).get());
				$("#user_region > option[value=0]").attr('checked', true);
				
				//Обработчик выбора региона 
				$("#user_region").change(function(){
					$.post("/xajax/xajax.php", {
						func: "getGeoList",
						geoParent: $(this).attr('value'),
						geoTarget: "city"
					}, function(xml){
						//Пришел ответ-список городов
						$("#user_city").html($("option", xml).get());
						$("#user_city > option[value=0]").attr('checked', true);
						
						if (user_cur_city > 0) {
							$("#user_city").attr('value', user_cur_city);
							user_cur_city = 0;
						}
					}, "html");
				});
				
				if (user_cur_region > 0) {
					$("#user_region").attr('value', user_cur_region);
					user_cur_region = 0;
					$("#user_region").change();
				}
			}, "html");
			
		});
		if (user_cur_country > 0) {
			$("#user_country").attr('value', user_cur_country);
			user_cur_country = 0;
			$("#user_country").change();
		}
		
		//Обработчик выбора страны компании
		$("#comp_country").change(function(e){
			e.preventDefault();
			$("#comp_country option[value=0]").attr('checked', false);
			
			$.post("/xajax/xajax.php", {
				func: "getGeoList",
				geoParent: $(this).attr('value'),
				geoTarget: "region"
			}, function(xml){
			
				//Пришел ответ-список регионов
				$("#comp_region").html($("option", xml).get());
				$("#comp_region > option[value=0]").attr('checked', true);
				
				//Обработчик выбора региона 
				$("#comp_region").change(function(){
					$.post("/xajax/xajax.php", {
						func: "getGeoList",
						geoParent: $(this).attr('value'),
						geoTarget: "city"
					}, function(xml){
						//Пришел ответ-список городов
						$("#comp_city").html($("option", xml).get());
						$("#comp_city > option[value=0]").attr('checked', true);
						
						if (comp_cur_city > 0) {
							$("#comp_city").attr('value', comp_cur_city);
							comp_cur_city = 0;
						}
						
					}, "html");
				});
				if (comp_cur_region > 0) {
					$("#comp_region").attr('value', comp_cur_region);
					comp_cur_region = 0;
					$("#comp_region").change();
				}
			}, "html");
			
		});
		
		if (comp_cur_country > 0) {
			$("#comp_country").attr('value', comp_cur_country);
			comp_cur_country = 0;
			$("#comp_country").change();
		}
	}, "html");
	
	//Заполнение годов рождения
	var years = '';
	for (var i = 2000; i >= 1950; i--) {
		years += '<option value="' + i + '">' + i + '</option>\n';
	}
	$("select[name='birth_year']").append(years);
	
	//Устанавливаем правильные даты
	var day = $("input[name='cur_b_day']").attr('value');
	var month = $("input[name='cur_b_month']").attr('value');
	var year = $("input[name='cur_b_year']").attr('value');
	
	$("select[name='birth_day'] > option[value=" + day + "]").attr('checked', true);
	$("select[name='birth_month'] > option[value=" + month + "]").attr('checked', true);
	$("select[name='birth_year'] > option[value=" + year + "]").attr('checked', true);
	
	
	
	//Обрабатываем "Да, я хочу"
	$("#wantSubscribe").change(function(){
		$.post("/xajax/xajax.php", {
			func: "changeSubscribeState",
			sub_state: $(this).attr('checked')
		}, function(xml){
			var state = $("state", xml).text();
		
		}, "html");
	});
});

