//{{{
// Blog macro
// v0.1
// 09th October 2007
// 
// Author: Craig Cook, craig [dot] cook [at] bt [dot] com
//
// Not quite sure how to do this at the moment

//Need to add the following into the main TiddlyWiki:
//config.commands.publishBlog: {},
//and to the div 'ViewTemplate' in the 'toolbar' class add publishBlog
//Add the publishBlog option into TiddlyWiki

merge(config.commands.publishBlog,{
	text: "publish as blog",
	tooltip: "This will publish to you online blog"});

config.commands.publishBlog.handler = function(event,src,title) {
    var pubTiddler = store.getTiddler(title);	
	var blog = new Blog();
	
	blog.setContent(pubTiddler.text);
	blog.setSubject(pubTiddler.title);
    blog.publish();	
};

function Blog() {
    this.appkey = "0123456789ABCDEF";
    this.username = "changeme";
    this.password = "changeme";
    this.blogid = "changeme";
    this.content = "";
    this.subject = "";
    this.method = "metaWeblog.newPost";
    this.blogURL = "http://boycook.wordpress.com/xmlrpc.php";   //This is for wordpress, will need to be changed for others
};

Blog.prototype.publish = function(){
	var xmlMsg = new XMLRPCMessage();
	xmlMsg.setMethod(this.method);
    xmlMsg.addStringXML(this.blogid);
    xmlMsg.addStringXML(this.username);
    xmlMsg.addStringXML(this.password);
    xmlMsg.addItemXML("<struct><member><name>description</name><value>" + this.content + "</value></member><member><name>title</name><value>" + this.subject + "</value></member></struct>");
    xmlMsg.addBoolXML(true);
    
    var xmlText = xmlMsg.xml();
    this.makeAjaxCall(this.blogURL, xmlText);
};

Blog.prototype.makeAjaxCall = function(url, postdata) {
    var xmlhttp = getXMLHttpRequest();

    if(window.Components && window.netscape && window.netscape.security && document.location.protocol.indexOf("http") == -1)
        window.netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");

    xmlhttp.open("POST", url, false);
    xmlhttp.setRequestHeader("Content-type", "text/xml");
    xmlhttp.setRequestHeader("Content-length", postdata.length);
    xmlhttp.setRequestHeader("Connection", "close");        
    xmlhttp.send(postdata);
    alert("You blog has been sent");
    //TODO: add response to blog submittion
}

Blog.prototype.setSubject = function(txt){
    if (!txt) return;
    this.subject = txt;
};

Blog.prototype.setContent = function(txt){
    if (!txt) return;
    this.content = txt;
};

function XMLRPCMessage() {
    this.method = "";
    this.msgParams = [];
}

XMLRPCMessage.prototype.setMethod = function(methodName){
    if (!methodName) return;
    this.method = methodName;
};

XMLRPCMessage.prototype.xml = function(){

  var method = this.method;
  
  // assemble the XML message header
  var xml = "";
  
  xml += "<?xml version=\"1.0\"?>\n";
  xml += "<methodCall>\n";
  xml += "<methodName>" + method+ "</methodName>\n";
  xml += "<params>\n";
  
  // do individual parameters
  for (var i = 0; i < this.msgParams.length; i++){
    xml += this.msgParams[i];
  }
  
  xml += "</params>\n";
  xml += "</methodCall>";
  
  return xml; // for now
};

XMLRPCMessage.prototype.addBoolXML = function(data){
    var value = (data==true)?1:0;  
    this.msgParams[this.msgParams.length] = "<param>\n<value><boolean>" + value + "</boolean></value>\n</param>\n"
};

XMLRPCMessage.prototype.addStringXML = function(data){
    this.msgParams[this.msgParams.length] = "<param>\n<value><string>" + data + "</string></value>\n</param>\n";
};

XMLRPCMessage.prototype.addItemXML = function(data){
    this.msgParams[this.msgParams.length] = "<param>\n" + data + "\n</param>\n"
};

//}}}