/**
 * 功能：类似于java中的StringBuffer类的功能，用于字符串的拼接
 *       这种方式比直接用"+"性能要高出很多
 */
package("com.henry.lang");
com.henry.lang.StringBuffer = function(v) {
	var self = null;
	var out = [];
	
	var StringBuffer = classBuilder.create(this.constructor);
	
	function getHtmlStyleName(jsName) {
		jsName = jsName.replace(/^([^A-Z]+)([A-Z])([^A-Z]+)$/g, "$1-$2$3");
		jsName = jsName.replace(/^([^A-Z]+)([A-Z])([^A-Z]+)([A-Z])([^A-Z]+)$/g, "$1-$2$3-$4$5");
		return jsName;
	}
	
	with(StringBuffer) {
		prototype.initialize = function() {
			self = this;
			if (typeof v != "undefined") {
				this.append(v);
			}
		}
		/**
		 * 在StringBuffer中追加value的字符串值。
		 * @param value 待加入StringBuffer中的值
		 * @return 此StringBuffer
		 */
		prototype.append = function(value) {
			if (!value) {
				value = "" + value;
				//return this;
			}
			
			if (value == "") {
				return this;
			} 
			out.push(value);

			return this;
		}
		
		prototype.openTag = function(tagName) {
			out.push("<", tagName);
			return this;
		}
		prototype.closeTag = function() {
			out.push(">");
			return this;
		}
		
		prototype.putEvents = function(eventList) {
			//return this;
			for (var i=0; i<eventList.length; i++) {
				var eventName = eventList[i];
				out.push(" on", eventName, "=\"return this.document.parentWindow.$e.call(this)\"");
			}
			return this;
		}
		
		prototype.putAttributes = function (attributes) {
			if (attributes == null) {
				return this;
			}
			for(var attrName in attributes) {
				var attrValue = attributes[attrName];
				if (attrValue == null || attrValue == "" || (typeof attrValue == "undefined")) {
					continue;
				}

				if (attrName == "className") {
					attrName = "class";
				}
				
				if (attrValue == "true" || ((attrValue instanceof Boolean) && attrValue == true)) {
					out.push(" ", attrName);
				}
				else {
					out.push(" ", attrName, "=\"", attrValue, "\"");
				}				

			}
			return this;
		}
		
		prototype.putStyles = function (styles) {
			if (styles == null || styles.length == 0) {
				return this;
			}
			var i=0;
			for(var styleName in styles) {
				if (i>0) {
					out.push(";");
				}
				else if (i==0) {
					out.push(" ", "style=\"");
				}
				
				var styleValue = styles[styleName];

				out.push(getHtmlStyleName(styleName), ":", styleValue);
				i++;
			}
			
			if (i>0) {
				out.push("\"");
			}

			return this;
		}
		prototype.endTag = function (tagName) {
			out.push("</", tagName, ">");
			return this;
		}
		
		prototype.out = function() {
			return out;
		}
		
		prototype.toString = function() {
			return out.join("");
		}
	}
	return new StringBuffer();
}

var StringBuffer = com.henry.lang.StringBuffer;