/*
	作者：戴铭
	功能：通用函数集
*/

/*
	功能：简化的创建标签方法
	描述：tagname为标签名如input，attributes为一个对象，里面包含如{id:'oneid', class:'oneclass'}，children是一个数组或字符串作为子节点内容。如果第二个参数attribute为空，那么第三个参数children会自动为第二参数。此函数来自http://mochikit.com这个js库。()
*/
function make(tagname, attributes, children) {
	if(arguments.length == 2 && (attributes instanceof Array || typeof attributes == "string")) {
		children = attributes;
		attributes = null;
	}
	
	var e = document.createElement(tagname);
	
	if(attributes) {
		for(var name in attributes) {
			//处理ie对style属性的不兼容
			if(name == 'style') {
				var re = /(;)$/;
				var stylestr = attributes[name].replace(re, '');
				var stylearr = stylestr.split(";");
				for(var i = 0; i < stylearr.length; i++) {
					var stylename = stylearr[i].split(":")[0];
					//去掉-然后把后面一个字母大写
					var re = /-([a-z]{1})/;
					stylename = stylename.replace(re, function(str) {
					return str.substring(1).toUpperCase();
					});					
					var stylevalue = stylearr[i].split(":")[1];
					if(stylename == 'float') {
						stylename = 'styleFloat';//ie
						eval("e.style." + stylename + " = '" + stylevalue + "'");
						stylename = 'cssFloat';//ff
					}
					eval("e.style." + stylename + " = '" + stylevalue + "'");
				}				
			} else {
				//如果是事件的处理
				if(name == 'onclick') {
					eval("e." + name + " = function() {" + attributes[name] + "};");
				} else {
					if(name == 'class') {
						e.className = attributes[name];
					} else {
						e.setAttribute(name, attributes[name]);
					}
				}
			}
		}
	}
	
	if(children != null) {
		if(children instanceof Array) {
			for(var i = 0; i < children.length; i++) {
				var child = children[i];
				if(typeof child == "string") {
					child = document.createTextNode(child);
				}
				e.appendChild(child);
			}
		} else if (typeof children == "string") {
			e.appendChild(document.createTextNode(children));
		} else {
			e.appendChild(children);
		}
	}
	
	return e;
}
//例：var table = maker("table"), tr = maker("tr"), td = maker("td");
//table(attributes, children);
function maker(tag) {
	return function(attrs, kids) {
		if(arguments.length == 1) {
			return make(tag, attrs);
		} else {
			return make(tag, attrs, kids);
		}
	}
}

/*
	功能：加一个父标签
	描述：
	第一个参数n指定为哪个element。
	第二个parenttag是标签的名字。
	第三个attribute是个二维数组，写法为[[属性名1,属性值1],[属性名2,属性名2],[属性名3,属性名3]]
*/
function wraptag(n, parenttag, attribute) {
	var b = document.createElement(parenttag);
	if(attribute) {
		for(var i = 0; i < attribute.length; i++) {
			
			b.setAttribute(attribute[i][0], attribute[i][1]);
		}
	}	
	var parent = n.parentNode;
	parent.replaceChild(b, n);
	b.appendChild(n);
}

/*
	取到classname的element内容
*/
function getelements(classname, tagname, root) {
	
	if(!root) {
		root = document;
	} else if(typeof root == "string") {
		root = document.getElementById(root);
	}
	
	if(!tagname) {	
		tagname = "*";
	}
	var all = root.getElementsByTagName(tagname);

	if(!classname) {
		return all;
	}	
	var elements = [];
	for(var i = 0; i < all.length; i++) {
		var element = all[i];
		if(ismember(element, classname)) {
			elements.push(element);
		}
	}
	return elements;
	
	function ismember(element, classname) {
		var classes = element.className;
		if(!classes) {
			return false;
		}
		if(classes == classname) {
			return true;
		}
		var whitespace = /\s+/;
		if(!whitespace.test(classes)) {
			return false;
		}
		var c = classes.split(whitespace);
		for(var i=0; i < c.length; i++) {
			if(c[i] == classname) {
				return true;
			}
		}
		return false;
	}
}

/*
	功能：取id,class,tag等的element
	描述：用法是
	$('#id')：返回指定id的element
	$('.classname')：返回指定的classname的elements集合
	$('tagname')：返回指定的tagname的element集合
	$('tagname', $('#id'))：返回以特定id的element下的指定tagname集合
*/
function m$(name, root) {
	
	var firstchar = name.substr(0,1);
	var newname = name.substr(1,name.length);
	var idelement;
	if(!root) {
		root = document;
	}	
	if(firstchar == '#') {	
		idelement = root.getElementById(newname);
	} else if(firstchar == '.') {
		idelement = getelements(newname, '', root);
	} else {
		idelement = root.getElementsByTagName(name);
	}
	return idelement;
}

/*
	功能：调试打印数组和对象用
*/
/*
function dstr(o)
{
	var br = "<br/><br/><br/>";
	var str = "{" + br;
	for(attr in o)	{
		if(typeof o[attr] == "object") {
			tmp = attr + " => " + dstr(o[attr]) + br;
		} else {
			tmp = attr + " => " + o[attr] + br;
		}
		str += tmp;

	}
	str += "}";
	return str;
}
function d(o) {
	str = dstr(o);
	document.write(str);
}
*/
function d(o) {
	var br = "<br/><br/><br/>";
	var str = "{" + br;
	for(attr in o) {
		tmp = attr + " => " + o[attr] + br;
		str += tmp;
	}
	str += "}";
	document.write(str);
}
/*
	功能：打印字符串
*/
function dw(str) {
	document.write(str);
}

/*
	功能：取到url里的参数
*/
function getargs() {
	var args = new Object();
	var query = location.search.substring(1);
	var pairs = query.split("&");
	for(val in pairs) {
		argobj = pairs[val].split("=");
		args[argobj[0]] = argobj[1];
	}
	return args;
}
/***************命名空间*************************/
var m = {};

/***************一些初始配置等*******************/
//浏览器不显示js错误
//window.onerror = function() { return true; }

