Ajax.Queue = Class.create();
Object.extend(Object.extend(Ajax.Queue.prototype, Ajax.Request.prototype), {
	initialize : function(urls, _options, __options){
		this.urls = urls;
		this._options = _options;
		this.count = this.urls.length;
		this.__options = __options||{};	
		this.transport = Ajax.getTransport();
		this.setOptions({
			onCreate : function(){
				if(this.i==0&&this.__options.onCreate){
					this.__options.onCreate();
				}
				if(this.option.onCreate){
					this.option.onCreate();
				}								
			}.bind(this),
			onComplete : function(transport, json){
				if(this.option.onComplete){
					try{
						this.option.onComplete(transport, json);
					}catch(e){
						this.dispatchException(e);
					}
				}
				if(this.i===this.count-1&&this.__options.onComplete){
					try{
						this.__options.onComplete(transport, json);
					}catch(e){
						this.dispatchException(e);
					}
				}
				var callback = function(){
					this.i++;
					this.start();	
				}.bind(this);
				/* This line is for IE, if you fire the callback immediately it chokes, so we wait a moment first */
				window.setTimeout(callback, 10);
			}.bind(this),
			onException : function(o, e){
				if(this.option.onException){
					this.option.onException(o, e);
				}
			}.bind(this),
			onInteractive : function(){
				if(this.option.onInteractive){
					this.option.onInteractive();
				}
			}.bind(this),
			onLoaded : function(){
				if(this.option.onLoaded){
					this.option.onLoaded();
				}
			}.bind(this),
			onLoading : function(){
				if(this.option.onLoading){
					this.option.onLoading();
				}
			}.bind(this),
			onUninitialized : function(){
				if(this.option.onUninitialized){
					this.option.onUninitialized();
				}
			}.bind(this)								
		});			
		this.i=0;
		this.start();
	},
	start : function(){							
		this._complete = false;	
		var url = this.urls[this.i];
		if (this._options instanceof Array){
			this.option = this._options.shift();
		}else{
			this.option = this._options;
		}			
		if(url){		
			this.request(url);
		}
	}	
});

Client = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	height: function(){
		var windowHeight;
		if (self.innerHeight) {    // all except IE
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { // IE 6 Strict Mode
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) { // other IEs
			windowHeight = document.body.clientHeight;
		}
		return windowHeight;
	}, 	
	width: function(){
		var windowWidth;
		if (self.innerWidth) {    // all except IE
			windowWidth = self.innerWidth;
		} else if (document.documentElement && document.documentElement.clientWidth) { // IE 6 Strict Mode
			windowWidth = document.documentElement.clientWidth;
		} else if (document.body) { // other IEs
			windowWidth = document.body.clientWidth;
		}
		return windowWidth;
	},
	scrollTop : function(){
		var y;
		if (self.pageYOffset){
			y = self.pageYOffset;
		}else if(document.documentElement && document.documentElement.scrollTop){
			y = document.documentElement.scrollTop;
		}else if (document.body){
			y = document.body.scrollTop;
		}
		return y;	
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
Client.init();

Element.addMethods({  
  appendText: function(element, text) {
    element = $(element);
    text = String.interpret(text);
    element.appendChild(document.createTextNode(text));
    return element;
  }
});