// ---------------------------------------------------------------------------
//    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: Chain.js 118 2008-12-18 00:06:20Z mspencer $
// ---------------------------------------------------------------------------

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


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

	server:		"",
	port:		undefined,
	uri:		"",
	commands:	new Array(),
	
	initialize: function(server, uri, port) {
		if(server != undefined)		this.setServer(server);
		if(uri != undefined)		this.setURI(uri);
		if(port != undefined)		this.setPort(port);
		
		this.commands	= new Array();
	},
	
	setServer: function(s) {
		this.server = s;
	},

	getServer: function() {
		return this.server;
	},

	setPort: function(n) {
		this.port = n;
	},

	getPort: function() {
		return this.port;
	},

	setURI: function(s) {
		this.uri = s;
	},

	getURI: function() {
		return this.uri;
	},

	addCommand: function(command) {
		this.commands.push(command);
	},

	deleteCommand: function(pos) {
		if(pos)
			this.commands.splice(pos, 1);
	},

	getCommand: function(pos) {
		return this.commands[pos];
	},

	findCommand: function(name, count) {
		var c = 0;

		if(count == undefined)
			count = -1
		
		for(var i=0; i<this.commands.length; i++) {
			if(this.commands[i].getName() == name) {
				c++;
				
				if(c > count)
					return i;
			}
		}
		
		return null;
	},

	getCommands: function() {
		return this.commands;
	},

	setCommand: function(pos, newCommand) {
		this.commands[pos] = newCommand;
	},

	numCommands: function() {
		return this.commands.length;
	},

	length: function() {
		return this.commands.length;
	},

	insertCommand: function(pos, newCommand) {
		if(pos)
			this.commands.splice(pos, 0, newCommand);
	},
	
	asString: function() {
		var i;
		var chain = '';
		
		for(i=0; i<this.commands.length; i++)
			chain += this.commands[i].asString() + '&';
			
		return chain.substring(0, chain.length -1);
	},

	asURL: function() {
		var chain = encodeURI(this.asString());

		chain = chain.replace(/#/g,"%23");

		return 'http://' + this.server + 
			(this.port != undefined && this.port != 80 ? this.port : '')
			+ '/' + this.uri + '?' + chain;
	},

	parse: function(s) {
		this.commands = new Array();
		var cStrings = s.split('&');
		for(var i = 0; i < cStrings.length; i++) {
			var c = new com.liquidpixels.liquifire.Command();
			c.parse(cStrings[i]);
			this.addCommand(c);
		}
	},

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





