
function prepData(data) {
    if(typeof(data) == "undefined") {
        return {_ts : new Date().getTime()};
    }
    else if(typeof(data) == "string") {
        return data + "&_ts=" + new Date().getTime();
    }
    else if(typeof(data) == "object") {
        data._ts = new Date().getTime();
        return data;
    }
    return data;
}

Prototype.taconiteGet = function(url, data) {
    var params = prepData(data);
    new Ajax.Request(url, {
	    method:'get',
	    parameters: params,
    	onSuccess: function(transport){
      	new TaconitePrototypeParser().parse(transport.responseText);
    	}
  	});    
};


Prototype.taconitePost = function(url, data) {
    var params = prepData(data);
    new Ajax.Request(url, {
	    method:'post',
	    parameters: params,
    	onSuccess: function(transport){
      	new TaconitePrototypeParser().parse(transport.responseText);
    	}
  	});    
};

function TaconitePrototypeParser() { }

TaconitePrototypeParser.prototype.parse = function(responseXml) {
    var nodes = null;
    if(responseXml != null) {
        nodes = responseXml.documentElement.childNodes;
    }
    
    if(nodes == null) {
        nodes = new Array();
    }
    
    for(var i = 0; i < nodes.length; i++) {
        this.parseNode(nodes[i]);
    }
}

TaconitePrototypeParser.prototype.parseNode = function(taconiteCommand) {
    if(taconiteCommand.nodeType != 1) {
        return;
    }
    
    var xmlTagName = taconiteCommand.tagName.toLowerCase();
    var selector = "";
    if(taconiteCommand.getAttribute("contextNodeID") != null) {
        selector = "#" + taconiteCommand.getAttribute("contextNodeID");
		    // escape css notation characters
		    selector = selector.replace(/(:|\.)/g,'\\$1');
    }
    else if(taconiteCommand.getAttribute("contextNodeSelector") != null) {
        selector = taconiteCommand.getAttribute("contextNodeSelector");
    }
    else {
        selector = taconiteCommand.getAttribute("selector");
    }
    
    var results = $$(selector);

    //remove attributes which are no longer needed
    taconiteCommand.removeAttribute("contextNodeID");
    taconiteCommand.removeAttribute("contextNodeSelector");
    taconiteCommand.removeAttribute("matchMode");
    taconiteCommand.removeAttribute("parseInBrowser");
    taconiteCommand.removeAttribute("selector");
    
    switch (xmlTagName) {
        case "taconite-append-as-children":
            // results.append(this.getContent(taconiteCommand));
						results.each(function(elmt){
							elmt.insert( { bottom: this.getContent(taconiteCommand) });
						}, this);	        
            break;
        case "taconite-delete":
            // results.remove();
						results.each(function(elmt){
							elmt.remove();
						}, this);	        
            break;
        case "taconite-append-as-first-child":
            //results.prepend(this.getContent(taconiteCommand));
						results.each(function(elmt){
							elmt.insert( { top: this.getContent(taconiteCommand) });
						}, this);	        
            break;                         
        case "taconite-insert-after":
            //results.after(this.getContent(taconiteCommand));
						results.each(function(elmt){
							elmt.insert( { after: this.getContent(taconiteCommand) });
						}, this);	        
            break;
        case "taconite-insert-before":
            //results.before(this.getContent(taconiteCommand));
						results.each(function(elmt){
							elmt.insert( { before: this.getContent(taconiteCommand) });
						}, this);	        
            break;
        case "taconite-replace-children":
            //results.empty();
            //results.append(this.getContent(taconiteCommand));
						results.each(function(elmt){
						
							//elmt.childElements().each(function(elmtChild) {elmtChild.remove()});
							//elmt.insert( { bottom: this.getContent(taconiteCommand) });
							elmt.update(this.getContent(taconiteCommand));
						}, this);	        
            break;
        case "taconite-replace":
            //results.replaceWith(this.getContent(taconiteCommand));
						results.each(function(elmt){
							elmt.replace(this.getContent(taconiteCommand));
						}, this);	        
            break;                         
        case "taconite-set-attributes":
            this.handleAttributes(selector, taconiteCommand);
            break;
        case "taconite-redirect":
            this.handleRedirect(taconiteCommand);
            break;
        case "taconite-execute-javascript":
            eval(this.getContent(taconiteCommand));
            break;
    }

}

TaconitePrototypeParser.prototype.getContent = function(taconiteCommand) {
    var content = "";
    var child = null;
    for(var p = 0; p < taconiteCommand.childNodes.length; p++) {
        child = taconiteCommand.childNodes[p];
        if(child.nodeType == 4) {
            content = child.nodeValue;
            break;
        }
    }
    return content;
}

TaconitePrototypeParser.prototype.isTaconiteTag = function(node) {
    return node.tagName.substring(0, 9) == "taconite-";
}

TaconitePrototypeParser.prototype.handleAttributes = function(selector, xmlNode) {
    var attr  = null;
    var name  = "";
    var value = "";

		var results = $$(selector);
		results.each(function(elmt){
	    for(var x = 0; x < xmlNode.attributes.length; x++) {
	        attr  = xmlNode.attributes[x];
	        name  = attr.name;
	        value = attr.value;
	        
	        if(name == "class") {
	            Element.addClassName(elmt, value)
	        }
	        else if (name == "style"){
		       	elmt.setStyle(value);
	        }
	        else if (name == "value"){
	        	elmt.setValue(value);
	        }
	        else {
	        	Element.writeAttribute(elmt, name, value);
	        }
	    }
		}, this);	        
}

TaconitePrototypeParser.prototype.handleRedirect = function(node) {
    window.location.href = node.getAttribute("targetUrl");
}

