jQuery(document).ready(function() {
	//Пилим прокрутку
	jQuery('#up_scroller').click(function() {
		jQuery('html, body').stop().animate({
                        scrollTop: 0},
			1500,
			'easeInOutExpo');
	});
	
	if(jQuery.browser.msie && jQuery.browser.version == '6.0'){
		jQuery('#up_scroller').remove();
	}
	
	jQuery(window).scroll(function () {
		if( jQuery(window).scrollTop() > 550 ){
			if( !jQuery('#up_scroller').hasClass('scroller_show') ){
				jQuery('#up_scroller').removeClass('scroller_hide');
				jQuery('#up_scroller').addClass('scroller_show');
				jQuery('#up_scroller').fadeIn();
			}
		}
		
		if( jQuery(window).scrollTop() < 550 ){
			if( !jQuery('#up_scroller').hasClass('scroller_hide') ){
				jQuery('#up_scroller').removeClass('scroller_show');
				jQuery('#up_scroller').addClass('scroller_hide');
				jQuery('#up_scroller').fadeOut();
			}
		}
	});
	
	//Отслеживаем нажатие ctrl+enter для функционала "сообщить об ошибке"
	jQuery(document).keypress(function(event){
		if( (event.ctrlKey) && ( (event.keyCode == 0xA) || (event.keyCode == 0xD) ) ){
			var modMinHeight = 290;
			var txt = '';
			if (window.getSelection) {
			    txt = window.getSelection();
			} else if (document.getSelection) {
			    txt = document.getSelection();
			} else if (document.selection) {
			    txt = document.selection.createRange().text;
			}
			txt = txt.toString();
			
			if(txt != '' && txt.length <= 500){
				
				txt = 'Выделенный текст: <i>«'+txt+'»</i>';
				
				if( jQuery('form#error table').has('tr td#err_selected_txt').length == 0 ){
					jQuery('form#error table tr:eq(0)').after('<tr><td id="err_selected_txt">'+txt+'</td></tr><tr><td>&nbsp;</td></tr>');
				}else{
					jQuery('form#error table td#err_selected_txt').html(txt);
					jQuery('form#error input[name=error_selected_text]').remove();
				}
				jQuery('form#error').append('<input type="hidden" name="error_selected_text" value="'+txt+'" />');
				
				modMinHeight = modMinHeight + txt.length/3.5;
			}
			jQuery('form#error').modal({close: true, overlayClose:true, minHeight: modMinHeight});
		}
	});
	
        jQuery('form.ajax_uni_form input[type=button]').button();
	
	jQuery('form.ajax_uni_form input.city').focusin(function(){
		if(jQuery(this).val() == 'Москва'){
			jQuery(this).val('');
		}
	});
	
	jQuery('form.ajax_uni_form input.city').focusout(function(){
		if(jQuery(this).val() == ''){
			jQuery(this).val('Москва');
		}					    
	});
	
	
	/*
	  Универсальный обработчик аяксовых форм для сайта
	  Антибардак!
	*/
	
	jQuery('form.ajax_uni_form input[type=button]').click(function(){
		//Проверка заполненности полей
		var zero_flag = false;
		
		var form_type = jQuery(this).parents('form.ajax_uni_form').attr('id');
		
		jQuery('form#'+form_type+'.ajax_uni_form .req').each(function(){
			
			if( jQuery(this).val() == '' ){
				zero_flag = true;
				jQuery(this).css('background-color','red');
				
				jQuery(this).focusin(function(){
					jQuery(this).css('background-color','white');
				});
				
				jQuery(this).focusout(function(){
					
					if( jQuery(this).val() == '' ){
						jQuery(this).css('background-color','red');
					}
				});
			}else{
				jQuery(this).css('background-color','white');
			}
		});
		
		if( zero_flag === true ){
			jQuery('form#'+form_type+'.ajax_uni_form td.about_form_msg').html('<span class="red">Необходимо заполнить все поля формы!</span>');
			return false;
		}else{
			jQuery('form#'+form_type+'.ajax_uni_form td.about_form_msg').html('&nbsp;');
		}
		
		//Отправим стафф
		var data_to_send = jQuery('form#'+form_type+'.ajax_uni_form').serialize();
		
		//Если надо будет получить инфу откуда юзер заполнял форму
		data_to_send    += '&from_page='+window.document.location.href;
		
		//UPD 14.09.2011 Доп. инфа о браузере
		data_to_send    += '&browser_name='+navigator.appName;
		data_to_send    += '&browser_vervion='+navigator.appVersion;
		data_to_send    += '&browser_resolution='+screen.availWidth +'x'+ screen.availHeight;
		
		jQuery.ajax({
			type: 'POST',
			url: '/ajax/uni_form_send.php',
			data: data_to_send,
			beforeSend: function(){
				jQuery('form#'+form_type+'.ajax_uni_form img.ajax_form_load').show();
			},
			success: function(msg){
				
				//Google Analitycs tracking
				_gaq.push(['_trackPageview', '/goal/'+form_type]);
				
				jQuery('form#'+form_type+'.ajax_uni_form img.ajax_form_load').hide();
				jQuery('form#'+form_type+'.ajax_uni_form td.about_form_msg').html('<span style="color: green;">'+msg+'</span>');
				window.setTimeout('jQuery.modal.close();', 3000)
			},
			error: function(){
				jQuery('form#'+form_type+'.ajax_uni_form img.ajax_form_load').hide();
				jQuery('form#'+form_type+'.ajax_uni_form td.about_form_msg').html('<span class="red">Произошла ошибка!</span>');
			}
		});
		
		
		
	});
});

