/**
 *   Copyright (C) 2008 Marcelo Jorge Vieira (metal) <metal@alucinados.com>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU 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.
 *
 * Public License can be found at http://www.gnu.org/copyleft/gpl.html
 *
 */
 
// info: http://www.xmpp.org/extensions/xep-0060.html

function Pubsub() {
    this.base = JSJaCIQ;
    this.base('pubsub');

    this.pubsubServer = PUBSUB_SERVER;
    this.userpath = PUBSUB_USERPATH;
}

Pubsub.prototype = new JSJaCIQ;

Pubsub.prototype.getDomain = function() {
    return this.pubsubServer;
}

Pubsub.prototype.setDomain = function(domain) {
    this.pubsubServer = domain;
}

Pubsub.prototype.getUserPath = function() {
    return this.userpath;
}

Pubsub.prototype.setUserPath = function(path) {
    this.userpath = path;
}

Pubsub.prototype.createId = function() {
	var id = "";
	var aux = "0123456789abcefghijklmnopqrstuvwxyz";

	for (var i=0; i<=10; i++)
  		id += aux.charAt(Math.random() * aux.length);

	return id;
}

Pubsub.prototype.setPubSub = function(xmlns, data) {
    var node = this.getDoc().createElementNS(xmlns, 'pubsub');
    node.appendChild(data);  
    this.getNode().appendChild(node);

    return this;
}

Pubsub.prototype.subscribe = function(node) {
    var data = this.getDoc().createElement('subscribe');
    data.setAttribute('node', node);
    data.setAttribute('jid', con.jid);

    return this.setPubSub(NS_PUBSUB, data);
}

Pubsub.prototype.unsubscribe = function(node) {
    var data = this.getDoc().createElement('unsubscribe');
    data.setAttribute('node', node);
    data.setAttribute('jid', con.jid);

    return this.setPubSub(NS_PUBSUB, data);
}

Pubsub.prototype.readByUser = function(user) {
    var objJID = new JSJaCJID(user)
	var node = this.userpath + objJID.getNode();

    var data = this.getDoc().createElement('items');
    data.setAttribute('node', node);

    return this.setPubSub(NS_PUBSUB, data);
}

Pubsub.prototype.deleteItemById = function(id) {
	var node = this.userpath + con.username;

    var data = this.getDoc().createElement('retract');
    data.setAttribute('node', node);
    
    itemNode = data.appendChild(this.getDoc().createElement('item'));
	itemNode.setAttribute('id', id);
    
    return this.setPubSub(NS_PUBSUB, data);
}

Pubsub.prototype.deleteAllItemsByNode = function() {
	var node = this.userpath + con.username;

    var data = this.getDoc().createElement('purge');
    data.setAttribute('node', node);

    return this.setPubSub(NS_PUBSUB, data);
}

Pubsub.prototype.getMySubscriptions = function() {
    var data = this.getDoc().createElement('subscriptions');

    return this.setPubSub(NS_PUBSUB, data);
}

Pubsub.prototype.publish = function(title, url) {
	var node = this.userpath + con.username;
	
	var data = this.getDoc().createElement('publish');
	data.setAttribute('node', node);

	var itemNode = data.appendChild(this.getDoc().createElement('item'));
	itemNode.setAttribute('id', this.createId());

	var titleNode = itemNode.appendChild(this.getDoc().createElement('title'));
	var titleNodeContent = titleNode.appendChild(
	    this.getDoc().createTextNode(title)
	);
	
	var urlNode = itemNode.appendChild(this.getDoc().createElement('url'));
	var urlNodeContent = urlNode.appendChild(this.getDoc().createTextNode(url));

    return this.setPubSub(NS_PUBSUB, data);
}
