kj.css3 = new function(){
	this.obj_widget = [];
	this.auto_widget = function() {
		var arr = kj.obj(".Auto_Widget");
		this.obj_widget = arr;
		var type,config,obj,arr_o,j;
		for(var i = 0 ; i < arr.length ; i++) {
			type = kj.getAttribute(arr[i] , 'data-widget-type');
			if(type == 'Popup') {
				config = kj.json(kj.getAttribute(arr[i] , 'data-widget-config'));
				kj.set(arr[i],'style.position','absolute');
				kj.setAttribute(config.trigger,'data-Popup',i);
				arr_o = kj.obj(config.trigger);
				if(config.triggerType == "mouse") {
					for( j = 0 ; j < arr_o.length ; j++ ) {
						kj.handler(kj.obj(".action",arr_o[j] ,true) , 'mouseover' , function() {
							kj.css3.showPopup(this.parentNode);
						});
						kj.handler(kj.obj(".action",arr_o[j] ,true) , 'mouseout' , function(o) {
							kj.css3.hidePopup(this.parentNode);
						});
					}
				} else {
					for( j = 0 ; j < arr_o.length ; j++ ) {
						kj.handler(kj.obj(".action",arr_o[j],true) , 'mousedown' , function() {
							kj.css3.showPopup(this.parentNode , true);
						});
					}
				}
			}
		}
	}
	this.showPopup = function(o , isclick) {
		var ii = kj.getAttribute(o , 'data-Popup');
		if(!ii) return;
		var arr = kj.css3.obj_widget;
		var config = kj.json(kj.getAttribute(arr[ii] , 'data-widget-config'));
		var triggerAlign = kj.css3._getXy(config.align.points[0]);
		var offset = kj.offset(o);
		var offset2 = kj.offset(arr[ii]);
		var target = [triggerAlign[0] * offset.width, triggerAlign[1] * offset.height];
		var nodeAlgin = kj.css3._getXy(config.align.points[1]);
		var top = target[1] - nodeAlgin[1] * offset2.height + config.align.offset[1]+'px';
		var left = target[0] - nodeAlgin[0] * offset2.width + config.align.offset[0]+'px';
		kj.set(arr[ii],'style.left',left);
		kj.set(arr[ii],'style.top',top);
		kj.set(arr[ii],"style.visibility","visible");
		if(isclick) {
			if(kj.data['clicker']) kj.set(kj.data['clicker'],"style.visibility","hidden");
			kj.data['clicker'] = arr[ii];
		}
		//显示所有上级菜单
		var arrp = kj.parent(arr[ii] , '.Auto_Widget');
		if(arrp) {
			kj.set(arrp,"style.visibility","visible");
			arrp = kj.parent(arrp , '.Auto_Widget');
			if(arrp) kj.set(arrp,"style.visibility","visible");
		}

	}
	this.hidePopup = function(o) {
		var ii = kj.getAttribute(o , 'data-Popup');
		if(!ii) return;
		alert(ii);
		var arr = kj.css3.obj_widget;
		kj.set(arr[ii],"style.visibility","hidden");
	}
	this._getXy = function(e) {
		var Pos = {
			tl: [0, 0],
			tc: [0.5, 0],
			tr: [1, 0],
			cl: [0, 0.5],
			cc: [0.5, 0.5],
			cr: [1, 0.5],
			bl: [0, 1],
			bc: [0.5, 1],
			br: [1, 1]
		};
		return Pos[e];
	}
}
kj.css3.auto_widget();
//body点击事件
kj.handler(document.documentElement,"mousedown",function(e){
	//隐藏购物车
	var target = kj.event_target(e);
	var objcart = kj.parent(target , "#id_cart_box");
	var objcart2 = kj.parent(target, '#id_total_cart');
	if(!objcart && !objcart2) kj.hide("#id_cart_box");
	if( !('clicker' in kj.data) || !kj.data['clicker'] ) return;
	var obj = kj.parent(target , ".dropdown");
	if(obj) {
		if('length' in kj.data['clicker']) {
			var obj2;
			for(var i  = 0  ; i < kj.data['clicker'].length ; i++) {
				obj2 = kj.parent(kj.data['clicker'][i] , ".dropdown");
				if(obj2 == obj) return;
			}
		} else if(kj.parent(kj.data['clicker'], ".dropdown") == obj) {
			return;
		}
	}
	kj.set( kj.data['clicker'] , "style.visibility" , "hidden" );
	kj.data['clicker'] = null;
});