// var _createElement = document.createElement;
// var _getElementById = document.getElementById;
// var _getElementsByTagName = document.getElementsByTagName;
// var _getElementsByName = document.getElementsByName;

/************************************************************************************************/
var arrClass = {
	// toQueryString : function() {
		// var arr = this;
		// var result = '';
		// for (var i in arr) {
			// if (i != 'toQueryString') result += (result ? '&' : '') + i + '=' + arr[i];
		// }
		// return result;
	// }
	push : function(value) {
		this[this.length] = value;
		return this.length;
	},

	removeDuplicates : function () {
		var _this = this;
		for (var i = 1; i < _this.length; i++) if (_this[i][0] == _this[i-1][0]) _this.splice(i,1);
		return _this;
	},
	
	empty : function () {
		var _this = this;
		_this = new Array();
		return _this;
		// for (var i = 0; i <= this.length; i++) this.shift();
	},
	
	del : function(v) {
		var arr = new Array();
		for (var i=0; i<this.length;i++) if (this[i] != v) arr.push(this[i]);
		return arr;
	},
	
	remove : function(from, to) {
	  var rest = this.slice((to || from) + 1 || this.length);
	  this.length = from < 0 ? this.length + from : from;
	  return this.push.apply(this, rest);
	}
}

/************************************************************************************************/


var strClass = {
	trim : function() {
		return this.replace(/(\s+)$/g, '').replace(/^\s+/g, '');
	}, 
	
	empty : function() {
		v = this.trim();
		return (v == '' || v == null || v == 'null' || v.undefined()) ? true : false;
	},
	
	undefined : function() {
		return (typeof(this) == 'undefined' || this == undefined || this == 'undefined') ? true : false;
	},
	
	isNum : function() {
		return (isNaN(this) || isEmpty(this)) ? false : true;
	},
	
	isEmail : function() {
			var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
			return !(reg.test(this) == false);
	},

	isCharNum : function() {
		var reg1 = /([A-Za-z])/;
		var reg2 = /([0-9])/;
		return !(reg1.test(this) == false || reg2.test(this) == false);
	},

	isDate : function() {
		var reg = /^\d{4}\-\d{1,2}\-\d{1,2}$/;
		return !(reg.test(this) == false);
	},

	isTime : function() {
		var reg = /^([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$/;
		return !(reg.test(this) == false);
	},

	isDateTime : function() {
		var reg = /^\d{4}\-\d{1,2}\-\d{1,2} ([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$/;
		return !(reg.test(this) == false);
	},
	
	currency : function() {
		v = parseFloat(this).toFixed(2);
		nStr = v + '';
		x = nStr.split('.');
		x1 = x[0];
		x2 = x.length > 1 ? '.' + x[1] : '';
		var rgx = /(\d+)(\d{3})/;
		while (rgx.test(x1)) x1 = x1.replace(rgx, '$1' + ',' + '$2');
		return x1 + x2;
	},
	
	preg_replace : function(search, replace) {
		var str = this;
		var len = search.length;
		for(var i = 0; i < len; i++) {
			str = str.replace(new RegExp(search[i], "ig"), typeof(replace) == 'string' ? replace : (replace[i] ? replace[i] : replace[0]));
		}
		return str;
	},
	
	param2array : function() {
		var result = new Array();
		var arr = this.split('&');
		for(var i=0;i<arr.length;i++) {
			t = instr('=',arr[i]) ? arr[i].split('=') : arr[i];
			if(is_array(t)) {
				result[t[0]] = t[1];
			} else {
				result[result.length] = t;
			}	
		}
		return result;		
	},
	
	replaceText : function(v) {
		var str = this;
		for (var key in v) str = str.replace(key,v[key]);
		return str;
	}
	
}

/************************************************************************************************/

var numClass = {
	isNaN : function() {
		return isNaN(this);
	}
}

/************************************************************************************************/

var funcClass = {
	closure : function() {
	  var _this = this;
		var args = toArray(arguments);
		var obj = args.shift()
	  return function() {
			return _this.apply(obj, args.concat(toArray(arguments)));
	  };
	},
	closureListener : function() {
	  var _this = this;
	  var args = toArray(arguments);
		var obj = args.shift()
	  return function(e) {
	    e = e || window.event;
			var target = e.target ? e.target : e.srcElement;
	    return _this.apply(obj, [e, target].concat(args));
	  }
	}
}

/************************************************************************************************/
/************************************************************************************************/
/************************************************************************************************/

var DOMElement = {
	String : function(func) { for(i in func) String.prototype[i] = func[i]; }, 
	Number : function(func) { for(i in func) Number.prototype[i] = func[i]; }, 
	Array : function(func) { for(i in func) Array.prototype[i] = func[i]; },
	Function : function(func) { for(i in func) Function.prototype[i] = func[i]; }
}

// Array.prototype.push = function(v) {
	// this[this.length] = v;
	// return this.length;
// }

// Array.prototype.del = function(v) {
	// var arr = new Array();
	// for (var i=0; i<this.length;i++) if (this[i] != v) arr.push(this[i]);
	// return arr;
// }

// DOMElement.Object(objClass);
// DOMElement.Array(arrClass);
DOMElement.String(strClass);
DOMElement.Function(funcClass);
// DOMElement.Number(numClass);
/****************************************************************************************************************************************************/

var ezExtend = function() {
  var args = arguments;
  if (args.length == 1) args = [this, args[0]];
  for (var a in args[1]) args[0][a] = args[1][a];
  return args[0];
};

function ezClass(){}

ezClass.prototype.construct = function() {};
ezClass.extend = function(C) {
	var A = function() {
		if (arguments[0] !== ezClass) {
			return this.construct.apply(this, arguments);
		}
	};
	var B = new this(ezClass);
	ezExtend(B, C);
	A.prototype = B;
	A.extend = this.extend;
	return A;
}

var events = {
	push : function(obj, event, handler) {
		if (obj.addEventListener) obj.addEventListener(event, handler, false); 
		else if (obj.attachEvent) obj.attachEvent('on'+event, handler); 
	},
	
	cancel : function(event) {
    event = event || window.event;
    if (event.preventDefault && event.stopPropagation) {
      event.preventDefault();
      event.stopPropagation();
    }
    return false;
	}
}

var ezElement = ezClass.extend({
  construct: function(tag) {
    if (typeof(tag) == 'string') tag = document.createElement(tag);
		tag = $(tag);
    return tag;
  },
	
  noSelect: function() {
    setNoSelect(this);
    return this;
  },
	
  addEvent: function(a,b) {
		eventPush(this, a, b);
    return this;
  },
	
  addEvents: function(v) {
		for (var key in v) eventPush(this, key, v[key]);
    return this;
  },
	
  append: function(obj) {
    this.appendChild(obj);
    return this;
  },
	
  appendTo: function(obj) {
    obj.appendChild(this);
    return this;
  },
	
  appendBefore: function(obj) {
    obj.parentNode.insertBefore(this, obj);
    return this;
  },
	
  addContent: function(v) {
		var tmp = this.getContent();
		tmp += v;
		this.setContent(tmp);
		return this;
  },
	
  setContent: function(v) {
		if (this.nodeName == 'INPUT') {
			if (this.type == 'text' || this.type == 'hidden' || this.type == 'radio') this.value = v;
		} else if (this.nodeName == 'TEXTAREA') {
			this.value = v;
		} else {
			this.innerHTML = v;
		}
		return this;
  },
	
  getContent: function() {
		if(this.nodeName == 'SELECT') {
			return this.options[this.selectedIndex].value;
		} else if (this.nodeName == 'INPUT') {
			if (this.type == 'text' || this.type == 'hidden' || this.type == 'radio') return this.value;
		} else if (this.nodeName == 'TEXTAREA') {
			return this.value;
		} else {
			return this.innerHTML;
		}
  },
	
  getText: function() {
		if(this.nodeName == 'SELECT') {
			return this.options[this.selectedIndex].text;
		} else if (this.nodeName == 'INPUT') {
			if (this.type == 'text' || this.type == 'hidden' || this.type == 'radio') return this.value;
		} else if (this.nodeName == 'TEXTAREA') {
			return this.value;
		} else {
			return this.innerText ? this.innerText : this.textContent;
		}
  },
	
  clrContent: function() {
    this.setContent('');
    return this;
  },
	
  pos: function() {
    var cur = curtop = 0;
    var obj = this;
    if (obj.offsetParent) {
      do {
        cur += obj.offsetLeft;
        curtop += obj.offsetTop
      } while ( obj = obj . offsetParent )
    }
    var border = (!isOpera) ? parseInt(this.getStyle('border-width') || this.style.border) || 0 : 0;
    return [cur + border, curtop + border + this.offsetHeight];
  },
	
  parentTag: function(node) {
    var _this = this;
    do {
      if (_this && _this.nodeName && _this.nodeName.toUpperCase() == node) return _this;
      _this = _this.parentNode;
    } while ( _this );
    return false;
  },
	
  setStyle: function(key,v) {
    var style = this.style;
    try {
			switch (key) {
				case 'float':
					style.cssFloat = style.styleFloat = v;
					break;
				case 'opacity':
					style.opacity = v;
					style.filter = 'alpha(opacity=' + Math.round(v * 100) + ')';
					break;
				case 'className':
					this.className = v;
					break;
				default:
					style[key] = v;
			}
    } catch(ex) { }
    return this;
  },
	
  setStyles: function(v) {
    var style = this.style;
    try {
      for (var key in v) {
        switch (key) {
	        case 'float':
	          style.cssFloat = style.styleFloat = v[key];
	          break;
	        case 'opacity':
	          style.opacity = v[key];
	          style.filter = 'alpha(opacity=' + Math.round(v[key] * 100) + ')';
	          break;
	        case 'className':
	          this.className = v[key];
	          break;
	        default:
	          style[key] = v[key];
        }
      }
    } catch(ex) { }
    return this;
  },
	
  getStyle: function(style, doc) {
    style = style == 'float' ? 'cssFloat' : style;
    var value = '';
    try {
        var value = this.style[style];
    } catch(ex) {
        return value;
    }
    try {
      if (!value) {
        if(this.currentStyle) {
          value = this.currentStyle[style];
        } else {
          var doc = (!doc) ? document.defaultView : doc;
          var css = doc.getComputedStyle(this, null);
          value = css.getPropertyValue(style);
        }
      }
    } catch(ex) {
      value = 'none';
    }
    if (style == 'opacity') return value ? parseFloat(value) : 1.0;
    return value == 'auto' ? null : value;
  },
	
  remove: function() {
		this.parentNode.removeChild(this);
    return this;
  },
	
  setAttributes: function(A) {
		try { for (var B in A) this.setAttribute(B,A[B]); } catch(e) { for (var B in A) this[B] = A[B]; };
    return this;
  },
	
  getAttributes: function(v) {
		// $('tmpabc').addContent('getAttributes =>' + v + '<br>');
		if (this.getAttribute) {
			var r = this.getAttribute(v) ? this.getAttribute(v) : this[v];
		} else {
			var r = this[v];
		}
		return !isUndefined(r) ? r : null;
  },
	
  hasClassName: function(className) {
    return (this.className.length > 0 && (this.className == className || new RegExp("(^|\\s)" + className + "(\\s|$)").test(this.className)));
  },

  addClassName: function(className) {
    if (!this.hasClassName(className)) this.className += (this.className ? ' ' : '') + className;
    return this;
  },

  removeClassName: function(className) {
		for(var i=0;i<arguments.length;i++) {
			this.className = this.className.replace(new RegExp("(^|\\s+)" + arguments[i] + "(\\s+|$)"), ' ');
		}
    return this;
  }, 
	
  replaceClassName: function(className,newClassName) {
		if (this.hasClassName(className)) {
			this.className = this.className.replace(new RegExp("(^|\\s+)" + className + "(\\s+|$)"), ' ' + newClassName + ' ');
		} else {
			this.addClassName(newClassName);
		}
    return this;
  },
	
  isShow: function() {
    return this.style.display == 'inline-block' || this.style.display == 'block' || this.style.display == '';
  },
	
  isHide: function() {
    return this.style.display == 'none';
  },
	
  show: function(t) {
		if (isEmpty(t)) t = 0;
		this.style.display = t == 0 ? 'block' : (t == 1 ? 'inline-block' : '');
    return this;
  },
	
  hide: function() {
    this.style.display = 'none';
    return this;
  },
	
  visible: function() {
    this.style.visibility = 'inherit';
    return this;
  },
	
  invisible: function() {
    this.style.visibility = 'hidden';
    return this;
  },
	
  toggle: function(t) {
		if (isEmpty(t)) t = 0;
    this.style.display = this.style.display == 'none' ? (t == 0 ? 'block' : (t == 1 ? 'inline-block' : '')) : 'none';
    return this;
  },
	
  getChilds: function(o,k) {
		var result = [];
		var obj = this.childNodes;
		for(var i=0;i<obj.length;i++) {
			var isAdd = true;
			if (o) for(var a in o) if ($(obj[i]).getAttributes(a) != o[a]) isAdd = false;
			if (isAdd) result.push($(obj[i]));
		}
		// for(qq=0;qq<result.length;qq++) $('tmpabc').addContent(qq + ' =>' + result[qq].className + '<br>');
    return isEmpty(k) ? result : result[k];
  },
	
	clone : function() {
		var newObject = new this.constructor();
		newObject.__proto__ = this;
		return newObject;
	}
});

/****************************************************************************************************************************************************/

function $(id) {
  if (typeof(id) == 'string') id = document.getElementById(id);
	if (id && !id.appendTo) {
		if(id.tagName == 'SELECT') id.removeOption = id.remove;
		return ezExtend(id, ezElement.prototype);
	} else {
		return id;
	}
}

function getElements(o,k) {
	var obj;
	var result = [];
	if (o.tagName) obj = document.getElementsByTagName(o.tagName);
	else if (o.nodeName) obj = document.getElementsByTagName(o.nodeName);
	else obj = document.all || document.getElementsByTagName('*');
	
	for(var i=0;i<obj.length;i++) {
		var isAdd = true;
		if (o) for(var a in o) {
			if (toLowerCase($(obj[i]).getAttributes(a)) != toLowerCase(o[a])) isAdd = false;
		}
		if (isAdd) result.push($(obj[i]));
	}
	// alert(result);
	return isEmpty(k) ? result : result[k];
}

// eventPush(window,'load',function() { $ce('DIV').setAttributes({id : 'tmpabc'}).appendTo(document.body) });

function $N(id,o) {
	if (!o) o = {};
	o.name = id;
	return getElements(o);
}

function $T(id,o) {
	if (!o) o = {};
	o.tagName = id;
	return getElements(o);
}

function $V(id) {
	if (typeof(id) == 'string') id = $(id);
	if (id) {
		if(id.tagName == 'SELECT') {
			return id.options[id.selectedIndex].value;
		} else if (id.tagName == 'INPUT') {
			if (id.type == 'text' || id.type == 'hidden' || id.type == 'radio') return id.value;
		} else if (id.tagName == 'TEXTAREA') {
			return id.value;
		} else {
			return id.innerHTML;
		}
	} else {
		return '';
	}
}

function $ce(tag) {
	return new ezElement(tag);
	// return document.createElement(tag);
}

function setNoSelect(obj) {
	if (obj.setAttribute && obj.nodeName.toLowerCase() != 'input' && obj.nodeName.toLowerCase() != 'textarea') {
		obj.setAttribute('unselectable', 'on');
	}
	for (var i = 0; i < obj.childNodes.length; i++) {
		setNoSelect(obj.childNodes[i]);
	}
}

function ealert(v) {
	var dialog = new ezDialog();
	dialog.type = 1;
	dialog.button = 1;
	dialog.title = lg('forum_title');
	dialog.msg = v;
	// dialog.onOK = function() { 
											// // if($('notshow').checked) setCookie('preorder_pop','Y',30);
											// addCart('add',qty,size,color,p_id,s_id);
	// };
	// dialog.onCancel = function() { 
											// // if($('notshow').checked) setCookie('preorder_pop','Y',30);
											// $('orderLoading').clrContent();
	// };
	dialog.show();
}