/*
  Copyright 2008, Stanziq  Inc.
*/

Peeq.Archive.prototype = {
    
    /*Function search:
      Used to search for static data archived by the realtime component.
      Parameters:
      (String) jid -  username for xmpp service
      (String) service - Name of search service.
      (String) options - List of search queries (strings)
      (Dictionary) set - The data used to page through archive results. A key 
               of max is the number of results to return.  The key of page, 
	       is the id to start the returned results.
      (Function) call_back -  Function used to handle the search request.
    */
    search: function(jid, service, options, set, call_back) {

	var subid = app.connection.getUniqueId("searcharchive");

	//create subscription options
	var sub_options = Strophe.xmlElement("pubsub",[["xmlns",
						       Peeq.NS.PUBSUB]]);
	var options_elem = Strophe.xmlElement("options",[["node","search"]]);
	var items = Strophe.xmlElement("items",[["node","search"]]);
	var x = Strophe.xmlElement("x",[["xmlns","jabber:x:data"],
					["type","submit"]]);
	var form_field = Strophe.xmlElement("field",[["var","FORM_TYPE"],
						     ["type","hidden"]]);
	var value = Strophe.xmlElement("value",[]);
	var text = Strophe.xmlTextNode(Peeq.NS.ARCHIVE_OPTIONS);
	value.appendChild(text);
	form_field.appendChild(value);
	x.appendChild(form_field);

	jQuery.each(options, function(i, val) {
	    var form_field = Strophe.xmlElement("field",
						[["var",Peeq.NS.DSL_OPTIONS]]);
	    var value = Strophe.xmlElement("value",[]);
	    var text = Strophe.xmlTextNode(val);
	    value.appendChild(text);
	    form_field.appendChild(value);
	    x.appendChild(form_field);
	});
	
	//Add paging options
	var set_xml = null;
	if(set && ('function' === typeof(set))) 
	{
	    //backwards compatibility test. set is a new
	    //parameter. Old functions have the call_back as the 4th
	    //argument.
	    call_back = set;
	}
	else
	{
	    set_xml = $build('set',{});
	    jQuery.each(set, function(i,val) {
		var text_node = Strophe.xmlTextNode(val);
		set_xml.c(i).cnode(text_node).up().up();
	    });

	}

	if(options.length != 0) {
	    options_elem.appendChild(x);
	    sub_options.appendChild(items);
	    sub_options.appendChild(options_elem);
	}

	var sub = $iq({from:jid, to:service, type:'get', id:subid})
	if(set_xml)
	{
	    options_elem.appendChild(set_xml.tree());
	}
	sub.cnode(sub_options);
	app.connection.addHandler(call_back,
				  null,
				  'iq',
				  null,
				  subid,
				  null);
	app.connection.send(sub.tree());
	
	return subid;
	
    },
    /*Function requestSearch:
      used to request the search parameters from the archive service.
     */
    requestSearch: function(jid,service,call_back) {

	var subid = app.connection.getUniqueId("archivequery");
	var sub = $iq({from:jid, 
				to:service, 
				type:'get', 
				id:subid}).c("query",
					     [["xmlns",Peeq.NS.SEARCH]]);
	
	
	app.connection.send(sub.tree());

	app.connection.addHandler(call_back,
				  null,
				  'iq',
				  null,
				  subid,
				  null);
	
	return subid;
    
    }

};
