/**
 * Login form
 * (requires jquery.js, popup_block.js)
 *
 * @author Stepan Reznikov [stepan.reznikov@gmail.com]
 * @version 2.0 (2008-07-08)
 *
 * @param {Object} options Object with options
 */

var login_form = function (options) {

	var that = popup_block(options);

	var form = options.container.find("form");
	var errors = options.container.find(".errors");
	var progress = options.container.find(".progress");

	var form_data = {
		"is-ajax": "true"
	};
	
	/**
	 * Replaces current login with the new one
	 * @param {String} login New login
	 */
	function replaceLogin(login) {
		$('#user_menu .cart a span').text(login);
		$('#user_menu .cart a .icon').addClass('icon_cart');
		// replace login icon
		$('#user_menu .login a').attr('href', '/?auth.logout=true').unbind('click')
			.each(function(){
//				console.log('found %d nodes', this.childNodes.length)
				// remove all text nodes
				for (var i = this.childNodes.length - 1; i >= 0; i--) {
					/** @type {Element} */
					var elem = this.childNodes[i];
					if (elem.nodeType == 3) {
						elem.parentNode.removeChild(elem);
					}
				}
			})
			.find('.icon').removeClass('icon_login').addClass('icon_logout');
	}

	var maySubmit = false;

	form.submit(function () {

		if (maySubmit) {
			return true;
		}

		errors.addClass("hidden");
		progress.removeClass("hidden");

		// Prepare form data for sending
		form.find("input").each(function () {
			form_data[this.name] = this.value;
		});

		// We are going to submit our form with AJAX
		$.ajax({

			// The URL to request
			url: "/login_ajax/",

			// POST на локале работать отказался
			type: "GET",

			// Force the requested page to not be cached by the browser
			cache: false,

			// We are expecting JSON back from the server
			dataType: "json",

			// Data to be sent to the server
			data: form_data,

			success: function (data) {
				if (data.status == "success") {
					form.attr('action', location.href);
					maySubmit = true;
					replaceLogin(data.username);
					that.hide();
					//form.submit();
					//document.location.reload();
					if (data.url) {
						document.location.href = data.url;
					}
				}
				else {
					progress.addClass("hidden");
					errors.html(data.message).removeClass("hidden");
				}
			}
		});

		// Prevent the form from being submitted
		return false;

	});

	that.superior = function (name) {
		var that = this,
		    method = that[name];
		return function () {
			return method.apply(that, arguments);
		};
	};

	var super_show = that.superior('show');
	var super_hide = that.superior('hide');

	var flash_objects = $("object");

	that.show = function (event) {
		flash_objects.css("visibility", "hidden");
		if ($(event.target).hasClass("login_link")) {
			options.container.removeClass("popup").addClass("popup_center");
		}
		else {
			options.container.removeClass("popup_center").addClass("popup");
		}
		super_show(event);
		form.find("input[name='login']").focus();
	};

	that.hide = function (event) {
		super_hide(event);
		flash_objects.css("visibility", "visible");
	};

	return that;
};


$(function () {
	login_form({
		container: $("#login_form"),
		link: $("#auth a"),
		fader: $("#fader"),
		close: $("#login_form .icon_close")
	});
});

