//处理下拉
kj.handler(".Txiala","mouseover",function(e){
	jsxiala.show(this);
});
kj.handler(".Txiala","mouseout",function(e){
	jsxiala.close(this);
});

kj.handler(".Txialaval","mouseover",function(e){
	var objp = this.parentNode;
	kj.addClassName(kj.obj('.Txiala',objp,true),'Thover');
	kj.show(this);
});
kj.handler(".Txialaval","mouseout",function(e){
	var objp = this.parentNode;
	kj.delClassName(kj.obj('.Txiala',objp,true),'Thover');
	kj.hide(this);
});

//处理排行
kj.handler(".Tpaihang .Tact","mouseover",function(e){
	var objp = kj.parent(this,'.Tpaihang');
	kj.hide(kj.obj(".Thide" , objp));
	kj.show(kj.obj(".Thide" , this));
});
//处理排行
kj.handler("#id_cart","mouseover",function(e){
	if(jscart.t_go = true) {
		jscart.time_cancel();
	}
});

var jsxiala = new function() {
	this.show = function(msgobj) {
		msgobj = kj.obj(msgobj);
		var objp = msgobj.parentNode;
		kj.addClassName(msgobj,'Thover');
		var arr_obj = kj.obj(".Txialaval" , objp);
		if(!arr_obj || !('length' in arr_obj) || arr_obj.length==0) return;
		var obj = arr_obj[0];
		if(obj.style.display != 'none') return;
		var offset = kj.offset(msgobj);
		var objdata = kj.getAttribute(msgobj,'xialadata');
		objdata = (objdata)? objdata = kj.json(objdata) : {};
		var align = left;
		if('align' in objdata) align = objdata.align;
		kj.show(obj);
		var position = '';
		var offset2 , left , top;
		offset2 = kj.offset(obj);
		left = offset.left;
		top = offset.top;
		if(!('valign' in objdata) || objdata.valign == "down") top += offset.height
		if('parent' in objdata) {
			var obj_p = kj.obj(objdata.parent);
			if(obj_p) {
				var offset_p = kj.offset(obj_p);
				top = top - offset_p.top;
				left = left - offset_p.left;
				var arr_sp = kj.obj(".xsp" , obj);
				var top2 = top + offset.height;
				if(offset2.height < top2) {
					top2 = top2 - offset2.height;
					top = top - top2;
					kj.set(arr_sp[0] , "style.top" , top + 'px');
					top = top2;
				} else {
					kj.set(arr_sp[0] , "style.top" , top + 'px');
					top = 0;
				}
			}
		}
		if('left' in objdata) left += objdata.left;
		if(align == 'right') left += offset.width-offset2.width;
		kj.set(obj,'style.left',left+'px');
		if('top' in objdata) top += objdata.top;
		kj.set(obj,'style.top',top+'px');
	}
	this.close = function(msgobj) {
		msgobj = kj.obj(msgobj);
		var objp = msgobj.parentNode;
		kj.delClassName(msgobj,'Thover');
		var arr_obj = kj.obj(".Txialaval" , objp);
		if(!arr_obj || !('length' in arr_obj) || arr_obj.length==0) return;
		kj.hide(arr_obj[0]);
	}
}
//处理幻灯片
//幻灯版
var jsslide = new function() {
	this.sliders = [];
	this.timeout = [];
	this.load = function() {
		var arr_obj = kj.obj(".Tslider .Tlist");
		if(!arr_obj) return;
		var arr_num = kj.obj(".Tslider .Tbtn");
		this.sliders = arr_num;
		var arr_c,j;
		for(var i = 0 ; i < arr_obj.length ; i++) {
			arr_c = kj.childs(arr_obj[i]);
			if(!arr_c) continue;
			kj.hide(arr_c);
			kj.show(arr_c[0]);
			arr_c = kj.childs(arr_num[i]);
			kj.handler(arr_c , 'mouseover' , function() {
				var p = kj.parent(this,'.Tslider');
				var ii = kj.index(jsslide.sliders , p);
				clearTimeout(jsslide.timeout[ii]);
				jsslide.show(this);
			});
			kj.handler(arr_c , 'mouseout' , function() {
				var p = kj.parent(this,'.Tslider');
				var ii = kj.index(jsslide.sliders , p);
				var arr_c = kj.childs(p);
				var jj = kj.index(arr_c , this);
				jsslide.autostart(ii , jj);
			});
			this.autostart(i , 0);
		}
		var arr_next = kj.obj(".Tslider .Tnext");
		kj.handler(arr_next , 'click' , function() {
			jsslide.next(this);
		});
		var arr_pre = kj.obj(".Tslider .Tpre");
		kj.handler(arr_pre , 'click' , function() {
			jsslide.pre(this);
		});
	}
	this.autostart = function( i , j ) {
		if(this.sliders.length <= i) return;
		var arr_c = kj.childs(this.sliders[i]);
		if(!arr_c || !('length' in arr_c) || j >= arr_c.length) return;
		this.show(arr_c[j]);
		j++;
		if(j>=arr_c.length) j = 0;
		var objp = kj.parent(this.sliders[i] , ".Tslider");
		var objdata = kj.getAttribute(objp,'sliderdata');
		objdata = (objdata) ? kj.json(objdata) : {};
		if('auto' in objdata && objdata.auto == '1') {
			var t = 3;
			if(objdata && 'time' in objdata) t = kj.toint(objdata.time);
			t = t * 1000;
			this.timeout[i] = setTimeout("jsslide.autostart(" + i + "," + j + ");" , t);
		}
	}
	this.next = function( o ) {
		var obj_p = kj.parent(o , ".Tslider");
		var arr_btns = kj.obj('.Tbtn' , obj_p);
		if(!arr_btns || !('length' in arr_btns) || arr_btns.length==0) return;
		var obj_li = kj.childs(arr_btns[0]);
		var obj_sel = kj.obj(".Tbtn .Thover" , obj_p);
		if(!obj_sel) return;
		var index = kj.index(obj_li , obj_sel[0] ,true);
		index++;
		if(index>=obj_li.length) index = 0;
		this.show(obj_li[index]);
	}
	this.pre = function( o ) {
		var obj_p = kj.parent(o , ".Tslider");
		var arr_btns = kj.obj('.Tbtn' , obj_p);
		if(!arr_btns || !('length' in arr_btns) || arr_btns.length==0) return;
		var obj_li = kj.childs(arr_btns[0]);
		var obj_sel = kj.obj(".Tbtn .Thover" , obj_p);
		if(!obj_sel) return;
		var index = kj.index(obj_li , obj_sel[0] ,true);
		index--;
		if(index<0) index = obj_li.length-1;
		this.show(obj_li[index]);
	}
	this.show = function( o ) {
		var obj_li = kj.childs(o.parentNode);
		kj.delClassName(obj_li , 'Thover');
		kj.addClassName(o , 'Thover');
		var index = kj.index(obj_li , o);
		var obj_p = kj.parent(o , ".Tslider");
		var obj_slider = kj.obj(".Tlist" , obj_p);
		if(!obj_slider) return;
		var obj_c = kj.childs(obj_slider[0]);
		kj.hide(obj_c);
		kj.show(obj_c[index]);
	}
	
}
jsslide.load();
//购物车
var jscart = new function() {
	this.t = null;
	this.t_go = false;
	this.no_save = false;
	this.add = function(o , num , isinit) {
		if(!kj.obj('#id_min_cart')) return;
		if(!isinit) jsxiala.show("#id_xiala_cart");
		this.time_cancel();
		num = kj.toint(num);
		num = (!num) ? 1 : num;
		var total = 0;
		if(kj.obj("#id_cart_"+o.id)) {
			this.change_num(o.id , num);
		} else {
			o.pic = kj.url_view(o.pic);
			total = kj.toint(num * o.price);
			var html = '<li id="id_cart_'+o.id+'" class="xli"><span class="xpic"><input type="hidden" name="cartid[]" value="'+o.id+'"><input type="hidden" name="price[]" id="id_cart_price_'+o.id+'" value="'+o.price+'"><a class="img_l" href="?app=goods&app_act=view&id='+o.id+'" target="_blank"><img src="'+o.pic+'" /></a></span>';
				html += '<span class="xtit">';
				html += '<a href="?app=goods&app_act=view&id='+o.id+'" target="_blank">'+o.name+'</a></span>';
				html += '<span class="xnum"><a href="javascript:jscart.change_num(\''+o.id+'\',-1);" class="btn_a_jian">&nbsp;</a>&nbsp;<input type="text" name="num'+o.id+'[]" value="'+num+'" class="Tcart_num_'+o.id+'" onkeyup="jscart.change_num(\''+o.id+'\')">&nbsp;<a href="javascript:jscart.change_num(\''+o.id+'\',1);" class="btn_a_jia">&nbsp;</a></span>';
				html += '<span class="xprice Ttotal Tgtotal'+o.id+'">'+kj.cfg("coinsign")+total+'</span>';
				html += '<span class="xdel"><a href="javascript:jscart.del(\''+ o.id +'\')" class="btn-a2">删除</a></span>';
				html += '</li>';

				var obj_box = kj.obj("#id_cart_list");
				if(obj_box) obj_box.innerHTML =  obj_box.innerHTML + html;
		}
		if(!isinit) this.t = setTimeout("jscart.time_close(1)" , 1000);
		//同步数量显示
		this.refresh_price(o.id);
	}
	this.tongbu_num = function(id) {
		var arr = kj.obj(".Tnumval"+id);
		var arrbtn = kj.obj(".Tnumbtn"+id);
		var num = kj.obj(".Tcart_num_"+id);
		num = (!num || num.length<1) ? 0 : kj.toint(num[0].value);
		for(var i = 0 ; i < arr.length ; i++) {
			if('value' in arr[i]) {
				arr[i].value = num;
			} else if('innerHTML' in arr[i]) {
				arr[i].innerHTML = num;
			}
		}
		if(num<1) {
			kj.hide(".Tghide"+id);
		} else {
			kj.show(".Tghide"+id);
		}
	}
	this.del = function(id) {
		if(id) {
			var obj = kj.obj("#id_cart_"+id);
			if(!obj) return;
			kj.remove(obj);
		} else {
			kj.set("#id_cart_list","innerHTML","");
		}
		this.reset_h();
		this.refresh_price(id);
	}
	//改变数量
	this.change_num = function(id , num , flag) {
		var obj_cart_num = kj.obj(".Tcart_num_" + id);
		val = kj.toint(obj_cart_num[0].value);
		var i;
		if(num) {
			val = flag ? num : num+val;
			if(val<1) {
				this.del(id);
				return;
			}
			for(i = 0 ; i < obj_cart_num.length ; i++) {
				obj_cart_num[i].value = val;
			}
		}
		if(obj_cart_num) {
			var obj_price = kj.obj("#id_cart_price_"+id);
			val = val * kj.toint(obj_price.value);
			var arr = kj.obj(".Tgtotal"+id);
			for(i = 0 ; i < arr.length ; i++) {
				arr[i].innerHTML = kj.cfg("coinsign") + kj.toint(val);
			}
			this.refresh_price(id);
		}
	}
	//刷新价格
	this.refresh_price = function(id) {
		if(id) this.tongbu_num(id);
		var obj = kj.obj("#id_cart_list .Ttotal");
		var price = 0;
		for(var i = obj.length-1 ; i >=0 ; i--) {
			price += kj.toint(obj[i].innerHTML);
		}
		price = kj.toint(price);
		kj.set(".TcartTotal" , 'innerHTML' , kj.cfg("coinsign")+price);
		//算数量
		arr_obj = kj.obj(".TcartAllNum");
		if(arr_obj && 'length' in arr_obj && arr_obj.length>0 ) {
			arr = kj.obj(".TcartNum");
			var num = 0;
			for(i = 0 ; i < arr.length; i++) {
				num += kj.toint(arr[i].value);
			}
			arr_obj[0].innerHTML = num;
		}
		//如果是提交页面
		var obj_t = kj.obj(".Ttotalprice");
		if(obj_t && obj_t.length>0) {
			kj.set(".Ttotalprice" , "innerHTML" ,  kj.cfg("coinsign")+price);
		}
		this.total = price;
		return price;
	}
	this.time_close = function(isfirst) {
		if(isfirst) jscart.t_go = true;
		if(jscart.t_go == false) return;
		var obj = kj.obj("#id_cart");
		h = kj.h(obj);
		h -= 30;
		if(h<0) h = 0;
		if(h > 0) {
			kj.set(obj , "style.height" , h+"px");
			jscart.t = kj.timeout(jscart.time_close , 20);
		} else if(jscart.t_go == true) {
			kj.set(obj , "style.height" , "auto");
			jsxiala.close("#id_xiala_cart");
			jscart.t_go = false;
		}
	}
	this.time_cancel = function() {
		if(jscart.t) clearTimeout(jscart.t);
		jscart.reset_h();
		if(jscart.t_go == false) return;
		jscart.t_go = false;
	}
	this.reset_h = function() {
		if(!kj.obj('#id_min_cart')) return;
		var arr = kj.obj("#id_cart_list li");
		if(arr.length>4) {
			kj.set("#id_cart_list","className","xscroll");
		} else {
			kj.set("#id_cart_list","className","");
		}
		kj.set("#id_cart" , "style.height" , "auto");
		jscart.t_go = false;
	}
	this.save_cookie = function() {
		if(this.no_save) return;
		var i,val,j,arr_1=[],arr = [];
		obj = kj.obj("#id_cart_list :cartid[]");
		for(i = 0 ; i < obj.length ; i++ ) {
			arr = kj.obj(".Tcart_num_"+obj[i].value);
			if(!arr || arr.length<1) continue;
			val = kj.toint(arr[0].value);
			arr_1[arr_1.length] = obj[i].value + "," + val;
		}
		var str_ids = arr_1.join("|");
		kj.cookie_set("mcart_ids" , str_ids , 24);
	}
	this.load = function() {
		if(!kj.obj('#id_min_cart')) return;
		kj.ajax.get(kj.url_view('index.php?app=ajax&app_act=cart.list') , function(data) {
			var obj = kj.json(data);
			if(!obj) return;
			for(var i = 0 ; i < obj['list'].length ; i++) {
				jscart.add(obj['list'][i] , obj['list'][i]['num'] , true);
			}
		});
	}
}

window.onbeforeunload = function() {
	jscart.save_cookie();
}
jscart.load();