/*
	Copyright (C) 2012 - 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.
*/

var VnGallery = new Class
({
	Extends: VnModule

	,activate: function (data)
	{
		var comment;
		var frame;
		var vbox;
		var hbox;
		var fullImg;
		var img;
		var bin;
		var model;
		var stmt;
		var gallery;
		var db = this.db;
		
		model = new DbModel (db);

		stmt = new SqlString ('SELECT id, title, comment FROM gallery');
		model.addSignal ('status-changed', this.modelStatusChanged, this);
		model.setStmt (stmt);

		frame = new HtkFrame (TEXT_Gallery);
		this.setChild (frame);
		
		vbox = new HtkVBox ();
		frame.setChild (vbox);

		hbox = new HtkHBox ();
		hbox.setSize (90, -1);
		vbox.add (hbox, false);
		
		img = document.createElement ('img');
		img.src = 'image/go-prev.png';
		img.style.margin = '10px';
		img.style.cursor = 'pointer';
		hbox.add (img, false);

		gallery = new HtkScroll ();
		hbox.add (gallery, true);

		img = document.createElement ('img');
		img.src = 'image/go-next.png';
		img.style.margin = '10px';
		img.style.cursor = 'pointer';
		hbox.add (img, false);
		
		comment = document.createElement ('p');
		comment.style.textAlign = 'center';
		vbox.add (comment, false);
		
		bin = new HtkScroll ();
		bin.style.textAlign = 'center';
		vbox.add (bin, true);

		fullImg = document.createElement ('img');
		fullImg.style.maxHeight = '400px';
		fullImg.style.padding = '15px';
		bin.setChild (fullImg);
		
		this.model = model;
		this.comment = comment;
		this.img = fullImg;
		this.gallery = gallery;
	}
	
	,modelStatusChanged: function (model, status)
	{
		if (status != DB_MODEL_STATUS_READY)
			return;

		this.gallery.removeChild ();
			
		var n;
		var hbox;
		var size = '80px';
		var data = model.data;

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

		for (n = 0; n < data.length; n++)
		{
			img = document.createElement ('img');
			img.src = 'image/gallery/icon/' + data[n][0] + '.png';
			img.style.maxHeight = size;
			img.style.maxWidth = size;
			img.style.cursor = 'pointer';
			img.obj = this;
			img.row = n;
			img.addEventListener ('click', this.imgClicked, false);
			hbox.add (img, false);
		}
	}
	
	,imgClicked: function (event)
	{
		this.obj.changeImage (this.row);
	}
	
	,changeImage: function (n)
	{
		var row = this.model.data[n];

		removeChilds (this.comment);
		this.comment.appendChild (document.createTextNode (row[1]));
		this.img.src = 'image/loading.gif';
		this.img.src = 'image/gallery/' + row[0] + '.png';
	}
});


