/**
 * ConCom框架
 * 
 * @author Kaven
 */
var ConCom = {
	version : '1.0'
};
ConCom.apply = function(o, c, defaults) {
	// no "this" reference for friendly out of scope calls
	if (defaults) {
		ConCom.apply(o, defaults);
	}
	if (o && c && typeof c == 'object') {
		for (var p in c) {
			o[p] = c[p];
		}
	}
	return o;
};
(function() {
	var toString = Object.prototype.toString, ua = navigator.userAgent
			.toLowerCase(), check = function(r) {
		return r.test(ua);
	}, DOC = document, isStrict = DOC.compatMode == "CSS1Compat", isOpera = check(/opera/), isChrome = check(/chrome/), isWebKit = check(/webkit/), isSafari = !isChrome
			&& check(/safari/), isSafari2 = isSafari && check(/applewebkit\/4/), isSafari3 = isSafari
			&& check(/version\/3/), isSafari4 = isSafari && check(/version\/4/), isIE = !isOpera
			&& check(/msie/), isIE7 = isIE && check(/msie 7/), isIE8 = isIE
			&& check(/msie 8/), isIE6 = isIE && !isIE7 && !isIE8, isGecko = !isWebKit
			&& check(/gecko/), isGecko2 = isGecko && check(/rv:1\.8/), isGecko3 = isGecko
			&& check(/rv:1\.9/), isBorderBox = isIE && !isStrict, isWindows = check(/windows|win32/), isMac = check(/macintosh|mac os x/), isAir = check(/adobeair/), isLinux = check(/linux/), isSecure = /^https/i
			.test(window.location.protocol);

	ConCom.apply(ConCom, {
		isEmpty : function(v, allowBlank) {
			return v === null || v === undefined
					|| ((ConCom.isArray(v) && !v.length))
					|| (!allowBlank ? v === '' : false)||v=="undefined";
		},
		// 判断是否数组
		isArray : function(v) {
			return toString.apply(v) === '[object Array]';
		},
		// 判断是否对象
		isObject : function(v) {
			return v && typeof v == "object";
		},
		// 判断是否函数
		isFunction : function(v) {
			return toString.apply(v) === '[object Function]';
		},
		// 判断是否数字
		isNumber : function(v) {
			// return typeof v === 'number' && isFinite(v);
			return isFinite(v);
		},
		// 判断是否字符串
		isString : function(v) {
			return typeof v === 'string';
		},
		// 判断是否布尔型
		isBoolean : function(v) {
			return typeof v === 'boolean';
		},
		// 判断是否未定义
		isDefined : function(v) {
			return typeof v !== 'undefined';
		},

		urlEncode : function(o, pre) {
			var undef, buf = [], key, e = encoConComRIComponent;

			for (key in o) {
				undef = !ConCom.isDefined(o[key]);
				ConCom.each(undef ? key : o[key], function(val, i) {
							buf.push("&", e(key), "=", (val != key || !undef)
											? e(val)
											: "");
						});
			}
			if (!pre) {
				buf.shift();
				pre = "";
			}
			return pre + buf.join('');
		},

		urlDecode : function(string, overwrite) {
			var obj = {}, pairs = string.split('&'), d = decodeURICConComonent, name, value;
			ConCom.each(pairs, function(pair) {
						pair = pair.split('=');
						name = d(pair[0]);
						value = d(pair[1]);
						obj[name] = overwrite || !obj[name] ? value : []
								.concat(obj[name]).concat(value);
					});
			return obj;
		},
		// 添加参数
		urlAppend : function(url, s) {
			if (!ConCom.isEmpty(s)) {
				return url + (url.indexOf('?') === -1 ? '?' : '&') + s;
			}
			return url;
		},
		/**
		 * Iterates an array calling the passed function with each item,
		 * stopping if your function returns false. If the passed array is not
		 * really an array, your function is called once with it. The supplied
		 * function is called with (Object item, Number index, Array allItems).
		 * 
		 * @param {Array/NodeList/Mixed}
		 *            array
		 * @param {Function}
		 *            fn
		 * @param {Object}
		 *            scope
		 */
		each : function(array, fn, scope) {
			if (ConCom.isEmpty(array, true)) {
				return;
			}
			if (!isIterable(array) || ConCom.isPrimitive(array)) {
				array = [array];
			}
			for (var i = 0, len = array.length; i < len; i++) {
				if (fn.call(scope || array[i], array[i], i, array) === false) {
					return i;
				};
			}
		},
		/**
		 * 添加enter事件
		 * 
		 * @param objId
		 *            添加事件的元素id
		 * @param fn
		 *            要执行的函数
		 */
		enterEvent : function(objId, fn) {
			if (document.getElementById(objId) != undefined) {
				if (isIE) {
					document.getElementById(objId).onkeyup = function() {
						if (window.event.keyCode == 13)
							fn();
					}
				} else {
					document.getElementById(objId).addEventListener("keyup",
							function(event) {
								if (event.keyCode == 13)
									fn();
							}, false);
				}
			}
		},
		/* 判断浏览器 */
		isOpera : isOpera,
		isWebKit : isWebKit,
		isChrome : isChrome,
		isSafari : isSafari,
		isSafari3 : isSafari3,
		isSafari4 : isSafari4,
		isSafari2 : isSafari2,
		isIE : isIE,
		isIE6 : isIE6,
		isIE7 : isIE7,
		isIE8 : isIE8,
		isGecko : isGecko,
		isGecko2 : isGecko2,
		isGecko3 : isGecko3,
		isBorderBox : isBorderBox,
		isLinux : isLinux,
		isWindows : isWindows,
		isMac : isMac,
		isAir : isAir,
		/**
		 * 自身跳转
		 * 
		 * @param url
		 *            要跳转的url
		 */
		goHref : function(url, jumpurl) {
			if (!ConCom.isEmpty(url))
				if (ConCom.isEmpty(jumpurl))
					window.location.href = url;
				else
					window.location.href = url + "?url=" + jumpurl;
			else
				alert("未传入url");
		},
		/**
		 * 自身跳转，优先jumpurl跳转
		 * 
		 * @param url
		 *            默认跳转url
		 * @param jumpurl
		 *            优先跳转url
		 */
		goJump : function(url, jumpurl) {
			if (!ConCom.isEmpty(url))
				if (ConCom.isEmpty(jumpurl))
					window.location.href = url;
				else
					window.location.href = jumpurl;
			else
				alert("未传入url");
		},
		/**
		 * 新开窗口跳转
		 * 
		 * @param url
		 *            要跳转的url
		 */
		goOpen : function(url) {
			if (!ConCom.isEmpty(url))
				window.open(url);
			else
				alert("未传入url");
		},
		/**
		 * 跳转历史
		 */
		goHistory : function() {
			url = window.history.back(-1);
			// goHref();
		},
		/**
		 * 获取url参数
		 * 
		 * @param argName
		 *            参数名称(可选)
		 * @return Object/String
		 */
		getCurArgs : function(argName) {
			var args = new Object();
			var query = location.search.substring(1); // Get query
			// string
			var pairs = query.split("&"); // Break at ampersand
			for (var i = 0; i < pairs.length; i++) {
				var pos = pairs[i].indexOf('='); // Look for
				// "name=value"
				if (pos == -1)
					continue; // If not found, skip
				var argname = pairs[i].substring(0, pos); // Extract
				// the name
				var value = pairs[i].substring(pos + 1); // Extract
				// the value
				value = decodeURIComponent(value); // Decode it, if
				// needed
				args[argname] = value; // Store as a property
			}
			if ((argName || "") == "")
				return args; // Return the object,you can use it by
			// this method:
			// "getCurArgs()["xx"]" or "getCurArgs().xx"
			else
				return args[argName]; // return the element,you can
			// use it by this
			// method: getCurArgs("xx")
		},
		// 定义Cookie管理
		Cookie : function() {
		},
		// 定义util
		util : {}
	});
})();
/**
 * 字符串添加自定义方法
 */
ConCom.apply(String.prototype, {
			/**
			 * 字符串去除空白
			 */
			tirm : function() {
				var re = /^\s+|\s+$/g;
				return function() {
					return this.replace(re, "");
				};
			},
			/**
			 * 计算中文长度
			 */
			gblen : function() {
				var len = 0;
				var txt = this.replace(/(^\s*)|(\s*$)/g, "");
				txt = txt.replace(/(^\s*)/g, "");
				txt = txt.replace(/(\s*$)/g, "");
				var templen = "";
				var temptype = 1;
				for (var i = 0; i < txt.length; i++) {
					if (txt.charCodeAt(i) > 127 || txt.charCodeAt(i) == 94) {
						len += 2;

					} else {
						len++;
					}
				}
				return len;
			},
			/**
			 * 截取中文
			 * 
			 * @param len
			 *            截取长度：英文长度的2倍
			 * @param s
			 *            自定义省略号
			 */
			gbtrim : function(len, s) {
				var txt = this.replace(/(^\s*)|(\s*$)/g, "");
				txt = txt.replace(/(^\s*)/g, "");
				txt = txt.replace(/(\s*$)/g, "");
				var str = '';
				var sp = s || '';
				var len2 = 0;
				for (var i = 0; i < txt.length; i++) {
					if (txt.charCodeAt(i) > 127 || txt.charCodeAt(i) == 94) {
						len2 += 2;
					} else {
						len2++;
					}
				}
				if (len2 <= len) {
					return txt;
				}
				len2 = 0;
				len = (len > sp.length) ? len - sp.length : len;
				for (var i = 0; i < txt.length; i++) {
					if (txt.charCodeAt(i) > 127 || txt.charCodeAt(i) == 94) {
						len2 += 2;
					} else {
						len2++;
					}
					if (len2 > len) {
						str += sp;
						break;
					}
					str += txt.charAt(i);
				}
				return str;
			},
			/**
			 * 去除特殊符号
			 * 
			 * @param string
			 *            字符串
			 * @return String
			 */
			delSpec : function() {
				var string = this;
				string = string.replace(/(;)/g, "；");
				string = string.replace(/(~)/g, "～");
				string = string.replace(/(')/g, "‘");
				string = string.replace(/(")/g, "“");
				string = string.replace(/(<)/g, "&lt;");
				string = string.replace(/(>)/g, "&gt;");
				string = string.replace(/(&nbsp;)/g, "");
				string = string.replace(/(&nbsp)/g, "");
				string = string.replace(/(<p>|<\/p>)/g, "");
				return string;
			}
		});

// 为数组添加方法
ConCom.apply(Array.prototype, {
			/**
			 * 数组查找元素，找到则返回索引值，没找到则返回-1
			 * 
			 * @param o
			 *            Object
			 * @return int
			 */
			indexOf : function(o) {
				for (var i = 0, len = this.length; i < len; i++) {
					if (this[i] == o) {
						return i;
					}
				}
				return -1;
			},

			/**
			 * Removes the specified object from the array. If the object is not
			 * found nothing happens.
			 * 
			 * @param {Object}
			 *            o The object to remove
			 * @return {Array} this array
			 */
			remove : function(o) {
				var index = this.indexOf(o);
				if (index != -1) {
					this.splice(index, 1);
				}
				return this;
			}
		});
/**
 * Date扩展
 */
ConCom.apply(Date.prototype, {
			format : function(fmt) {
				var o = {
					"M+" : this.getMonth() + 1, // 月份
					"d+" : this.getDate(), // 日
					"h+" : this.getHours() % 12 == 0 ? 12 : this.getHours()
							% 12, // 小时
					"H+" : this.getHours(), // 小时
					"m+" : this.getMinutes(), // 分
					"s+" : this.getSeconds(), // 秒
					"q+" : Math.floor((this.getMonth() + 3) / 3), // 季度
					"S" : this.getMilliseconds()
					// 毫秒
				};
				var week = {
					"0" : "\u65e5",
					"1" : "\u4e00",
					"2" : "\u4e8c",
					"3" : "\u4e09",
					"4" : "\u56db",
					"5" : "\u4e94",
					"6" : "\u516d"
				};
				if (/(y+)/.test(fmt)) {
					fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "")
									.substr(4 - RegExp.$1.length));
				}
				if (/(E+)/.test(fmt)) {
					fmt = fmt.replace(RegExp.$1,
							((RegExp.$1.length > 1) ? (RegExp.$1.length > 2
									? "\u661f\u671f"
									: "\u5468") : "")
									+ week[this.getDay() + ""]);
				}
				for (var k in o) {
					if (new RegExp("(" + k + ")").test(fmt)) {
						fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1)
										? (o[k])
										: (("00" + o[k])
												.substr(("" + o[k]).length)));
					}
				}
				return fmt;
			},
			/**
			 * 日期比较，返回两日期之间的毫秒数 Returns the number of milliseconds between this
			 * date and date
			 * 
			 * @param {Date}
			 *            date (optional) Defaults to now
			 * @return {Number} The diff in milliseconds
			 * @member Date getElapsed
			 */
			getElapsed : function(date) {
				return Math
						.abs((date || new Date()).getTime() - this.getTime());
			},
			/**
			 * 两个日期比较
			 * 
			 * @param String
			 *            date1
			 * @param String
			 *            date2
			 * @return int
			 */
			compareDate : function(date1, date2) {
				date1 = new Date(date1.replace(/-/g, "/"));
				date2 = new Date(date2.replace(/-/g, "/"));
				if ((Date.parse(date1) - Date.parse(date2)) > 0)
					return (Date.parse(date1) - Date.parse(date2));
				else
					return -1;
			}
		});

/**
 * cookie管理 设置，获取，删除
 */
ConCom.apply(ConCom.Cookie.prototype, {
			/**
			 * 设置cookie
			 * 
			 * @param key
			 *            cookie名称
			 * @param value
			 *            值
			 * @param option
			 *            配置项
			 */
			setCookie : function(key, value, option) {
				var str = key + "=" + encodeURIComponent(value);
				if (option) {
					if (option.expireDays) {
						var date = new Date();
						var ms = option.expireDays * 24 * 3600 * 1000;
						date.setTime(date.getTime() + ms);
						str += "; expires=" + date.toGMTString();
					}
					if (option.path)
						str += "; path=" + option.path;
					if (option.domain)
						str += "; domain" + option.domain;
					if (option.secure)
						str += "; true";
				}
				document.cookie = str;
			},
			/**
			 * 获取cookie
			 * 
			 * @param key
			 *            cookie名称
			 */
			getCookie : function(key) {
				var cookieArray = document.cookie.split("; ");
				var cookie = new Object();
				for (var i = 0; i < cookieArray.length; i++) {
					var arr = cookieArray[i].split("=");
					if (arr[0] == key)
						return decodeURIComponent(arr[1]);
				}
				return "";
			},
			/**
			 * 删除cookie
			 * 
			 * @param key
			 *            cookie名称
			 */
			deleteCookie : function(key) {
				ConCom.Cookie.setCookie(key, "", {
							expireDays : -1
						})
			}
		});

/**
 * 工具类
 */
ConCom.apply(ConCom.util, {
			Format : {
				trimRe : /^\s+|\s+$/g,

				// 去除空格
				trim : function(value) {
					return String(value).replace(trimRe, "");
				},

				// 替换标签正则
				stripTagsRE : /<\/?[^>]+>/gi,

				/**
				 * Strips all HTML tags
				 * 
				 * @param {Mixed}
				 *            value The text from which to strip tags
				 * @return {String} The stripped text
				 */
				stripTags : function(v) {
					return !v ? v : String(v).replace(this.stripTagsRE, "");
				}

			}
		});

