/*
	Copyright (C) 2008 - Juan Ferrer Toribio

	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU Lesser General Public
	License as published by the Free Software Foundation; either
	version 2.1 of the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
	Lesser General Public License for more details.

	You should have received a copy of the GNU Lesser General Public
	License along with this program; if not, write to the Free
	Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
	02111-1307 USA.
*/

window.addEventListener ('load', main, false);

/**
 * Función principal, llamada al inicio de la aplicación.
 **/
function main ()
{
	var vn = new VnMain ();
	vn.run ();
}

/**
 * Gestiona todo el sistema gráfico de la aplicación.
 **/
var VnMain = new Class
({	
	initialize: function ()
	{
		var db;
		var body;
		
		db = new DbConnection ();
		db.addSignal ('opened', this.opened, this);
		db.addSignal ('closed', this.closed, this);
		
		body = document.body;
		body.style.height = 'auto';
		body.style.width = 'auto';

		this.db = db;
		this.body = body;
		this.login = new VnLogin (db);
	}
	
	/**
	 * Arranca el programa.
	 **/
	,run: function ()
	{
		this.closed ();

		if (Cookie.check ('hedera_user'))
			this.login.send ();
	}
	
// Private

	/**
	 * Método que se llamará al abrir la conexión con la BD.
	 **/
	,opened: function ()
	{
		var gui = new VnGui (this.db);
		removeChilds (this.body);
		this.body.appendChild (gui.getNode ());
	}
	
	/**
	 * Método que se llamará al cerrar la conexión con la BD.
	 **/
	,closed: function ()
	{
		removeChilds (this.body);
		this.body.appendChild (this.login.getNode ());
		this.login.show ();
	}
});

//+++++++++++++++++++++++++++++++++++++++++++++++++++ Login

var VnLogin = new Class
({
	Extends: HtkWidget

	,initialize: function (db)
	{		
		var div;
		var n, p;
		var info;
		var hr;
		var linkList;
		var box;
		var mtable;
		var mtbody;
		var mtr;
		var mtd;
		var img;
		var table;
		var tbody;
		var tr;
		var td;
		var input;
		var label;
		var user;
		var password;
		var remember;
		var title;
		var body;
		var obj = this;

		this.parent ('div');
		this.style.margin = '0px';
		this.style.width = '100%';
		this.style.height = '100%';
		this.style.position = 'absolute';
		this.style.backgroundColor = '#333';
		
		div = document.createElement ('div');
		div.style.position = 'absolute';
		div.style.top = '40%';
		div.style.left = '50%';
		div.style.marginLeft = '-110px';
		div.style.marginTop = '-90px';
		this.node.appendChild (div);

		box = document.createElement ('div');
		box.className = 'box';
		box.style.width = '210px';
		box.style.padding = '10px';
		box.style.backgroundColor = '#EEE';
		div.appendChild (box);

		mtable = document.createElement ('table');
		mtable.style.borderSpacing = '0px';
		mtable.style.margin = '0px';
		box.appendChild (mtable);

		mtbody = document.createElement ('tbody');
		mtable.appendChild (mtbody);
	
		mtr = document.createElement ('tr');
		mtbody.appendChild (mtr);
	
		mtd = document.createElement ('td');
		mtr.appendChild (mtd);
	
		table = document.createElement ('table');
		mtd.appendChild (table);
	
		tbody = document.createElement ('tbody');
		table.appendChild (tbody);
	
		tr = document.createElement ('tr');
		tbody.appendChild (tr);
	
		td = document.createElement ('td');
		tr.appendChild (td);
	
		img = document.createElement ('img');
		img.style.align = 'center';
		img.src = 'image/logo.png';
		img.style.paddingBottom = '10px';
		td.appendChild (img);
/*
		tr = document.createElement ('tr');
		tr.style.height = '1px';
		tbody.appendChild (tr);
	
		td = document.createElement ('td');
		td.style.color = 'red';
		td.style.fontSize = '10px';
		td.style.fontWeight = 'bold';
		td.style.textAlign = 'right';
		td.appendChild (document.createTextNode (TEXT_Beta));
		tr.appendChild (td);
*/
		mtr = document.createElement ('tr');
		mtbody.appendChild (mtr);
	
		mtd = document.createElement ('td');
		mtr.appendChild (mtd);
	
		table = document.createElement ('table');
		table.style.borderSpacing = '4px';
		mtd.appendChild (table);
	
		tbody = document.createElement ('tbody');
		table.appendChild (tbody);
	
		tr = document.createElement ('tr');
		tbody.appendChild (tr);
	
		td = document.createElement ('td');
		td.style.textAlign = 'right';
		tr.appendChild (td);
	
		label = document.createElement ('label');
		label.appendChild (document.createTextNode (TEXT_User));
		td.appendChild (label);
	
		td = document.createElement ('td');
		tr.appendChild (td);
	
		user = document.createElement ('input');
		user.type = 'text';
		user.style.width = '90px';
		td.appendChild (user);
	
		tr = document.createElement ('tr');
		tbody.appendChild (tr);
	
		td = document.createElement ('td');
		td.style.textAlign = 'right';
		tr.appendChild (td);
	
		label = document.createElement ('label');
		label.appendChild (document.createTextNode (TEXT_Password));
		td.appendChild (label);
	
		td = document.createElement ('td');
		tr.appendChild (td);
	
		password = document.createElement ('input');
		password.type = 'password';
		password.style.width = '90px';
		td.appendChild (password);
	
		tr = document.createElement ('tr');
		tbody.appendChild (tr);
	
		td = document.createElement ('td');
		tr.appendChild (td);
	
		td = document.createElement ('td');
		tr.appendChild (td);
	
		remember = document.createElement ('input');
		remember.type = 'checkbox';
		td.appendChild (remember);

		td.appendChild (document.createTextNode (TEXT_Remember));
	
		tr = document.createElement ('tr');
		tbody.appendChild (tr);

		td = document.createElement ('td');
		td.style.textAlign = 'center';
		td.colSpan = 2;
		tr.appendChild (td);
	
		input = document.createElement ('input');
		input.value = TEXT_Enter;
		input.type = 'button';
		input.addEventListener ('click',
			function () { obj.send (); }, false);
		td.appendChild (input);
	
		tr = document.createElement ('tr');
		tbody.appendChild (tr);

		td = document.createElement ('td');
		td.style.textAlign = 'center';
		td.colSpan = 2;
		tr.appendChild (td);
		
		p = document.createElement ('a');
		p.href = '#';
		p.addEventListener ('click',
			function (e) { obj.send (true); }, false);
		p.appendChild (document.createTextNode (TEXT_YoureVisitor));
		td.appendChild (p);	
		
		box = document.createElement ('div');
		box.style.marginTop = '20px';
		box.style.textAlign = 'center';
		box.style.color = '#FFF';
		div.appendChild (box);
		
		info = [
			 'Avenida Espioca, 100'
			,'46460 Silla, Valencia'
			,'clientes@verdnatura.es'
			,'96 324 21 00'
		]
		
		for (n = 0; n < info.length; n++)
		{
			p = document.createElement ('p');
			p.style.margin = '4px';
			p.appendChild (document.createTextNode (info[n]));
			box.appendChild (p);
		}
		
		p = document.createElement ('a');
		p.href = 'http://www.facebook.com/verdnatura';
		p.target = '_blank';
		p.appendChild (document.createTextNode ('Facebook'));
		box.appendChild (p);
		
		box = document.createElement ('div');
		box.style.position = 'absolute';
		box.style.textAlign = 'right';
		box.style.left = '30px';
		box.style.right = '30px';
		box.style.bottom = '0px';
//		this.appendChild (box);
		
		hr = document.createElement ('hr');
		hr.style.backgroundColor = 'EEE';
		hr.style.color = '#EFE';
		box.appendChild (hr);
	
		linkList = document.createElement ('ul');
		linkList.style.listStyleType = 'none';
		linkList.style.textAlign = 'right';
		linkList.style.padding = '0px';
		linkList.style.margin = '8px';
		box.appendChild (linkList);

		this.node.addEventListener ('keypress',
			function (e) { obj.enterPress (e) }, false);
		this.linkList = linkList;
		this.user = user;
		this.password = password;
		this.remember = remember;
		this.db = db;
	}

	,send: function (def)
	{
		if (def)
			this.db.connect (
				 'visitor'
				,'123456'
				,false
			);
		else
			this.db.connect (
				 this.user.value
				,this.password.value
				,this.remember.checked
			);

		this.password.value = '';
		this.remember.checked = false;
		this.show ();
	}

	,show: function ()
	{
		this.user.focus ();
		this.user.select ();
	}

	,enterPress: function (event)
	{
		if (event.keyCode == 13)
			this.send (false);
	}
	
	,addLink: function (text, href)
	{
		var li, a;
	
		li = document.createElement ('li');
		li.style.display = 'inline';
		li.style.marginRight = '15px';
		this.linkList.appendChild (li);
		
		a = document.createElement ('a');
		a.appendChild (document.createTextNode (text));
		a.href = href;
		li.appendChild (a);		
	}
});

//+++++++++++++++++++++++++++++++++++++++++++++++++++ Interface

var VnGui = new Class
({
	Extends: HtkVBox

	,windowResized: function ()
	{
		this.setSize (
			 getInnerHeight ()
			,getInnerWidth ()
		);
	}

	,initialize: function (db)
	{
		var frame;
		var box;
		var vbox;
		var img;
		var h;
		var button;
		var ul;
		var body;
		var obj = this;

		this.parent ();
		this.setBorder (8);
		this.windowResized ();
		window.addEventListener ('resize', 
			function () { obj.windowResized (); }, false);
	
// Header

		frame = new HtkFrame ();
		frame.setSize (45, -1);
		frame.node.style.backgroundColor = '#333';
		frame.node.style.color = '#FFF';
		this.add (frame, false);

		box = new HtkHBox ();
		box.style.padding = '6px';
		frame.setChild (box);

		img = document.createElement ('img');
		img.src = 'image/icon.png';
		img.alt = TEXT_AppName;
		box.add (img, false);
	
		button = document.createElement ('button');
		button.style.padding = '0px';
		button.title = TEXT_Exit;
		button.addEventListener ('click',
			function () { obj.logoutClicked () }, false);
		box.add (button, false);

		img = document.createElement ('img');
		img.style.margin = '2px';
		img.src = 'image/logout.png';
		img.alt = TEXT_Exit;
		button.appendChild (img);

// Menu

		box = new HtkHBox ();
		this.add (box, true);
		
		var vbox = new HtkVBox ();
		vbox.style.width = '100px';
		box.add (vbox, false);

		frame = new HtkFrame (TEXT_Menu);
		vbox.add (frame, true);

		ul = document.createElement ('ul');
		ul.className = 'menu';
		frame.setChild (ul);
	
// Body

		body = new HtkScroll ();
		box.add (body, true);

		this.db = db;
		this.ul = ul;
		this.body = body;
		this.data = new Array ();
		this.module = new Array ();

		db.multiQuery ('CALL module_list ()', this, this.loadMods);
	}

	,loadMods: function (json, error)
	{
		if (json[1])
		{
			var klass;
			var data = json[0].data;

			for (var n = 0; n < data.length; n++)
			{
				try {
					klass = eval (data[n][1]);
					this.loadModule (data[n][0], klass);
				}
				catch (e)
				{
					alert (TEXT_ErrorLoadingMod);
				}
			}
			
			this.openModule (VnHome, null);
		}
	}

	,findData: function (module)
	{
		var data = this.data;
		
		for (var n = 0; n < data.length; n++)
		{
			if (data[n].module == module)
				return n;
		}
		return -1;	
	}
	
	,setData: function (module, data)
	{
		var n = this.findData (module);
	
		if (n == -1)
		{
			var object = new Object ();
			object.module = module;
			object.data = data;
			this.data.push (object);
		}
		else
			this.data[n].data = data;
	}
	
	,getData: function (module)
	{
		var n = this.findData (module);	
		if (n != -1)
			return this.data[n].data;
		return null;
	}
	
	,clearData: function (module)
	{	
		var n = this.findData (module);

		if (n != -1)
			this.data.splice (n, 1);
	}
	
	,logoutClicked: function ()
	{
		Cookie.del ('hedera_user');
		Cookie.del ('hedera_pass');
		this.db.close ();
	}
	
	,loadModule: function (modName, klass)
	{
		var a;
		var li;
		var obj = this;

		li = document.createElement ('li');
		this.ul.appendChild (li);

		a = document.createElement ('a');
		a.href = '#';
		a.appendChild (document.createTextNode (modName));
		a.addEventListener ('click',
			function () { obj.openModule (klass, null); }, false);
		li.appendChild (a);
	}
	
	,openModule: function (klass, data)
	{
		var mod;
		
		this.db.startTransaction ();

		mod = new klass (this);
		this.body.setChild (mod);
		mod.activate (data);

		this.db.commit ();
	}
});

//+++++++++++++++++++++++++++++++++++++++++++++++++++ Module

var VnModule = new Class
({
	Extends: HtkScroll

	,initialize: function (gui)
	{
		this.parent ();
		this.gui = gui;
		this.db = gui.db;
	}
});


