// ---------------------------------------------------------------------------
//    Copyright (c) 2006, LiquidPixels, Incorporated. All Rights Reserved.
//
//    This file represents Intellectual Property which is proprietary and
//  confidential to LiquidPixels, Incorporated. Any disclosure, in whole
//  or in part, is prohibited without express written permission.
//
//   $Id: Command.js 158 2009-03-27 23:05:38Z mspencer $
// ---------------------------------------------------------------------------

//import org.prototypejs.prototype.Prototype
//import com.liquidpixels.liquifire.LiquiFire

com.liquidpixels.liquifire.Command = Class.create({
	revision: "$Revision: 158 $",
	Version: function() {
	  	return revision.substr(11).substr(0, -2);
	},

	name:	"",
	atts:	{},
	
	initialize: function(name, atts) {
		this.name = name;
		this.atts = atts;
	},
	
	addAtt: function(key, value) {
		this.atts[key] = value;
	},
	
	asString: function() {
		var i;
		var c = "" + this.name + '=';
		
		var key;
		for(key in this.atts) {
			c +=  key + '[' + this.get(key) + '],';
		}
			
		return c.substring(0, c.length -1);
	},
	
	asHtml: function() {
		var c = '<span class="lfcommand">';

		if(this.name.match(/^#/)) {
			c += '<span class="comment">' + this.name;
			c += '=';

			for(var key in this.atts) {
				c += key + '[' + this.get(key) + ']';
			}
			c = c.substring(0, c.length);
			c = c.replace(/=$/, "");
		} else {
			c += '<span class="name">' + this.name + '</span>';
			c += '<span class="op">=</span>';
		
			for(var key in this.atts) {
				c += '<span class="argName">' + key + '</span>';
				c += '<span class="op">[</span>';
				c += '<span class="argValue">' + this.get(key) + '</span>';
				c += '<span class="op">],</span>';
			}
			c = c.substring(0, c.length -8);
		}
		
		c += "</span></span>";

		return c;
	},

	get: function(key) {
		return this.atts[key];
	},

	set: function(key, value) {
		if(this.atts == null) {
			this.atts = {};
		}
	
		this.atts[key] = value;
	},

	getName: function() {
		return this.name;
	},

	setName: function(newName) {
		this.name = newName;
	},

	attributes: function(key) {
		return this.atts;
	},

	parse: function(s) {
		var state = 'name';
		
		this.name = '';
		this.atts = {};
		
		var k = '';
		var v = '';
		
		for(var i=0; i<s.length; i++) {
			var c = s.substring(i, i+1);
		
			switch(state) {
				case 'name':
					if(c == '=') {
						state = 'key';
						break;
					}
						
					this.name += c;
				break;
	
				case 'key':
					if(c == '[') {
						state = 'val';
						break;
					}
						
					k += c;
				break;
	
				case 'val':
					if(c == ']') {
						this.set(k, v);
						state = 'haveatt';
						break;
					}
						
					v += c;
				break;
				
				case 'haveatt':
					k = '';
					v = '';
					
					if(c == ',')
						state = 'key';
				break;
	
			}
		}
	},

	clone: function() {
		var to = new com.liquidpixels.liquifire.Command();
		
		for (var i in this) {
			if(i == "atts") {
				to.atts = new Array();
				for(var j in this.atts) {
					to.atts[j] = this.atts[j];
				}
			} else {
				to[i] = this[i];
			}
		}
		
		return to;
	}
});




