/*
	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.
*/

//+++++++++++++++++++++++++++++++++++++++++++++++++++ News

var VnHome = new Class
({
	Extends: VnModule

	,activate: function (data)
	{
		var p;
		var div;
		var news;
		var model;
		var box;
		var vbox;
		var hbox;
		var frame;
		var link;
		var db = this.db;

		hbox = new HtkHBox ();
		this.setChild (hbox);

		vbox = new HtkVBox ();
		hbox.add (vbox, true);
		hbox.add (new VnSurvey (db), false);

		frame = new HtkFrame ();
		frame.setColor (HTK_FRAME_YELLOW);
		frame.setSize (80, -1);
		vbox.add (frame, false);

		box = new HtkVBox ();
		box.setBorder (10);
		frame.setChild (box);
		
		p = document.createElement ('div');
		p.appendChild (document.createTextNode (TEXT_BrownserRecommend));
		box.add (p, false);
		
		link = document.createElement ('a');
		link.appendChild (document.createTextNode (TEXT_PressHere));
		link.target = '_blank';
		link.href = 'http://www.mozilla-europe.org/es/firefox/';
		box.add (link, false);	
		
		news = new HtkScroll ();
		this.news = news;
		vbox.add (news, true);
	
		model = new DbModel (db);
		this.model = model;
		model.addSignal ('status-changed', this.modelStatusChanged, this);
		model.setSql (
			'SELECT title, date_time, Cliente, text, id ' + 
				'FROM news INNER JOIN vn2008.Clientes ON user_id = Id_cliente ' + 
					'ORDER BY date_time DESC'
		);
	}

	,modelStatusChanged: function (model, status)
	{
		this.news.removeChild ();

		if (status == DB_MODEL_STATUS_READY)
		{
			var p;
			var cal;
			var frame;
			var hbox;
			var n, m;
			var data;
			var img;
			var row;
			var vbox;
			var box;
		
			vbox = new HtkVBox ();
			this.news.setChild (vbox);

			data = model.data;

			for (n = 0; n < data.length; n++)
			{
				m = -1;
				row = data[n];
				
				frame = new HtkFrame (row[++m]);
				vbox.add (frame, false);
		
				hbox = new HtkHBox ();
				hbox.setBorder (15);
				frame.setChild (hbox);
				
				box = document.createElement ('div');
				hbox.add (box, true);

				p = document.createElement ('p');
				box.appendChild (p);

				cal = new HtkDate ();
				cal.setValue (row[++m]);
				p.appendChild (cal.getNode ());

				p = document.createElement ('p');
				p.appendChild (document.createTextNode (TEXT_Author + ': ' + row[++m]));
				box.appendChild (p);

				p = document.createElement ('div');
				p.innerHTML = '' + row[++m];
				box.appendChild (p);

				img = document.createElement ('img');
				img.alt = row[++m];
				img.src = 'image/news/' + row[m] + '.png';
				img.style.maxHeight = '350px';
				img.style.maxWidth = '350px';
				hbox.add (img, true);
			}
		}
	}
});

//+++++++++++++++++++++++++++++++++++++++++++++++++++ Survey

var VnSurvey = new Class
({
	Extends: HtkFrame

	,initialize: function (db)
	{
		var model;
		var form;
		var input;
		var stmt;
		var entry;
		var expr;
		var total;
		var grid;
		var rbGroup = new HtkRadio ();
		var obj = this;

		model = new DbModel (db);
		model.setSql ('SELECT question, @id := id FROM survey ORDER BY id DESC LIMIT 1');

		form = new DbForm (model);
		form.setRow (0);

		this.parent (TEXT_Survey);
		this.setSize (-1, 150);
		
		box = new HtkVBox ();
		box.setBorder (8);
		this.setChild (box);
		
		entry = new HtkLabel ();
		form.bindParam (entry, 0);
		box.add (entry);

		grid = new HtkGrid (2);
		grid.setColWidth (0, 10);
		box.add (grid);
		
		total = document.createTextNode (0);
		div = document.createElement ('div');
		div.style.textAlign = 'center';
		div.appendChild (document.createTextNode (TEXT_Total + ' '));
		div.appendChild (total);
		div.appendChild (document.createTextNode (' ' + TEXT_Votes));
		box.add (div);

		input = document.createElement ('input');
		input.type = 'button';
		input.value = TEXT_Vote;
		input.addEventListener ('click',
			function () { obj.vote (); }, false);
		box.add (input);

		stmt = new SqlString ('SELECT id, answer, votes FROM survey_answer WHERE survey_id = @id');

		model = new DbModel (db);
		model.setStmt (stmt);
		model.addSignal ('status-changed', this.modelStatusChanged, this);

		this.answer = grid;
		this.model = model;
		this.total = total;
		this.rbGroup = rbGroup;
		this.db = db;
	}

	,modelStatusChanged: function (model, status)
	{
		switch (status)
		{
			case DB_MODEL_STATUS_READY:
			{
				var data;
				var text;
				var label;
				var total = 0;
				var grid = this.answer;
				var votes = new Array ();
	
				data = model.data;
				grid.removeChilds ();

				for (n = 0; n < data.length; n++)
				{
					label = document.createElement ('label');
					label.appendChild (document.createTextNode (data[n][1]));

					text = document.createTextNode (data[n][2] + ' ' + TEXT_Votes);
					
					grid.attach (this.rbGroup.newRadio (data[n][0]));
					grid.attach (label);
					grid.attach (null);
					grid.attach (text);

					votes[n] = text;
					total = total + data[n][2];
				}

				this.total.nodeValue = total;
				
				break;
			}
		}
	}

	,vote: function ()
	{
		var radio = this.rbGroup;

		if (radio.getValue () != null)
		{
			var stmt = new SqlString ('CALL survey_vote (%s)');
			stmt.addParam (radio);
			this.db.stmt (stmt, this, this.voteDone, null, false);
		}
		else
			alert (TEXT_NoAnswerSelected);
	}, 

	voteDone: function (json, error)
	{
		if (!json)
			return;

		this.total.textContent++;
		alert (TEXT_ThanksForVote);
	}
});


