var MooUrchin = new Class({
	properties: {
		"screenWidth": screen.width,
		"screenHeight": screen.height,
		"screenColorDepth": screen.colorDepth,
		"clientWidth":0,
		"clientHeight":0,
		"documentTitle":escape(document.title.replace(/[\\"]/g,"")),
		"documentUrl":escape(window.location.href.replace(/[\\"]/g,"")),
		"documentReferrer":escape(document.referrer.replace(/[\\"]/g,"")),
		"documentQueryString":escape(window.location.search.replace(/[\\"]/g,"")),
		"navigatorUserAgent":navigator.userAgent,
		"navigatorLanguage": window.ie ? navigator.userLanguage : navigator.language,
		"navigatorPlatform":navigator.platform,
		"navigatorCookieEnabled":navigator.cookieEnabled
	},
	
	initialize: function(cssSelector, options){
		this.properties.clientWidth = document.body ? document.body.clientWidth : 0;
		this.properties.clientHeight = document.body ? document.body.clientHeight : 0;
		
		this.setOptions(this.properties, options);
		this.elements = $$(cssSelector);
		this.urchinObjects = [];
		
		this.elements.each((function(dl){
			this.urchinObjects.push({"name":dl.getProperty("title"), "stats": this.parse(dl) });
		}).bind(this));
	},
	
	parse: function(dl){
		dl.addClass("parsed");
		var urchins = [];
			
		dl.getChildren().each(function(node){
			if(node.hasClass("key")){
				urchins.push({key:node.getProperty("title"),values:[]});
			}
			if(node.hasClass("value")){
				var urchin = urchins.getLast();
				if(urchin)
					urchin.values.push(node.getProperty("title"));
							
				$ES("dl", node).each(function(child){
					if(!child.hasClass("parsed"))
						urchin.values.push({"name": child.getProperty("title"), "stats": this.parse(child)});
				}.bind(this));
			}
		}.bind(this));
		
		return urchins;
	},
	
	serialize: function(method){
		if(method == "ajax"){
			return Json.toString({ "clientInformation": this.options, "pageTags": this.urchinObjects });
		} else {
			return { "clientStatistics": this.options, "pageStatistics": this.urchinObjects };
		}
	}
});

MooUrchin.implement(new Options);
