
module.exports = new Class
({
	Extends: Vn.Component
	,Tag: 'htk-social-bar'
	,Properties:
	{
		conn:
		{
			type: Db.Connection
			,set: function (x)
			{	
				this._conn = x;
				this._refresh ();
			}
			,get: function ()
			{
				return this._conn;
			}
		},
		priority:
		{
			type: Number
			,set: function (x)
			{	
				this._priority = x;
				this._refresh ();
			}
			,get: function ()
			{
				return this._priority;
			}
		}	
	}
	
	,_priority: 0
	
	,initialize: function ()
	{
		var node = this.createRoot ('div');
		node.className = 'htk-social-bar';
	}

	,_refresh: function ()
	{
		if (!this._conn || this._priority === null)
			return;
		
		var params = {priority: this._priority};
		var query = 'SELECT title, link, icon FROM social '
			+'WHERE priority >= #priority ORDER BY priority';
		this._conn.execQuery (query, this._onQueryDone.bind (this), params);
	}

	,_onQueryDone: function (resultSet)
	{
		Vn.Node.removeChilds (this._node);	
		var res = resultSet.fetchResult ();
		var row;
		
		while (row = res.fetchObject ())
		{
			var a = this.createElement ('a');
			a.href = row.link;
			a.target = '_blank';
			a.className = 'clickable-img';
			this._node.appendChild (a);
			
			var img = this.createElement ('img');
			img.src = 'image/social/'+ row.icon;
			img.alt = row.title;
			img.title = row.title;
			a.appendChild (img);
		}
	}
});

