if (cab_home)
	var server_url = cab_home + 'cmd.php';
else
	var server_url = '/cmd.php';

var server = jQuery.rpc(server_url, 'xml'), svf_lock = null;

jQuery(function() {
	jQuery('ul.menu li a').each(function() {
		if (jQuery(this).attr('href').indexOf('Itemid=15') != -1)
			jQuery(this).click(function() {
				return cabShowLoginWindow();
			});
	});
	jQuery('#cab-login-form').dialog(
			{
				autoOpen : false,
				height : 270,
				width : 300,
				minHeight : 270,
				minWidth : 300,
				modal : true,
				buttons : {
					'Вход' : function() {
						jQuery('#cab-login-form #email, #cab-login-form #pass')
								.removeClass('ui-state-error');
						server.cabinet.login_data(
								function(data) {
									if (!cabAjaxIsError(data)
											&& !cabFormIsError(
													'cab-login-form', data)) {
										jQuery('#cab-login-form').dialog(
												'close');
										jQuery('#cab-railcar-form').dialog(
												'open');
									}
								}, cabGetFormData('cab-login-form'));
					},
					'Отмена' : function() {
						jQuery(this).dialog('close');
					}
				},
				open : function() {
					cabHideSWF(this);
				},
				close : function() {
					cabShowSWF(this);
					jQuery('#cab-login-form #email, #cab-login-form #pass')
							.val('').removeClass('ui-state-error');
					jQuery('#cab-login-form .validateTips').text(
							'Введите Ваш email и пароль.');
				}
			});
	jQuery('#cab-remind-form').dialog(
			{
				autoOpen : false,
				height : 200,
				width : 300,
				minHeight : 200,
				minWidth : 300,
				modal : true,
				buttons : {
					'ОК' : function() {
						jQuery('#cab-remind-form #email').removeClass(
								'ui-state-error');
						server.cabinet.remind_data(
								function(data) {
									if (!cabAjaxIsError(data)
											&& !cabFormIsError(
													'cab-remind-form', data)) {
										jQuery('#cab-remind-form').dialog(
												'close');
										cabMessage(data.result.text);
									}
								}, cabGetFormData('cab-remind-form'));
					},
					'Отмена' : function() {
						jQuery(this).dialog('close');
						jQuery('#cab-login-form').dialog('open');
					}
				},
				open : function() {
					cabHideSWF(this);
				},
				close : function() {
					cabShowSWF(this);
					jQuery('#cab-remind-form #email').val('').removeClass(
							'ui-state-error');
					jQuery('#cab-remind-form .validateTips').html('&nbsp;');
				}
			});
	jQuery('#cab-registration-form')
			.dialog(
					{
						autoOpen : false,
						height : 470,
						width : 300,
						minHeight : 470,
						minWidth : 300,
						modal : true,
						buttons : {
							'ОК' : function() {
								jQuery(
										'#cab-registration-form #name, #cab-registration-form #company_name, #cab-registration-form #phone, #cab-registration-form #email, #cab-registration-form #pass, #cab-registration-form #re_pass')
										.removeClass('ui-state-error');
								server.cabinet
										.registration_data(
												function(data) {
													if (!cabAjaxIsError(data)
															&& !cabFormIsError(
																	'cab-registration-form',
																	data)) {
														jQuery(
																'#cab-registration-form')
																.dialog('close');
														cabMessage(data.result.text);
													}
												},
												cabGetFormData('cab-registration-form'));
							},
							'Отмена' : function() {
								jQuery(this).dialog('close');
								jQuery('#cab-login-form').dialog('open');
							}
						},
						open : function() {
							cabHideSWF(this);
						},
						close : function() {
							cabShowSWF(this);
							jQuery(
									'#cab-registration-form #name, #cab-registration-form #company_name, #cab-registration-form #phone, #cab-registration-form #email, #cab-registration-form #pass, #cab-registration-form #re_pass')
									.val('').removeClass('ui-state-error');
							jQuery('#cab-registration-form .validateTips')
									.html('&nbsp;');
						}
					});
	jQuery('#cab-railcar-form')
			.dialog(
					{
						autoOpen : false,
						height : 270,
						width : 600,
						minHeight : 270,
						minWidth : 600,
						modal : true,
						buttons : {
							'Выйти из кабинета' : function() {
								server.cabinet.logout(function(data) {
									if (!cabAjaxIsError(data))
										jQuery('#cab-railcar-form').dialog(
												'close');
								});
							},
							'Закрыть' : function() {
								jQuery(this).dialog('close');
							}
						},
						open : function() {
							cabHideSWF(this);
						},
						close : function() {
							cabShowSWF(this);
							jQuery('#cab-railcar-form #number').val('')
									.removeClass('ui-state-error');
							jQuery(
									'#cab-railcar-form .validateTips, #cab-railcar-container')
									.html('&nbsp;');
						}
					});
	jQuery('#cab-railcar-form #cab-railcar-submit').button().click(
			function() {
				jQuery('#cab-railcar-form #number').removeClass(
						'ui-state-error');

				server.cabinet
						.rail_car_search(
								function(data) {
									if (!cabAjaxIsError(data)
											&& !cabFormIsError(
													'cab-railcar-form', data)) {
										jQuery('#cab-railcar-container').html(
												data.result.html);
									}
								}, cabGetFormData('cab-railcar-form'));
			});
});

/** *********************************************************************** */

jQuery.extend({
	getUrlVars : function() {
		var vars = [], hash;
		var hashes = window.location.href.slice(
				window.location.href.indexOf('?') + 1).split('&');
		for ( var i = 0; i < hashes.length; i++) {
			hash = hashes[i].split('=');
			vars.push(hash[0]);
			vars[hash[0]] = hash[1];
		}
		return vars;
	},
	getUrlVar : function(name) {
		return jQuery.getUrlVars()[name];
	},
	textToHtml : function(str) {
		str = str.replace('\r\n', '<br />').replace('\n', '<br />').replace(
				'\r', '');

		return str;
	}
});

if (jQuery.getUrlVar('uaction') == 'confirm') {
	jQuery(function() {
		data = {
			'id' : jQuery.getUrlVar('uid'),
			'key' : jQuery.getUrlVar('ukey')
		};
		server = jQuery.rpc(server_url, 'xml', function() {
			server.cabinet.registration_confirm(function(data) {
				if (!cabAjaxIsError(data))
					cabMessage(data.result.text);
			}, data);
		});
	});
}

/** *********************************************************************** */

function cabShowSWF(obj) {
	if (svf_lock == obj) {
		jQuery('#spot object').show();
		svf_lock = null;
	}
}

function cabHideSWF(obj) {
	if (!svf_lock)
		svf_lock = obj;
	jQuery('#spot').css({
		'width' : jQuery('#spot').width(),
		'height' : jQuery('#spot').height()
	});
	jQuery('#spot object').hide();
}

function cabGetFormData(id) {
	var data = {};
	jQuery('#' + id).find(':input').each(function() {
		if (this.name != '') {
			if (this.type == 'radio') {
				if (jQuery(this).is(':checked'))
					data[this.name] = jQuery(this).val();
			} else
				data[this.name] = jQuery(this).val();
		}
	});

	return data;
}

function cabAjaxIsError(data) {
	if (data.error) {
		cabError(data.error.faultString);
		return true;
	} else
		return false;
}

function cabFormIsError(id, data) {
	if (data.result.error) {
		ivnUpdateTips(id, data.result.text);
		if (data.result.field)
			jQuery('#' + id + ' #' + data.result.field).addClass(
					'ui-state-error').focus();
		return true
	}
	return false;
}

function ivnUpdateTips(id, t) {
	var tips = jQuery('#' + id + ' .validateTips');
	tips.text(t).addClass('ui-state-highlight');
	setTimeout(function() {
		tips.removeClass('ui-state-highlight', 1500);
	}, 500);
}

/** *********************************************************************** */

function cabConfirm(title, text, yes, no) {
	if (!no)
		no = function() {
			jQuery(this).dialog('close');
		};
	jQuery('#confirm-dialog').attr({
		'title' : title
	}).find('.dialog-message').html(text);
	jQuery('#confirm-dialog').dialog({
		resizable : false,
		modal : true,
		buttons : {
			'Да' : yes,
			'Нет' : no
		},
		open : function() {
			cabHideSWF(this);
		},
		close : function() {
			cabShowSWF(this);
		}
	});
}
function cabError(text, ok) {
	if (!ok)
		ok = function() {
			jQuery(this).dialog('close');
		};
	jQuery('#error-dialog .dialog-message').html(text);
	jQuery('#error-dialog').dialog({
		resizable : false,
		modal : true,
		buttons : {
			'ОК' : ok
		},
		open : function() {
			cabHideSWF(this);
		},
		close : function() {
			cabShowSWF(this);
		}
	});
}
function cabMessage(text, ok) {
	if (!ok)
		ok = function() {
			jQuery(this).dialog('close');
		};
	jQuery('#message-dialog .dialog-message').html(text);
	jQuery('#message-dialog').dialog({
		resizable : false,
		modal : true,
		buttons : {
			'ОК' : ok
		},
		open : function() {
			cabHideSWF(this);
		},
		close : function() {
			cabShowSWF(this);
		}
	});
}

/** *********************************************************************** */

function cabShowLoginWindow() {
	server.cabinet.is_logged(function(data) {
		if (!cabAjaxIsError(data)) {
			if (data.result.logged)
				jQuery('#cab-railcar-form').dialog('open');
			else {
				jQuery('#cab-login-form').dialog('open');
			}
		}
	});

	return false;
}

function cabShowRemindWindow() {
	server.cabinet.remind_form(function(data) {
		if (!cabAjaxIsError(data)) {
			jQuery('#cab-login-form').dialog('close');
			jQuery('#cab-remind-form').dialog('open');
		}
	});
}

function cabShowRegistrationWindow() {
	server.cabinet.registration_form(function(data) {
		if (!cabAjaxIsError(data)) {
			jQuery('#cab-login-form').dialog('close');
			jQuery('#cab-registration-form').dialog('open');
		}
	});
}
