var jQuery_1_3_2 = $.noConflict();
(function ($) {
	$(function () {
		$("#dialog-login, #dialog-register, #dialog-forgot").bind("keydown", function (e) {
			var key = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0; 
			switch (key) { 
				case 13:
					return false;
					break;
			}			
		});

		$(".downloadPDF").live("click", function (e) {
			e.preventDefault();
			$this = $(this);
			$.ajax({
				type: "GET",
				data: {action: 'check-login'},
				dataType: "json",
				url: "chat-blog/include/ajax.php",
				success: function (json) {
					switch (json.status)
					{
						case true:
							window.location = "chat-blog/include/ajax.php?action=download&file="+ encodeURIComponent($this.attr("rel"));
							return;
							break;
						case false:
							$("#dialog-login").dialog("open");
							break;
					}
				}
			});
		});

		$("a.forgot-link").live("click", function (e) {
			e.preventDefault();
			if ($("#dialog-login").dialog("isOpen"))
			{
				$("#dialog-login").dialog("close");
			}
			if ($("#dialog-register").dialog("isOpen"))
			{
				$("#dialog-register").dialog("close");
			}
			$("#dialog-forgot").dialog("open");
		});

		$('#dialog-login').dialog({
			autoOpen: false,
			bgiframe: true,
			resizable: false,
			draggable: false,
			width: 580,
			height: 300,
			modal: true,
			overlay: {
				backgroundColor: '#909090',
				opacity: 0.5
			},
			close: function(){

			},
			open: function (event, ui) {
				$.get('chat-blog/include/ajax.php', {action: "login-form"}, function (data) {
					$('#dialog-login').html(data);
				});
			},
			buttons: {
				"Sign Up. It's free and anyone can join": function() {
					$(this).dialog('close');
					$('#dialog-register').dialog('open');
				},
				'Close': function() {
					$(this).dialog('close');
				},
				'Login': function() {
					var $this = $(this);
					$.ajax({
						type: 'POST',
						data: {action : "login", email: $("#dialog-login").find("input[name='email']").val(), password: $("#dialog-login").find("input[name='password']").val(), keep: ($("#dialog-login").find("input[name='keep']").is(":checked") ? 1 : 0)},
						dataType: "json",
						url: 'chat-blog/include/ajax.php',
						success: function (json) {
							switch (json.status) {
								case true:
									$this.dialog('close');
								break;
								case false:
									jQuery_1_3_2("#dialog-login span").show();
								break;
							}
						}
					});
				}
			}
		});
		$('#dialog-register').dialog({
			autoOpen: false,
			bgiframe: true,
			resizable: false,
			draggable: false,
			width: 540,
			height: 400,
			modal: true,
			overlay: {
				backgroundColor: '#909090',
				opacity: 0.5
			},
			close: function(){

			},
			open: function (event, ui) {
				$.get('chat-blog/include/ajax.php', {action: "register-form"}, function (data) {
					$('#dialog-register').html(data);
				});
			},
			buttons: {
				"Already have an account. Sign In": function () {
					$(this).dialog('close');
					$('#dialog-login').dialog('open');
				},
				'Close': function() {
					$(this).dialog('close');
				},
				'Register': function() {
					var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
					var msg = "";
					if ($("#dialog-register").find("input[name='email']").val() == '')
					{
						msg += "E-Mail is required<br />";
					}
					if($("#dialog-register").find("input[name='email']").val() != "" && reg.test($("#dialog-register").find("input[name='email']").val()) == false) {
						msg += "Invalid E-Mail<br />";
					}
					if ($("#dialog-register").find("input[name='password']").val() == '')
					{
						msg += "Password is required<br />";
					}
					if ($("#dialog-register").find("input[name='username']").val() == '')
					{
						msg += "Your name is required<br />";
					}
					if ($("#dialog-register").find("select[name='user_id']").val() == '')
					{
						msg += "Realtor is required<br />";
					}
					if (msg != "")
					{
						$("#dialog-register").find("span:last").html("Please fill in all fields").show();
					} else {

						var $this = $(this);
						$.ajax({
							type: 'POST',
							data: {action : "register", email: $("#dialog-register").find("input[name='email']").val(), password: $("#dialog-register").find("input[name='password']").val(), username: $("#dialog-register").find("input[name='username']").val(), user_id: $("#dialog-register").find("select[name='user_id']").val()},
							dataType: "json",
							url: 'chat-blog/include/ajax.php',
							success: function (json) {
								switch (json.status) {
									case true:
										jQuery_1_3_2("#dialog-register").dialog('close');
										jQuery_1_3_2("#dialog-info").html(json.message).dialog("open");
									break;
									case false:
										jQuery_1_3_2("#dialog-register span.status").text(json.message).show();
									break;
								}
							}
						});

					}
				}
			}
		});
		$('#dialog-forgot').dialog({
			autoOpen: false,
			bgiframe: true,
			resizable: false,
			draggable: false,
			width: 545,
			height: 270,
			modal: true,
			overlay: {
				backgroundColor: '#909090',
				opacity: 0.5
			},
			close: function(){

			},
			open: function (event, ui) {
				$.get('chat-blog/include/ajax.php', {action: "forgot-form"}, function (data) {
					$('#dialog-forgot').html(data);
				});
			},
			buttons: {
				"Sign Up. It's free and anyone can join": function() {
					$(this).dialog('close');
					$('#dialog-register').dialog('open');
				},
				'Close': function() {
					$(this).dialog('close');
				},
				'Send': function() {
					var $this = $(this);
					$.ajax({
						type: 'POST',
						data: {action : "forgot", email: $("#dialog-forgot").find("input[name='email']").val()},
						dataType: "json",
						url: 'chat-blog/include/ajax.php',
						success: function (json) {
							switch (json.status) {
								case true:
									jQuery_1_3_2("#dialog-forgot").dialog('close');
									jQuery_1_3_2("#dialog-info").html(json.message).dialog("open");
								break;
								case false:
									jQuery_1_3_2("#dialog-forgot span").text(json.message).show();
								break;
							}
						}
					});
				}
			}
		});
		$('#dialog-info').dialog({
			autoOpen: false,
			bgiframe: true,
			resizable: false,
			draggable: false,
			width: 500,
			height: 270,
			modal: true,
			overlay: {
				backgroundColor: '#909090',
				opacity: 0.5
			},
			buttons: {
				'Close': function() {
					$(this).dialog('close');
				}
			}
		});
	});
})(jQuery_1_3_2);
