/**
 * cadeauth.js: authentication routines for the CadeMET project.
 *
 * by Eduardo Hernando Izcara. Copyright 2009 agroGEX.
 */
// ------------------------------------------------------------------------

// Make sure we haven't already been loaded
var CadeAuth;
if (CadeAuth && (typeof CadeAuth != "object" || CadeAuth.NAME))
{
	throw new Error("Namespace 'CadeAuth' already exists");
}

// Create our namespace, and specify some meta-information
CadeAuth = {};
CadeAuth.NAME = "CadeAuth";    // The name of this namespace
CadeAuth.VERSION = 1.0;        // The version of this namespace

// Algunas variables globales del namespace:

CadeAuth.authorized = {};
CadeAuth.loginDialogo = {};
CadeAuth.protectedLinks = {};
CadeAuth.redirect = {};
// ------------------------------------------------------------------------

CadeAuth.createLoginDialogo = function()
{
	CadeAuth.authorized.logged = false;
	CadeAuth.authorized.rowdata = '';
	CadeAuth.authorized.isoper = false;

	var handleSuccess = function(o) {
		var json_data = eval( "(" + o.responseText + ")" );
		if (json_data.datos.logged) {
			Rutinas.setHTML(json_data.datos.loginfo, 'login-info');
			CadeAuth.authorized.logged = true;
			CadeAuth.authorized.rowdata = json_data.datos.rowdata;
			CadeAuth.authorized.isoper = json_data.datos.rowdata.isoper;

			for (link_id in CadeAuth.protectedLinks) {
				document.getElementById(link_id).setAttribute('href', CadeAuth.protectedLinks[link_id]);
			}

			if (CadeAuth.redirect.doit) {
				CadeAuth.redirect.doit = false;
				window.location = CadeAuth.protectedLinks[CadeAuth.redirect.link_id];
				//window.open(CadeAuth.protectedLinks[CadeAuth.redirect.link_id],window.name,"status=yes",true);
			}
		}
		else {
			Rutinas.setHTML(json_data.datos.msg, 'login-dialogo-result');
			CadeAuth.loginDialogo.show();
		}
	};
	var handleFailure = function(o) {};

	var handleSubmit = function() { this.submit(); };
	var handleCancel = function() { this.cancel(); };

	document.getElementById("login-dialogo").style.position = "relative";
	CadeAuth.loginDialogo = new YAHOO.widget.Dialog("login-dialogo",
			{ width:"240px",
			  //fixedcenter:true,
			  xy:[260,350],
			  visible:false,
			  modal:true,
			  constraintoviewport:true,
			  buttons:[	{text:"Enviar", handler:handleSubmit, isDefault:true},
						{text:"Cancel", handler:handleCancel} ]
			});

	CadeAuth.loginDialogo.callback = { success: handleSuccess, failure: handleFailure };
	CadeAuth.loginDialogo.render();
}
// ------------------------------------------------------------------------

CadeAuth.userLogin = function()
{
	Rutinas.setHTML("", 'login-dialogo-result');
	CadeAuth.loginDialogo.show();
}
// ------------------------------------------------------------------------

// Se establece protección de zona con autorización para el 'link' pasado como parámetro
CadeAuth.protect = function(enlace_id)
{
	var href = document.getElementById(enlace_id).getAttribute('href');
	if (href && href != "") {
		CadeAuth.protectedLinks[enlace_id] = href;
		document.getElementById(enlace_id).removeAttribute('href');
	}

	var click_func = function(e)
	{
		if (CadeAuth.authorized.logged) {
			window.location = CadeAuth.protectedLinks[enlace_id];
			//window.open(CadeAuth.protectedLinks[enlace_id],window.name,"status=yes",true);
		}
		else {
			CadeAuth.redirect = {doit:true, link_id:enlace_id};
			CadeAuth.userLogin();
		}
	}

	YAHOO.util.Event.addListener(enlace_id, 'click', click_func);
}
// ------------------------------------------------------------------------

CadeAuth.getLoggedStatus = function()
{
	// se comprueba si hay alguien registrado
	var url = Rutinas.baseUrl() + "/auth/hasidentity/format/json";
	var callback =
	{
		success: function(o) {
					 var json_data = eval( "(" + o.responseText + ")" );
					 CadeAuth.authorized.logged = json_data.datos.hasidentity;
				 },
		failure: function(o) {}
	};
	YAHOO.util.Connect.initHeader('X_REQUESTED_WITH', 'XMLHttpRequest');
	YAHOO.util.Connect.asyncRequest('GET', url, callback);
}
// ------------------------------------------------------------------------


