
//document.getElementById 的快捷方式
function $(ID){
	var node = typeof ID == "string" ? document.getElementById(ID) : ID;
	if (node != null) {
		return node;
	}
	return null;
}

//获取当前对象所应用的样式
function GetCurrentStyle(el, prop) {
	if (el.currentStyle) {
		return el.currentStyle[prop];
	}else if (window.getComputedStyle) {
		prop = prop.replace(/([A-Z])/g, "-$1");
		prop = prop.toLowerCase();
		return window.getComputedStyle(el, "").getPropertyValue(prop);
	}
	return null;
}

//切换HTML元素显示/隐藏状态
function $toggle(el, flag){
	var el = typeof el == "string" ? $(el) : el;
	var curState = GetCurrentStyle(el, 'display');
	var flag = (curState == 'none' ? 'block' : 'none');
	el.style.display = flag;
}

//显示一个HTML元素
function $show(el){
	el = typeof(el) == "string" ? $(el) : el;
	el.style.display = 'block';
}

//隐藏一个HTML元素
function $hide(el){
	el = typeof(el) == "string" ? $(el) : el;
	el.style.display = 'none';
}

//当文档载入后执行一段函数
function onReady(fn) {
	if (typeof fn != "function") return;
	if (window.addEventListener) {
		window.addEventListener("load", fn, false);
	}else {
		window.attachEvent("onload", fn);
	}
}

/**
 * 非IE浏览器增加contains方法
 * @example obj.contains(obj);
 */
if(!(/msie/).test(navigator.userAgent.toLowerCase())) {
	if(typeof(HTMLElement) != "undefined") {
		HTMLElement.prototype.contains = function (obj) {
			while(obj != null && typeof(obj.tagName) != "undefind") {
				if(obj == this) return true;
				obj = obj.parentNode;
			}
			return false;
		};
	}
}

/**
 * 在指定节点上绑定相应的事件
 * @method $addEvent
 * @param {String} elm 需要绑定的节点id
 * @param {Function} func 事件发生时响应的函数
 * @param {String} evType 事件的类型如:click, mouseover
 * @example
 * 		//鼠标点击testEle则alert "clicked"
 * 		$addEvent2("testEle",function () {
 * 			alert("clicked")
 * 		},'click');
 */
function $addEvent(elm, func, evType, useCapture) {
	var elm = $(elm);
	if(!elm) return;
	var useCapture = useCapture || false;
	var evType = evType || 'click';
	if (elm.addEventListener) {
		elm.addEventListener(evType, func, useCapture);
		return true;
	}
	else if (elm.attachEvent) {
		var r = elm.attachEvent('on' + evType, func);
		return true;
	}
	else {
		elm['on' + evType] = func;
	}
};

//错误信息 ID_info
function $Red(el, Err){
	var el = el + "_info";
	$setClassName(el, "info_false");
	$inner(el, Err);
}

//通过 ID_info
function $Green(el){
	var el = el + "_info";
	$setClassName(el, "info_true");
	$inner(el, '&nbsp;');
}

//innerHTML 快捷方式
function $inner(el, info){
	el = typeof(el) == "string" ? $(el) : el;
	el.innerHTML = info;
}

//innerHTML 快捷方式
function Inner(el, info){
	el = typeof(el) == "string" ? $(el) : el;
	el.innerHTML = el.innerHTML + info;
}

//是否为空
function $empty(el){
	var el = $(el).value;
	if($trim(el) == '')
		return true;
	else
		return false;
}

//去除前后空格
function $trim(string){
	return string.replace(/(^\s*)|(\s*$)/g, "");
}

/**
 * 判断某字符串是否在目标数组中
 * @param {String}  目标关键字
 * @param {Array}   目标数组
 * @param {Boolean}   是否全等(即完全相同)
 */
function $inArr(key, arr, same) {
	if(!same) return arr.join(' ').indexOf(key) != -1;
	for (var i=0, l = arr.length; i< l ; i++) if(arr[i]==key) return true;
	return false;
}

/**
 * 为对象添加 className
 * @param {Object} 需要添加className的节点
 * @param {String}  要添加的 className
 */
function $addClassName(el, cls) {
	var el = $(el);
	if(!el) return;
	var clsNames = el.className.split(/\s+/);
	if(!$inArr(cls, clsNames, true)) el.className += ' '+cls;
};

/**
 * 覆盖对象的 className
 * @param {Object} 需要覆盖className的节点
 */
function $setClassName(el, cls){
	var el = $(el);
	if(!el) return;
	el.className = cls;
}

/**
 * 获取字串的长度 中文算两位
 * @param {Object} 需要计算的节点
 */
function ShowLength(el){
	var el = $(el);
	if(!el) return;
	var value = el.value;
    var strlen = value.length;
	var n=strlen;
    for (var i=0;i<strlen;i++){if (value.charCodeAt(i)<0||value.charCodeAt(i)>255) n++;}
	return n;
}

function Random(){
	var date = new Date();
	return date.getTime();
}

/**
 * 获取浏览器高度
 */
function GetClientHeight(){
	return document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight;
}

/**
 * 获取浏览器宽度
 */
function GetClientWidth(){
	return document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body.clientWidth;
}

function Moveit(obj, total, n){
	for(var i = 0; i < total; i++){
		$hide(obj + '_' + i);
		$setClassName(obj + '_li_' + i, '');
	}
	$show(obj + '_' + n);
	$setClassName(obj + '_li_' + n, 'current');
}

function Descript(n){
	var t = 3;
	var info = [
		'<p class="green">MINI</p><p class="info" style="color:Green">产品宽度：50mm<br />产品高度：84mm<br />产品厚度：12mm<br />产品重量：60g</p>',
		'<p class="white">MINI</p><p class="info">产品宽度：50mm<br />产品高度：84mm<br />产品厚度：12mm<br />产品重量：60g</p>',
		'<p class="red">MINI</p><p class="info" style="color:Red">产品宽度：50mm<br />产品高度：84mm<br />产品厚度：12mm<br />产品重量：60g</p>'
	];
	for(var i = 0; i < t; i++){
		$setClassName('cbox_' + i, 'c_box');
	}
	$setClassName('cbox_' + n, 'c_box_current');
	$inner('mini', info[n]);
}