
$(function() { 
	
	$("#call").dialog({
		autoOpen: false,
		//height: 530,
		width: 430,
		autoHeight:true,
		autoWidth:true,
		resizable: false,
		zIndex: 29000,
		modal:true
	});
	$("#dialog-wait").dialog({
		autoOpen: false,
		height: 100,
		width: 250,
		resizable: false,
		modal:true,
		zIndex: 29001
	});
	$("#dialog-error").dialog({
		autoOpen: false,
		minHeight: 100,
		width: 250,
		resizable: false,
		modal:true,
		zIndex: 29002
	});
});
function dialogError(text){
	$("#dialog-error").dialog('open');
	Ext.get('results_form').update(text);
}
function showForm(){
	$("#call").dialog('open');
}
function callbackpostForm(){
	
	Ext.Ajax.request({
		url:'/form.php',
		form:'form_r',
		method:'post',
		success:function(o){
		 $('#dialog-wait').dialog('close');
		var res = Ext.decode(o.responseText);
		if (res.success){
			$('#call').dialog("destroy");
			dialogError('Спасибо. Ждите звонка в указанное время');
			
		}
		else {
			if (res.msg){
				dialogError(res.msg);
				//alert(res.msg);
			}
			else {
				alert(o.responseText);
			}
		}
		
	},
	failure:function(){
		 $('#dialog-wait').dialog('close');
		 dialogError('Во время отправки данных произошла ошибка.');
	}
	});
	
}
/**
 * --------------------------------------------------------------------
 * jQuery customfileinput plugin
 * Author: Scott Jehl, scott@filamentgroup.com
 * Copyright (c) 2009 Filament Group 
 * licensed under MIT (filamentgroup.com/examples/mit-license.txt)
 * --------------------------------------------------------------------
 */
$.fn.customFileInput = function(){
	return $(this).each(function(){
	//apply events and styles for file input element
		
	var fileInput = $(this)
		.addClass('customfile-input') //add class for CSS
		.mouseover(function(){ upload.addClass('customfile-hover'); })
		.mouseout(function(){ upload.removeClass('customfile-hover'); })
		.focus(function(){
			upload.addClass('customfile-focus'); 
			fileInput.data('val', fileInput.val());
		})
		.blur(function(){ 
			upload.removeClass('customfile-focus');
			$(this).trigger('checkChange');
		 })
		 .bind('disable',function(){
		 	fileInput.attr('disabled',true);
			upload.addClass('customfile-disabled');
		})
		.bind('enable',function(){
			fileInput.removeAttr('disabled');
			upload.removeClass('customfile-disabled');
		})
		.bind('checkChange', function(){
			if(fileInput.val() && fileInput.val() != fileInput.data('val')){
				fileInput.trigger('change');
			}
		})
		.bind('change',function(){
			//get file name
			var fileName = $(this).val().split(/\\/).pop();
			//get file extension
			var fileExt = 'customfile-ext-' + fileName.split('.').pop().toLowerCase();
			//update the feedback
			uploadFeedback
				.text(fileName) //set feedback text to filename
				.removeClass(uploadFeedback.data('fileExt') || '') //remove any existing file extension class
				.addClass(fileExt) //add file extension class
				.data('fileExt', fileExt) //store file extension for class removal on next change
				.addClass('customfile-feedback-populated'); //add class to show populated state
			//change text of button	
			uploadButton.text('Change');	
		})
		.click(function(){ //for IE and Opera, make sure change fires after choosing a file, using an async callback
			fileInput.data('val', fileInput.val());
			
			setTimeout(function(){
				fileInput.trigger('checkChange');
			},100);
		});
		
	//create custom control container
	var upload = $('<div class="customfile"></div>');
	//create custom control button
	var uploadButton = $('<span class="customfile-button" aria-hidden="true">&nbsp;</span>').appendTo(upload);
	//create custom control feedback
	var uploadFeedback = $('<span class="customfile-feedback" aria-hidden="true">Файл не выбран...</span>').appendTo(upload);
	
	//match disabled state
	if(fileInput.is('[disabled]')){
		fileInput.trigger('disable');
	}
		
	
	//on mousemove, keep file input under the cursor to steal click
	upload.mousemove(function(e){
		//alert( e.pageX  - ((fileInput.outerWidth() + 20)*2))
		//console.log(e.pageX+' -- '+e.pageY);
		
			fileInput.css({
				'left':100,//e.pageX  - (355*2), //position right side 20px right of cursor X)
				'top':'0px',
				'height':'50px',
				'width':'355px'
			});	
		})
		.insertAfter(fileInput); //insert after the input
	
	fileInput.appendTo(upload);
		
	});
};
