//下拉
kj.handler(kj.obj(".M_action" , ".M_Drop" , true) , 'mouseover' , function() {
	var obj_p = kj.parent(this , ".M_Drop");
	var obj = kj.obj(".M_execution" , obj_p , true);
	if(this.id == 'id_cart_view') {
		jscart.time_cancel();
	}
	kj.show(obj);
});
kj.handler(kj.obj(".M_action" , ".M_Drop" , true) , 'mouseout' , function() {
	var obj_p = kj.parent(this , ".M_Drop");
	var obj = kj.obj(".M_execution" , obj_p , true);
	kj.hide(obj);
});

kj.handler(kj.obj(".M_execution" , ".M_Drop" , true) , 'mouseover' , function() {
	if(this.id == 'id_cart') {
		jscart.time_cancel();
	}
	kj.show(this);
});
kj.handler(kj.obj(".M_execution" , ".M_Drop" , true) , 'mouseout' , function() {
	kj.hide(this);
});
//点击
kj.handler(kj.obj(".M_action" , ".M_Click" , true) , 'click' , function() {
	var obj_p = kj.parent(this , ".M_Click");
	var obj = kj.obj(".M_execution" , obj_p , true);
	if(obj && obj.length>0) {
		if(obj[0].style.display=='') {
			kj.hide(obj);
		} else {
			kj.show(obj);
			kj.data['clicker'] = obj_p;
		}
	}
});
//body点击事件
kj.handler(document.documentElement,"mousedown",function(e){
	if( !('clicker' in kj.data) || !kj.data['clicker'] ) return;
	var target = kj.event_target(e);
	var obj = kj.parent(target , ".M_Click");
	if(obj) {
		if('length' in kj.data['clicker']) {
			for(var i  = 0  ; i < kj.data['clicker'].length ; i++) {
				if(kj.data['clicker'][i] == obj) return;
			}
		} else if(kj.data['clicker'] == obj) {
			return;
		}
	}
	kj.hide(kj.obj(".M_execution" , kj.data['clicker'] , true));
	kj.data['clicker'] = null;
});


//购物车
var jscart = new function() {
	this.t = null;
	this.t_go = false;
	this.add = function(o , isinit) {
		if(!kj.obj('#id_min_cart')) return;
		if(!isinit) kj.show("#id_cart");
		this.time_cancel();
		var num = 1;
		if('num' in o && kj.toint(o.num)>0) num = kj.toint(o.num);
		var total = 0;
		if(kj.obj("#id_cart_num_"+o.id)) {
			this.change_num(o.id , num);
		} else {
			o.pic = kj.url_view(o.pic);
			total = num * o.price;
			var html = '<li id="id_cart_'+o.id+'"><input type="hidden" name="cartid[]" value="'+o.id+'"><input type="hidden" name="price[]" id="id_cart_price_'+o.id+'" value="'+o.price+'"><p class="thumbnail"><a class="img_l" href="pro_view.html#article"><img src="'+o.pic+'" /></a></p>';
				html += '<h2>';
				html += '<a href="#nogo"><font>'+o.name+'</font></a>';
				html += '<span class="cart_num"><a class="x_jian" href="javascript:jscart.change_num(\''+o.id+'\',-1);"></a> <input type="text" name="num'+o.id+'[]" value="'+num+'" id="id_cart_num_'+o.id+'" class="x_num TcartNum" onkeyup="jsshop.change_num(\''+o.id+'\')"> <a class="x_jia" href="javascript:jscart.change_num(\''+o.id+'\',1);"></a></span>';
				html += '<span class="price_count">';
				html += '<em class="Ttotal">￥'+total+'</em>';
				html += '</span><a href="javascript:jscart.del(\''+ o.id +'\')">删除</a>';
				html += '</h2></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();
	}

	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.refresh_price();
	}
	//改变数量
	this.change_num = function(id , num) {
		var obj_cart_num = kj.obj("#id_cart_num_" + id);
		val = kj.toint(obj_cart_num.value);
		if(num) {
			val+=num;
			if(val<1) return;
			obj_cart_num.value = val;
		}
		if(obj_cart_num) {
			var obj_price = kj.obj("#id_cart_price_"+id);
			var obj_p = kj.parent(obj_price , "#id_cart_"+id);
			var arr = kj.obj(".Ttotal" , obj_p);
			val = val * kj.toint(obj_price.value);
			if(arr.length>0) arr[0].innerHTML = "￥"+val.toFixed(2);
			this.refresh_price();
		}
	}
	//刷新价格
	this.refresh_price = function() {
		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);
		}
		kj.set(".TcartTotal" , 'innerHTML' , '￥'+price);
		//算数量
		arr_obj = kj.obj(".TcartAllNum");
		if(arr_obj) {
			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) {
			arr_obj = kj.obj(".Ttotalsendprice");
			if(arr_obj) price += kj.toint(arr_obj[0].innerHTML);
			kj.set(".Ttotalprice" , "innerHTML" , 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;
		kj.set(obj , "style.height" , h+"px");
		if(h > 0) {
			jscart.t = kj.timeout(jscart.time_close , 20);
		} else if(jscart.t_go == true) {
			kj.hide("#id_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;
		kj.set("#id_cart" , "style.height" , "auto");
		if(kj.h("#id_cart_list")>350) {
			kj.set("#id_cart_list" , "style.height" , "350px");
			kj.set("#id_cart_list" , "style.overflowX" , "scroll");
		}
	}
	this.save_cookie = function() {
		var i,val,j,arr_1=[];
		obj = kj.obj("#id_cart_list :cartid[]");
		for(i = 0 ; i < obj.length ; i++ ) {
			val = kj.toint(kj.obj("#id_cart_num_"+obj[i].value).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] , true);
			}
		});
	}
}
window.onbeforeunload = function() {
	jscart.save_cookie();
}
//幻灯版
var jsslide = new function() {
	this.load = function() {
		var arr_obj = kj.obj(".M_flashAD .M_slider");
		if(!arr_obj) return;
		var arr_num = kj.obj(".M_flashAD .M_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() {
				jsslide.show(this);
			});
		}
		var arr_next = kj.obj(".M_flashAD .sliderA_next");
		kj.handler(arr_next , 'click' , function() {
			jsslide.next(this);
		});
		var arr_pre = kj.obj(".M_flashAD .sliderA_pre");
		kj.handler(arr_pre , 'click' , function() {
			jsslide.pre(this);
		});
	}
	this.next = function( o ) {
		var obj_p = kj.parent(o , ".M_flashAD");
		var obj_li = kj.obj(".M_num li" , obj_p);
		var obj_sel = kj.obj(".M_num .M_sels" , obj_p);
		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 , ".M_flashAD");
		var obj_li = kj.obj(".M_num li" , obj_p);
		var obj_sel = kj.obj(".M_num .M_sels" , obj_p);
		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 , 'M_sels');
		kj.addClassName(o , 'M_sels');
		var index = kj.index(obj_li , o);
		var obj_p = kj.parent(o , ".M_flashAD");
		var obj_slider = kj.obj(".M_slider" , obj_p);
		if(!obj_slider) return;
		var obj_c = kj.childs(obj_slider[0]);
		kj.hide(obj_c);
		kj.show(obj_c[index]);

	}

}
//隐藏菜单
kj.hide(kj.obj(".M_execution" , ".M_Drop" , true));
//隐藏tab
var obj_act = kj.obj( ".M_tab .M_action");
var obj_tab = kj.obj( ".M_tab .M_execution");
var o;
kj.handler(".M_tab .M_action .item_tab" , 'click' , function() {
	var obj_p = this.parentNode;
	var obj_item_childs = kj.childs(obj_p);
	var obj_tab = kj.obj( ".M_execution" , obj_p.parentNode);
	var index = kj.index( obj_item_childs, this);
	kj.delClassName(obj_item_childs , "M_active");
	kj.addClassName(this , "M_active");
	if(obj_tab.length>0) {
		var obj_childs = kj.childs(obj_tab[0]);
		kj.hide(obj_childs);
		kj.show(obj_childs[index]);
	}
});
for(var i = 0 ; i < obj_act.length ; i++ ) {
	o = kj.obj('.item_tab' , obj_act[i]);
	if(o && o.length>0) kj.addClassName(o[0] , "M_active");
}
for( i = 0 ; i < obj_tab.length ; i++) {
	o = kj.childs(obj_tab[i]);
	if(o && o.length>0) {
		kj.hide(o);
		kj.show(o[0]);
	}
}

jsslide.load();