jQuery(document).ready(function(){

	var Phone = {

		param: {
			action: "http://universe.uiscom.ru/centrex/callme/make_call/",
			h: "8OL3yiaMfgmi0yzbHgaaHaI0Ydcce3KmQuonNz03Svk14TcK0mVQxC_vQbI4erp5",
			captcha_key: "",
			captcha_value: "",
			phone: ""
		},

		div: '',
		form: '',
		status: '',
		url: {
			capcha: "http://universe.uiscom.ru/centrex/callme/get_captcha_image/?key="
		},

		init: function() {
			this.div = jQuery('#phone');
			this.form = jQuery('#phone-form');
			this.title = jQuery('#phone-title');

			this.hideForm();
			this.div.click(function() {
				Phone.showForm();
			});

			this.form.find('button').click(function() {
				jQuery("#phone-msg").html('');

				Phone.param.captcha_value = jQuery("#phone-capcha").val();
				Phone.param.phone = '7' + jQuery("#phone-city").val() + jQuery("#phone-tel").val();

				jQuery.ajax({
					type: "POST",
					url: "proxy.php",
					dataType: "json",
					data: Phone.param,
					success: function(res){
						if (res.status) {

						} else {
							jQuery("#phone-msg").html(res.msg);
							jQuery("#phone-capcha").val('');
							Phone.setCapche();
						}
					}
				});

			});
		},

		hideForm: function() {
			this.form.hide();
			this.title.show();
			this.setCapche();
		},

		setCapche: function() {
			var key = this._uuid(70);
			this.param.captcha_key = key;
			this.form.find('img').attr('src', this.url.capcha + key);
		},

		showForm: function() {
			this.title.hide();
			this.form.show();
		},

		_uuid: function(len) {
			var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
			var uuid = [];

			for (var i = 0; i < len; i++) {
				uuid[i] = chars[Math.floor(Math.random()*64)];
			}
			return uuid.join('');
		}
	};

	Phone.init();

});

