/* my silly servo parser so I don't have to keep typing in those servo
    specifications every 5 minutes... read from xml, thanks!
*/


var ServoLoader = Class.create();
Object.extend(ServoLoader.prototype, {
	
	feedURL:            'rss/servos.rss',
	items:              null,
    
	initialize: function(container,feed)
	{
        this.feedURL = feed;
        this.objref = $(container);
		this.items = [];
        
        this.loadingMessage();
		
		new Ajax.Request(
			this.feedURL,
			{
				method: "get",
                onLoading: function(transport)
                    {
                    },

				onSuccess: function(response)
					{
					this.parseXML(response.responseXML);
                    this.filltable();
					}.bind(this),
                    
				onFailure: function()
					{
					//console.log("Please visit http://www.fatlion.com/sailplanes/servochart.html.");
					},
                    
				onException: function(req, err) 
					{
					// throw(err);
					}
			}
		);
	},
    
    fetchItem: function(item, itemstr, extremes)
    {
        // allow empty elements
        try {
            var fetched = item.getElementsByTagName(itemstr)[0].childNodes[0].nodeValue;
            
            if (fetched === null)
                return "";
            
            // match extreme values to items, make 'em blue
            if (extremes.indexOf(itemstr) > -1)
                {
                fetched = '<font color="blue"><b>' + fetched + '</b></font>';
                }
                
            return fetched.toString().gsub("'cr'","<br>");
            }
        catch (e)
            {
            return "";
            }
    },
    
	parseXML: function(xml)
    {
    	var odd = 0;
    	
		// build the array of news titles
		$A(xml.getElementsByTagName("item")).each(function(item)
            {
            // make extreme mark this servo as red
            var ext     = this.fetchItem(item, "extremes", "");
            var name    = this.fetchItem(item, "name", ext);
            var desc    = this.fetchItem(item, "desc", ext);
            var weight  = this.fetchItem(item, "weight", ext);
            var dim     = this.fetchItem(item, "dims", ext);
            var torque  = this.fetchItem(item, "torque", ext);
            var speed   = this.fetchItem(item, "speed", ext);
            var type    = this.fetchItem(item, "type", ext);
            var price   = this.fetchItem(item, "price", ext);
            var note    = this.fetchItem(item, "note", ext);
            var link    = this.fetchItem(item, "link", ext);
            
            if (link != "")
                {
                name = '<a href="' + link + '" target="_blank">' + name + '</a>';
                }
                            
			//this.items.push({name: string, weight: string, size: string});
            //this.items.push('<tr><td align=\"center\" nowrap>' + name + '</td><td align=\"center\">' + desc + '</td><td align=\"center\">' + weight + '</td><td align=\"center\">' + dim + '</td><td align=\"center\">' + torque + '</td><td align=\"center\">' + speed + '</td><td align=\"center\">' + motor + '</td><td align=\"center\">' + price + '</td><td>' + note + '</td></tr>');
            // lets do alternating colored rows, no matter the browser!
            if (odd & 1)
            	{
            	this.items.push('<tr bgcolor="#b8e1e0" align=\"center\"><td nowrap><b>' + name + '</b></td><td>' + desc + '</td><td nowrap>' + weight + '</td><td nowrap>' + dim + '</td><td nowrap>' + torque + '</td><td nowrap>' + speed + '</td><td>' + type + '</td><td>' + price + '</td><td>' + note + '</td></tr>');
            	}
            else 
            	this.items.push('<tr align=\"center\"><td nowrap><b>' + name + '</b></td><td>' + desc + '</td><td nowrap>' + weight + '</td><td nowrap>' + dim + '</td><td nowrap>' + torque + '</td><td nowrap>' + speed + '</td><td>' + type + '</td><td>' + price + '</td><td>' + note + '</td></tr>');
            
            odd++;
            }.bind(this));
	},

    setFirstChild: function(parentEl, childEl)
    {
        if (parentEl.firstChild)
            {
            parentEl.insertBefore(childEl, parentEl.firstChild);
            parentEl.removeChild(parentEl.childNodes[1]);
            }
        else
            {
            parentEl.appendChild(childEl);
            }
    },
    
    foo: function() {
    },
    
    loadingMessage: function()
    {
        var node = document.createElement('div');
        
        node.innerHTML = "Loading...";
        this.setFirstChild(this.objref, node);
    },
    
    filltable: function()
    {
        // wipe out the Loading message
        try {
            this.objref.removeChild(this.objref.childNodes[0]);
            }
        catch (x)
            {
            }
    	
        var index = 0;
        var node = document.createElement('div');
        var stuff = "";
        
        stuff = "<table><tr align=\"center\"><th nowrap><b>Servo #</b></th> \
                            <th nowrap><b>Description</b></th> \
                            <th nowrap><b>Weight</b></th> \
                            <th nowrap><b>Dimensions<br>(W x L x H)</b></th> \
                            <th nowrap><b>Torque</b></th> \
                            <th nowrap><b>Speed</b></th> \
                            <th><b>Motor/Bearings</b></th> \
                            <th><b>Street Price</b></th> \
                            <th><b>Note</b></th> \
                            </tr>";
        do {
            stuff += this.items[index++];
        } while (index < this.items.length);
        
        node.innerHTML = stuff + "</table>";
        
        /*
		node.style.visibility = "visible";
		node.style.position = "absolute";
		node.style.width = 780 + "px";
		node.style.height = 500 + "px";
		node.style.overflow = "hidden";
		*/
        
       
        this.objref.appendChild(node);
	}
	
});
