var LoginManager = Class.create();

LoginManager.prototype = {
	initialize: function(){
	},
	isLogin: function(){
		var uri = 'http://' + location.host;
		var params = {
			'stage': 'ajax_is_logined',
			'ts': (new Date()).getTime()
		};
		this.login_status = false;
		new Ajax.Request(uri, {
			method: 'get',
			parameters: $H(params).toQueryString(),
			onComplete: (function(res){
				if (res.responseText == '1') {
					this.login_status = true;
				}
			}).bindAsEventListener(this),
			asynchronous: false
		});
		
		return this.login_status;
	}
}


function set_login_link(elm){
	if (elm == null) {
		elms0 = document.getElementsByClassName("login_link");
		elms1 = document.getElementsByClassName("logout_link");
	} else {
		elms0 = document.getElementsByClassName("login_link", elm);
		elms1 = document.getElementsByClassName("logout_link", elm);
	}
	
	$A(elms0).each(function(e, i){
		Event.observe(e, 'click', clickLoginForm, false);
	});
	
	$A(elms1).each(function(e, i){
		Event.observe(e, 'click', logout, false);
	});
	return false;
}

function login(){
	var back = 'http://' + location.host + location.pathname + location.search;
	var uri = "http://" + location.host;
	$("float_login_notice").innerHTML = "";
	var param = Form.serialize("login_form");
	
	new Ajax.Request(uri, {
		method: "post",
		parameters: param,
		onComplete: function(){
			if (login_manager.isLogin()) {
				location.href = back; // reload without hash.
				return false;
			} else {
				$("float_login_notice").innerHTML = 'ログインに失敗しました。';
			}
		}
	});
}

function logout(evt){
	var back = 'http://' + location.host + location.pathname + location.search;
	var uri = "http://" + location.host;
	var param = $H({
		"special": "special_signout"
	}).toQueryString();
	
	new Ajax.Request(uri, {
		method: "post",
		parameters: param,
		onComplete: function(){
			location.href = back;
			return false;
		}
	});
	Event.stop(evt);
}

function reloadLoginStatus(){
	var uri = "http://" + location.host;
	var param = $H({
		"stage": "ajax_get_login_status",
		"ts": (new Date).getTime()
	}).toQueryString();
	
	new Ajax.MyUpdater("login_status", uri, {
		method: "get",
		parameters: param
	});
	hideLoginForm();
}

function clickLoginForm(evt){
	var x = Event.pointerX(evt);
	var y = Event.pointerY(evt);
	showLoginForm(x, y);
	Event.stop(evt);
}

function showLoginForm(x, y, notice, tip){
	hideSelectBoxes();

	if ($('overlay')) {
		Event.observe('overlay', 'click', hideLoginForm);
	}
	var arrayPageSize = getPageSize();
	Element.setHeight('overlay', arrayPageSize[1]);
	new Effect.Appear('overlay', {
		duration: 0.2,
		from: 0.0,
		to: 0.8
	});
	
	var f = $("float_login_form");
	if (notice != undefined) {
		$("float_login_notice").innerHTML = notice;
	}
	if (tip != undefined) {
		$("float_login_beacon").setAttribute('src', 'http://img.date2.jp/img/1pix.gif?' + tip);
	}
	
	f.style.top = document.viewport.getScrollOffsets()[1] + 100 + "px";
	f.style.display = "block";
}

function hideLoginForm(){
	Element.hide('float_login_form');
	$("float_login_notice").innerHTML = "";
	new Effect.Fade('overlay', {
		duration: 0.2
	});
	showSelectBoxes();
}

var login_manager = null;

Event.observe(window, 'load', function(){
	set_login_link();
	login_manager = new LoginManager();
	updateFunctions.push([set_login_link, null]);
});
